Skip to content

Non-expiring upstream tokens incorrectly marked as 1h-expiring #5046

Description

@lorr1

Summary

Non-expiring upstream tokens (Slack user tokens, GitHub user tokens) are incorrectly marked as expiring in 1 hour, causing the refresh path to fire unnecessarily and — when the provider returns the same non-expiring token — creating churn or failures on subsequent lookups.

Root cause

pkg/authserver/upstream/oauth2.go:224-228 fabricates a 1-hour expiry whenever the upstream token response has no expires_in:

expiresAt := token.Expiry
if expiresAt.IsZero() {
    // Default to 1 hour if not specified
    expiresAt = time.Now().Add(defaultTokenExpiration)
}

convertOAuth2Token is called from both the initial code exchange (ExchangeCode, line ~455) and subsequent refreshes (RefreshTokens, line ~495), so the fabricated expiry taints every leg.

Downstream code at pkg/auth/upstreamtoken/service.go:104-108 explicitly special-cases a zero ExpiresAt as "never expires, don't refresh":

if tokens.ExpiresAt.IsZero() || !tokens.IsExpired(time.Now()) {
    result[providerName] = tokens.AccessToken
    continue
}

This correct branch is defeated by the fabricated 1h value.

Affected providers

Any upstream that does not return expires_in:

  • Slack user tokens (non-rotating, do not expire)
  • GitHub user access tokens without expiration
  • Any OAuth 2.0 provider configured to issue long-lived tokens

Proposed fix

Two changes are required — removing only the oauth2.go fabrication is insufficient because the storage layer also applies a default TTL.

  1. pkg/authserver/upstream/oauth2.go:224-228 — delete the if expiresAt.IsZero() block and let ExpiresAt remain zero. Also remove the unused defaultTokenExpiration constant at line 93-94.

  2. Storage layer default TTL — pkg/authserver/storage/memory.go:705-712 and pkg/authserver/storage/redis.go:888-896 currently apply a default TTL to upstream token records when ExpiresAt is zero. For records explicitly marked as non-expiring, they should be stored with no TTL (memory) or a configured long TTL (Redis). Alternatively, carry a separate NeverExpires bool field on UpstreamTokens to disambiguate.

Related

Compounds with #5047 to produce the observed symptom of "all upstream providers require re-authorization every ~24 hours." This issue (fake expiry) triggers the re-authorization cascade; the linked issue (session-keyed storage + callback token clobber) ensures the cascade is fatal.

Reproduction

  1. Configure ToolHive with a Slack OAuth upstream.
  2. Complete initial authorization. Observe Slack token is stored.
  3. Wait 1 hour.
  4. Trigger a downstream request that reads the Slack token.
  5. Observe the refresher is invoked even though Slack issued a non-expiring token.

Activity

  1. added
    bug
    on Apr 23, 2026
  2. self-assigned this
    on Apr 27, 2026
  3. added theissue type on Apr 28, 2026
  4. added
    applied-ai
    on Apr 28, 2026
  5. added 2 commits that reference this issue on Apr 28, 2026
    c1aa1be
    a98c991
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

applied-aiauthbugoauth

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions