Sub topic 5
Choosing an OAuth flow
This chapter assumes you can read an HTTP request and know what a connected app's consumer key and callback URL are. It also assumes you know that a connected app or external client app (ECA) holds the OAuth configuration. The chapter covers how to pick a flow. The later chapters on authorization code with PKCE, JWT bearer, and client credentials cover how to run each one.
What a flow decides
An OAuth flow is the sequence of requests that ends with an access token. In Salesforce you pick a flow by sending a specific grant_type to the token endpoint, or by sending the user to the authorize endpoint first. The choice answers three questions:
- Who is the acting identity? It may be a person who logs in, a fixed integration user, or a user the server names in a signed assertion.
- Is a human present? Some flows need a browser. Others must run unattended.
- Where can the secret live? A server can hold a secret or a private key. A browser app or mobile app cannot.
Get these three answers first. The flow usually follows from them.
The flows Salesforce supports
| Flow | Human present | Acting identity | Typical use | Recommendation |
|---|---|---|---|---|
| Authorization code with PKCE | Yes | The user who logs in | Web apps, mobile apps, single-page apps, desktop tools | Preferred for user-facing apps |
| JWT bearer | No | The user named in the `sub` claim | CI pipelines, server-to-server jobs | Preferred for unattended access as a specific user |
| Client credentials | No | The run-as user set in the app policies | Service integrations with one fixed identity | Preferred when no per-user identity is needed |
| Device flow | Yes, on a second device | The user who approves | TVs, command-line tools, devices without a browser | Use only for constrained devices |
| Refresh token | No | The user from the original grant | Renewing access without a new login | Use with a user-facing flow |
| User-agent (implicit) | Yes | The user who logs in | Older browser apps | Avoid. Use the authorization code flow with PKCE |
| Username-password | No | The user whose credentials the app holds | Legacy scripts | Avoid. Blocked by default in newer orgs |
The refresh token flow is not a way to start. It extends a session that another flow created. The user-agent and username-password flows are listed only because you will meet them in older integrations. Both expose credentials or tokens in ways the other flows avoid.
A decision path
PKCE (Proof Key for Code Exchange) is useful for server-side apps and for public clients such as browser and mobile apps. A public client cannot protect a secret. Both branches therefore lead to the same flow. A server-side app should still use PKCE, because it protects the code if it is intercepted.
Criteria that break ties
Per-user identity and auditing
Choose authorization code when each end user must act under their own permissions. Salesforce then enforces that user's profile, permission sets, sharing, and field-level security. Choose client credentials when the integration is one service with one identity. Every call is attributed to the run-as user, so the audit trail does not show which person triggered it.
Unattended operation
Authorization code needs a login at least once. After that, a refresh token can keep the session going, as long as the app's refresh token policy allows it. If the work runs on a schedule in a pipeline, JWT bearer or client credentials is simpler, because nothing needs a stored refresh token.
Secret management
- Client credentials relies on a consumer secret. Anyone who has the key and secret gets the run-as user's access.
- JWT bearer relies on a private key. Salesforce only holds the public certificate, so a leak on the Salesforce side cannot expose the key. A leak on your side still gives full access as the named user.
- Authorization code with PKCE needs no stored client secret in a public client. The verifier is created per request and used once.
Setup effort
JWT bearer takes the most setup. You create a key pair, upload the certificate to the app, and pre-authorize users. Pre-authorization means setting the app's permitted users policy to admin-approved users and assigning a profile or permission set. Client credentials takes less: enable the flow and set the run-as user. Authorization code needs a registered callback URL and a place in your app to receive the code.
The refresh token request
If you pick authorization code and want long-lived access, you will use the refresh token flow. Only the grant type and parameters change:
curl -X POST https://MyDomain.my.salesforce.com/services/oauth2/token \
-d grant_type=refresh_token \
-d client_id="$CONSUMER_KEY" \
-d refresh_token="$REFRESH_TOKEN"This works only if the original request included the refresh_token scope and the app's refresh token policy has not expired it. If the app requires a secret for this flow, add -d client_secret="$CONSUMER_SECRET".
Common mistakes
- Using client credentials to avoid a login screen. The integration then acts as one shared user for everyone. If the business needs per-user permissions or audit, this is the wrong flow.
- Giving the run-as user too much access. Because the token carries that user's full permissions, a broad profile turns a leaked secret into a broad breach. Use a dedicated integration user with a permission set that lists only what the integration needs.
- Using the username-password flow because it is easy to script. It requires the app to hold a real password, and it bypasses multi-factor authentication. Replace it with JWT bearer.
- Selecting a flow but forgetting the scopes. A flow gets a token, but the scopes on the token decide what the token can do. A token without
apicannot call the REST API. - Skipping pre-authorization for JWT bearer. The request fails with an error such as
user hasn't approved this consumer, even when the signature is correct. - Sending users to a login URL that does not match the org. Use your My Domain URL for production. Use the sandbox login URL, or your sandbox My Domain URL, for a sandbox. Using the wrong host is a common cause of failed JWT requests, because the
audclaim must match the host you are calling.
Verifying the result
Whatever flow you chose, confirm that the token works the same way. Call the versions resource with it:
curl https://MyDomain.my.salesforce.com/services/data/ \
-H "Authorization: Bearer $ACCESS_TOKEN"A JSON list of API versions means the token is valid for the API. A 401 with INVALID_SESSION_ID means the token is expired, revoked, or sent to the wrong host. To see who the token acts as, request the identity URL returned in the token response. For a client credentials token, confirm that it is the run-as user you set. For JWT bearer, confirm that it is the sub user.
Summary of decisions
- User-facing app: authorization code with PKCE, plus the refresh token flow if the session must last.
- Unattended job acting as a named user: JWT bearer.
- Unattended service with one shared identity: client credentials.
- Device without a browser: device flow.
- Existing username-password or user-agent integration: plan a move to one of the above.
Exercise
A nightly data-sync job runs on a build server with no browser. The business wants every record change in Salesforce to be attributed to a specific named integration user, and wants no consumer secret stored on the build server. Which flow fits best, and which two things must you set up in Salesforce before the first token request will succeed?
Show answer
JWT bearer. The build server keeps only a private key, and Salesforce holds the matching public certificate, so no consumer secret is stored. Before the first request, upload the certificate to the app and pre-authorize the integration user. To pre-authorize, set the app's permitted users policy to admin-approved users and assign a profile or permission set that includes the app. The JWT's sub claim must be that user's username, and aud must match the login host.
Exercise
A team wants to replace its username-password flow for a mobile app, where each employee signs in as themselves. A colleague suggests client credentials because it needs no login screen. Why is that wrong, and what should they use?
Show answer
Client credentials issues tokens for one fixed run-as user, so every employee would act with that user's permissions and the audit trail would not show who did what. The app needs per-user identity, so it should use authorization code with PKCE. A mobile app cannot hold a secret safely, which PKCE accounts for. The refresh token flow can keep users signed in.