fix: parse TikTok's OAuth exchange envelope so the token reaches the form - #329
Open
aaaaahaaaaa wants to merge 1 commit into
Open
fix: parse TikTok's OAuth exchange envelope so the token reaches the form#329aaaaahaaaaa wants to merge 1 commit into
aaaaahaaaaa wants to merge 1 commit into
Conversation
…form
TikTok Business answers the code exchange with HTTP 200 and a
{code, message, data} envelope on success and failure alike. The route
relayed that body verbatim, and the app read `refresh_token ?? access_token`
at the top level, so the token field stayed silently empty behind a green
"Connected" toast; a rejection inside a 200 looked exactly the same.
Providers already own the request side of the code exchange; give them the
response side too: OAuthProvider.parse_authorization_code_response (base =
RFC passthrough) mirrors parse_refresh_token_response, and TikTokProvider
unwraps `data` and raises on a non-zero `code`. The route applies it before
returning, so an in-band rejection becomes a 500 the popup can display.
App side, `oauthToken()` becomes the single owner of the credential pick and
OAuthSignIn treats a token-less response as a failure rather than emitting
success; SchemaForm just places the issued token.
Also restore the trailing slash on TikTok's token_url: the slash-less URL
308-redirects, which only worked thanks to follow_redirects.
By Digitl
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
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
"Sign in with TikTok" in production: the popup completes, TikTok reports success, but the connection form never shows a fetched token.
TikTok Business answers
POST /open_api/v1.3/oauth2/access_token/with HTTP 200 for success and failure alike, wrapping the outcome in a{code, message, data}envelope (live-probed:{"code":40002,"message":"app_id: invalid params..."}). The route relayed that body verbatim, and the app readtokens.refresh_token ?? tokens.access_tokenat the top level, so:dataand the field stayed silently empty behind a green "Connected to TikTok" toast;raise_for_status()never tripped and the flow also reported success.Both outcomes were indistinguishable from the outside, which is why this read as "exchange issue or UI issue?".
Fix
Providers already own the request side of the code exchange (38a2949); this gives them the response side, mirroring the existing
parse_refresh_token_response:OAuthProvider.parse_authorization_code_response(payload): base is RFC passthrough.TikTokProviderunwrapsdataand raisesValueErrorcarrying TikTok'smessagewhencode != 0.oauthToken()(composables/oauth.ts) is the single owner of the refresh_token/access_token pick;OAuthSignInnow throws on a token-less response instead of emitting success, routed through the existingerrorToast;SchemaForm.handleOAuthSuccess(token: string)just places it.token_url: the slash-less URL 308-redirects and only worked thanks tofollow_redirects=True.Tests
TDD: 5 new tests failed against the old code, then passed.
core/tests/oauth/test_base.py: base parser is passthrough.core/tests/oauth/test_providers.py: TikTok unwrapsdata; raises on non-zerocode.api/tests/routes/test_oauth.py: route returns the parsed body for TikTok; in-band rejection is a 500 carrying the message.pytest(44 in scope) /ruff check/ty/pnpm run lint/nuxt typecheckall green; pre-commit passed on commit.Reviewer notes
datasuccess shape is TikTok's documented shape, corroborated by the error probe's envelope and byTiktokAdsConnection.advertisers()already readingbody["data"]. Worth one real sign-in after deploy.ruff format --checkflags two pre-existing spots inoauth/base.pyandtest_base.pyuntouched by this change; left out deliberately (pre-commit runsruff-checkonly).OAuthSignIn'ssuccessemit changes fromRecord<string, unknown>tostring;SchemaFormis its only consumer.By Digitl