From 3add083f6bf2fbbfe0c498006e9e3f9bd2f5fc77 Mon Sep 17 00:00:00 2001 From: Brahim Hadriche Date: Fri, 7 Jul 2023 13:28:42 -0400 Subject: [PATCH] Save scope and expire fields with auth token --- CHANGELOG.md | 1 + .../WebServer/Middleware/InvidiousRouter.bs | 8 +- playlet-lib/src/source/services/Invidious.bs | 78 +++++++++++++++++-- .../src/source/services/InvidiousSettings.bs | 8 +- tools/profile-sync.js | 4 +- 5 files changed, 88 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4de6ecba..73328929 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Links to the app info in web app - Loading indicator for search page in web app - Feedback link through email +- Scopes and expire fields to the Invidious auth token. This is to detect when a token is close to expiry, or if we have missing permissions. ### Fixed diff --git a/playlet-lib/src/components/WebServer/Middleware/InvidiousRouter.bs b/playlet-lib/src/components/WebServer/Middleware/InvidiousRouter.bs index 51031bdc..d729db63 100644 --- a/playlet-lib/src/components/WebServer/Middleware/InvidiousRouter.bs +++ b/playlet-lib/src/components/WebServer/Middleware/InvidiousRouter.bs @@ -19,11 +19,17 @@ namespace Http token = request.query["token"].DecodeUri().DecodeUriComponent() referer = request.query["ref"].DecodeUri() username = request.query["username"] + scopes = request.query["scopes"] + expire = request.query["expire"] + if username <> invalid username = username.DecodeUriComponent().DecodeUriComponent() end if + if scopes <> invalid + scopes = scopes.DecodeUriComponent() + end if - InvidiousSettings.SetAuthToken(token, referer, username) + InvidiousSettings.SetAuthToken(token, referer, username, scopes, expire) server.task.top.login = true end if response.Redirect("/") diff --git a/playlet-lib/src/source/services/Invidious.bs b/playlet-lib/src/source/services/Invidious.bs index fa0ecc3a..63b2fd1e 100644 --- a/playlet-lib/src/source/services/Invidious.bs +++ b/playlet-lib/src/source/services/Invidious.bs @@ -18,10 +18,11 @@ namespace Invidious const SEARCH_SUGGEST_ENDPOINT = "/api/v1/search/suggestions" const SEARCH_ENDPOINT = "/api/v1/search" - const AUTH_SCOPE = "GET:feed,GET:playlists,POST:tokens/unregister,POST:history*" + const AUTH_SCOPES = "GET:feed,GET:playlists,POST:tokens/register,POST:tokens/unregister,POST:history*" const AUTH_FEED = "/api/v1/auth/feed" const AUTH_PLAYLISTS = "/api/v1/auth/playlists" const AUTH_TOKENS_UNREGISTER = "/api/v1/auth/tokens/unregister" + const AUTH_TOKENS_REGISTER = "/api/v1/auth/tokens/register" function DefaultArgs() as object return { @@ -155,7 +156,7 @@ namespace Invidious expire = TimeUtils.Now().AsSeconds() + 60 * 60 * 24 * 365 * 2 '2 years from now callbackUrl = `http://${localIp}:${webServerPort}${Invidious.TOKEN_CALLBACK}?ref=${instance}`.EncodeUriComponent() - scope = `${Invidious.AUTH_SCOPE}`.EncodeUriComponent() + scope = `${Invidious.AUTH_SCOPES}`.EncodeUriComponent() return `${invidiousHost}/authorize_token?scopes=${scope}&callback_url=${callbackUrl}&expire=${expire}` end function @@ -165,11 +166,63 @@ namespace Invidious } end function - function Logout() as boolean - authToken = InvidiousSettings.GetAuthToken() - if authToken = invalid + function RefreshAuthToken() as boolean + currentAuthToken = InvidiousSettings.GetAuthToken() + if currentAuthToken = invalid return false end if + + expire = TimeUtils.Now().AsSeconds() + 60 * 60 * 24 * 365 * 2 '2 years from now + + newToken = RegisterToken(currentAuthToken, expire) + if newToken = invalid + return false + end if + InvidiousSettings.SetAuthToken(newToken.token, newToken.instance, newToken.username, newToken.scopes, newToken.expire) + + UnregisterToken(currentAuthToken) + + return true + end function + + function RegisterToken(authToken as object, expire as integer) as dynamic + headers = Invidious.GetAuthenticationHeaders(authToken.token) + url = `${authToken.instance}${Invidious.AUTH_TOKENS_REGISTER}` + + scopes = authToken.scopes + if scopes = invalid + parsedToken = ParseJson(authToken.token) + if GetInterface(parsedToken.scopes, "ifArray") <> invalid + scopes = parsedToken.scopes.join(",") + else + scopes = Invidious.AUTH_SCOPES + end if + end if + + args = { + headers: headers, + json: { + "scopes": scopes.Split(","), + "expire": expire + }, + parseJson: false + } + + response = Requests().post(url, args) + if not response.ok + return invalid + end if + + return { + token: response.text, + instance: authToken.instance, + username: authToken.username, + scopes: scopes, + expire: expire + } + end function + + function UnregisterToken(authToken as object) as boolean headers = Invidious.GetAuthenticationHeaders(authToken.token) url = `${authToken.instance}${Invidious.AUTH_TOKENS_UNREGISTER}` @@ -178,10 +231,21 @@ namespace Invidious headers["Content-Type"] = "application/json" args = { headers: headers, - data: authToken.token + data: authToken.token, + parseJson: false } - Requests().post(url, args) + response = Requests().post(url, args) + return response.ok + end function + + function Logout() as boolean + authToken = InvidiousSettings.GetAuthToken() + if authToken = invalid + return false + end if + + UnregisterToken(authToken) RegistryUtils.Delete(RegistryUtils.INVIDIOUS_TOKEN) return true diff --git a/playlet-lib/src/source/services/InvidiousSettings.bs b/playlet-lib/src/source/services/InvidiousSettings.bs index b9ff4ecf..507ec700 100644 --- a/playlet-lib/src/source/services/InvidiousSettings.bs +++ b/playlet-lib/src/source/services/InvidiousSettings.bs @@ -21,7 +21,7 @@ namespace InvidiousSettings return InvidiousSettings.DEFAULT_INSTANCE end function - function SetAuthToken(token as string, instance as string, username as dynamic) + function SetAuthToken(token as string, instance as string, username as dynamic, scopes as dynamic, expire as dynamic) obj = { token: token, instance: instance @@ -29,6 +29,12 @@ namespace InvidiousSettings if username <> invalid obj.username = username end if + if scopes <> invalid + obj.scopes = scopes + end if + if expire <> invalid + obj.expire = expire + end if json = FormatJson(obj) RegistryUtils.Write(RegistryUtils.INVIDIOUS_TOKEN, json) diff --git a/tools/profile-sync.js b/tools/profile-sync.js index 406c5e4a..84ce9854 100644 --- a/tools/profile-sync.js +++ b/tools/profile-sync.js @@ -203,10 +203,10 @@ async function getAccessToken(invidiousInstance) { server.close() }) - const scope = ":*" + const scopes = ":*" const expire = Date.now() + 60 * 60 * 2; const callbackUrl = `http://${ip.address()}:${port}/invidious/token_callback` - const authLink = `${invidiousInstance}/authorize_token?scopes=${scope}&callback_url=${callbackUrl}&expire=${expire}` + const authLink = `${invidiousInstance}/authorize_token?scopes=${scopes}&callback_url=${callbackUrl}&expire=${expire}` server = app.listen(port, () => { console.log(`server is listening on http://localhost:${port}`);