Skip to content

Commit

Permalink
oauth refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
iBicha committed Dec 19, 2024
1 parent f2f8922 commit a4f278c
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ namespace InnertubeService
return invalid
end if

result2 = Innertube.GetDeviceAndUserCode(result.clientId, cancellation)
result2 = Innertube.GetDeviceAndUserCode(result.client_id, cancellation)

if CancellationUtils.IsCancelled(cancellation)
return invalid
Expand All @@ -210,4 +210,35 @@ namespace InnertubeService
return Innertube.PollForAccessToken(code, code, cancellation)
end function

function AuthRefreshAccessTokenIfNeeded(accessToken as object, cancellation = invalid as object) as boolean
if not IsAssociativeArray(accessToken)
return false
end if

nowSeconds = TimeUtils.Now().AsSeconds()
expiresTimestamp = accessToken["expires_timestamp"]

if expiresTimestamp = invalid or expiresTimestamp < nowSeconds
refreshed = Innertube.RefreshAccessToken(accessToken, cancellation)

if IsAssociativeArray(refreshed) and refreshed.DoesExist("access_token")
accessToken.Append(refreshed)
if accessToken.DoesExist("expires_in")
accessToken["expires_timestamp"] = TimeUtils.Now().AsSeconds() + accessToken["expires_in"]
end if
return true
end if
end if

return false
end function

function AuthRevokeAccessToken(accessToken as object, cancellation = invalid as object) as boolean
if not IsAssociativeArray(accessToken)
return false
end if

return Innertube.RevokeAccessToken(accessToken["access_token"], cancellation)
end function

end namespace
33 changes: 21 additions & 12 deletions playlet-lib/src/components/Services/Innertube/OAuth.bs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import "pkg:/source/services/HttpClient.bs"
import "pkg:/source/utils/TimeUtils.bs"

namespace Innertube

Expand Down Expand Up @@ -45,8 +46,8 @@ namespace Innertube
end if

return {
"clientId": match[1]
"clientSecret": match[2]
"client_id": match[1]
"client_secret": match[2]
}
end function

Expand All @@ -73,13 +74,17 @@ namespace Innertube
throw "Failed to get device code: " + ToString(responseData)
end if

if responseData.DoesExist("expires_in")
responseData["expires_timestamp"] = TimeUtils.Now().AsSeconds() + responseData["expires_in"]
end if

return responseData
end function

function PollForAccessToken(clientIdentity as object, deviceAndUserCode as object, cancellation = invalid as dynamic) as object
payload = {
"client_id": clientIdentity.clientId
"client_secret": clientIdentity.clientSecret
"client_id": clientIdentity.client_id
"client_secret": clientIdentity.client_secret
"code": deviceAndUserCode.device_code
"grant_type": "http://oauth.net/grant_type/device/1.0"
}
Expand All @@ -101,6 +106,11 @@ namespace Innertube

responseData = response.Json()
if not responseData.DoesExist("error")
if responseData.DoesExist("expires_in")
responseData["expires_timestamp"] = TimeUtils.Now().AsSeconds() + responseData["expires_in"]
end if
responseData["client_id"] = clientIdentity.client_id
responseData["client_secret"] = clientIdentity.client_secret
return responseData
end if

Expand Down Expand Up @@ -130,12 +140,11 @@ namespace Innertube
return invalid
end function

' TODO: validate
function RefreshAccessToken(clientIdentity as object, refreshToken as string, cancellation = invalid as dynamic) as object
function RefreshAccessToken(accessToken as object, cancellation = invalid as dynamic) as object
payload = {
"client_id": clientIdentity.clientId
"client_secret": clientIdentity.clientSecret
"refresh_token": refreshToken
"client_id": accessToken.client_id
"client_secret": accessToken.client_secret
"refresh_token": accessToken.refresh_token
"grant_type": "refresh_token"
}

Expand All @@ -155,16 +164,16 @@ namespace Innertube
return responseData
end function

' TODO: validate
function RevokeAccessToken(accessToken as string, cancellation = invalid as dynamic) as boolean
request = HttpClient.Post("https://www.youtube.com/o/oauth2/revoke", "")
request.QueryParam("token", accessToken)
request.Cancellation(cancellation)
response = request.Await()
if not response.IsSuccess()
success = response.IsSuccess()
if success
LogError(`Failed to revoke token: ${response.ErrorMessage()}`)
end if
return response.IsSuccess()
return success
end function

end namespace

0 comments on commit a4f278c

Please sign in to comment.