Sub topic 1
OAuth 2.0 in Salesforce: roles, grants, and tokens
Prerequisites
This chapter assumes you know basic HTTP (methods, headers, form-encoded bodies, redirects) and can read JSON. You should have a Salesforce org (Developer Edition or sandbox) where you have administrator access. Later chapters use curl, short Python snippets, and public/private key pairs.
Why OAuth 2.0
An integration needs to call Salesforce without holding a user's password. OAuth 2.0 solves this by letting the app present a short-lived token instead. Salesforce checks the token on each API call and applies the permissions of the user it represents.
The four roles
OAuth 2.0 names four parties. Each maps to something concrete in Salesforce.
| OAuth role | What it does | In Salesforce |
|---|---|---|
| Resource owner | Owns the data and can grant access | A Salesforce user |
| Client | The app that wants access | Your app, registered as a connected app |
| Authorization server | Authenticates and issues tokens | Salesforce login servers (`/services/oauth2/...`) |
| Resource server | Holds the protected data | The Salesforce REST API on your org's instance |
Salesforce plays two roles at once. It issues the tokens and it also accepts them.
Grants
A grant is the method the client uses to prove it may receive a token. OAuth calls these grant types. Salesforce calls the full procedures flows. This tutorial covers three:
- Web server flow uses the
authorization_codegrant. A user logs in and approves the app. The app then acts on behalf of that user. - JWT bearer flow uses the
urn:ietf:params:oauth:grant-type:jwt-bearergrant. The server signs an assertion with a private key and acts as a named user, with no login screen. - Client credentials flow uses the
client_credentialsgrant. The server sends its key and secret and acts as a fixed run-as user.
The first flow needs a person at a browser. The other two are server-to-server.
Tokens
Three kinds of token appear in these flows.
- Authorization code. A one-time value returned to your redirect URL in the web server flow. You exchange it for tokens right away.
- Access token. The credential you send with each API call. It is short-lived and expires when the session ends.
- Refresh token. A long-lived credential that gets a new access token without another login. Only some flows issue one, and only when the app requests the right scope.
The access token travels in an HTTP header:
curl https://MyDomainName.my.salesforce.com/services/data/v60.0/limits \
-H "Authorization: Bearer <ACCESS_TOKEN>""Bearer" means that anyone holding the token can use it. Treat it like a password and send it only over HTTPS.
How the pieces fit
Every flow has the same shape. The client proves itself to the authorization server, receives a token, and presents that token to the resource server.
The grant in the first step is the only part that changes between flows.
Common mistakes
- Treating an access token as permanent. It expires, so your code must handle a rejected call.
- Assuming every flow returns a refresh token. Server-to-server flows usually do not need one, because the app can request a new access token whenever it needs one.
- Forgetting that the token carries a user's permissions. A token for a user with narrow access cannot reach data that user cannot see.
Exercise
A nightly job runs on a server with no person present. Which role does the job play, and which kind of token does it send with each REST API call?
Show answer
The job is the client. It sends an access token in the Authorization: Bearer header. It would get that token through a server-to-server flow (JWT bearer or client credentials), since no user is available to log in.
Exercise
Why is an access token called a bearer token, and what does that imply for how you handle it?
Show answer
Whoever holds, or "bears", the token can use it, with no further proof of identity. So you must keep it secret and send it only over HTTPS.