Sub topic 9
Restricting where and how the user can log in
This chapter assumes you have already created the integration user, its minimal profile, and its permission set. It also assumes the OAuth connected app (or external client app) is set up. Here you limit where the user can connect from and which kinds of sessions it can open. Even with narrow object permissions, a stolen credential is dangerous if it works from anywhere.
Why network and session limits matter
Least privilege covers what a user can do. Login restrictions cover where and how it can do it. An integration usually runs from one or a few known servers, so you can reject every other source address. If the private key or token leaks, the attacker also needs to be on your network.
Login IP ranges on the profile
Login IP ranges are a profile setting. Permission sets cannot hold them. When a profile has ranges, any login from an address outside them is refused. This applies to API logins as well as UI logins. Set the ranges on the integration user's dedicated profile so the restriction applies only to that user.
You can set them in Setup under Profiles > Login IP Ranges, or deploy them as metadata:
<?xml version="1.0" encoding="UTF-8"?>
<Profile xmlns="http://soap.sforce.com/2006/04/metadata">
<loginIpRanges>
<description>Integration server egress addresses</description>
<endAddress>203.0.113.20</endAddress>
<startAddress>203.0.113.10</startAddress>
</loginIpRanges>
</Profile>Use the narrowest range you can. A single address, where start equals end, is best. Avoid ranges like 0.0.0.0 to 255.255.255.255, which turn the control off.
Block interactive logins
Enable API Only User on the integration user's profile. The user can then authenticate through the API but cannot sign in to the Salesforce UI. Nobody can use the integration account for interactive browsing, even if they somehow obtain a password.
You can also set Login Hours on the profile if the integration runs on a schedule, for example nightly. Logins outside those hours are refused. Skip this if the integration must respond at any time.
The connected app can override the profile
The connected app has its own IP Relaxation policy, and it controls whether the profile's ranges apply to OAuth logins.
| Setting | Effect on profile IP ranges |
|---|---|
| Enforce IP restrictions | Profile ranges apply. Use this one. |
| Relax IP restrictions with second factor | Ranges are skipped if a second factor is completed. |
| Relax IP restrictions | Profile ranges are ignored for this app. |
If the app is set to relax restrictions, your profile ranges do nothing for that flow. Check this setting after you configure the ranges.
Also set Permitted Users to Admin approved users are pre-authorized, and assign the app only to the integration user's permission set or profile. Any other user then gets an error when trying to authorize.
Session settings
For a server-to-server flow, set a short Session Timeout on the connected app or profile, so an access token expires quickly. With the JWT bearer flow the integration can request a new token at any time, so short sessions cost little. Avoid issuing refresh tokens that never expire when you do not need them.
How the checks combine
Check that it works
Test from an address outside the allowed range, such as your laptop. The login should fail. Then review the history:
SELECT LoginTime, SourceIp, LoginType, Status, Application
FROM LoginHistory
WHERE UserId = '005XXXXXXXXXXXXXXX' AND Status != 'Success'
ORDER BY LoginTime DESC
LIMIT 20The Status column shows why each attempt failed.
Common mistakes
- Adding ranges to a shared profile, which locks out other users.
- Using Trusted IP Ranges under Network Access. These only skip identity verification. They do not block other addresses.
- Leaving the connected app on Relax IP restrictions.
- Forgetting that the server's outbound address changes behind a cloud NAT. Use a fixed egress address.
Exercise
The integration user's profile has a login IP range of one address, but logins from other addresses still succeed over OAuth. What is the most likely cause, and how do you fix it?
Show answer
The connected app's IP Relaxation is set to "Relax IP restrictions", so the profile's ranges are ignored. Change it to "Enforce IP restrictions", then retest from an address outside the range and confirm the failure in LoginHistory.