Skip to content

Commit

Permalink
Handle auth tokens with insufficient permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
iBicha committed Sep 10, 2023
1 parent d6cc192 commit fe30fb6
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ This version went through a major refactor, which resulted in a different arcite
- The web server is now decoupled from Playlet specific logic
- When not logged in, authenticated feed (like user Subscriptions and Playlists) show a "Login to view X" message, with a QR Code that redirects to the login screen
- [Breaking change] web apis changed a bit (for example the `/api/command` endpoint is removed) refer to the [Open API spec](docs/playlet-web-api.yml)
- If the Invidious auth token is missing permissions (A token aquired using a previous version of Playlet) you will be auto logged out.

### Removed

Expand Down
12 changes: 6 additions & 6 deletions playlet-app/src/components/BootstrapScene/BootstrapScene.bs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ function Init() as void
end function

function GetPlayletLibUrls() as object
savedUrls = GetPlayletLibUrlsFromRegistry()
if savedUrls <> invalid
m.shouldClearRegistryOnLoadFail = true
return savedUrls
end if

#if DEBUG
debug = { type: "debug", link: ReadManifestValue("playlet_lib_debug_url") }
return [debug]
#else
savedUrls = GetPlayletLibUrlsFromRegistry()
if savedUrls <> invalid
m.shouldClearRegistryOnLoadFail = true
return savedUrls
end if

github = { type: "github", link: ReadManifestValue("playlet_lib_remote_url") }
embedded = { type: "embedded", link: ReadManifestValue("playlet_lib_embedded_url") }
return [github, embedded]
Expand Down
4 changes: 0 additions & 4 deletions playlet-lib/src/components/Services/Invidious/Invidious.bs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ function GetAuthorizeTokenLink(unused as dynamic) as dynamic
return m.service.GetAuthorizeTokenLink()
end function

function Logout(unused as dynamic)
m.service.Logout()
end function

function SetAuthToken(token as string, instance as string, username as dynamic)
m.service.SetAuthToken(token, instance, username)
end function
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
<field id="authToken" type="assocarray" />
<field id="apiDefinitions" type="assocarray" />

<function name="Logout" />
<function name="GetCurrentInstance" />
<function name="GetAuthorizeTokenLink" />
<function name="SetAuthToken" />
Expand Down
29 changes: 25 additions & 4 deletions playlet-lib/src/components/Services/Invidious/InvidiousService.bs
Original file line number Diff line number Diff line change
Expand Up @@ -289,13 +289,21 @@ namespace Invidious
if tokenPayload = invalid
return invalid
end if
authData = ParseJson(tokenPayload)
' TODO:P0 delete token if we're missing scope
if authData = invalid or authData.instance = invalid or authData.token = invalid
authToken = ParseJson(tokenPayload)
if authToken = invalid or authToken.instance = invalid or authToken.token = invalid
RegistryUtils.Delete(RegistryUtils.INVIDIOUS_TOKEN)
return invalid
end if
return authData
if not m.VerifyTokenScope(authToken.token)
' TODO:P1 automatically logging out the user is not a good experience
' We can at least show a message to the user on why they are logged out
' TODO:P2 right now we're probably in a UI thread, and we can't unregister the token
' So we're just going to ignore it for now
' m.UnregisterToken(authToken)
RegistryUtils.Delete(RegistryUtils.INVIDIOUS_TOKEN)
return invalid
end if
return authToken
end function

function SetAuthToken(token as string, instance as string, username as dynamic)
Expand All @@ -311,6 +319,19 @@ namespace Invidious
m.node.authToken = obj
end function

function VerifyTokenScope(token as string) as boolean
tokenObject = ParseJson(token)
if tokenObject = invalid
return false
end if
if not IsArray(tokenObject.scopes)
return false
end if

scopes = tokenObject.scopes.join(",")
return scopes = Invidious.AUTH_SCOPES
end function

function DeleteAuthToken()
m.node.authToken = invalid
RegistryUtils.Delete(RegistryUtils.INVIDIOUS_TOKEN)
Expand Down
8 changes: 8 additions & 0 deletions playlet-lib/src/source/services/HttpClient.bs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,11 @@ namespace HttpClient
end if

m.urlTransfer = m.CreateRoUrlTransfer()
if m.urlTransfer = invalid
m.log.error("Failed to create roUrlTransfer. We're probably not in a background thread.")
return m
end if

m.urlTransfer.setUrl(m.BuildUrl())
if m._headers <> invalid
m.urlTransfer.SetHeaders(m._headers)
Expand Down Expand Up @@ -321,6 +326,9 @@ namespace HttpClient

private function CreateRoUrlTransfer() as object
urlTransfer = CreateObject("roUrlTransfer")
if urlTransfer = invalid
return invalid
end if
urlTransfer.EnableEncodings(true)
urlTransfer.RetainBodyOnError(true)
if LCase(left(m._url, 6)).StartsWith("https:")
Expand Down

0 comments on commit fe30fb6

Please sign in to comment.