Sub topic 17
Security best practices
This chapter assumes you can create a connected app or external client app (ECA), know the main OAuth flows (authorization code with PKCE, JWT bearer, client credentials), and can manage profiles and permission sets in Setup. It collects the controls from earlier chapters into one checklist and explains why each one matters.
Think in terms of blast radius
An OAuth integration has three things an attacker can steal: the app's credentials (consumer secret or private key), a user's tokens (access and refresh), and the permissions behind those tokens. Most best practices do one of two things. They make a stolen item harder to use, or they make what it unlocks smaller.
The diagram shows where each control sits.
Design: scopes and app boundaries
Request only the scopes the integration needs. Scopes limit what a token can do, regardless of what the user can do.
- Use
apifor REST calls. Do not addfullunless you have a specific reason. It grants everything the user can access through the APIs and the UI. - Add
refresh_tokenonly if the client must act after the user is gone. A server using the JWT bearer flow does not need a refresh token, because it can sign a new assertion at any time. - Create one app per integration and per environment. A shared app means one leaked secret affects every consumer, and you cannot revoke one consumer without breaking the others.
- Use exact HTTPS callback URLs. Avoid wildcards and avoid registering
localhostcallbacks on production apps. Native or desktop clients that need a custom scheme should still register it explicitly.
Choose the flow that removes secrets
The flow you choose decides what a leak looks like.
| Situation | Preferred flow | Why |
|---|---|---|
| Browser or mobile app, user signs in | Authorization code with PKCE | The verifier is created per request, so a stolen code is useless without it. |
| Server-to-server, user identity matters | JWT bearer | The private key never leaves your server and no secret is sent over the wire. |
| Server-to-server, one fixed identity | Client credentials | Simple, but the consumer secret is a long-lived password for the run-as user. |
Avoid the username-password flow. It sends a real user's password to the client and cannot support multi-factor authentication. Salesforce restricts it by default in new orgs. If a legacy integration still uses it, put its replacement on the migration list.
For web server apps that hold a consumer secret, still use PKCE. It protects the code exchange even when the secret leaks.
Protect credentials
- Keep the consumer secret, private key, and refresh tokens in a secrets manager or CI secret store. Never commit them to source control or print them in logs.
- Generate the JWT signing key pair per environment. Upload only the certificate (public part) to the app.
- Set the JWT
expclaim a few minutes ahead. A short window limits replay of a captured assertion. - Rotate on a schedule and after any suspected exposure. For JWT, upload the new certificate, deploy the new key, then remove the old certificate. For a consumer secret, issue a new one and update every client before you delete the old one.
- In CI, write the key to a temporary file with restricted permissions and delete it when the job ends:
umask 077
KEY_FILE="$(mktemp)"
trap 'rm -f "$KEY_FILE"' EXIT
printf '%s' "$SERVER_KEY_PEM" > "$KEY_FILE"
sf org login jwt \
--client-id "$CONSUMER_KEY" \
--jwt-key-file "$KEY_FILE" \
--username [email protected] \
--instance-url https://login.salesforce.com \
--alias ci-orgThe trap removes the key file even when the login fails.
Control who can use the app
Policies decide who gets tokens. Settings describe how the app talks OAuth. In an ECA, these live in separate places so an administrator can tighten policies without editing developer-owned settings.
- Set Permitted Users to admin-approved users. Then assign the app to specific profiles or permission sets, so unlisted users cannot authorize it.
- Keep IP Relaxation at its enforcing option and set trusted IP ranges on the integration user's profile. Relax IP restrictions only for apps that must run from unpredictable addresses, and treat that as an exception you document.
- Set the Refresh Token Policy to the shortest lifetime the use case allows. Use "valid until revoked" only for integrations where re-login is truly impractical.
- Set session timeout and require high-assurance sessions when the app exposes sensitive data.
- Enable "Require secret for Web Server Flow" and "Require Proof Key for Code Exchange (PKCE)" where your client supports them. The exact checkbox names differ between connected apps and ECAs, so verify them in Setup.
Give the run-as user the least privilege
JWT bearer and client credentials flows act as a specific user. That user's access is the integration's access.
- Create a dedicated integration user for each integration. Never use a person's account or a System Administrator.
- Use a minimal profile, such as Minimum Access, and grant capability through a permission set that lists only the objects, fields, and Apex classes the integration needs.
- Grant the API Only User permission so the account cannot sign in through the UI.
- Do not give the integration user "Modify All Data" or "View All Data". Use sharing and field-level security to restrict the data instead.
- Review the permission set when the integration changes. Privileges tend to accumulate.
Monitor and revoke
Security is also what you do after tokens are issued.
- Check OAuth Connected Apps Usage in Setup for use counts and last-used times. An app nobody uses is a liability. Remove it.
- Review the Login History of integration users for unexpected IPs, and use event monitoring if it is licensed.
- Revoke access to an app through the user's OAuth Connected Apps list, or through the app's usage page for all users.
- Revoke a single token programmatically when a client signs out or a device is lost:
curl -X POST https://MyDomain.my.salesforce.com/services/oauth2/revoke \
--data-urlencode token="$REFRESH_TOKEN"Revoking a refresh token also invalidates the access tokens issued from it. Revoking only the access token leaves the refresh token usable.
Where ECAs help
ECAs support these practices more directly than connected apps do:
- Developer settings and admin policies are separate metadata, so a developer can deploy an app without being able to widen who may use it in production.
- Secrets are not exposed through metadata, and they are not retrievable after an app is packaged and installed in another org.
- Local distribution keeps an app inside one org. Use it for internal integrations so you do not publish a package you do not need.
New integrations should start as ECAs. Existing connected apps can follow the migration path from the previous chapter.
Exercise
A nightly job uses the client credentials flow and runs as a System Administrator's account. The consumer secret was pasted into a wiki page. List four changes, in a sensible order, that reduce the risk.
Show answer
1) Rotate the consumer secret and revoke the existing tokens and sessions for the app, because the wiki copy is compromised. 2) Move the new secret into a secrets manager and remove it from the wiki. 3) Create a dedicated integration user with API Only User and a minimal permission set containing only what the job needs, and set it as the run-as user. 4) Set Permitted Users to admin-approved and add trusted IP ranges for the job's network, then review the usage page. Optionally, switch to the JWT bearer flow so no shared secret needs storing.