Sub topic 8
Authenticating the Integration with OAuth
This chapter covers how the integration user signs in without a password prompt. It assumes you have the basic OAuth 2.0 knowledge listed in the tutorial prerequisites, and that the user, license, profile, and permission set from earlier chapters already exist.
The run-as user model
In a server-to-server integration, no person is present to approve a login screen. The integration still needs an identity, because Salesforce evaluates every API call against one user. That user is the run-as user. Every permission you granted in the permission set applies to the access token issued for that user.
OAuth does not widen the user's access. A token can have fewer rights than the user, because scopes limit it, but it can never have more. The user's permissions are the upper bound.
Choosing a connected app or external client app
Salesforce needs an application record that represents your integration. You can create one of two kinds:
- A connected app is the long-standing record type for OAuth clients.
- An external client app is the newer record type, which Salesforce is promoting for new integrations.
Both hold the consumer key, the OAuth scopes, the certificate, and the access policies. The steps below use connected-app terms. External client apps expose the same settings under different menu names.
Choosing a flow
For a headless integration, the JWT bearer flow is a common choice. The integration signs a short-lived JWT (JSON Web Token) with a private key. Salesforce checks the signature against the certificate you uploaded to the app, then returns an access token. No password or interactive consent is involved.
The client credentials flow is another option. It binds the app to one run-as user and uses only the consumer key and secret. It is simpler, but the secret is a bearer credential, so protect it carefully.
Configuring the app for least privilege
Apply these settings so the app does not become a second source of excess access:
- Enable OAuth and add a callback URL. JWT flows do not use it, but the form requires a value.
- Upload the public certificate that matches your private key.
- Grant the narrowest scopes. Choose Manage user data via APIs (api). Skip
fulland avoidrefresh_tokenunless you need it. - Set Permitted Users to Admin approved users are pre-authorized.
- Assign the integration user's permission set, or its profile, to the app. This is what pre-authorizes the user.
Pre-authorization is the key control. Without it, a JWT request for that user fails with an error such as user hasn't approved this consumer. With it, only users you list can obtain tokens, so a leaked consumer key alone does not let someone sign in as any user.
Pair this with the IP Relaxation and refresh token policies. Enforcing login IP restrictions is covered in the next chapter.
Testing the login
This command logs in with the JWT flow and then runs a query:
sf org login jwt \
--client-id "$SF_CLIENT_ID" \
--jwt-key-file server.key \
--username [email protected] \
--instance-url https://test.salesforce.com \
--alias integration-sandbox
sf data query \
--target-org integration-sandbox \
--query "SELECT Id, Name, Industry FROM Account LIMIT 5"Use https://test.salesforce.com for a sandbox and https://login.salesforce.com for production. A wrong instance URL is a common reason for login failure.
Common errors
| Error | Likely cause |
|---|---|
| `invalid_grant: user hasn't approved this consumer` | The user is not pre-authorized for the app. |
| `invalid_grant: audience is invalid` | The JWT audience does not match the login URL you are calling. |
| `invalid_client_id` | The consumer key is wrong, or the app is in another org. |
| `invalid_app_access` | The user's profile or permission set is not assigned to the app. |
Keep the private key and consumer secret in a secrets manager, not in source control. Rotation is covered in the final chapter.
Exercise
Why set Permitted Users to "Admin approved users are pre-authorized" for an integration app?
Show answer
Only users you explicitly assign through a profile or permission set can obtain tokens. A leaked consumer key is then not enough to sign in as another user, and no interactive consent step is needed.
Exercise
An OAuth token was issued with the api scope for the integration user. Can it read an object the user's permission set does not grant? Why or why not?
Show answer
No. Scopes can only narrow what a token does. The run-as user's permissions are the upper limit, so the call is denied.