Sub topic 12
MFA, API logins, and integrations
This chapter assumes you know how to manage profiles and permission sets, and you can find connected apps in Setup. It also assumes you know the difference between a person signing in through a browser and a program calling the Salesforce API. The previous chapters covered enabling MFA for the user interface (UI). Here you will see what happens when no person is present to approve a prompt.
Why API logins are a separate problem
MFA works by asking a human for a second proof, such as a push approval or a six-digit code. An integration, such as a nightly ETL job, has no phone to tap. Salesforce therefore treats two kinds of login differently:
- UI logins happen in a browser, a login page, or the mobile app. The MFA requirement applies to these.
- API logins happen when software authenticates to get a session or an access token. This includes SOAP
login()calls, OAuth flows, and tools like Data Loader.
A weak API credential still gives an attacker the same data access that a UI login would. Your goal is to keep MFA on people and give programs credentials that are strong, narrow, and monitored.
The ways a program can authenticate
| Method | Who or what signs in | MFA involvement | Typical use |
|---|---|---|---|
| Username, password, and security token (SOAP or the OAuth username-password flow) | A stored credential | None by default. Optional MFA for API logins applies if enabled. | Legacy integrations |
| OAuth web server flow (authorization code) | A person in a browser, once | The browser login triggers MFA like any UI login | Apps acting for a user |
| OAuth JWT bearer flow | A signed token from a certificate | No prompt. Trust comes from the pre-approved certificate. | Server-to-server jobs |
| OAuth client credentials flow | The app's key and secret | No prompt. Salesforce runs the app as a chosen user. | Server-to-server jobs |
The username-password methods are the weakest. The password is stored somewhere, and if it leaks, the attacker can sign in from anywhere you allow.
MFA on API logins
Salesforce has a user permission named Multi-Factor Authentication for API Logins. When a user has it, a username-and-password API login must also include a time-based one-time password (TOTP) code from an authenticator app. The code is typically appended to the password, followed by the security token if one is required. Check your release's Help documentation for the exact format, because this behavior has changed between releases.
This option suits a human who runs Data Loader or a scripting tool with their own credentials. It does not suit unattended jobs, because a TOTP code changes every 30 seconds and a script cannot supply it.
Two details matter in practice:
- The permission for API logins is separate from the permission for UI logins. A user can have either, both, or neither.
- The two permissions govern different login paths. A user who has MFA for UI logins is not automatically protected when a script logs in through the SOAP API.
OAuth flows and where MFA fits
With the web server flow, your app redirects the person to the Salesforce login page. Salesforce handles the full UI login there, including MFA, and then returns an authorization code. The app never sees the password or the second factor. This is the preferred pattern for anything acting on behalf of a person.
With the JWT bearer flow and the client credentials flow, no person signs in at run time. You do the approval once, when you set up the connected app (or external client app):
- For JWT, you upload a certificate and pre-authorize the integration user with a profile or permission set. The app proves its identity by signing a token with the matching private key.
- For client credentials, you pick a "run as" user in the app's settings, and Salesforce issues tokens as that user.
These flows are not MFA-protected in the usual sense. Your protection comes from the private key or secret, the narrow permissions of the integration user, and network restrictions.
The following Python script requests an access token with the JWT bearer flow. It needs the PyJWT, cryptography, and requests packages. It reads its settings from environment variables.
import os
import time
import jwt
import requests
consumer_key = os.environ["SF_CONSUMER_KEY"]
username = os.environ["SF_USERNAME"]
key_path = os.environ["SF_PRIVATE_KEY_PATH"]
login_url = os.environ.get("SF_LOGIN_URL", "https://login.salesforce.com")
with open(key_path, "rb") as key_file:
private_key = key_file.read()
claims = {
"iss": consumer_key,
"sub": username,
"aud": login_url,
"exp": int(time.time()) + 180,
}
assertion = jwt.encode(claims, private_key, algorithm="RS256")
response = requests.post(
f"{login_url}/services/oauth2/token",
data={
"grant_type": "urn:ietf:params:oauth:grant-type:jwt-bearer",
"assertion": assertion,
},
timeout=30,
)
response.raise_for_status()
token = response.json()
print(token["instance_url"])If the user has not been pre-authorized for the app, the call fails with an invalid_grant error. That failure is the control working as designed.
Integration users
Give each integration its own user. Do not reuse an administrator's account. A well-built integration user has these properties:
- A dedicated license and profile. Salesforce offers an integration user license for some uses, and many orgs use a standard license with a minimal profile.
- The API Only User permission. This blocks the account from signing in through the UI, so no one can use it in a browser, and no one needs to register an MFA method for it.
- Least privilege. Grant object and field access with a permission set that covers only what the integration touches.
- Login IP ranges on the profile. Set the ranges to the fixed addresses of the servers that run the integration. A leaked credential is then useless from other networks.
- A password that does not expire, if you still use passwords. Set it deliberately. An expired password silently breaks the job, and a forgotten one can break production.
Step-up authentication for connected apps
A connected app has a session security level setting for its OAuth sessions. If you require High Assurance for an app, Salesforce asks the user to complete MFA before the session can use that app. This is how you protect a sensitive app even when the user's regular login only used a password, for example through some SSO setups. It applies only to flows where a person is present. It has no effect on a JWT bearer flow, because no person is there to be prompted.
Seeing how your integrations authenticate
Login history records API logins too. The query below finds non-UI logins in the last seven days, along with the login type, the API type, and the source IP address. Use it to find forgotten scripts and unexpected locations.
SELECT UserId, LoginTime, LoginType, ApiType, ApiVersion, Application, SourceIp, Status
FROM LoginHistory
WHERE LoginTime = LAST_N_DAYS:7
AND LoginType != 'Application'
ORDER BY LoginTime DESC
LIMIT 200The Application login type represents ordinary browser logins. Anything else is worth reviewing. Look for these patterns:
- A source IP address that is not one of your known servers.
- Repeated failures in the
Statuscolumn for one user. This often means a stale password or an attack. - A real person's account appearing in API activity, which suggests a script is using their credentials.
A decision flow
Common errors
- Assuming the MFA requirement covers integrations. It does not protect a password-only API login. Review each integration separately.
- Using a personal account for an integration. The job fails when that person leaves or the password changes. It also runs with more access than it needs.
- Forgetting the security token. Password-based API logins from outside trusted IP ranges need the token appended. After a password reset, the old token stops working.
- Skipping pre-authorization. JWT and client credentials flows return
invalid_grantor similar errors until the user or profile is approved for the app. - Leaving a secret in source control. Store keys and secrets in a vault or in environment variables, and rotate them when anyone with access leaves.
- Locking out your own job with IP rules. If you move the job to new infrastructure, update the profile's login IP ranges first.
Exercise
A nightly job loads data into Salesforce from a fixed server. A developer proposes giving it the credentials of an administrator who has MFA turned on for the UI. What would you set up instead, and how would you limit the damage if its credential leaked?
Show answer
Create a dedicated integration user with a minimal permission set and the API Only User permission, so it cannot sign in through the browser and needs no MFA registration. Authenticate with the JWT bearer flow (or client credentials), with the user pre-authorized for the connected app. Store the private key in a vault. Set login IP ranges on the profile to the server's fixed addresses, so a leaked key cannot be used from elsewhere. Then monitor the account in LoginHistory for unexpected IP addresses or failures.
Exercise
You run the LoginHistory query for non-UI logins and find a named employee's account logging in through the API from an IP address you do not recognize. List two things to check or do.
Show answer
First, find out whether the employee is running a script or a tool such as Data Loader from a personal machine or a new network, and whether that account has the MFA for API Logins permission. Second, if the activity is not explained, treat the credential as compromised: reset the password, which also invalidates the security token, revoke the user's OAuth sessions, and move the legitimate job to a dedicated integration user.