Add legal consent and reliable external redirects
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
extends Node
|
||||
|
||||
signal acceptance_succeeded
|
||||
signal acceptance_failed(message: String)
|
||||
|
||||
const EndpointConfig := preload("res://scripts/autoload/api_endpoint_config.gd")
|
||||
const DEFAULT_AUTH_BASE_URL := "https://pauth.ranaze.com/api/Auth"
|
||||
const DOCUMENT_PATH := "res://legal/legal_documents.json"
|
||||
|
||||
var documents: Dictionary = {}
|
||||
var auth_base_url := DEFAULT_AUTH_BASE_URL
|
||||
var request: HTTPRequest
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
auth_base_url = EndpointConfig.resolve(DEFAULT_AUTH_BASE_URL, "/api/Auth")
|
||||
_load_documents()
|
||||
request = HTTPRequest.new()
|
||||
request.timeout = 20.0
|
||||
request.request_completed.connect(_on_accept_completed)
|
||||
add_child(request)
|
||||
|
||||
|
||||
func get_document(kind: String) -> Dictionary:
|
||||
var value: Variant = documents.get(kind, {})
|
||||
return value if typeof(value) == TYPE_DICTIONARY else {}
|
||||
|
||||
|
||||
func public_url(kind: String) -> String:
|
||||
if kind == "terms" and not AuthState.terms_url.is_empty():
|
||||
return AuthState.terms_url
|
||||
if kind == "privacy" and not AuthState.privacy_url.is_empty():
|
||||
return AuthState.privacy_url
|
||||
return "https://doctor.ranaze.com/%s.html" % kind
|
||||
|
||||
|
||||
func accept_current() -> void:
|
||||
if request.get_http_client_status() != HTTPClient.STATUS_DISCONNECTED:
|
||||
return
|
||||
if not AuthState.can_send_authenticated_request():
|
||||
acceptance_failed.emit("Your session expired. Sign in again.")
|
||||
return
|
||||
var headers := PackedStringArray([
|
||||
"Content-Type: application/json",
|
||||
"Authorization: Bearer %s" % AuthState.access_token
|
||||
])
|
||||
var error := request.request(
|
||||
"%s/legal/accept" % auth_base_url.rstrip("/"),
|
||||
headers,
|
||||
HTTPClient.METHOD_POST,
|
||||
JSON.stringify({"accept": true}))
|
||||
if error != OK:
|
||||
acceptance_failed.emit("Unable to contact the authentication service. Try again.")
|
||||
|
||||
|
||||
func _load_documents() -> void:
|
||||
var file := FileAccess.open(DOCUMENT_PATH, FileAccess.READ)
|
||||
if file == null:
|
||||
push_error("Unable to load legal documents")
|
||||
return
|
||||
var parsed: Variant = JSON.parse_string(file.get_as_text())
|
||||
if typeof(parsed) == TYPE_DICTIONARY:
|
||||
documents = parsed
|
||||
|
||||
|
||||
func _on_accept_completed(result: int, code: int, _headers: PackedStringArray, body: PackedByteArray) -> void:
|
||||
if result != HTTPRequest.RESULT_SUCCESS or code < 200 or code >= 300:
|
||||
acceptance_failed.emit("Unable to save your agreement. Check your connection and try again.")
|
||||
return
|
||||
var parsed: Variant = JSON.parse_string(body.get_string_from_utf8())
|
||||
if typeof(parsed) != TYPE_DICTIONARY:
|
||||
acceptance_failed.emit("The authentication service returned an invalid response.")
|
||||
return
|
||||
AuthState.set_legal_state(parsed)
|
||||
acceptance_succeeded.emit()
|
||||
Reference in New Issue
Block a user