# How does the OAuth 2.0 JWT Bearer Token Flow enable unattended server-to-server authentication?

**Topic:** Integration · **Difficulty:** Medium · **Tags:** Integration, JWT Bearer Flow, Server-to-Server

Read it on the site: https://www.forcechallenges.com/interview-questions/how-does-the-oauth-2-0-jwt-bearer-token-flow-enable-unattended-server-to-server-authentication

## Answer

**Basics:** Server-to-server integrations (CI/CD, middleware, scheduled jobs) cannot show a login screen. With JWT Bearer you upload the public certificate to a Connected App (or External Client App); the integration proves its identity by signing with the matching private key. Salesforce checks the signature, confirms the user is pre-authorized, and returns an access token.

**How it works:**
1. Create the key pair and certificate; upload the certificate in the Connected App (enable OAuth, "Use digital signatures", scopes such as `api`).
2. Pre-authorize the user: set the Connected App to "Admin approved users are pre-authorized" and assign a profile or permission set.
3. Build the JWT: header `{"alg":"RS256"}`; claims `iss` = consumer key, `sub` = Salesforce username, `aud` = `https://login.salesforce.com` (or `https://test.salesforce.com` for sandboxes, or the My Domain URL), `exp` = a few minutes ahead (Salesforce rejects expiry beyond 3 minutes).
4. POST to `/services/oauth2/token` with `grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer` and `assertion=<jwt>`.
5. Use the returned `access_token` and `instance_url`.

**Advanced / deep dive:** No refresh token is issued; just sign a new JWT when the token expires or a 401 occurs. Treat the private key as the credential: store it in a vault/HSM, rotate certificates before expiry, and use a dedicated API-only integration user with a minimal permission set. Access can be limited by IP ranges and session policies on the connected app. Compared with the Client Credentials flow (no user key pair; runs as a named run-as user) JWT is better when you want per-key control and nonrepudiation. Inside Apex, `Auth.JwtBearerTokenExchange`, `Auth.JWT` and `Auth.JWS` support the outbound equivalent. Note that from Spring '26 new Connected App creation is restricted by default in favor of External Client Apps, which also support JWT bearer (verify in release notes).

**Code / config example:**
```bash
# claims: {"iss":"<consumer_key>","sub":"integ@acme.com","aud":"https://login.salesforce.com","exp":1760000000}
curl -X POST https://login.salesforce.com/services/oauth2/token \
  -d "grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer" \
  -d "assertion=$SIGNED_JWT"
```

**Hidden gotchas & must-remember points:**
- `invalid_grant: user hasn't approved this consumer` means the user is not pre-authorized.
- `aud` must match the login host exactly (production vs sandbox vs My Domain).
- Clock skew and an `exp` too far ahead cause rejection.
- Changing the uploaded certificate invalidates old signatures; plan rotation.
- The user's profile still controls what the token can do; scopes only narrow it.

**Best practice:** Use a dedicated least-privilege integration user, protect the private key in a vault, and rotate certificates on a schedule.

---
Source: ForceChallenges · last updated 2026-11-02
