Repository navigation
Description
Background
pkg/llm/tokensource.go (introduced in the thv-llm-token branch) and pkg/registry/auth/oauth_token_source.go share substantial duplication: ensureOfflineAccess is byte-for-byte identical; DeriveSecretKey differs only in prefix; Token, tryRestoreFromCache, performBrowserFlow, buildFlowConfig, buildOAuth2Config, makeTokenPersister, and refreshTokenKey are near-identical in structure.
Flagged in PR review by @aponcedeleonch.
Also absorbs #5066 (factor shared non-caching refresher and lift duplicated helpers).
Goal
Extract a shared OAuthTokenSource into pkg/auth/tokensource/ parameterized by:
- An OIDC config getter (issuer, clientID, scopes, audience, callbackPort)
- A secret-key derivation function (so registry gets
REGISTRY_OAUTH_prefix, LLM getsLLM_OAUTH_) - A
ConfigPersistercallback (replaces both the hardcodedconfig.UpdateConfigin the registry side and the injectedTokenRefUpdaterin the LLM side)
The LLM implementation is the better starting point — it has:
- Principled
lastErrpropagation (vs silent error swallow in registry) errCacheMisssentinel +IsNotFoundErrorclassificationPersistingTokenSourcewrapping ontryRestoreFromCache(registry side is missing this — latent bug: rotated refresh tokens aren't re-persisted on cache restore)- Injected callback for config persistence (more testable than direct
config.UpdateConfig)
The genuinely new capabilities — preemptive refresh window and access-token cross-process cache — should apply to both consumers after extraction.
Scope (including #5066)
Shared OAuthTokenSource (pkg/auth/tokensource)
- Extract into
pkg/auth/tokensourceparameterized as above - Both
pkg/llm/tokensource.goandpkg/registry/auth/oauth_token_source.gobecome thin wrappers - Registry
tryRestoreFromCachewraps withPersistingTokenSource(parity with LLM fix)
Lifted helpers
ensureOfflineAccess→tokensource.EnsureOfflineAccess(was byte-identical in both packages)DeriveSecretKey→tokensource.DeriveSecretKey(prefix, url, issuer)(parameterized prefix)
Non-caching refresher (from #5066)
NonCachingRefresheralready extracted intopkg/auth/oauth(landed in pkg/llm: fix withPreemptiveRefresh composition and stale _AT on rotation #5052)resourceTokenSourcealready delegates to it- Duplication in
pkg/llm.nonCachingRefresherremoved as part of this extraction
Acceptance criteria
- Shared
OAuthTokenSourceinpkg/auth/tokensourceparameterized as above -
pkg/llm/tokensource.gouses the shared type -
pkg/registry/auth/oauth_token_source.gomigrated to shared type (deleted) - Registry
tryRestoreFromCachewraps withPersistingTokenSource(parity with LLM fix) -
EnsureOfflineAccessandDeriveSecretKeylifted to shared package (closes pkg/auth/oauth: factor shared non-caching refresher and lift duplicated helpers #5066 scope 2) -
pkg/llm.nonCachingRefresherremoved — usesoauth.NonCachingRefresher(closes pkg/auth/oauth: factor shared non-caching refresher and lift duplicated helpers #5066 scope 1) - No behaviour change in either consumer (intentional improvement: backend errors now surface specifically in registry path)
Activity
Extract shared OAuthTokenSource into pkg/auth/tokensource
Extract shared OAuthTokenSource into pkg/auth/tokensource
Extract shared OAuthTokenSource into pkg/auth/tokensource
Extract shared OAuthTokenSource into pkg/auth/tokensource
Extract shared OAuthTokenSource into pkg/auth/tokensource (#5090)