# How does the OAuth 2.0 Web Server Flow secure token exchanges for server-side external applications?

**Topic:** Integration · **Difficulty:** Medium · **Tags:** Integration, OAuth Flows, Web Server Flow

Read it on the site: https://www.forcechallenges.com/interview-questions/how-does-the-oauth-2-0-web-server-flow-secure-token-exchanges-for-server-side-external-applications

## Answer

**Basics:** It is designed for apps with a secure server side (a web app, an integration portal) that can keep a secret. The user's credentials go only to Salesforce. The app obtains tokens to call APIs on behalf of that user.

**How it works:**
1. The app redirects the browser to `/services/oauth2/authorize` with `response_type=code`, `client_id`, `redirect_uri`, `scope`, `state` and optionally `code_challenge` (PKCE).
2. The user logs in and approves.
3. Salesforce redirects back with `?code=...&state=...`.
4. The server verifies `state`, then POSTs to `/services/oauth2/token` with `grant_type=authorization_code`, the code, `client_id`, `client_secret`, `redirect_uri` and `code_verifier`.
5. Response includes `access_token`, `refresh_token` (if `refresh_token` scope), `instance_url`, `id`, `issued_at`, and `id_token` with `openid`.
6. The server stores tokens securely and calls APIs; when the access token expires, it uses `grant_type=refresh_token`.

**Advanced / deep dive:** Security layers: the code is single-use and expires quickly; the secret authenticates the client; `state` prevents CSRF; **PKCE** protects against code interception and is recommended even for confidential clients (and required to make public clients safe). Access token lifetime follows the session timeout of the Connected App or org; refresh tokens persist per the refresh token policy (for example until revoked or valid for N days). Revoke through `/services/oauth2/revoke`. For machine-to-machine flows with no user, prefer JWT Bearer or Client Credentials. For multi-tenant apps, handle sandbox (`test.salesforce.com`) versus production hosts, and prefer My Domain URLs. Keep tokens encrypted at rest and scope them narrowly.

**Code / config example:**
```
GET https://MyDomain.my.salesforce.com/services/oauth2/authorize?response_type=code
 &client_id=<KEY>&redirect_uri=https://app.example.com/cb&scope=api%20refresh_token
 &state=<random>&code_challenge=<S256>&code_challenge_method=S256
```

**Hidden gotchas & must-remember points:**
- The redirect URI must match the Connected App callback exactly, including trailing slashes.
- Use the returned `instance_url` for API calls.
- Missing the `refresh_token` scope means no refresh token.
- Refresh token policy can silently expire tokens ("valid until revoked" versus time-based).
- Authorization codes are one-time use and short-lived.
- Do not put the client secret in JavaScript or mobile bundles.

**Best practice:** Use Web Server flow with PKCE and `state`, keep tokens server-side and encrypted, and request only the scopes you need.

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