Sub topic 9
Restricting where and how the user can log in
This chapter assumes you have already created the integration user, given it a minimal profile and permission sets, and set up OAuth with a connected app or external client app. You should be comfortable with profile settings and with reading LoginHistory. Permissions limit what the user can do. The controls here limit where and when the user can start a session at all, so a stolen key or token is much less useful.
Why login controls matter for an integration user
An integration user has no human to notice a strange login prompt, and it usually has no multi-factor step. Its credential is a private key, a client secret, or a refresh token. If one of those leaks, the attacker gets the user's permissions from anywhere on the internet.
Login restrictions add a second condition. The credential must be valid, and the request must come from a place and time you expect. For a server that calls Salesforce from one fixed address, this is cheap to set up and removes most of the exposure.
The controls at a glance
| Control | Where you set it | What it does | Applies to |
|---|---|---|---|
| Login IP Ranges | Profile | Blocks logins from addresses outside the listed ranges | Every user on that profile |
| Login Hours | Profile | Blocks logins outside the listed hours | Every user on that profile |
| API Only User | Profile or permission set | Blocks interactive UI login, allows API access | Users who hold the permission |
| IP Relaxation | Connected app or external client app policy | Decides whether profile IP rules are enforced for that app | Logins through that app |
| Permitted Users | Connected app or external client app policy | Requires the user to be pre-authorized | Logins through that app |
| Network Access (Trusted IP Ranges) | Setup, Network Access | Marks addresses as trusted, skipping identity verification | The whole org |
| Session settings | Setup, Session Settings | Sets timeouts and optional IP locking | The org, with some profile overrides |
Login IP Ranges and Login Hours exist only on profiles. Permission sets cannot hold them. This is a main reason the integration user needs its own profile, as the earlier chapter on licenses and profiles explained.
Login IP ranges on the profile
Login IP Ranges are a list of start and end IPv4 addresses on the profile. Once the list contains at least one range, a user on that profile can log in only from an address inside a range, and that includes API logins.
Create a profile used by the integration user alone. If you add ranges to a profile that humans also use, you either lock them out or you open the integration to their ranges.
Here is the relevant part of a profile in metadata format. It limits logins to one egress address and to weekday business hours. Times in loginHours are minutes since midnight, so 360 is 06:00 and 1200 is 20:00. Saturday and Sunday are left out, so logins on those days are blocked.
<?xml version="1.0" encoding="UTF-8"?>
<Profile xmlns="http://soap.sforce.com/2006/04/metadata">
<loginHours>
<mondayStart>360</mondayStart>
<mondayEnd>1200</mondayEnd>
<tuesdayStart>360</tuesdayStart>
<tuesdayEnd>1200</tuesdayEnd>
<wednesdayStart>360</wednesdayStart>
<wednesdayEnd>1200</wednesdayEnd>
<thursdayStart>360</thursdayStart>
<thursdayEnd>1200</thursdayEnd>
<fridayStart>360</fridayStart>
<fridayEnd>1200</fridayEnd>
</loginHours>
<loginIpRanges>
<description>Integration server egress address</description>
<endAddress>203.0.113.25</endAddress>
<startAddress>203.0.113.25</startAddress>
</loginIpRanges>
</Profile>For a single address, set the start and end to the same value. Prefer the narrowest range that works. A /24 block is easier to maintain than a list of single addresses, but it also admits 254 other hosts.
Login hours are about starting a session
Login Hours stop new logins outside the allowed window. They also end sessions that are still open when the window closes. A nightly batch job that runs past the end time can fail partway through, so allow a margin. If the integration is a 24-hour service, do not set login hours at all. A restriction that breaks production gets removed, and removing it leaves you worse off than before.
Block the user interface with API Only User
The API Only User permission stops the user from signing in through the browser. The user can still authenticate through the API and OAuth flows. This helps when someone obtains the credentials and tries the normal login page. It does not replace IP restrictions, because an attacker can still call the API.
Add ApiOnlyUser to the permission set the same way you add ApiEnabled:
<userPermissions>
<enabled>true</enabled>
<name>ApiOnlyUser</name>
</userPermissions>Enable this only after the OAuth flow works. Some setup steps, such as first-time authorization with the web server flow, need a browser login.
IP relaxation in the connected app
The profile's IP ranges are only enforced for OAuth logins if the connected app lets them be. The app's OAuth policy has an IP Relaxation setting with these main options:
- Enforce IP restrictions. The profile's login IP ranges apply. This is what you want.
- Relax IP restrictions with second factor. Profile ranges are skipped if the user completes a second factor. An integration user cannot do that, so this setting gives no benefit here.
- Relax IP restrictions. Profile ranges are ignored for logins through this app.
Check this setting before you trust your IP ranges. If the app says "Relax IP restrictions", the ranges on the profile do nothing for that app, and nothing in the profile warns you. Set the policy to enforce, and set Permitted Users to "Admin approved users are pre-authorized" so only users with the app's permission set or profile can use it.
The tutorial's OAuth chapter covers the pre-authorization step. The labels in Setup differ a little between connected apps and external client apps, so look for the equivalent IP and user-approval policies in whichever one you use.
Trusted IP ranges are not login restrictions
Network Access in Setup holds the org's Trusted IP Ranges. They are often mistaken for a firewall. They are not. A login from outside the list still succeeds. The list only means that logins from inside it skip identity verification for browser logins.
For an integration user, trusted ranges do not enforce anything. Use the profile's Login IP Ranges to block logins, and treat Trusted IP Ranges as a convenience for humans.
Session settings
Session settings limit how long a session lasts and how it can be reused. These are the ones to review for an integration user:
- Session timeout. Shorter timeouts reduce the value of a stolen session ID. An integration that requests a new access token per run copes well with short sessions. One that holds a token for days does not.
- Lock sessions to the IP address from which they originated. A session ID used from a different address is rejected. This works well for one fixed server, but it breaks setups where requests leave through a pool of addresses.
- Refresh token policy in the app. Choose the shortest policy your flow supports. The JWT bearer flow needs no refresh token, because the server signs a new assertion for each token request. That is one reason JWT is the preferred flow for servers.
Session settings are mostly org-wide, so test changes in a sandbox. A change meant for the integration user can cut off every human.
How the checks combine
A login passes only if every applicable check passes. The order Salesforce uses internally is not something to depend on, but the logic is a chain of gates:
The "Relax" branch is the trap. A setting in the app can skip a control that is correctly configured on the profile.
Testing the restrictions
Test both the success case and the denial case. A restriction you have never seen deny anything is not proven.
- From an allowed address, run the JWT login and a small query. Both should succeed.
- From a disallowed address, such as a laptop on a different network, run the same login. It should fail.
- Check
LoginHistoryfor the failed attempt.
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 FROM Account LIMIT 1"Run this block from each network. The second run should end at the login step with an authorization error.
Then query the failures:
SELECT LoginTime, SourceIp, LoginType, Status, Application
FROM LoginHistory
WHERE UserId = '005XXXXXXXXXXXXXXX'
AND Status != 'Success'
ORDER BY LoginTime DESC
LIMIT 20The Status text for a blocked attempt names the reason. This is also a useful monitoring query once the restrictions are live, because failures from unknown addresses are worth investigating.
Common errors
- Login works from everywhere. The profile has no ranges, or the app's IP Relaxation is set to relax. Check both.
- Integration fails after a server change. The egress address changed, as happens with cloud-hosted CI runners, serverless functions, or NAT gateways. Use a fixed egress address, or a self-hosted runner behind one.
- Humans are locked out. The ranges were added to a shared profile. Clone a dedicated profile for the integration.
- Nightly job dies at the end of the window. Login Hours closed the session. Widen the window or remove the hours restriction for that user.
- Sandbox works, production fails. Sandboxes and production have different instance URLs and often different egress addresses. Keep the ranges in each environment's own configuration.
Summary of the recommended setup
For a fixed-address server integration using JWT, set a dedicated profile with Login IP Ranges for the egress address, grant ApiOnlyUser through a permission set, set the app's IP Relaxation to enforce, set Permitted Users to admin-approved, and use a moderate session timeout. Add Login Hours only if the integration has a predictable schedule.
Exercise
An integration user's profile has Login IP Ranges set to the company's server address. A tester successfully logs in as the integration user through the connected app from a home network. Give two possible causes and say how you would check each.
Show answer
First, the connected app's IP Relaxation may be set to "Relax IP restrictions", which skips the profile's ranges for logins through that app. Check the app's OAuth policies in Setup. Second, the user may be assigned to a different profile than the one you edited, or the ranges may be saved on a profile that does not apply to this user. Check the user's profile in Setup, then open that profile and confirm its Login IP Ranges. After fixing, retry from the home network and confirm that the attempt is denied and that it appears with a non-success status in LoginHistory.
Exercise
Why can't you use a permission set to restrict the integration user to a single IP address, and what is the practical consequence for how you design the user?
Show answer
Login IP Ranges and Login Hours are profile settings, and permission sets cannot contain them. So the integration user needs its own dedicated profile, not a shared one, so that the IP and hours restrictions affect only the integration and not human users.