OAuth 2.0 Authorization Code flow with Proof Key for Code Exchange (PKCE) for public clients.
## When to Use
- Single Page Applications (SPAs)
- Mobile native applications
- Desktop applications
- Any client that cannot securely store a client_secret
## Mermaid Template
```mermaid
%%{init: {'theme': 'base', 'themeVariables': {
'actorBkg': '#ddd6fe',
'actorTextColor': '#1f2937',
'actorBorder': '#6d28d9',
'signalColor': '#334155',
'signalTextColor': '#1f2937',
'noteBkgColor': '#f8fafc',
'noteTextColor': '#1f2937',
'noteBorderColor': '#334155'
}}}%%
sequenceDiagram
autonumber
box rgba(165,243,252,0.3) USER DEVICE
participant U as 👤 User
participant App as 📱 Mobile App<br/>or SPA
end
box rgba(167,243,208,0.3) SALESFORCE
participant SF as ☁️ Salesforce<br/>Authorization Server
end
Note over U,SF: Authorization Code + PKCE Flow (RFC 7636)
U->>App: 1. Tap "Login with Salesforce"
App->>App: 2. Generate PKCE Parameters
Note over App: code_verifier = random(128 bytes)<br/>code_challenge = BASE64URL(SHA256(code_verifier))
App->>App: 3. Generate state (CSRF protection)
App->>SF: 4. Open browser/webview to /authorize
Note over App,SF: response_type=code<br/>client_id=CONSUMER_KEY<br/>redirect_uri=CALLBACK_URL<br/>scope=api refresh_token<br/>state=RANDOM_STATE<br/>code_challenge=CODE_CHALLENGE<br/>code_challenge_method=S256
SF->>U: 5. Display Login Page
U->>SF: 6. Enter Username & Password
SF->>SF: 7. Authenticate User
SF->>U: 8. Display Consent Screen
Note over SF,U: "App requests access to:<br/>• API Access<br/>• Refresh Token"
U->>SF: 9. Grant Consent (Allow)
SF->>SF: 10. Generate Authorization Code
Note over SF: Store code_challenge with code
SF->>App: 11. Redirect to callback with code
Note over SF,App: scheme://callback?code=AUTH_CODE<br/>&state=RANDOM_STATE
App->>App: 12. Verify state matches
App->>SF: 13. POST /services/oauth2/token
Note over App,SF: grant_type=authorization_code<br/>code=AUTH_CODE<br/>client_id=CONSUMER_KEY<br/>redirect_uri=CALLBACK_URL<br/>code_verifier=CODE_VERIFIER
SF->>SF: 14. Validate Code & PKCE
Note over SF: Verify: BASE64URL(SHA256(code_verifier))<br/>== stored code_challenge