Skip to content

Extract shared OAuthTokenSource from pkg/registry/auth and pkg/llm #5050

Closed

Description

@yrobla

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 gets LLM_OAUTH_)
  • A ConfigPersister callback (replaces both the hardcoded config.UpdateConfig in the registry side and the injected TokenRefUpdater in the LLM side)

The LLM implementation is the better starting point — it has:

  • Principled lastErr propagation (vs silent error swallow in registry)
  • errCacheMiss sentinel + IsNotFoundError classification
  • PersistingTokenSource wrapping on tryRestoreFromCache (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/tokensource parameterized as above
  • Both pkg/llm/tokensource.go and pkg/registry/auth/oauth_token_source.go become thin wrappers
  • Registry tryRestoreFromCache wraps with PersistingTokenSource (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)

Acceptance criteria

Activity

  1. added
    go
    on Apr 24, 2026
  2. added 5 commits that reference this issue on Apr 28, 2026
    471470b
    a91f841
    b6f011f
    5b09a41
    9c66743
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    authenticationgotech-debt

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions