moved common redirecting auth code to superclass
This commit is contained in:
parent
df5f39f7b0
commit
bd1b0b8a8e
@ -32,26 +32,14 @@ func poll() -> void:
|
||||
if (server != null):
|
||||
super.poll()
|
||||
|
||||
func _process_response(response : String) -> void:
|
||||
if (response == ""):
|
||||
print("Empty response. Check if your redirect URL is set to %s." % redirect_url)
|
||||
return
|
||||
var start : int = response.find("?")
|
||||
if (start == -1):
|
||||
print ("Response from Twitch does not contain the required data.")
|
||||
else:
|
||||
response = response.substr(start + 1, response.find(" ", start) - start)
|
||||
var data : Dictionary = {}
|
||||
for entry in response.split("&"):
|
||||
var pair = entry.split("=")
|
||||
data[pair[0]] = pair[1] if pair.size() > 0 else ""
|
||||
if (data.has("error")):
|
||||
var msg = "Error %s: %s" % [data["error"], data["error_description"]]
|
||||
print(msg)
|
||||
send_response("400 BAD REQUEST", msg.to_utf8_buffer())
|
||||
else:
|
||||
print("Success.")
|
||||
send_response("200 OK", "<html><head><title>Twitch Login</title></head><body>Success!</body></html>".to_utf8_buffer())
|
||||
func _handle_empty_response() -> void:
|
||||
super._handle_empty_response()
|
||||
auth_code_received.emit("")
|
||||
|
||||
func _handle_success(data : Dictionary) -> void:
|
||||
super._handle_success(data)
|
||||
auth_code_received.emit(data["code"])
|
||||
peer.disconnect_from_host()
|
||||
peer = null
|
||||
|
||||
func _handle_error(data : Dictionary) -> void:
|
||||
super._handle_error(data)
|
||||
auth_code_received.emit("")
|
||||
|
@ -8,7 +8,7 @@ func login(client_id : String, scopes : PackedStringArray, force_verify : bool =
|
||||
print("Waiting for user to login.")
|
||||
var token_data : Dictionary = await(token_received)
|
||||
server.stop()
|
||||
if (token_data != null):
|
||||
if (!token_data.is_empty()):
|
||||
var token : UserAccessToken = UserAccessToken.new(token_data, client_id)
|
||||
token.fresh = true
|
||||
return token
|
||||
@ -22,27 +22,13 @@ func poll() -> void:
|
||||
elif (peer.get_status() == StreamPeerTCP.STATUS_CONNECTED):
|
||||
_poll_peer()
|
||||
|
||||
func _process_response(response : String) -> void:
|
||||
if (response == ""):
|
||||
print("Empty response. Check if your redirect URL is set to %s." % redirect_url)
|
||||
return
|
||||
var start : int = response.substr(0, response.find("\n")).find("?")
|
||||
if (start == -1):
|
||||
func _handle_empty_response() -> void:
|
||||
send_response("200 OK", "<html><script>window.location = window.location.toString().replace('#','?');</script><head><title>Twitch Login</title></head></html>".to_utf8_buffer())
|
||||
else:
|
||||
response = response.substr(start + 1, response.find(" ", start) - start)
|
||||
var data : Dictionary = {}
|
||||
for entry in response.split("&"):
|
||||
var pair = entry.split("=")
|
||||
data[pair[0]] = pair[1] if pair.size() > 0 else ""
|
||||
if (data.has("error")):
|
||||
var msg = "Error %s: %s" % [data["error"], data["error_description"]]
|
||||
print(msg)
|
||||
send_response("400 BAD REQUEST", msg.to_utf8_buffer())
|
||||
else:
|
||||
data["scope"] = data["scope"].uri_decode().split(" ")
|
||||
print("Success.")
|
||||
send_response("200 OK", "<html><head><title>Twitch Login</title></head><body>Success!</body></html>".to_utf8_buffer())
|
||||
|
||||
func _handle_success(data : Dictionary) -> void:
|
||||
super._handle_success(data)
|
||||
token_received.emit(data)
|
||||
peer.disconnect_from_host()
|
||||
peer = null
|
||||
|
||||
func _handle_error(data : Dictionary) -> void:
|
||||
super._handle_error(data)
|
||||
token_received.emit({})
|
||||
|
@ -27,3 +27,36 @@ func send_response(response : String, body : PackedByteArray) -> void:
|
||||
peer.put_data("Content-Type: text/html; charset=UTF-8\r\n".to_utf8_buffer())
|
||||
peer.put_data("\r\n".to_utf8_buffer())
|
||||
peer.put_data(body)
|
||||
|
||||
func _process_response(response : String) -> void:
|
||||
if (response == ""):
|
||||
print("Empty response. Check if your redirect URL is set to %s." % redirect_url)
|
||||
return
|
||||
var start : int = response.substr(0, response.find("\n")).find("?")
|
||||
if (start == -1):
|
||||
_handle_empty_response()
|
||||
else:
|
||||
response = response.substr(start + 1, response.find(" ", start) - start)
|
||||
var data : Dictionary = {}
|
||||
for entry in response.split("&"):
|
||||
var pair = entry.split("=")
|
||||
data[pair[0]] = pair[1] if pair.size() > 0 else ""
|
||||
if (data.has("error")):
|
||||
_handle_error(data)
|
||||
else:
|
||||
_handle_success(data)
|
||||
peer.disconnect_from_host()
|
||||
peer = null
|
||||
|
||||
func _handle_empty_response() -> void:
|
||||
print ("Response from Twitch does not contain the required data.")
|
||||
|
||||
func _handle_success(data : Dictionary) -> void:
|
||||
data["scope"] = data["scope"].uri_decode().split(" ")
|
||||
print("Success.")
|
||||
send_response("200 OK", "<html><head><title>Twitch Login</title></head><body>Success!</body></html>".to_utf8_buffer())
|
||||
|
||||
func _handle_error(data : Dictionary) -> void:
|
||||
var msg = "Error %s: %s" % [data["error"], data["error_description"]]
|
||||
print(msg)
|
||||
send_response("400 BAD REQUEST", msg.to_utf8_buffer())
|
||||
|
Loading…
x
Reference in New Issue
Block a user