# How does the JWT Bearer Token Flow secure server-to-server integrations without requiring user credentials?

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

Read it on the site: https://www.forcechallenges.com/interview-questions/how-does-the-jwt-bearer-token-flow-secure-server-to-server-integrations-without-requiring-user-credentials

## Answer

**Basics:** Unattended integrations (nightly jobs, middleware) cannot show a login screen. With JWT bearer, the app signs a short-lived assertion saying "I am this client, acting as this user", and Salesforce verifies the signature with the uploaded certificate.

**How it works:**
1. Generate a key pair and certificate. Keep the private key on the server.
2. Create a connected app (or external client app, which Salesforce is steering new integrations toward, so verify availability in your org), enable OAuth, tick "Use digital signatures", upload the certificate, and add scopes such as `api`.
3. Pre-authorize the user: set the app to "Admin approved users are pre-authorized" and assign a profile or permission set.
4. Build the JWT with claims `iss` (consumer key), `sub` (Salesforce username), `aud` (`https://login.salesforce.com`, `https://test.salesforce.com` or your My Domain) and `exp` (a few minutes ahead), then sign it with RS256.
5. POST to `/services/oauth2/token` with `grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer` and `assertion=<jwt>`.
6. Salesforce returns an `access_token` and `instance_url`.

**Advanced / deep dive:** No refresh token is issued. When the token expires, the client simply signs a new JWT. The access token lives as long as the session timeout settings allow. Use a dedicated integration user with the API Only access and minimal permissions, and rotate the certificate before expiry. Combine with IP restrictions on the connected app, and with mTLS where required. Keep the private key in a vault or HSM.

**Code / config example:**
```javascript
const jwt = require('jsonwebtoken');
const fs = require('fs');
const assertion = jwt.sign(
  { iss: CONSUMER_KEY, sub: 'integration@example.com.prod', aud: 'https://login.salesforce.com' },
  fs.readFileSync('server.key'),
  { algorithm: 'RS256', expiresIn: '3m' }
);
// POST https://login.salesforce.com/services/oauth2/token
// grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer&assertion=<assertion>
```

**Hidden gotchas & must-remember points:**
- `invalid_grant` usually means the user is not pre-authorized, the `aud` is wrong, or the `sub` username is wrong (sandbox usernames carry a suffix).
- Use `test.salesforce.com` as `aud` for sandboxes, or the My Domain URL.
- The assertion's expiry must be short (a few minutes at most).
- The certificate expiring breaks the integration without warning.
- The token endpoint call itself still needs HTTPS.
- Using an admin as the integration user is a common security mistake.

**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
