oauth refactor

This commit is contained in:
Brahim Hadriche committed 2026-07-03 21:34:35 -04:00
1 parent 78ecdec28d
commit 6cb5bfe985
40 files changed
+782 -301

No files matched your search

@@ -0,0 +1,51 @@
import "pkg:/components/Services/Innertube/InnertubeService.bs"
import "pkg:/components/Services/Innertube/OAuth.bs"
import "pkg:/source/utils/StringUtils.bs"
namespace Tests
@tags("integration")
@suite
class InnertubeRefreshTokenIntegrationTests extends Rooibos.BaseTestSuite
protected override function setup()
m.logger = CreateObject("roSGNode", "Logger")
m.logger.logLevel = -1
' A failed refresh goes through HttpClient's retry loop, which emits a
' telemetry warning; give the stub a no-op telemetry sink to write to.
telemetry = CreateObject("roSGNode", "Node")
telemetry.addField("LogWarn", "string", false)
telemetry.addField("LogEvent", "assocarray", false)
m.global.addFields({ telemetry: telemetry })
end function
@describe("Innertube refresh-token terminal classification (live)")
@slow(20000)
@it("A dead refresh token throws an error classified terminal (so it is cleared + re-auth, not retry-stormed)")
function _()
' A real client_id/secret with an invalid refresh token gets a real invalid_grant from Google.
identity = Innertube.GetClientIdentity(invalid)
m.assertFalse(IsAssociativeArray(identity) and identity.DoesExist("error"), "GetClientIdentity failed: " + FormatJson(identity))
m.assertFalse(StringUtils.IsNullOrEmpty(identity.clientId), "no client id scraped")
accessTokenObject = {
clientId: identity.clientId
clientSecret: identity.clientSecret
refreshToken: "invalid-refresh-token"
}
threw = false
try
InnertubeService.AuthRefreshAccessToken(accessTokenObject, invalid)
catch error
threw = true
end try
m.assertTrue(threw, "a dead refresh token must surface as a thrown error")
m.assertTrue(ValidBool(accessTokenObject.terminalRefreshError), "a dead refresh token must flag terminalRefreshError on the shared accessToken object")
end function
end class
end namespace
@@ -0,0 +1,27 @@
import "pkg:/components/Services/Innertube/OAuth.bs"
namespace Tests
@suite
class InnertubeOAuthTerminalErrorTests extends Rooibos.BaseTestSuite
@describe("Innertube.IsTerminalRefreshError")
@it("Returns true for terminal OAuth error codes (RFC 6749 §5.2)")
function _()
m.assertTrue(Innertube.IsTerminalRefreshError({ error: "invalid_grant" }))
m.assertTrue(Innertube.IsTerminalRefreshError({ error: "invalid_client" }))
m.assertTrue(Innertube.IsTerminalRefreshError({ error: "unauthorized_client" }))
end function
@it("Returns false for transient, successful, or non-object bodies")
function _()
m.assertFalse(Innertube.IsTerminalRefreshError({ error: "authorization_pending" }))
m.assertFalse(Innertube.IsTerminalRefreshError({ access_token: "ya29.abc" }))
m.assertFalse(Innertube.IsTerminalRefreshError({}))
m.assertFalse(Innertube.IsTerminalRefreshError(invalid))
end function
end class
end namespace
@@ -0,0 +1,135 @@
import "pkg:/components/ContentNode/ProfileContentAuthTokenUtils.bs"
namespace Tests
@SGNode("Group")
@suite
class ProfileContentAuthTokenUtilsTests extends Rooibos.BaseTestSuite
@describe("ProfileContentAuthTokenUtils.DeriveAuthState")
@it("A YouTube profile with a refresh token is Authenticated, even with a stale access token")
function _()
m.assertEqual(ProfileContentAuthTokenUtils.DeriveAuthState("youtube", "", "rt"), ProfileContentAuthTokenUtils.ProfileAuthState.Authenticated)
end function
@it("A YouTube profile without a refresh token is NeedsReauth, even with an access token")
function _()
m.assertEqual(ProfileContentAuthTokenUtils.DeriveAuthState("youtube", "stale", ""), ProfileContentAuthTokenUtils.ProfileAuthState.NeedsReauth)
end function
@it("An Invidious profile derives from its access token")
function _()
m.assertEqual(ProfileContentAuthTokenUtils.DeriveAuthState("invidious", "token", ""), ProfileContentAuthTokenUtils.ProfileAuthState.Authenticated)
m.assertEqual(ProfileContentAuthTokenUtils.DeriveAuthState("invidious", "", ""), ProfileContentAuthTokenUtils.ProfileAuthState.NeedsReauth)
end function
@it("An Invidious token past its embedded expire is NeedsReauth")
function _()
nowSeconds = CreateObject("roDateTime").AsSeconds()
expired = FormatJson({ session: "v1:abc", expire: nowSeconds - 60, scopes: ["GET:subscriptions"], signature: "sig" })
m.assertEqual(ProfileContentAuthTokenUtils.DeriveAuthState("invidious", expired, ""), ProfileContentAuthTokenUtils.ProfileAuthState.NeedsReauth)
end function
@it("An Invidious token with a future expire is Authenticated")
function _()
nowSeconds = CreateObject("roDateTime").AsSeconds()
live = FormatJson({ session: "v1:abc", expire: nowSeconds + 3600, scopes: ["GET:subscriptions"], signature: "sig" })
m.assertEqual(ProfileContentAuthTokenUtils.DeriveAuthState("invidious", live, ""), ProfileContentAuthTokenUtils.ProfileAuthState.Authenticated)
end function
@describe("ProfileContentAuthTokenUtils.EnsureFreshAccessToken")
@it("Reports NeedsReauth for a NeedsReauth YouTube profile without starting a refresh")
function _()
node = CreateObject("roSGNode", "ProfileContentNode")
node.type = "youtube"
node.authState = ProfileContentAuthTokenUtils.ProfileAuthState.NeedsReauth
result = ProfileContentAuthTokenUtils.EnsureFreshAccessToken(node)
m.assertEqual(result.state, ProfileContentAuthTokenUtils.AccessTokenState.NeedsReauth)
m.assertEqual(result.token, "")
end function
@it("Reports NeedsReauth for a NeedsReauth Invidious profile")
function _()
node = CreateObject("roSGNode", "ProfileContentNode")
node.type = "invidious"
node.authState = ProfileContentAuthTokenUtils.ProfileAuthState.NeedsReauth
result = ProfileContentAuthTokenUtils.EnsureFreshAccessToken(node)
m.assertEqual(result.state, ProfileContentAuthTokenUtils.AccessTokenState.NeedsReauth)
m.assertEqual(result.token, "")
end function
@it("Returns the token of an authenticated Invidious profile")
function _()
node = CreateObject("roSGNode", "ProfileContentNode")
node.type = "invidious"
node.accessToken = "inv-token"
result = ProfileContentAuthTokenUtils.EnsureFreshAccessToken(node)
m.assertEqual(result.state, ProfileContentAuthTokenUtils.AccessTokenState.Ok)
m.assertEqual(result.token, "inv-token")
end function
@it("Carries the request context so consumers need no further profile reads")
function _()
node = CreateObject("roSGNode", "ProfileContentNode")
node.type = "invidious"
node.accessToken = "inv-token"
node.serverUrl = "https://instance.example"
node.activeIdentifyToken = "delegation-token"
result = ProfileContentAuthTokenUtils.EnsureFreshAccessToken(node)
m.assertEqual(result.serverUrl, "https://instance.example")
m.assertEqual(result.activeIdentifyToken, "delegation-token")
m.assertTrue(result.profile.isSameNode(node))
end function
@it("Returns a still-valid YouTube token without refreshing")
function _()
node = CreateObject("roSGNode", "ProfileContentNode")
node.type = "youtube"
node.accessToken = "fresh-token"
node.refreshToken = "rt"
expiresTimestamp = CreateObject("roDateTime").AsSeconds() + 7200
node.expiresTimestamp = expiresTimestamp
result = ProfileContentAuthTokenUtils.EnsureFreshAccessToken(node)
m.assertEqual(result.state, ProfileContentAuthTokenUtils.AccessTokenState.Ok)
m.assertEqual(result.token, "fresh-token")
m.assertTrue(result.freshUntilSeconds = expiresTimestamp - 3600)
end function
@it("An empty access token with a live-looking expiry resolves immediately instead of waiting on a refresh that was never started")
function _()
' No current code writes this state; the registry is a shared boundary (other
' builds, tooling), and no stored state may park a waiter on a dead 30s wait.
node = CreateObject("roSGNode", "ProfileContentNode")
node.type = "youtube"
node.accessToken = ""
node.refreshToken = ""
node.expiresTimestamp = CreateObject("roDateTime").AsSeconds() + 7200
result = ProfileContentAuthTokenUtils.EnsureFreshAccessToken(node)
m.assertEqual(result.state, ProfileContentAuthTokenUtils.AccessTokenState.NeedsReauth)
m.assertEqual(node.authState, ProfileContentAuthTokenUtils.ProfileAuthState.NeedsReauth)
end function
@describe("ProfileContentNode.MarkNeedsReauth")
@it("Clears the credential and marks the profile in one transition")
function _()
node = CreateObject("roSGNode", "ProfileContentNode")
node.type = "youtube"
node.username = "dying"
node.accessToken = "tok"
node.refreshToken = "rt"
node.expiresTimestamp = 12345
node@.MarkNeedsReauth()
m.assertEqual(node.accessToken, "")
m.assertEqual(node.refreshToken, "")
m.assertEqual(node.expiresTimestamp, 0&)
m.assertEqual(node.authState, ProfileContentAuthTokenUtils.ProfileAuthState.NeedsReauth)
end function
end class
end namespace