OAuth: token endpoint errors aren't decoded, and optional response fields are treated as required

Open
#660 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
swift

Research direction

Start with the token types TokenResponse and RefreshTokenResponse, then OAuthButton.getToken and AccountManager.renewExpiredToken, which the issue says share this path. Check how the HTTP status and error body are handled today, and note that the linked RealOAuth2TokenProvider.getToken in thunderbird-android is a reference for the intended behavior, not code in this repo. Done when an invalid_grant response yields a sign-in-again error without retries, optional expires_in and refresh_token decode, and non-Bearer token types are rejected.

Written by the indexing model from the issue text.

Description

Summary

The code that exchanges an authorization code and refreshes a token only understands a successful response of one exact shape. An error response, or a successful one without the optional fields, turns into a decoding failure.

(Found by reading the code, and confirmed by decoding sample responses.)

Details

  • Errors. {"error":"invalid_grant","error_description":"…"} doesn't decode as RefreshTokenResponse, so the caller sees a DecodingError and can't tell a revoked refresh token from a malformed reply. The HTTP status isn't checked.
  • Retries. AccountManager.renewExpiredToken retries every failure twice, including one that can never succeed.
  • Optional fields. TokenResponse fails to decode without refresh_token or without expires_in.
  • Token type. token_type isn't read. A response with "token_type":"mac" is accepted and used as a bearer token.

Expected behavior

  • An error response is decoded into an error that carries the server's error code.
  • invalid_grant is treated as "sign in again" and isn't retried. Network and server errors are treated as temporary.
  • A response without expires_in or refresh_token is accepted.
  • A token whose type isn't Bearer is rejected.

Suggested fix

Decode the error shape first, make expires_in and refresh_token optional, check token_type without regard to case, and retry only temporary failures. OAuthButton.getToken and AccountManager.renewExpiredToken can share this.

References

  • RFC 6749, section 5.1: "expires_in RECOMMENDED. The lifetime in seconds of the access token." and "refresh_token OPTIONAL. The refresh token, which can be used to obtain new access tokens".
  • RFC 6749, section 5.2, invalid_grant: "The provided authorization grant (e.g., authorization code, resource owner credentials) or refresh token is invalid, expired, revoked, does not match the redirection URI used in the authorization request, or was issued to another client."
  • RFC 6749, section 7.1: "The client MUST NOT use an access token if it does not understand the token type."
  • RFC 6749, section 5.1: "The client MUST ignore unrecognized value names in the response."

In thunderbird-android

Links are pinned to ac1f0e8.

Dominant language
Swift
Stars
1.3k
Forks
51
Avg merge
1d 7h
Merged PRs (30d)
33

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from thunderbird/thunderbird-ios

All issues in thunderbird/thunderbird-ios

Similar issues

More Swift issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.