Files
playlet-piped/playlet-lib/src/components/Web/PlayletWebServer/Middleware/InvidiousRouter.bs
T
Brahim Hadricheandgithub-action linter d70a07049a Add Job System (#656)
* Add Job System

* Remove testing stuff

* changelog

* plugin work

* Convert a couple more jobs

* simplify callback

* assert

* Logging changes and rendezvous perf

* changelog

* sb segment viewed job

* clear cache, and few changes

* Lint fix

* a couple more jobs

* reduce rendezvous in web server

* refactor

* Lint fix

* no message

* test shutdown

* comment

* free fields

---------

Co-authored-by: github-action linter <githubaction@githubaction.com>
2025-06-17 20:13:48 -04:00

96 lines
3.3 KiB
Plaintext

import "pkg:/components/Services/Invidious/InvidiousService.bs"
import "pkg:/components/Web/WebServer/Http/HttpRouter.bs"
import "pkg:/source/utils/CryptoUtils.bs"
namespace Http
class InvidiousRouter extends HttpRouter
function new(server as object)
super()
m.server = server
m.profilesNode = server.sceneNodes.profilesService
m.invidiousNode = server.sceneNodes.invidious
m.invidiousService = new Invidious.InvidiousService(m.invidiousNode)
end function
@get("/invidious/login")
function Login(context as object) as boolean
response = context.response
authLink = m.invidiousNode@.GetAuthorizeTokenLink()
response.Redirect(authLink)
return true
end function
@get("/invidious/token_callback")
function OnTokenReceived(context as object) as boolean
request = context.request
response = context.response
if request.query["token"] <> invalid and request.query["ref"] <> invalid
' Invidious encodes token and username twice
token = request.query["token"].DecodeUriComponent().DecodeUriComponent()
username = request.query["username"].DecodeUriComponent().DecodeUriComponent()
referer = request.query["ref"].DecodeUriComponent()
profile = CreateObject("roSGNode", "ProfileContentNode")
profile.username = username
profile.accessToken = token
profile.serverUrl = referer
m.profilesNode@.LoginWithProfile(profile)
end if
response.Redirect("/")
return true
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
@get("/invidious/authenticated-request")
function AuthRequestProxy(context as object) as boolean
request = context.request
response = context.response
token = m.invidiousNode.authToken
if token = invalid
response.Default(403, "No token, not logged in")
return true
end if
feedSource = request.query["feed-source"]
if feedSource = invalid
response.Default(400, "Expected feed-source")
return true
end if
feedSource = ParseJson(feedSource)
if feedSource = invalid
response.Default(400, "Expected valid JSON for feed-source")
return true
end if
options = {
httpServer: m.server
}
if feedSource.state <> invalid and feedSource.state.queryParams <> invalid
options.additionalQueryParams = feedSource.state.queryParams
end if
invResponse = m.invidiousService.MakeRequest(feedSource, options)
if not invResponse.success
response.Default(500, `Failed to make request: ${invResponse.error}`)
return true
end if
response.Json(invResponse.result)
return true
end function
end class
end namespace