Skip to content

fix: reuse persisted auth tokens across server restarts - #62

Open
mjdavidson wants to merge 1 commit into
okta:mainfrom
mjdavidson:fix/persist-tokens-on-exit
Open

fix: reuse persisted auth tokens across server restarts#62
mjdavidson wants to merge 1 commit into
okta:mainfrom
mjdavidson:fix/persist-tokens-on-exit

Conversation

@mjdavidson

Copy link
Copy Markdown

The lifespan cleared stored tokens in a finally on every shutdown, and startup always ran a fresh device grant — so each restart/reconnect re-prompted for device login and never used the refresh token. This removes the teardown wipe and makes startup reuse is_valid_token() (silent refresh), falling back to a device grant only when no valid token can be obtained.

Tests: new tests/test_lifespan.py (normal exit, exception path, reuse vs. re-auth).

Two problems made every restart re-prompt for device authorization:

1. The lifespan cleared the stored tokens in a finally block on every shutdown,
   destroying the refresh token so it could never be reused.
2. Startup called authenticate() unconditionally, which always begins a new
   device-authorization flow regardless of any cached token. The silent-refresh
   path (is_valid_token -> refresh_access_token) only ran on per-tool calls, so
   it never helped at startup.

Remove the shutdown wipe so the access and refresh tokens persist, and have the
lifespan reuse them via is_valid_token() — which silently refreshes when the
access token is stale — falling back to an interactive device grant only when no
valid token can be obtained. clear_tokens() is unchanged and remains the logout
primitive. A restart or reconnect now refreshes silently instead of prompting.
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