Repository navigation
fix(oauth): send the OAuth token request to the region's auth host - #189
Closed
JingHao-Leon wants to merge 2 commits into
Closed
JingHao-Leon wants to merge 2 commits into
JingHao-Leon wants to merge 2 commits into
Conversation
The OAuth token endpoint is hardcoded to the global host, so a China client redeems its authorization code (and refreshes its access token) against fleet-auth.prd.vn.cloud.tesla.com instead of auth.tesla.cn, while the authorize URL already switches host for China. Covers TeslaFleetOAuth.get_refresh_token/refresh_access_token and TeslaFleetApi.partner_login for both regions.
China is served by its own auth host (auth.tesla.cn); every other region shares the global one. The authorize URL already switched host for China, but the token endpoint stayed hardcoded to the global host, so a China client redeemed its authorization code and refreshed its access token against the wrong region. Add const.auth_server() as the single source of truth and use it for TeslaFleetOAuth.get_refresh_token, TeslaFleetOAuth.refresh_access_token and TeslaFleetApi.partner_login.
Author
|
Closing this in favour of #188, which covers the same change. The report in #187 landed, and this PR was the follow-up implementation of it. In the meantime #188 opened with The No release note needed - #188 covers the same behaviour. |
Author
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.
What was broken
The OAuth token endpoint was hardcoded to the global auth host, so a client configured for the
cnregion exchanged its authorization code and refreshed its access token againstfleet-auth.prd.vn.cloud.tesla.cominstead ofauth.tesla.cn.The authorize endpoint already switched host for China (
tesla_fleet_api/tesla/oauth.py:52), so a China client was sent toauth.tesla.cnto log in and then had the resulting code redeemed by a different region's auth host - the two halves of one OAuth flow ended up on different hosts. The partner-token request had the same split.Root cause
The token endpoint was written as a string literal at each call site instead of being derived from the region (line numbers on
main):tesla_fleet_api/tesla/oauth.py:71-TeslaFleetOAuth.get_refresh_tokenposts the authorization code to the global hosttesla_fleet_api/tesla/oauth.py:109-TeslaFleetOAuth.refresh_access_tokenposts the refresh token to the global hosttesla_fleet_api/tesla/fleet.py:270-TeslaFleetApi.partner_loginposts the partner credentials to the global hostThe region-aware authorize host already existed at
tesla_fleet_api/tesla/oauth.py:52; no equivalent switch was applied to the token endpoint.This change adds
const.auth_server(region)as the single source of truth for the token endpoint and uses it at all three call sites.cnresolves tohttps://auth.tesla.cn/oauth2/v3/token; every other region - and an unset region - keepshttps://fleet-auth.prd.vn.cloud.tesla.com/oauth2/v3/token.How this was verified
Reproduced first, then fixed - the same test file shows both states.
Before the fix, against unmodified
main(1.18.0) with the new tests added:Every failure is the same assertion - the request went to
https://fleet-auth.prd.vn.cloud.tesla.com/oauth2/v3/tokeninstead ofhttps://auth.tesla.cn/oauth2/v3/token:The whole suite, same unfixed source, with the new tests added:
After the fix:
Baseline for comparison - unmodified
main, without the new tests:The 7 added tests are the whole difference between 945 and 952. The
nacases and thecnauthorize-URL characterization pass in both states, so only the China token host changes.The end-to-end behaviour, same script run against
mainand against this branch:Lint and types:
One note on a pre-existing flake, unrelated to this change:
tests/test_tesla_private_key.py::GetRsaPrivateKeyOptionalValidationTests::test_existing_key_read_does_not_block_event_loopis timing-sensitive (it expects >10 event-loop ticks across a mocked 0.15s key read) and fails intermittently on a loaded machine. It also failed on unmodifiedmainin 2 of 3 full-suite runs (1 failed, 944 passed). This change does not touch the code path it exercises (tesla_fleet_api/tesla/tesla.py).CI status
The repository's own
CIworkflow (lint / test / build) is held behind GitHub's "Approve and run workflows" gate on this branch: the run finished with conclusionaction_requiredand reports 0 jobs, so the suite has not executed on the upstream runner - it needs a maintainer's approval to run. That is a gate rather than a failure: no job reported an error, and the run's head SHA matches this PR's head.gh pr checkscurrently lists only the third-party CodeRabbit review check, stillpending. The results above were produced locally withuv sync --locked(Python 3.13.9, uv 0.11.19) using the same commands CI runs.Fixes #187
This change was prepared with the assistance of an AI coding agent.