Save scope and expire fields with auth token

This commit is contained in:
Brahim Hadriche committed 2023-07-07 13:28:42 -04:00
1 parent e49aa9b7d8
commit 3add083f6b
5 files changed
+88 -11

No files matched your search

+1
View File
@@ -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 - Links to the app info in web app
- Loading indicator for search page in web app - Loading indicator for search page in web app
- Feedback link through email - 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 ### Fixed
@@ -19,11 +19,17 @@ namespace Http
token = request.query["token"].DecodeUri().DecodeUriComponent() token = request.query["token"].DecodeUri().DecodeUriComponent()
referer = request.query["ref"].DecodeUri() referer = request.query["ref"].DecodeUri()
username = request.query["username"] username = request.query["username"]
scopes = request.query["scopes"]
expire = request.query["expire"]
if username <> invalid if username <> invalid
username = username.DecodeUriComponent().DecodeUriComponent() username = username.DecodeUriComponent().DecodeUriComponent()
end if 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 server.task.top.login = true
end if end if
response.Redirect("/") response.Redirect("/")
+71 -7
View File
@@ -18,10 +18,11 @@ namespace Invidious
const SEARCH_SUGGEST_ENDPOINT = "/api/v1/search/suggestions" const SEARCH_SUGGEST_ENDPOINT = "/api/v1/search/suggestions"
const SEARCH_ENDPOINT = "/api/v1/search" 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_FEED = "/api/v1/auth/feed"
const AUTH_PLAYLISTS = "/api/v1/auth/playlists" const AUTH_PLAYLISTS = "/api/v1/auth/playlists"
const AUTH_TOKENS_UNREGISTER = "/api/v1/auth/tokens/unregister" const AUTH_TOKENS_UNREGISTER = "/api/v1/auth/tokens/unregister"
const AUTH_TOKENS_REGISTER = "/api/v1/auth/tokens/register"
function DefaultArgs() as object function DefaultArgs() as object
return { return {
@@ -155,7 +156,7 @@ namespace Invidious
expire = TimeUtils.Now().AsSeconds() + 60 * 60 * 24 * 365 * 2 '2 years from now expire = TimeUtils.Now().AsSeconds() + 60 * 60 * 24 * 365 * 2 '2 years from now
callbackUrl = `http://${localIp}:${webServerPort}${Invidious.TOKEN_CALLBACK}?ref=${instance}`.EncodeUriComponent() 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}` return `${invidiousHost}/authorize_token?scopes=${scope}&callback_url=${callbackUrl}&expire=${expire}`
end function end function
@@ -165,11 +166,63 @@ namespace Invidious
} }
end function end function
function Logout() as boolean function RefreshAuthToken() as boolean
authToken = InvidiousSettings.GetAuthToken() currentAuthToken = InvidiousSettings.GetAuthToken()
if authToken = invalid if currentAuthToken = invalid
return false return false
end if 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) headers = Invidious.GetAuthenticationHeaders(authToken.token)
url = `${authToken.instance}${Invidious.AUTH_TOKENS_UNREGISTER}` url = `${authToken.instance}${Invidious.AUTH_TOKENS_UNREGISTER}`
@@ -178,10 +231,21 @@ namespace Invidious
headers["Content-Type"] = "application/json" headers["Content-Type"] = "application/json"
args = { args = {
headers: headers, 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) RegistryUtils.Delete(RegistryUtils.INVIDIOUS_TOKEN)
return true return true
@@ -21,7 +21,7 @@ namespace InvidiousSettings
return InvidiousSettings.DEFAULT_INSTANCE return InvidiousSettings.DEFAULT_INSTANCE
end function 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 = { obj = {
token: token, token: token,
instance: instance instance: instance
@@ -29,6 +29,12 @@ namespace InvidiousSettings
if username <> invalid if username <> invalid
obj.username = username obj.username = username
end if end if
if scopes <> invalid
obj.scopes = scopes
end if
if expire <> invalid
obj.expire = expire
end if
json = FormatJson(obj) json = FormatJson(obj)
RegistryUtils.Write(RegistryUtils.INVIDIOUS_TOKEN, json) RegistryUtils.Write(RegistryUtils.INVIDIOUS_TOKEN, json)
+2 -2
View File
@@ -203,10 +203,10 @@ async function getAccessToken(invidiousInstance) {
server.close() server.close()
}) })
const scope = ":*" const scopes = ":*"
const expire = Date.now() + 60 * 60 * 2; const expire = Date.now() + 60 * 60 * 2;
const callbackUrl = `http://${ip.address()}:${port}/invidious/token_callback` 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, () => { server = app.listen(port, () => {
console.log(`server is listening on http://localhost:${port}`); console.log(`server is listening on http://localhost:${port}`);