Skip to content

Use PKCE for the Trakt OAuth sign-in - #1803

Open
julienbeugras wants to merge 1 commit into
FuzzyGrim:devfrom
julienbeugras:trakt-oauth-pkce
Open

julienbeugras wants to merge 1 commit into
FuzzyGrim:devfrom
julienbeugras:trakt-oauth-pkce

Conversation

@julienbeugras

Copy link
Copy Markdown

Closes #1744
Closes #1772

Trakt now requires PKCE: the private import's sign-in fails with invalid_request: code_challenge is required for this client. Apps created on Trakt today are also not issued a client secret (Trakt's PKCE guide).

Changes

  • trakt_oauth generates a code verifier for each sign-in (trakt.generate_pkce_pair), stores it with the existing OAuth state in the session, and sends the S256 code_challenge to https://auth.trakt.tv/oauth/authorize, the URL given in Trakt's guide (the old trakt.tv/oauth/authorize redirects there).
  • The callback passes the stored verifier to handle_oauth_callback, which sends it as code_verifier. A sign-in started before this change has no stored verifier and exchanges its code as before.
  • client_secret is sent to the token endpoint only when TRAKT_API_SECRET is set, for both the code exchange and the refresh-token request the importer makes. Trakt asks PKCE apps not to send one, and new apps have none.

Tests

  • The authorize redirect carries an S256 challenge matching the stored verifier (43–128 characters), and each sign-in gets a new verifier.
  • The callback passes the stored verifier to the token exchange, which sends it, and omits it when the state has none.
  • client_secret is omitted when not configured (code exchange and refresh) and sent when configured.

The new tests fail against dev. manage.py test app users integrations lists events --parallel passes apart from test_hardcover_book, which fails on dev too (live provider data). ruff check src, ruff format --check src and the pre-commit hooks pass.

Tested against Trakt with a newly created app (no client secret): on dev the authorize request returns the code_challenge is required error; with this branch the sign-in completes, the code exchange succeeds, and the private import runs, refreshing its token without a secret. Not tested live: an older app that has a client secret, which still sends it.

Trakt now rejects the authorization request with "code_challenge is
required for this client" (FuzzyGrim#1744), which breaks private-profile imports.
Apps created on Trakt today are not issued a client secret at all.

Each sign-in generates a code verifier, stores it with the OAuth state in
the session, and sends its S256 challenge to the authorize endpoint, now
https://auth.trakt.tv/oauth/authorize as in Trakt's PKCE guide (the old
URL redirects there). The token exchange sends the verifier back. A
sign-in started before this change has no stored verifier and exchanges
its code as before.

The client secret is sent to the token endpoint only when one is
configured, for both the code exchange and the refresh used by imports,
since Trakt asks PKCE apps not to send one.

Closes FuzzyGrim#1744
Closes FuzzyGrim#1772

This branch has not been deployed

No deployments
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.

[BUG] Trakt requires pkce [BUG] Trakt now requires PKCE for OAUTH

1 participant