Sub topic 4
Choosing OAuth scopes
This chapter assumes you know how a connected app is configured and what an authorization request looks like. A scope limits what an access token can do, so it is the main tool for least privilege.
What a scope controls
A scope names a category of access that the token may use. Scopes do not replace user permissions. The token can only do what both the scope and the user's profile and permission sets allow. A token with the api scope for a user who cannot read Accounts still cannot read Accounts.
Scopes you will use most
| Scope | What it allows |
|---|---|
| `api` | Calls to the REST API, SOAP API, Bulk API, and similar data APIs as the user |
| `refresh_token` | Issues a refresh token so the app can get new access tokens later |
| `openid` | Returns an ID token and enables the identity (userinfo) endpoint |
| `id`, `profile`, `email` | Basic identity details about the user |
| `web` | Use of the access token on the web, such as opening a Salesforce page in a browser |
| `full` | All data the user can access, plus the other scopes except `refresh_token` |
The refresh_token scope is also called offline_access. Salesforce has more scopes for specific products, such as Chatter, Visualforce, and Data Cloud. Check the connected app's scope list for the ones your org offers.
Two places scopes are set
Scopes appear in two places, and they interact.
- The connected app lists the scopes it is allowed to grant. This is the ceiling.
- The authorization request lists the scopes the app asks for. This must be within the ceiling.
If the request asks for a scope the connected app does not list, Salesforce rejects it with invalid_scope. Separate scopes with a space, which is %20 in a URL:
https://login.salesforce.com/services/oauth2/authorize?response_type=code&client_id=<CONSUMER_KEY>&redirect_uri=https%3A%2F%2Fapp.example.com%2Foauth%2Fcallback&scope=api%20refresh_tokenScopes by flow
- Web server flow: the app sends
scopein the authorization request. Ask forrefresh_tokenonly if the app needs to work after the first access token expires. - JWT bearer flow: there is no consent screen. The token gets the scopes configured on the connected app. The flow does not return a refresh token, because the server can sign a new assertion whenever it needs one.
- Client credentials flow: the token gets the scopes configured on the connected app, and the run-as user's permissions limit them further.
Check what you received
The token response includes a scope field. Compare it with what you requested, because a token with fewer scopes than expected is a common cause of failures that look like permission errors.
{
"access_token": "<ACCESS_TOKEN>",
"scope": "api refresh_token",
"instance_url": "https://MyDomainName.my.salesforce.com",
"token_type": "Bearer"
}Choosing well
Start from what the integration does. A job that only calls the REST API needs api. Add refresh_token for user-facing apps that must stay signed in. Add openid only if you read identity information.
Exercise
A server uses the JWT bearer flow. A developer adds refresh_token to the connected app's scopes so the server can refresh its token. Does this work, and what should the server do when its access token expires?
Show answer
It does not help, because the JWT bearer flow does not return a refresh token. The server should sign a new JWT assertion and exchange it for a new access token. The refresh_token scope can be removed from the connected app, and api is enough for REST calls.