Skip to content

Stop sending access_token as a refresh_token - #1928

Open
aroh3006 wants to merge 2 commits into
python-social-auth:masterfrom
aroh3006:fix-refresh-token-fallback
Open

aroh3006 wants to merge 2 commits into
python-social-auth:masterfrom
aroh3006:fix-refresh-token-fallback

Conversation

@aroh3006

@aroh3006 aroh3006 commented Aug 24, 2026

Copy link
Copy Markdown

Fixes #826.

refresh_token() fell back to access_token when no refresh_token was stored, sending it to the token endpoint as if it were one. A refresh_token is optional per RFC 6749 4.1.4, and providers reject a request carrying the wrong kind of token. This always failed with a 401 instead of doing nothing.

Removed the fallback. This also exposed that Zoom and PayPal never actually saved a real refresh_token in the first place, since it wasn't listed in their EXTRA_DATA. Refresh never had a chance to work for those two even with a correct fallback removed. Added it there.

The AzureAD and AzureADB2C tests build their own mocked login response and it didn't include a refresh_token. Their test_refresh_token tests broke once the fallback was gone. Updated those fixtures to match what the providers actually return.

Added tests for the fixed fallback in test_storage.py. Ran the full test suite (1152 passed, 176 skipped, 8 xfailed) and ruff.

refresh_token() fell back to access_token when no refresh_token was
stored, sending it to the token endpoint as if it were one. Per
RFC 6749 4.1.4 a refresh_token is optional and providers reject a
request that sends the wrong kind of token, so this always failed
with a 401 instead of doing nothing.

Removed the fallback. Also added refresh_token to Zoom's and
PayPal's EXTRA_DATA, since without it a real refresh_token was
never even saved for those two backends.

Updated the AzureAD/AzureADB2C test fixtures to include a
refresh_token in the mocked login response, matching what those
providers actually return. Added tests for the fixed fallback too.
@codecov

codecov Bot commented Aug 25, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.87500% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 89.16%. Comparing base (d145777) to head (da09e42).
⚠️ Report is 54 commits behind head on master.

Files with missing lines Patch % Lines
social_core/storage.py 75.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1928      +/-   ##
==========================================
+ Coverage   87.32%   89.16%   +1.83%     
==========================================
  Files         352      345       -7     
  Lines       13713    13898     +185     
  Branches      675      696      +21     
==========================================
+ Hits        11975    12392     +417     
+ Misses       1494     1262     -232     
  Partials      244      244              
Flag Coverage Δ
unittests 89.16% <96.87%> (+1.83%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request fixes an OAuth2 refresh flow bug where refresh_token() incorrectly fell back to sending the access_token as a refresh_token when no refresh token was stored, which providers reject.

Changes:

  • Remove the access_token fallback when selecting a refresh token in UserMixin.refresh_token().
  • Ensure Zoom and PayPal persist refresh_token in EXTRA_DATA so refresh can actually work.
  • Update AzureAD/AzureADB2C test fixtures to include refresh_token, and add regression tests for the corrected refresh-token behavior.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
social_core/storage.py Stops falling back to access_token when no refresh_token is present.
social_core/backends/zoom.py Adds refresh_token to stored extra data.
social_core/backends/paypal.py Adds refresh_token to stored extra data.
social_core/tests/test_storage.py Adds regression tests ensuring refresh does not use access_token as a refresh token.
social_core/tests/backends/test_azuread.py Updates mocked token response fixture to include refresh_token.
social_core/tests/backends/test_azuread_b2c.py Updates mocked token response fixture to include refresh_token.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread social_core/storage.py Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@nijel

nijel commented Sep 15, 2026

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 15, 2026

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review Completed 2026-09-15T11:20:04.048045Z da09e42 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: da09e42689

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread social_core/storage.py
Comment on lines +74 to +76
token = self.extra_data.get("refresh_token")
if not token:
return

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Preserve Facebook's access-token exchange path

For Facebook users, FacebookOAuth2.EXTRA_DATA never stores a refresh_token, because its refresh implementation deliberately exchanges the existing access token via fb_exchange_token in FacebookOAuth2.refresh_token_params(). When get_access_token() detects an expired Facebook token, this early return now skips that exchange and returns the expired credential unchanged. Select the token according to the backend's refresh mechanism rather than unconditionally requiring a stored refresh token.

Useful? React with 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

OAuth2 grant type refresh_token sends the access token when no refresh token is available

3 participants