Sub topic 13
Choosing and securing the run-as user
This chapter assumes you can configure a connected app, read a token response, and call the REST API with a bearer token. You should also know the client credentials request shape from the earlier chapters. The next chapter walks through the full flow. Here the focus is the one setting that decides what that flow can do: the run-as user.
Why the run-as user matters
In the client credentials flow, no person logs in. The request carries only the consumer key and secret. Salesforce therefore needs to know whose identity the resulting access token represents. You answer that by selecting a run-as user in the connected app's client credentials settings.
Every call made with that token behaves as if that user made it:
- Object and field permissions come from the user's profile and permission sets.
- Sharing rules and record visibility apply to that user.
- The user becomes the owner and creator on records the integration inserts. Audit fields such as
CreatedByIdandLastModifiedByIdpoint to that user. - The user's login restrictions can still apply, such as profile login IP ranges and login hours. The connected app's IP relaxation setting also affects this.
Scopes on the connected app are a second limit, not a replacement. The effective access is the overlap of the scopes granted to the app and the permissions the user holds. A token with the api scope still cannot read an object the run-as user cannot read.
Choosing the user
You have three realistic options.
| Option | Strength | Weakness |
|---|---|---|
| Dedicated integration user | Least privilege, clear audit trail, survives staff changes | One more user to maintain |
| A real employee's account | Fast to set up | Token breaks when the person leaves or is deactivated, and their broad access is exposed |
| A System Administrator | Nothing is ever denied | A leaked secret gives full control of the org |
Use a dedicated integration user for each integration. Name it so its purpose is obvious, for example [email protected]. Do not reuse one user for several integrations. A separate user per integration lets you deactivate or restrict one without breaking the others, and the audit fields show which integration wrote a record.
Licence and profile
The user must have API access, which means the API Enabled permission. Salesforce also offers a Salesforce Integration user license intended for API-only integration users. Check your edition for how many you get and which permission sets they need. Whichever license you use, apply these rules:
- Create a minimal profile, or assign a minimal profile and grant access through permission sets.
- Create a permission set for this integration only. Grant read, create, edit, or delete only on the objects and fields it needs.
- Remove access you do not need. Be careful with View All Data, Modify All Data, and View All or Modify All on objects, because these bypass sharing.
- Keep the user out of the System Administrator profile.
Setting it up
In Setup, open the connected app's policies and check the following:
- Enable the client credentials flow on the app.
- Under the client credentials settings, choose the run-as user.
- Confirm the app's scopes cover what the integration needs and no more.
- Confirm the user is active.
Then request a token from your My Domain URL. The request has the same form as in the earlier chapter:
curl -X POST https://MyDomainName.my.salesforce.com/services/oauth2/token \
-d grant_type=client_credentials \
-d client_id=<CONSUMER_KEY> \
-d client_secret=<CONSUMER_SECRET>Notice that the request names no user. Anyone who holds the key and secret gets a token for the run-as user. That is the main security consequence of this flow, and it is why the permissions you give the user are your main control.
Auditing what the user can do
Do not trust your memory of what you assigned. Query it. Use an administrator's access token, not the integration's, to list the permission sets assigned to the integration user:
curl -G https://MyDomainName.my.salesforce.com/services/data/v60.0/query \
-H "Authorization: Bearer <ADMIN_ACCESS_TOKEN>" \
--data-urlencode "q=SELECT PermissionSet.Name, PermissionSet.Profile.Name FROM PermissionSetAssignment WHERE Assignee.Username = '[email protected]'"Rows where PermissionSet.Profile.Name is set represent the user's profile. The other rows are permission sets you assigned. Review this list on a schedule, and again whenever someone adds a permission set to the user.
You can also test from the other side. Request a token with the integration credentials, then try an operation you expect to be denied, such as deleting a record on an object you did not grant. A 403 with INSUFFICIENT_ACCESS or an insufficient access rights message confirms that the limit works. Test in a sandbox first.
Common errors and gotchas
- The user is deactivated or frozen. The token request fails. A common cause is an offboarding process that treats the integration user like any other account. Mark the user as a service account in your process so it is not deactivated by accident.
- No run-as user is selected. The token request is rejected, and Salesforce can return an
invalid_granterror with a message saying that no client credentials user is enabled. Check the policy page before you check the secret. - The token works but queries return nothing or
INSUFFICIENT_ACCESS. The scope is fine and the user lacks object, field, or record access. Look at field-level security as well as object permissions. - Records appear to be owned by the wrong person. The integration user owns what it creates. If records must be owned by a queue or a business user, set
OwnerIdexplicitly in the request. - The user's password expires. The client credentials flow does not use the password, so this does not block it. Do not set up a human-style password routine for the account, but do keep the account locked down by other means such as restricted login IP ranges.
- Changing the run-as user changes the identity of new tokens. Previously issued tokens may keep working until they expire or you revoke them. Revoke them if you need an immediate switch.
Securing the user and the secret
The secret and the user form a pair. Protect both:
- Store the consumer secret in a secrets manager or an environment variable, never in source control or logs.
- Restrict the user by login IP range on the profile, where your integration has a stable egress address.
- Give the user no UI use. If a profile or setting allows interactive login, remove it where you can.
- Rotate the consumer secret on a schedule, and immediately after any suspected leak.
- Monitor the user's activity in login history and event monitoring. Alert on logins from unexpected IP addresses.
Exercise
A teammate sets the run-as user of a client credentials connected app to their own System Administrator account "to make sure nothing is denied". Give two problems with this choice and what you would do instead.
Show answer
First, anyone who obtains the consumer key and secret gets a token with full administrator access, so a leak has the largest possible impact. Second, the integration depends on a person: if they leave and their account is deactivated, token requests fail, and records the integration creates are attributed to them in audit fields. Instead, create a dedicated integration user with a minimal profile and a permission set granting only the objects and fields the integration needs, and select that user as the run-as user.