Skip to content
Back to log

Exchanging authorization codes with OAuth and PKCE

Chainabit's Chao · · 7 min read

In short

PKCE binds the exchange of an authorization code for a token to a verifier. Explore the request fields, verification steps, and token request in Chainabit.

What problem does PKCE address?

The IETF’s Proof Key for Code Exchange standard addresses authorization code interception attacks against public clients that use the Authorization Code Grant. PKCE mitigates this threat by linking the authorization request to the subsequent token request.

In the attack described by the standard, a malicious app can register itself as a handler for the same custom URI scheme as the legitimate app. The malicious app can intercept the authorization code during the redirect. The attacker can then use the intercepted code to request an access token.

This scenario depends on conditions such as the operating system allowing multiple apps to register for the same custom URI scheme. The standard also notes that a client secret embedded in the application binary, as in its native app examples, cannot be considered confidential.

In the PKCE flow, the client generates a fresh code_verifier for each authorization request. It derives a code_challenge from that value and sends it to the authorization server with the authorization request. The client later sends the original code_verifier to the token endpoint along with the authorization code.

The authorization server applies the specified transformation to the received code_verifier and compares the result with the code_challenge it previously associated with the authorization code. If the values do not match, the server denies access. In the flow described by the standard, intercepting the authorization code alone leaves the attacker without the code_verifier.

Preparing the verifier and challenge

The IETF standard defines the code_verifier as a high-entropy, cryptographically random string. It links the authorization request to the token request. The client generates and stores one for each authorization request.

The code_verifier can contain uppercase and lowercase ASCII letters, digits, and the characters -, ., _, and ~. The standard requires enough entropy to make guessing the value impractical for an attacker.

The client generates the code_challenge by applying a transformation to the code_verifier. The standard defines two methods. With plain, the code_challenge has the same value as the code_verifier. With S256, the client applies SHA-256 to the ASCII representation of the code_verifier and base64url-encodes the result.

The IETF’s definition of base64url omits trailing = characters. The encoded value contains no line breaks, spaces, or other added characters. This definition governs the format of the code_challenge produced by the S256 transformation.

The standard requires clients that can use S256 to do so. A client may use plain only if it cannot support S256 for technical reasons and knows from its configuration that the server supports plain. The plain method is defined for compatibility with existing deployments and for constrained environments that cannot use S256.

The authorization request includes the required code_challenge field. The code_challenge_method field is optional and defaults to plain if omitted. When the client supplies a method, the server uses it during verification.

How is the authorization code verified?

Under the IETF standard, when the authorization server issues a code in the authorization response, it associates the code_challenge and code_challenge_method with that code. The token endpoint uses that association to verify the verifier in the subsequent request.

The server can embed these values in encrypted form within the code or store them server-side and associate them with the code. The code_challenge must not be included in client requests in a form that other parties can extract.

After receiving the authorization code, the client sends an access token request to the token endpoint. This request includes the authorization code and the code_verifier. The token endpoint uses the code_challenge_method associated with the authorization code when it was issued.

If the method is S256, the server applies SHA-256 to the received code_verifier, base64url-encodes the result, and compares it with the stored code_challenge. If the method is plain, the server compares the values directly. If they match, the token endpoint continues processing the request as usual. Otherwise, it returns invalid_grant.

PKCE errors can also occur at the authorization endpoint. If the server requires PKCE for a public client and the request omits code_challenge, the server must return invalid_request. A server that supports PKCE returns the same error if it does not support the requested transformation. The response should explain the error using error_description or error_uri.

IETF security requirements

Turn this insight into action.

Use the mobile app to continue with guided workflows.

The IETF’s OAuth security standard requires public clients to use PKCE and recommends it for confidential clients. This guidance applies to native apps and other types of OAuth clients, including web applications.

The standard requires the PKCE challenge to be unique to the transaction and securely bound to the client and user agent that initiated it. It also recommends using a challenge method that keeps the verifier out of the authorization request.

The authorization server must support PKCE. If the authorization request contains a valid code_challenge, the server must ensure that the code_verifier is used correctly at the token endpoint. The server must also provide a way to detect PKCE support.

The OAuth security standard also addresses PKCE downgrade attacks. If an authorization request omits code_challenge, the token endpoint must reject requests that include a code_verifier and attempt to redeem the code issued for that authorization request. This rule matters for servers that support PKCE and require it only for some flows.

Exchanging authorization codes in Chainabit

Chainabit defines its OAuth operations around an existing, configured OAuth client. The client must already be registered and configured with a matching redirect URI and the scopes it can request. The configured browser consent flow must be completed before exchanging the code.

Chainabit requires the client to validate state before accepting the authorization code in the callback and to store the original PKCE verifier securely. When exchanging the code, a public client sends its client ID and the verifier associated with the code.

In Chainabit’s POST /oauth/token request for exchanging a code, grant_type is set to authorization_code. The request also includes client_id, code, redirect_uri, and code_verifier. The request body is sent as application/x-www-form-urlencoded.

curl --fail-with-body -sS -X POST "$BASE_URL/oauth/token" \
  -H "Content-Type: application/x-www-form-urlencoded" \
  --data-urlencode "grant_type=authorization_code" \
  --data-urlencode "client_id=$OAUTH_CLIENT_ID" \
  --data-urlencode "code=$AUTHORIZATION_CODE" \
  --data-urlencode "redirect_uri=$REDIRECT_URI" \
  --data-urlencode "code_verifier=$CODE_VERIFIER"

A confidential client includes these fields and authenticates with its configured client credentials. Chainabit instructs developers to keep client secrets out of public browser applications.

A successful token response includes access_token, token_type, expires_in, refresh_token, and the granted scope. Chainabit instructs clients to read the token lifetime from the response, avoiding a hard-coded value. The token response uses its own OAuth response format and omits the standard data envelope.

Chainabit instructs clients to send the returned access token as a Bearer token only to operations that accept the granted scopes. Even after consent is granted and the token is issued, its access is more limited than that of an unrestricted interactive session.

Scope and workspace boundaries

Chainabit evaluates each scope independently. For example, bits:write does not include bits:read. The bits:read scope covers reading Bits and the tree, calendar, and Project Gantt views. The bits:write scope allows creating and updating Bits and excludes delete and execute operations.

Requests using projects:read, bits:read, or bits:write operate on a workspace in the account where the user authorized the application. By default, they use the user’s workspace in that account. To select another workspace the user can access within the same account, use the x-workspace-id header.

Chainabit rejects requests for workspaces outside the account or beyond the user’s access with 403. An Organization that requires single sign-on also rejects these requests with 403 and the SSO_REQUIRED error code.

Errors and limitations of the flow

In Chainabit error responses, invalid_client points to issues with client identity or required client authentication. The invalid_grant error covers issues with authorization code or refresh token validity, the redirect URI, or the PKCE verifier. For invalid_request, check the required fields and request encoding. For unsupported_grant_type, check which grant types the configured integration supports.

In the IETF PKCE standard’s attack model, plain addresses the case where an attacker can observe authorization endpoint responses. If the attacker can also observe requests, the standard requires S256 or a cryptographically secure extension method.

The IETF OAuth security standard describes how PKCE protection can be bypassed if an attacker can modify the code_challenge in the victim’s authorization request and certain other conditions are met. Chainabit instructs developers to keep authorization codes, verifiers, tokens, and client secrets out of logs and source control.

Ready to start?

Download the app and continue from your phone.

End of Entry