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):
|
if (server != null):
|
||||||
super.poll()
|
super.poll()
|
||||||
|
|
||||||
func _process_response(response : String) -> void:
|
func _handle_empty_response() -> void:
|
||||||
if (response == ""):
|
super._handle_empty_response()
|
||||||
print("Empty response. Check if your redirect URL is set to %s." % redirect_url)
|
auth_code_received.emit("")
|
||||||
return
|
|
||||||
var start : int = response.find("?")
|
func _handle_success(data : Dictionary) -> void:
|
||||||
if (start == -1):
|
super._handle_success(data)
|
||||||
print ("Response from Twitch does not contain the required data.")
|
auth_code_received.emit(data["code"])
|
||||||
else:
|
|
||||||
response = response.substr(start + 1, response.find(" ", start) - start)
|
func _handle_error(data : Dictionary) -> void:
|
||||||
var data : Dictionary = {}
|
super._handle_error(data)
|
||||||
for entry in response.split("&"):
|
auth_code_received.emit("")
|
||||||
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())
|
|
||||||
auth_code_received.emit(data["code"])
|
|
||||||
peer.disconnect_from_host()
|
|
||||||
peer = null
|
|
||||||
|
@ -8,7 +8,7 @@ func login(client_id : String, scopes : PackedStringArray, force_verify : bool =
|
|||||||
print("Waiting for user to login.")
|
print("Waiting for user to login.")
|
||||||
var token_data : Dictionary = await(token_received)
|
var token_data : Dictionary = await(token_received)
|
||||||
server.stop()
|
server.stop()
|
||||||
if (token_data != null):
|
if (!token_data.is_empty()):
|
||||||
var token : UserAccessToken = UserAccessToken.new(token_data, client_id)
|
var token : UserAccessToken = UserAccessToken.new(token_data, client_id)
|
||||||
token.fresh = true
|
token.fresh = true
|
||||||
return token
|
return token
|
||||||
@ -22,27 +22,13 @@ func poll() -> void:
|
|||||||
elif (peer.get_status() == StreamPeerTCP.STATUS_CONNECTED):
|
elif (peer.get_status() == StreamPeerTCP.STATUS_CONNECTED):
|
||||||
_poll_peer()
|
_poll_peer()
|
||||||
|
|
||||||
func _process_response(response : String) -> void:
|
func _handle_empty_response() -> void:
|
||||||
if (response == ""):
|
send_response("200 OK", "<html><script>window.location = window.location.toString().replace('#','?');</script><head><title>Twitch Login</title></head></html>".to_utf8_buffer())
|
||||||
print("Empty response. Check if your redirect URL is set to %s." % redirect_url)
|
|
||||||
return
|
func _handle_success(data : Dictionary) -> void:
|
||||||
var start : int = response.substr(0, response.find("\n")).find("?")
|
super._handle_success(data)
|
||||||
if (start == -1):
|
token_received.emit(data)
|
||||||
send_response("200 OK", "<html><script>window.location = window.location.toString().replace('#','?');</script><head><title>Twitch Login</title></head></html>".to_utf8_buffer())
|
|
||||||
else:
|
func _handle_error(data : Dictionary) -> void:
|
||||||
response = response.substr(start + 1, response.find(" ", start) - start)
|
super._handle_error(data)
|
||||||
var data : Dictionary = {}
|
token_received.emit({})
|
||||||
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())
|
|
||||||
token_received.emit(data)
|
|
||||||
peer.disconnect_from_host()
|
|
||||||
peer = null
|
|
||||||
|
@ -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("Content-Type: text/html; charset=UTF-8\r\n".to_utf8_buffer())
|
||||||
peer.put_data("\r\n".to_utf8_buffer())
|
peer.put_data("\r\n".to_utf8_buffer())
|
||||||
peer.put_data(body)
|
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