Skip to content

fix(google): round-trip PKCE code_verifier through OAuth state - #108

Open
DouwMarx wants to merge 1 commit into
mruwnik:masterfrom
DouwMarx:google-oauth-pkce-fix
Open

fix(google): round-trip PKCE code_verifier through OAuth state#108
DouwMarx wants to merge 1 commit into
mruwnik:masterfrom
DouwMarx:google-oauth-pkce-fix

Conversation

@DouwMarx

Copy link
Copy Markdown

Problem

Google OAuth setup returns 500 Internal Server Error after the user grants consent.

google-auth-oauthlib defaults autogenerate_code_verifier=True, so /authorize and /callback each build their own Flow and generate a different PKCE verifier. /authorize sends the challenge for verifier A; /callback then calls fetch_token with verifier B, Google rejects the PKCE check, fetch_token raises, and with no error handling around it FastAPI returns a 500.

Fix

Mint the verifier once and round-trip it through the existing Redis OAuth state:

  • google_authorize / reauthorize_account: generate the verifier, pass it to the Flow, and store it in the state payload alongside the user id.
  • GoogleOAuthState.create/validate: state payload is now JSON {user_id, code_verifier} instead of a bare user id.
  • google_callback: read the verifier back and pass it with autogenerate_code_verifier=False so the challenge matches.

Test

Updated test_google_callback_parses_scopes_from_url for the new validate() tuple return. Real end-to-end verification is the browser Google-setup flow.

google-auth-oauthlib defaults autogenerate_code_verifier=True, so
/authorize and /callback each generated a different verifier in their
own Flow. fetch_token then failed Google's PKCE check and raised,
surfacing as a 500 Internal Server Error after the user granted consent.

Mint the verifier once in google_authorize (and reauthorize_account),
persist it in the Redis OAuth state next to the user id, and echo it in
google_callback with autogenerate_code_verifier=False.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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