Sub topic 2
OAuth Flows and Access Control in Salesforce
Assume you know basic Salesforce administration, HTTP requests, authentication versus authorization, and command-line curl. The right OAuth flow depends on whether a human signs in and which Salesforce user should own the access.
Authorization Code Flow with PKCE
The authorization code flow with Proof Key for Code Exchange (PKCE) is for applications where a user signs in. PKCE protects the temporary authorization code from being reused by an attacker.
The application creates a random code_verifier and derives a S256 code_challenge. It sends the challenge to Salesforce, receives an authorization code after user approval, and exchanges that code with the verifier for tokens.
CODE_VERIFIER=$(openssl rand -base64 64 | tr -d '=+/\n' | cut -c1-64)
CODE_CHALLENGE=$(printf '%s' "$CODE_VERIFIER" | openssl dgst -sha256 -binary | openssl base64 -A | tr '+/' '-_' | tr -d '=')
echo "https://MyDomain.my.salesforce.com/services/oauth2/authorize?response_type=code&client_id=$CONSUMER_KEY&redirect_uri=https%3A%2F%2Fapp.example.com%2Fcallback&scope=api%20refresh_token&code_challenge=$CODE_CHALLENGE&code_challenge_method=S256"
curl -X POST https://MyDomain.my.salesforce.com/services/oauth2/token \
-d grant_type=authorization_code \
-d code="$AUTH_CODE" \
-d client_id="$CONSUMER_KEY" \
-d redirect_uri=https://app.example.com/callback \
-d code_verifier="$CODE_VERIFIER"The callback URL must exactly match the configured URL.
Server-to-Server Flows
The JWT bearer flow uses a signed JSON Web Token. Salesforce verifies the signature with the connected app's certificate, so no interactive login is needed. The sub claim identifies the Salesforce username. This flow suits scheduled jobs and CI pipelines.
The client credentials flow also avoids user interaction. Salesforce runs the request as the user configured in the app's policy. That run-as user determines record access and API permissions.
Policies and Permissions
Policies add controls after an OAuth request reaches the app.
- Permitted users can allow all users to self-authorize or require pre-approval.
- IP relaxation determines whether normal profile login IP restrictions apply to OAuth sessions.
- Refresh token behavior controls whether tokens remain valid, expire, or are revoked after inactivity.
Profiles and permission sets then control what the authorized user can do. Assign the smallest API, object, field, and record permissions needed.
SAML single sign-on can authenticate users through an identity provider. It complements OAuth: SAML proves identity, while OAuth grants API or connected-app access.
External Client Apps
External client apps (ECAs) are Salesforce's recommended replacement for new connected-app integrations. They separate developer settings, such as OAuth configuration, from administrator policies, such as access and distribution. Local ECAs stay in one org. Packaged ECAs can be distributed across orgs with controlled settings.
Exercise
Which flow is usually appropriate for a scheduled integration that should run as one known Salesforce user without interactive login?
Show answer
The JWT bearer flow. It uses a signed assertion whose subject identifies the Salesforce user.