Skip to content

fix(oauth): treat zero ExpiresAt as never-expires in HasPersistedToken - #2

Merged
electrolobzik merged 1 commit into
halo-mainfrom
fix/oauth-zero-expiry-halo-main
May 12, 2026
Merged

electrolobzik merged 1 commit into
halo-mainfrom
fix/oauth-zero-expiry-halo-main

Conversation

@electrolobzik

@electrolobzik electrolobzik commented May 12, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Fork mirror of the upstream PR (smart-mcp-proxy#453) so halo-main gets the fix immediately rather than waiting for upstream review.

Replaces #1, which had the wrong base (branch was forked off upstream/main, so the diff included all of halo-main..upstream/main). This branch is cherry-picked onto halo-main, so the diff is only the 3 oauth files.

What this fixes

OAuth re-auth loop observed against Atlassian's MCP endpoint (https://mcp.atlassian.com/v1/mcp) — authorization succeeds in the browser, the token is persisted, but HasPersistedToken was treating time.Time{} ExpiresAt as expired (time.Now().After(time.Time{}) is always true), so every reconnect looped asking for re-auth even though a valid bearer token was on disk.

Details, root cause, and test coverage: see the upstream PR.

Test plan

  • go test ./internal/oauth/ — green (14.9 s)
  • go test ./internal/upstream/... — green
  • Reproduced fix end-to-end against Atlassian with a patched binary — upstream stays connected across restarts after a single OAuth login

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes

    • Resolved an issue where OAuth tokens without expiration information were incorrectly marked as expired, causing repeated authentication requests for compatible providers.
  • Tests

    • Added test coverage validating proper handling of tokens without expiration data, future expiration, and past expiration scenarios.

Review Change Stack

Authorization servers that omit `expires_in` from the /token response
(e.g. Atlassian's MCP endpoint at https://mcp.atlassian.com/v1/mcp)
land in BBolt with ExpiresAt = time.Time{}. HasValidToken and mcp-go's
own Token.IsExpired() already treat that as "never expires", but
HasPersistedToken was computing `time.Now().After(time.Time{})` which
is always true — so the persisted token was reported as expired on
every reconnect, skipBrowserFlow stayed false, and the upstream
looped forever asking for re-auth even though a valid access token
sat in storage.

Fix HasPersistedToken to short-circuit on IsZero(), matching
HasValidToken. Also clean up PersistentTokenStore.GetToken so it no
longer logs a spurious "token has expired" warning for those records.

Add three unit tests covering the zero / future / past ExpiresAt
cases against HasPersistedToken so the IsZero branch can't regress.
@coderabbitai

coderabbitai Bot commented May 12, 2026 •

Copy link
Copy Markdown

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 2ed52dec-3347-4702-a184-d00d3740450e

📥 Commits

Reviewing files that changed from the base of the PR and between 2b9b5f9 and 7b26418.

📒 Files selected for processing (3)
  • internal/oauth/config.go
  • internal/oauth/config_test.go
  • internal/oauth/persistent_token_store.go

📝 Walkthrough

Walkthrough

OAuth token expiration handling is updated to recognize tokens with unset ExpiresAt (Go zero time) as indefinitely valid. Both HasPersistedToken and GetToken now check ExpiresAt.IsZero() to avoid false expiry for providers omitting expires_in. Three test cases validate the new semantics.

Changes

OAuth Zero-Expiry Token Semantics

Layer / File(s) Summary
Zero-expiry handling in HasPersistedToken
internal/oauth/config.go, internal/oauth/config_test.go
HasPersistedToken checks record.ExpiresAt.IsZero() to treat zero time as not expired. Documentation clarifies the behavior. Three test cases validate zero, future, and past expiry outcomes.
Zero-expiry handling in GetToken
internal/oauth/persistent_token_store.go
GetToken introduces hasExpiry to gate expiration calculations, computes timeUntilExpiry, isExpired, and needsRefresh only for non-zero ExpiresAt, and uses switch-based logging to distinguish no-expiration, expired, and valid states.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Poem

🐰 Zero time, no longer cursed—
Your tokens need not expire first!
Switch-based logic, clean and bright,
Expiration now feels just right. 🕐✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically describes the main fix: treating zero ExpiresAt as never-expires in HasPersistedToken, which is the core change addressing the OAuth re-auth loop.
Description check ✅ Passed The description provides context (upstream PR mirror), explains the OAuth re-auth loop issue, details root cause, lists test coverage, and includes test plan results, though it lacks explicit checkboxes for required template sections.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/oauth-zero-expiry-halo-main

Comment @coderabbitai help to get the list of available commands and usage tips.

@electrolobzik
electrolobzik merged commit 089facf into halo-main May 12, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant