Repository navigation
Fix OAuth token exchange duplicate key error on request_id - #8
Merged
Merged
Conversation
During authorization_code grant flow, CreateAccessTokenSession was INSERTing a new row with the same request_id from the authorize step, violating the UNIQUE constraint idx_auth_session_uniq_request_id. Root cause: Fosite reuses the same request.GetID() across the authorize and token exchange steps, so both CreateAuthorizeCodeSession and CreateAccessTokenSession were trying to create separate rows with the same request_id. Fix: CreateAccessTokenSession now: 1. Looks up the existing session by request_id (from authorize step) 2. Updates it with the new access token signature and session data 3. Only INSERTs if no existing session is found (edge case) This matches the approach used in CreatePKCERequestSession (PR #6) and aligns with how fosite manages OAuth sessions - a single session row evolves through the authorize → token exchange lifecycle. Added TestAuthorizationCodeToAccessTokenFlow to verify the full flow: CreateAuthorizeCodeSession → CreateAccessTokenSession correctly updates the existing session instead of creating a duplicate. Co-authored-by: Dmitry <demdxx@gmail.com>
demdxx
marked this pull request as ready for review
September 18, 2026 15:27
Coverage Report for CI Build 35361913195Coverage increased (+0.1%) to 4.061%Details
Uncovered Changes
Coverage RegressionsNo coverage regressions found. Coverage Stats
💛 - Coveralls |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
OAuth token exchange was failing in production with:
This occurred during the authorization_code grant flow when
CreateAccessTokenSessiontried to INSERT a newauth_sessionrow with the samerequest_idthat was already created byCreateAuthorizeCodeSession.Production Context
mcp-client, PKCE, usersuper@affyard.comCreatePKCERequestSessionRoot Cause
Fosite reuses the same
request.GetID()across the OAuth flow lifecycle:request_id=req-123,access_token=auth-coderequest_id=req-123(same!),access_token=new-token→ UNIQUE constraint violationThe issue is that
CreateAccessTokenSessionwas always INSERTing instead of UPDATing the existing session.Solution
Changed
CreateAccessTokenSessionto follow the same pattern asCreatePKCERequestSession:request_idThis correctly models the OAuth flow: a single session row that evolves through the authorize → token exchange lifecycle.
Changes
storage.goCreateAccessTokenSessionto UPDATE existing sessions byrequest_idrequest_idandaccess_token(wheredeleted_at IS NULL)storage_test.goTestAuthorizationCodeToAccessTokenFlowto verify the full flowTesting
✅ All existing tests pass
✅ New test covers authorization_code → access_token exchange
✅ Preserves behavior from PR #6 (
CreatePKCERequestSession)Next Steps for Affyard
After this PR merges, bump the
blaze-apisubmodule in the Affyard repository to pick up this fix.