Skip to content

Commit

Permalink
moved common redirecting auth code to superclass
Browse files Browse the repository at this point in the history
  • Loading branch information
issork committed Dec 4, 2023
1 parent df5f39f commit bd1b0b8
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 48 deletions.
34 changes: 11 additions & 23 deletions addons/gift/auth/grant_flows/authorization_code_grant_flow.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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())
auth_code_received.emit(data["code"])
peer.disconnect_from_host()
peer = null
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"])

func _handle_error(data : Dictionary) -> void:
super._handle_error(data)
auth_code_received.emit("")
36 changes: 11 additions & 25 deletions addons/gift/auth/grant_flows/implicit_grant_flow.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
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())
token_received.emit(data)
peer.disconnect_from_host()
peer = null
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())

func _handle_success(data : Dictionary) -> void:
super._handle_success(data)
token_received.emit(data)

func _handle_error(data : Dictionary) -> void:
super._handle_error(data)
token_received.emit({})
33 changes: 33 additions & 0 deletions addons/gift/auth/grant_flows/redirecting_flow.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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())

0 comments on commit bd1b0b8

Please sign in to comment.