From b02d5d6a10a2b09d26214e1b6e30597162f2ec02 Mon Sep 17 00:00:00 2001 From: Brahim Hadriche Date: Thu, 8 Oct 2026 11:31:52 -0400 Subject: [PATCH] Add nouce to invidious login --- .../components/Dialog/InvidiousLoginDialog.bs | 6 +-- .../Dialog/InvidiousLoginDialog.xml | 2 +- .../Screens/ProfileScreen/LoginUtils.bs | 4 +- .../ProfileScreen/ProfileView/ProfileView.bs | 2 +- .../Services/Invidious/Invidious.bs | 4 -- .../Services/Invidious/Invidious.xml | 1 - .../Services/Invidious/InvidiousService.bs | 4 +- .../Middleware/InvidiousRouter.bs | 40 +++++++++++++++++-- .../Middleware/StateApiRouter.bs | 1 - playlet-web/src/lib/ProfilesDialog.svelte | 16 +++++--- 10 files changed, 57 insertions(+), 23 deletions(-) diff --git a/playlet-lib/src/components/Dialog/InvidiousLoginDialog.bs b/playlet-lib/src/components/Dialog/InvidiousLoginDialog.bs index 3c3143bc..c6106bb3 100644 --- a/playlet-lib/src/components/Dialog/InvidiousLoginDialog.bs +++ b/playlet-lib/src/components/Dialog/InvidiousLoginDialog.bs @@ -19,9 +19,9 @@ function OnNodeReady() isValidAddress = not StringUtils.IsNullOrEmpty(address) if isValidAddress url = `${address}/invidious/login` - serverUrl = m.top.serverUrl - if not StringUtils.IsNullOrEmpty(serverUrl) - url = UrlUtils.AppendQueryParams(url, { server: serverUrl }) + profileId = m.top.profileId + if not StringUtils.IsNullOrEmpty(profileId) + url = UrlUtils.AppendQueryParams(url, { profile: profileId }) end if m.top.url = url else diff --git a/playlet-lib/src/components/Dialog/InvidiousLoginDialog.xml b/playlet-lib/src/components/Dialog/InvidiousLoginDialog.xml index 463f9f35..fff03170 100644 --- a/playlet-lib/src/components/Dialog/InvidiousLoginDialog.xml +++ b/playlet-lib/src/components/Dialog/InvidiousLoginDialog.xml @@ -3,7 +3,7 @@ - + diff --git a/playlet-lib/src/components/Screens/ProfileScreen/LoginUtils.bs b/playlet-lib/src/components/Screens/ProfileScreen/LoginUtils.bs index 70455f78..640d89d1 100644 --- a/playlet-lib/src/components/Screens/ProfileScreen/LoginUtils.bs +++ b/playlet-lib/src/components/Screens/ProfileScreen/LoginUtils.bs @@ -4,9 +4,9 @@ import "pkg:/source/utils/Types.bs" namespace LoginUtils - function ShowInvidiousLoginDialog(serverUrl = "" as string) as void + function ShowInvidiousLoginDialog(profileId = "" as string) as void dialog = CreateObject("roSGNode", "InvidiousLoginDialog") - dialog.serverUrl = serverUrl + dialog.profileId = profileId dialog@.BindNode() m.top.getScene().dialog = dialog end function diff --git a/playlet-lib/src/components/Screens/ProfileScreen/ProfileView/ProfileView.bs b/playlet-lib/src/components/Screens/ProfileScreen/ProfileView/ProfileView.bs index 9bd6d240..f52d7bde 100644 --- a/playlet-lib/src/components/Screens/ProfileScreen/ProfileView/ProfileView.bs +++ b/playlet-lib/src/components/Screens/ProfileScreen/ProfileView/ProfileView.bs @@ -134,7 +134,7 @@ function Login() as void backendType = content.type if backendType = "invidious" - LoginUtils.ShowInvidiousLoginDialog(ValidString(content.serverUrl)) + LoginUtils.ShowInvidiousLoginDialog(content.id) else if backendType = "youtube" LoginUtils.ShowYouTubeLoginDialog() else diff --git a/playlet-lib/src/components/Services/Invidious/Invidious.bs b/playlet-lib/src/components/Services/Invidious/Invidious.bs index 478a8a6b..e24087c9 100644 --- a/playlet-lib/src/components/Services/Invidious/Invidious.bs +++ b/playlet-lib/src/components/Services/Invidious/Invidious.bs @@ -12,7 +12,3 @@ end function function GetInvidiousInstance(_unused as dynamic) as string return m.service.GetInvidiousInstance() end function - -function GetAuthorizeTokenLink(serverUrl as dynamic) as dynamic - return m.service.GetAuthorizeTokenLink(ValidString(serverUrl)) -end function diff --git a/playlet-lib/src/components/Services/Invidious/Invidious.xml b/playlet-lib/src/components/Services/Invidious/Invidious.xml index 3a293aa6..5975c023 100644 --- a/playlet-lib/src/components/Services/Invidious/Invidious.xml +++ b/playlet-lib/src/components/Services/Invidious/Invidious.xml @@ -9,6 +9,5 @@ - \ No newline at end of file diff --git a/playlet-lib/src/components/Services/Invidious/InvidiousService.bs b/playlet-lib/src/components/Services/Invidious/InvidiousService.bs index 5e7df7de..cfbdc5ab 100644 --- a/playlet-lib/src/components/Services/Invidious/InvidiousService.bs +++ b/playlet-lib/src/components/Services/Invidious/InvidiousService.bs @@ -750,7 +750,7 @@ namespace Invidious return request.Await() end function - function GetAuthorizeTokenLink(serverUrl = "" as string) as dynamic + function GetAuthorizeTokenLink(serverUrl as string, loginNonce as string) as dynamic instance = serverUrl if StringUtils.IsNullOrEmpty(instance) instance = m.GetInvidiousInstance() @@ -769,7 +769,7 @@ namespace Invidious expire = TimeUtils.Now().AsSeconds() + Invidious.TOKEN_TIMESPAN - callbackUrl = `${address}${Invidious.TOKEN_CALLBACK}?ref=${instance}`.EncodeUriComponent() + callbackUrl = `${address}${Invidious.TOKEN_CALLBACK}?ref=${instance}&nonce=${loginNonce}`.EncodeUriComponent() scope = `${Invidious.AUTH_SCOPES}`.EncodeUriComponent() return `${invidiousHost}/authorize_token?scopes=${scope}&callback_url=${callbackUrl}&expire=${expire}` end function diff --git a/playlet-lib/src/components/Web/PlayletWebServer/Middleware/InvidiousRouter.bs b/playlet-lib/src/components/Web/PlayletWebServer/Middleware/InvidiousRouter.bs index c3d36002..f500ed38 100644 --- a/playlet-lib/src/components/Web/PlayletWebServer/Middleware/InvidiousRouter.bs +++ b/playlet-lib/src/components/Web/PlayletWebServer/Middleware/InvidiousRouter.bs @@ -21,9 +21,23 @@ namespace Http request = context.request response = context.response - server = ValidString(request.query["server"]) + ' The login link carries the nonce, so its server comes from the + ' device (a profile's, or the configured instance), never the URL. + server = "" + profileId = request.query["profile"] + if profileId <> invalid + server = m.GetProfileServer(ValidString(profileId)) + if server = invalid + response.Default(400, "Unknown profile") + return true + end if + end if - authLink = m.invidiousNode@.GetAuthorizeTokenLink(server) + if m.loginNonce = invalid + m.loginNonce = CreateObject("roDeviceInfo").GetRandomUUID() + end if + + authLink = m.invidiousService.GetAuthorizeTokenLink(server, m.loginNonce) if authLink = invalid response.Default(503, "Invidious server not configured") return true @@ -37,7 +51,16 @@ namespace Http request = context.request response = context.response - if request.query["token"] <> invalid and request.query["ref"] <> invalid + if request.query["token"] <> invalid and request.query["ref"] <> invalid and request.query["username"] <> invalid + ' The callback is a plain GET any web page can trigger, so it only + ' accepts a login that went through a link this device handed out. + nonce = request.query["nonce"] + if m.loginNonce = invalid or not IsString(nonce) or nonce <> m.loginNonce + response.Default(403, "Invalid login link. Please start the login again.") + return true + end if + m.loginNonce = invalid + ' Invidious encodes token and username twice token = request.query["token"].DecodeUriComponent().DecodeUriComponent() username = request.query["username"].DecodeUriComponent().DecodeUriComponent() @@ -54,6 +77,17 @@ namespace Http return true end function + function GetProfileServer(profileId as string) as dynamic + profilesDto = m.profilesNode@.GetProfilesDto(false) + profiles = profilesDto.profiles + for each profile in profiles + if profile.id = profileId and profile.type = "invidious" + return profile.serverUrl + end if + end for + return invalid + end function + ' This is an endpoint allowing the web app to make an authenticated request ' without needing the Invidious token ' This is a GET not a POST for better caching diff --git a/playlet-lib/src/components/Web/PlayletWebServer/Middleware/StateApiRouter.bs b/playlet-lib/src/components/Web/PlayletWebServer/Middleware/StateApiRouter.bs index 1b64deef..5b85e467 100644 --- a/playlet-lib/src/components/Web/PlayletWebServer/Middleware/StateApiRouter.bs +++ b/playlet-lib/src/components/Web/PlayletWebServer/Middleware/StateApiRouter.bs @@ -61,7 +61,6 @@ namespace Http state["invidious"] = { instance: m.invidiousNode@.GetInstance() invidious_instance: m.invidiousNode@.GetInvidiousInstance() - auth_url: m.invidiousNode@.GetAuthorizeTokenLink() } end if diff --git a/playlet-web/src/lib/ProfilesDialog.svelte b/playlet-web/src/lib/ProfilesDialog.svelte index 3443826c..9ae37df4 100644 --- a/playlet-web/src/lib/ProfilesDialog.svelte +++ b/playlet-web/src/lib/ProfilesDialog.svelte @@ -23,14 +23,12 @@ let profiles = []; let currentProfile; - let authUrl; let currentInstance; let currentInstanceName; let accordionState = null; playletStateStore.subscribe((value) => { - authUrl = value?.invidious?.auth_url; currentInstance = value?.invidious?.invidious_instance || ""; if (!currentInstance) { const trFn = get(translate); @@ -72,12 +70,16 @@ return; } - if (!authUrl) { - alert("Error with login, please refresh the page."); + window.location.href = "/invidious/login"; + } + + function relogin(profile) { + if (profile.type === "youtube") { + loginToYoutube(); return; } - window.location = authUrl; + window.location.href = `/invidious/login?profile=${encodeURIComponent(profile.id)}`; } function loginToYoutube() { @@ -133,6 +135,10 @@ "The session has expired. Please sign in again with this profile, or switch to another profile." )} + {/if} {#if profile.id !== currentProfile?.id}