Sub topic 1
OAuth 2.0 in Salesforce: Roles, Grants, and Tokens
This chapter sets up the vocabulary used in the rest of the tutorial. You will learn who the parties in an OAuth 2.0 exchange are, what a grant type is, which tokens Salesforce issues, and how the three flows covered later differ at a high level. No flow is implemented here. The goal is a working mental model so that later chapters read as variations on one pattern.
Prerequisites
This chapter assumes you already know:
- Basic HTTP: methods, headers, form-encoded bodies, and redirects.
- JSON and the
curlcommand line. - Short Python snippets, which appear in the JWT chapters.
- The idea of a public/private key pair.
- You also need a Salesforce org (Developer Edition or a sandbox) where you have administrator access, so you can follow along in later chapters.
What OAuth 2.0 solves
An integration needs to call Salesforce APIs. The naive approach is to store a username and password in the integration and send them with every request. That approach has real problems:
- The integration sees the user's password and can use it for anything.
- Changing the password breaks the integration.
- You cannot limit what the integration can do, or cut off one integration without affecting others.
OAuth 2.0 replaces the password with a token. A token is a string that the API accepts as proof of authorization. It can be limited in scope, it can expire, and it can be revoked without changing anyone's password. The application never has to handle the user's credentials in the first place, or in the server-to-server flows, never has to handle an interactive login at all.
The four roles
OAuth 2.0 names four parties. Mapping them onto Salesforce makes later chapters easier to follow.
| OAuth role | Meaning | In a Salesforce integration |
|---|---|---|
| Resource owner | The party who owns the data and can grant access to it | A Salesforce user |
| Client | The application that wants access | Your web app, script, or middleware, registered in Salesforce as a connected app |
| Authorization server | Authenticates the owner and issues tokens | Salesforce, at `login.salesforce.com`, `test.salesforce.com`, or your My Domain URL |
| Resource server | Hosts the protected data and checks tokens | The Salesforce REST API, at your org's `instance_url` |
Salesforce plays two of the four roles. The same platform issues tokens and accepts them. That is why the token response, covered in a later chapter, includes an instance_url telling the client where to send API calls.
A connected app is the Salesforce record that represents the client. It holds the consumer key (the client ID), the consumer secret, the allowed scopes, and the policies for each flow. Salesforce also offers External Client Apps as a newer packaging model for the same idea. Chapter 3 covers setup. For now, remember that a client cannot request tokens until it has been registered.
Grants and flows
A grant is the method a client uses to obtain a token. In the token request, the grant is named by the grant_type parameter. People often say "flow" for the whole sequence of requests around a grant. This tutorial uses "flow" for the sequence and grant_type for the parameter value.
The three flows in this tutorial correspond to three grant types:
| Flow | `grant_type` value | Who authorizes | Typical use |
|---|---|---|---|
| Web server flow | `authorization_code` | The user, at a login screen | A web app acting for the logged-in user |
| JWT bearer flow | `urn:ietf:params:oauth:grant-type:jwt-bearer` | An administrator, ahead of time | A server job acting as a named user |
| Client credentials flow | `client_credentials` | An administrator, ahead of time | A server job acting as a fixed run-as user |
Two additional values matter. refresh_token is a grant type used to get a new access token from a refresh token. It is not a way to start a session. Salesforce supports other flows too, such as the username-password flow and device flow, but they are outside this tutorial's scope.
The key difference is where the consent happens. In the web server flow, a person sees a consent screen. In the two server-to-server flows, no person is present when the token is requested. Consent is replaced by configuration done earlier: either pre-authorization of users (JWT bearer) or a designated run-as user (client credentials).
The tokens
Salesforce issues several kinds of tokens and related values. You will see each of them in the token response.
Access token
The access token is the credential you send on API calls. In Salesforce it is an opaque string, so you cannot decode it or read information from it. Treat it as a secret. It is sent in an Authorization: Bearer header. "Bearer" means that anyone holding the string can use it, so there is no further proof of identity. This is why transport security and storage matter so much.
An access token's lifetime is tied to the session settings of the org and the connected app, not to a fixed value set by the client. When it expires or is revoked, API calls fail with HTTP 401 and you must get a new one.
Refresh token
A refresh token lets the client get a new access token without sending the user through login again. It lives much longer than an access token. It is issued only if the client requested the refresh_token scope (or offline_access) and the connected app's policy allows it. Refresh tokens belong to the web server flow in this tutorial. The JWT bearer and client credentials flows do not need them, because the client can simply repeat its own request to get a new access token.
Authorization code
The authorization code is a short-lived, single-use value. It exists only in the web server flow. Salesforce returns it to the client through the browser redirect, and the client exchanges it at the token endpoint for tokens. Because it passes through the browser, it is exposed more than the other values. That is the reason it is exchangeable only once and only together with the client's own proof, a secret or a PKCE verifier.
JWT assertion
A JWT assertion is a signed JSON Web Token that the client builds itself in the JWT bearer flow. The client signs it with a private key. Salesforce verifies the signature against a certificate uploaded to the connected app. The assertion is not an access token. It is a signed request that is exchanged for one.
Consumer key and consumer secret
The consumer key and secret identify the client. They are credentials for the app, not for a user. The consumer key is sent as client_id. The secret is sent as client_secret where the flow requires it. The JWT bearer flow needs no secret, because the signature proves the client's identity.
One pattern, three variations
All three flows share the same shape. The client proves something to the authorization server, then receives an access token, then calls the API.
What changes between flows is the "proof" in the first request:
- Web server flow: an authorization code, obtained after the user logs in, plus the client's secret or PKCE verifier.
- JWT bearer flow: a signed assertion naming the consumer key as issuer and the user as subject.
- Client credentials flow: the consumer key and secret alone.
The last step is identical in all three. The token goes in an Authorization header, and the request is sent to the instance_url.
curl https://MyDomainName.my.salesforce.com/services/data/v60.0/limits \
-H "Authorization: Bearer <ACCESS_TOKEN>"Scopes and the user's permissions
A scope limits what a token may do. Examples are api (call REST APIs), refresh_token (receive a refresh token), and web (use the access token on the web UI). Scopes are requested by the client and must also be enabled on the connected app.
Scopes do not grant permissions. A token with the api scope acts as a Salesforce user, and what it can read or change is still decided by that user's profile, permission sets, sharing rules, and field-level security. The effective access is the overlap of the two. A token can never do more than its user could do, and the scope can narrow it further.
This matters for choosing flows. In the web server flow, the user is whoever logs in. In the JWT bearer flow, it is the user named in the assertion's sub claim. In the client credentials flow, it is the single run-as user configured on the connected app. In each case, a limited integration user is a safer choice than an administrator.
Common misunderstandings
- "OAuth authenticates the user to my app." OAuth 2.0 authorizes API access. Using it for login is a separate layer, OpenID Connect, which adds an ID token. This tutorial does not cover it.
- "A longer-lived token is more convenient, so use it." Long-lived bearer credentials increase exposure. Prefer short access tokens and refresh or re-request as needed.
- "The scope list defines what the integration can do." The user's permissions are the ceiling. Scopes only narrow them.
- "The access token is a JWT I can decode." Salesforce access tokens are opaque. Do not write code that parses them.
- "I can send any call to login.salesforce.com." The login host issues tokens. API calls go to the
instance_url.
Where this chapter fits
The concepts here feed the next chapters in order. Endpoints and My Domain explain where requests go. The connected app chapter shows how to register a client. The scopes and token-response chapters cover the values you just met in detail. Then each flow is built in turn, followed by guidance on choosing among them, revocation, troubleshooting, and security practices.
Exercise
Your nightly batch job runs on a server with no person present. It needs to read Account records as a specific integration user, and you want the server to prove its identity with a private key instead of a shared secret. Which flow and grant_type fit, and what is the one value you send in the token request to prove identity?
Show answer
The JWT bearer flow, with grant_type urn:ietf:params:oauth:grant-type:jwt-bearer. The proof is the assertion, a JWT signed with the server's private key. Its iss claim is the consumer key and its sub claim is the integration user's username. Salesforce checks the signature against the certificate uploaded to the connected app.
Exercise
A token request with the api scope succeeds, but a call to read an object returns an insufficient access error. Using the idea that scopes narrow but do not grant permissions, give the most likely cause.
Show answer
The user the token acts as lacks permission on that object or its fields, through profile, permission sets, or field-level security. The api scope allows calling the API, but the user's own permissions still set the ceiling. Fix it by changing the user's permissions or by using a different user, not by changing the scope.