Sub topic 6
Authorization Code Flow with PKCE
This chapter assumes you know what a connected app is, where the consumer key and callback URL live, and how scopes work. It also assumes you can read a curl command.
Why this flow exists
The authorization code flow suits apps where a person signs in through a browser. The app never sees the user's password. It receives a short-lived authorization code and trades that code for tokens.
PKCE (Proof Key for Code Exchange, pronounced "pixy") protects that trade. The app invents a random secret, the code verifier. It sends only a hash of that secret, the code challenge, when it starts the login. When it redeems the code, it sends the original verifier. If an attacker steals the code from the redirect, they can't redeem it without the verifier.
PKCE matters most for apps that can't keep a consumer secret, such as single-page apps and mobile apps.
The flow
Step 1: Create the verifier and challenge
The challenge is the Base64URL-encoded SHA-256 hash of the verifier. Base64URL replaces + and / with - and _ and drops the = padding.
# Generate a PKCE code verifier and its S256 challenge
CODE_VERIFIER=$(openssl rand -base64 64 | tr -d '=+/\n' | cut -c1-64)
CODE_CHALLENGE=$(printf '%s' "$CODE_VERIFIER" | openssl dgst -sha256 -binary | openssl base64 -A | tr '+/' '-_' | tr -d '=')Step 2: Send the user to authorize
Open this URL in a browser. The redirect_uri must exactly match a callback URL registered on the app.
echo "https://MyDomain.my.salesforce.com/services/oauth2/authorize?response_type=code&client_id=$CONSUMER_KEY&redirect_uri=https%3A%2F%2Fapp.example.com%2Fcallback&scope=api%20refresh_token&code_challenge=$CODE_CHALLENGE&code_challenge_method=S256"After the user approves, Salesforce redirects to your callback with a code query parameter. Save it as AUTH_CODE.
Step 3: Redeem the code
curl -X POST https://MyDomain.my.salesforce.com/services/oauth2/token \
-d grant_type=authorization_code \
-d code="$AUTH_CODE" \
-d client_id="$CONSUMER_KEY" \
-d redirect_uri=https://app.example.com/callback \
-d code_verifier="$CODE_VERIFIER"The response contains an access_token, and a refresh_token if you requested the refresh_token scope and the app's policies allow it.
Step 4: Verify the token
curl https://MyDomain.my.salesforce.com/services/data/ \
-H "Authorization: Bearer $ACCESS_TOKEN"A list of API versions confirms the token works.
Common errors
| Error | Likely cause |
|---|---|
| `invalid_grant` with "expired authorization code" | The code was used late. Codes are short-lived and single-use. |
| `invalid_grant` with "invalid code verifier" | The verifier differs from the one used to build the challenge, or the challenge was encoded incorrectly. |
| `redirect_uri_mismatch` | The URI differs from the registered callback, including scheme, path, or trailing slash. |
| `invalid_client_id` | The consumer key is wrong, or the app isn't available in this org. |
Exercise
An attacker intercepts the authorization code from the redirect URL. Why can't they get tokens from the token endpoint?
Show answer
The token request must include the code verifier. The attacker saw only the code, and the authorize request carried only the hashed challenge. They can't recover the verifier from the hash, so Salesforce rejects the request with an invalid_grant error.
Exercise
Your token request fails with "invalid code verifier", but the code is fresh. Name two things to check.
Show answer
Check that the verifier sent at the token step is exactly the one used to compute the challenge, for example that it wasn't regenerated between steps. Also check that the challenge was encoded as Base64URL (no padding, with - and ) and that codechallenge_method=S256 was sent.