attempt refresh with refresh token if access token is invalid

This commit is contained in:
Max Kross 2023-10-25 18:16:07 +02:00
parent 204928ad5c
commit 0545456faa
2 changed files with 25 additions and 7 deletions

View File

@ -102,7 +102,7 @@ var connected : bool = false
var user_regex : RegEx = RegEx.new() var user_regex : RegEx = RegEx.new()
var twitch_restarting : bool = false var twitch_restarting : bool = false
const USER_AGENT : String = "User-Agent: GIFT/4.1.4 (Godot Engine)" const USER_AGENT : String = "User-Agent: GIFT/4.1.5 (Godot Engine)"
enum RequestType { enum RequestType {
EMOTE, EMOTE,
@ -159,22 +159,40 @@ func authenticate(client_id, client_secret) -> void:
if (!token["scope"].has(scope)): if (!token["scope"].has(scope)):
get_token() get_token()
token = await(user_token_received) token = await(user_token_received)
else:
get_token()
token = await(user_token_received)
else: else:
get_token() get_token()
token = await(user_token_received) token = await(user_token_received)
username = await(is_token_valid(token["access_token"])) username = await(is_token_valid(token["access_token"]))
while (username == ""): while (username == ""):
print("Token invalid.") print("Token invalid.")
get_token() var refresh : String = token.get("refresh_token", "")
if (refresh != ""):
refresh_access_token(refresh)
else:
get_token()
token = await(user_token_received) token = await(user_token_received)
username = await(is_token_valid(token["access_token"])) username = await(is_token_valid(token["access_token"]))
print("Token verified.") print("Token verified.")
user_token_valid.emit() user_token_valid.emit()
refresh_token() refresh_token()
func refresh_access_token(refresh : String) -> void:
print("Refreshing access token.")
var request : HTTPRequest = HTTPRequest.new()
add_child(request)
request.request("https://id.twitch.tv/oauth2/token", [USER_AGENT, "Content-Type: application/x-www-form-urlencoded"], HTTPClient.METHOD_POST, "grant_type=refresh_token&refresh_token=%s&client_id=%s&client_secret=%s" % [refresh.uri_encode(), client_id, client_secret])
var reply : Array = await(request.request_completed)
request.queue_free()
var response : Dictionary = JSON.parse_string(reply[3].get_string_from_utf8())
if (response.has("error")):
print("Refresh failed, requesting new token.")
get_token()
else:
token = response
var file : FileAccess = FileAccess.open_encrypted_with_pass("user://gift/auth/user_token", FileAccess.WRITE, client_secret)
file.store_string(reply[3].get_string_from_utf8())
user_token_received.emit(response)
# Gets a new auth token from Twitch. # Gets a new auth token from Twitch.
func get_token() -> void: func get_token() -> void:
print("Fetching new token.") print("Fetching new token.")
@ -237,7 +255,7 @@ func send_response(peer : StreamPeer, response : String, body : PackedByteArray)
peer.put_data("\r\n".to_utf8_buffer()) peer.put_data("\r\n".to_utf8_buffer())
peer.put_data(body) peer.put_data(body)
# If the token is valid, returns the username of the token bearer. # If the token is valid, returns the username of the token bearer. Returns an empty String if the token was invalid.
func is_token_valid(token : String) -> String: func is_token_valid(token : String) -> String:
var request : HTTPRequest = HTTPRequest.new() var request : HTTPRequest = HTTPRequest.new()
add_child(request) add_child(request)

View File

@ -3,5 +3,5 @@
name="Godot IRC For Twitch" name="Godot IRC For Twitch"
description="Godot websocket implementation for Twitch IRC." description="Godot websocket implementation for Twitch IRC."
author="issork" author="issork"
version="4.1.4" version="4.1.5"
script="gift.gd" script="gift.gd"