Sub topic 11
Pre-authorizing users for the JWT bearer flow
This chapter assumes you can create a connected app, know the JWT bearer flow exists, and have a key pair and certificate from the previous chapter. It covers the Salesforce settings that let a JWT request succeed without any user interaction.
Why pre-authorization is required
In the web server flow, the user clicks Allow on a consent screen. The JWT bearer flow has no browser and no consent screen. Salesforce must already know that the named user (the sub claim) is allowed to use the connected app. That setup is called pre-authorization.
If it is missing, the token request fails with invalid_grant and the description "user hasn't approved this consumer".
Configure the connected app
- In Setup, open the connected app and select Manage, then Edit Policies.
- Set Permitted Users to Admin approved users are pre-authorized.
- Save the policies.
- Under Profiles or Permission Sets, assign the profile or permission set that the integration user holds.
Salesforce will then trust any user holding that profile or permission set. A user who holds neither is rejected, even with a valid signature.
Check the supporting settings
Pre-authorization works together with settings you made earlier. Check each of these:
| Setting | Required value |
|---|---|
| Digital certificate | `server.crt` uploaded to the connected app |
| OAuth scopes | Include `api` (and any others the integration needs) |
| Device or flow option | Use digital signatures enabled |
| Permitted Users | Admin approved users are pre-authorized |
| User assignment | Integration user's profile or a permission set is listed |
How the checks fit together
Common mistakes
- Leaving Permitted Users on "All users may self-authorize". A JWT request does not trigger self-authorization, so it fails.
- Approving the wrong profile. The integration user may hold a different profile than the one you added.
- Using the wrong login URL. A sandbox user must use
https://test.salesforce.comas theaudvalue and the login host. - Misspelling the username in
sub. The username is the login name, not the email address, and the two can differ.
Verify your setup
Run the Python script from the JWT bearer chapter. A successful call prints your instance_url. If it fails, read the error_description in the response body first, because it names the failing check.
Exercise
A JWT bearer request has a valid signature, but Salesforce returns invalid_grant with "user hasn't approved this consumer". Name the setting you should check first and the change that fixes it.
Show answer
Check the connected app's Permitted Users policy. Set it to "Admin approved users are pre-authorized", then assign the integration user's profile or a permission set that the user holds to the connected app.
Exercise
Why does the JWT bearer flow need pre-authorization when the web server flow does not?
Show answer
The web server flow gets the user's approval through an interactive consent screen. The JWT bearer flow has no browser or user prompt, so an administrator must approve users in advance through profiles or permission sets.