Add nouce to invidious login

This commit is contained in:
Brahim Hadriche committed 2026-10-08 11:31:52 -04:00
1 parent eea5392d9d
commit b02d5d6a10
10 files changed
+57 -23

No files matched your search

@@ -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
@@ -3,7 +3,7 @@
<field id="webServer" type="node" bind="/WebServer" />
<field id="profilesService" type="node" bind="/ProfilesService" />
<field id="url" type="string" onChange="OnUrlSet" />
<field id="serverUrl" type="string" />
<field id="profileId" type="string" />
<field id="alwaysOnTop" type="boolean" />
</interface>
<children>
@@ -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
@@ -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
@@ -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
@@ -9,6 +9,5 @@
<function name="GetInstance" />
<function name="GetInvidiousInstance" />
<function name="GetAuthorizeTokenLink" />
</interface>
</component>
@@ -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
@@ -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
@@ -61,7 +61,6 @@ namespace Http
state["invidious"] = {
instance: m.invidiousNode@.GetInstance()
invidious_instance: m.invidiousNode@.GetInvidiousInstance()
auth_url: m.invidiousNode@.GetAuthorizeTokenLink()
}
end if
+11 -5
View File
@@ -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."
)}
</div>
<button
on:click={() => relogin(profile)}
class="btn btn-primary m-1">{$translate("Login")}</button
>
{/if}
{#if profile.id !== currentProfile?.id}
<button