Concept 2 of 8OAuth and apps

OAuth 2.0 flows on Salesforce

Which OAuth flow fits which client: web server with PKCE, JWT bearer, client credentials, device, refresh token, SAML bearer and token exchange, and why the username-password flow is being phased out.

Your confidence:Not rated

Simple explanation

OAuth lets a client application get an access token to call Salesforce APIs without handling the user's password. The flow depends on the kind of client.

FlowUse it forKey points
Web server (authorization code) + PKCEWeb and mobile apps acting for a signed-in userUser logs in at Salesforce and consents; app exchanges the code (with PKCE verifier) for tokens
Refresh tokenKeeping a user's session without re-loginPolicy controls lifetime and rotation
JWT bearerServer-to-server, acting as a pre-authorised userApp signs a JWT with its private key; certificate uploaded to the app; no interactive login
Client credentialsServer-to-server with a configured run-as userClient ID + secret; simple, but secret management matters
DeviceDevices with limited input (TVs, CLI tools)User approves on another device
SAML bearer assertionReusing an existing SAML assertion to get a tokenEnterprise SSO landscapes
Token exchangeExchanging a token from a trusted identity provider for a Salesforce tokenModern SSO and headless scenarios βš‘ verify availability
User-agent (implicit)Older browser-only appsTokens in the URL; prefer web server + PKCE
Username-passwordLegacy onlyBlocked by default in orgs created from Summer '23; not supported by External Client Apps; migrate away

For integrations choose JWT bearer (no shared secret over the wire, certificate rotation) or client credentials (simpler), both with a dedicated integration user and minimal scopes.

Real-world analogy

OAuth is a hotel key card system. You show ID at reception once (login and consent), and reception gives you a key card (access token) that opens only certain doors (scopes) and expires (token lifetime). A refresh token is the right to get a new key card without queuing again. JWT bearer is a trusted supplier with a signed letter from management who gets a staff card without coming to the desk.

Worked example

JWT bearer flow for a middleware integration (no user interaction):

  1. Create an External Client App with OAuth enabled, upload the integration's X.509 certificate, enable the JWT bearer flow, and pre-authorise the integration user through a permission set (admin-approved users).
  2. The middleware builds and signs a JWT with its private key:
{
  "iss": "<ECA consumer key>",
  "sub": "integration.erp@acme.example",
  "aud": "https://login.salesforce.com",
  "exp": 1796140800
}
  1. It posts the signed assertion to the token endpoint:
POST https://acme.my.salesforce.com/services/oauth2/token
Content-Type: application/x-www-form-urlencoded

grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer&assertion=<signed JWT>
  1. Salesforce validates the signature and user, and returns an access_token (no refresh token for this flow). The middleware caches it until it expires.

Use https://test.salesforce.com as the audience for sandboxes, keep exp short (a few minutes), and rotate the certificate before it expires.

Diagram

Rendering diagram…

Key points

  • User-facing apps: web server flow with PKCE, plus refresh tokens.
  • System-to-system: JWT bearer (certificate) or client credentials (run-as user).
  • Device flow for limited-input devices; SAML bearer and token exchange for SSO landscapes.
  • Username-password flow: blocked by default in newer orgs and not in External Client Apps. Migrate.
  • Scopes limit what the token can do; permissions and sharing still apply.
  • Token policies: lifetimes, refresh token rotation, IP restrictions.

Limits and gotchas

  • The JWT bearer flow needs the user pre-authorised for the app, or token requests fail with user hasn't approved this consumer.
  • Client credentials tokens act as the configured run-as user; protect the secret and rotate it.
  • Implicit (user-agent) tokens appear in URLs and browser history. Avoid for new apps.
  • Login URLs differ between production, sandboxes and My Domain; misconfigured audiences or hosts are a frequent cause of failures.
  • Tokens are bearer credentials: anyone holding one can use it until it expires or is revoked.

🎀 How to say this in an interview

"Which OAuth flow would you use for X?" is guaranteed. Classify the client first (user present? confidential server? device?), then pick the flow, then mention scopes, integration user and token policies. Bring up the username-password retirement to show you're current.

Common follow-ups

JWT bearer or client credentials for middleware?

JWT bearer when you can manage certificates: no shared secret is transmitted and rotation is controlled. Client credentials when the client can't sign JWTs easily, accepting the need to protect and rotate a secret. Both use a dedicated, least-privilege integration user.

Why is PKCE important?

It binds the authorization code to the client that requested it, so an intercepted code can't be exchanged by someone else. It's essential for public clients (mobile, single-page apps) and good practice for all.