# What roles do consumer keys and consumer secrets play during OAuth authentication handshakes?

**Topic:** Integration · **Difficulty:** Medium · **Tags:** Integration, Consumer Keys, OAuth Credentials

Read it on the site: https://www.forcechallenges.com/interview-questions/what-roles-do-consumer-keys-and-consumer-secrets-play-during-oauth-authentication-handshakes

## Answer

**Basics:** OAuth separates the user's identity from the client application's identity. When the Connected App is created, Salesforce generates the **Consumer Key** (`client_id`) and **Consumer Secret** (`client_secret`). The key says which app is asking; the secret proves it really is that app. The user's own password is never given to the app in the code-based flows.

**How it works:**
1. The app sends the user to the authorization endpoint with `client_id` (the key).
2. The user authenticates and approves; Salesforce returns an authorization code.
3. The app's server posts the code to `/services/oauth2/token` with `client_id` and `client_secret` (and the PKCE `code_verifier`).
4. Salesforce validates the pair and returns an access token and refresh token.
5. Later refresh requests also present the key (and the secret if required by policy).
6. Flows differ: Web Server and Client Credentials use the secret; JWT Bearer uses the key plus a signed assertion from a certificate (no secret); User-Agent flow uses only the key.

**Advanced / deep dive:** The key is not a secret in the cryptographic sense (it appears in browser URLs), so the secret and any certificates are what need protection. For public clients, PKCE replaces the secret by binding the code to a one-time verifier. Salesforce lets you require the secret for the Web Server flow and refresh token flow, or relax it for PKCE-only public apps. Treat the secret like a password: store in a vault or Named/External Credential, never in client-side code or Git, and rotate it (you can reset it; resetting breaks existing deployments). Combine with IP restrictions and admin pre-authorization. For server-to-server with no user, prefer JWT Bearer with certificates because there is no shared secret to leak.

**Code / config example:**
```
POST https://MyDomain.my.salesforce.com/services/oauth2/token
grant_type=authorization_code
&code=<AUTH_CODE>
&client_id=<CONSUMER_KEY>
&client_secret=<CONSUMER_SECRET>
&redirect_uri=https://app.example.com/callback
&code_verifier=<PKCE_VERIFIER>
```

**Hidden gotchas & must-remember points:**
- The consumer key and secret are per Connected App, not per user.
- Anyone with both values can impersonate the app within the permissions of any user who has authorized it.
- Never embed the secret in a mobile app or SPA.
- Regenerating the secret invalidates the old one immediately for new requests.
- Sandbox and production Connected Apps have different keys.
- Use the correct token host (My Domain or `test.salesforce.com` for sandboxes).

**Best practice:** Keep the secret in a secrets manager, rotate on schedule, and prefer PKCE or JWT Bearer to reduce reliance on shared secrets.

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