Configuring OAuth 2.0 Authorization Flow
Overview
Configure secure OAuth 2.0 authorization flows including Authorization Code with PKCE, Client Credentials, and Device Authorization Grant. This skill covers flow selection, PKCE implementation, token lifecycle management, scope design, and alignment with OAuth 2.1 security requirements.
Objectives
- Implement Authorization Code flow with PKCE for public and confidential clients
- Configure Client Credentials flow for machine-to-machine communication
- Design least-privilege scope hierarchies
- Implement secure token storage, refresh, and revocation
- Apply OAuth 2.1 best practices and RFC 9700 security recommendations
- Validate token integrity and prevent common OAuth attacks
Key Concepts
OAuth 2.0 Grant Types
- Authorization Code + PKCE: Recommended for all client types (web, mobile, SPA). PKCE is mandatory in OAuth 2.1.
- Client Credentials: Machine-to-machine authentication without user context.
- Device Authorization Grant (RFC 8628): For input-constrained devices (smart TVs, CLI tools).
- Refresh Token: Long-lived token to obtain new access tokens without re-authentication.
PKCE (Proof Key for Code Exchange)
PKCE (RFC 7636) prevents authorization code interception attacks:
- Client generates random
code_verifier (43-128 characters, unreserved URI chars)
- Client computes
code_challenge = BASE64URL(SHA256(code_verifier))
- Authorization request includes
code_challenge and code_challenge_method=S256
- Token request includes original
code_verifier
- Server validates
SHA256(code_verifier) matches stored code_challenge
Token Types
- Access Token: Short-lived (5-60 min), bearer or DPoP-bound
- Refresh Token: Long-lived, single-use with rotation
- ID Token (OIDC): JWT containing user identity claims
Implementation Steps
Step 1: Authorization Code Flow with PKCE
- Generate cryptographically random code_verifier (min 43 chars)
- Compute code_challenge using S256 method
- Redirect user to authorization endpoint with parameters:
- response_type=code
- client_id, redirect_uri, scope, state
- code_challenge, code_challenge_method=S256
- User authenticates and consents
- Authorization server redirects with authorization code
- Exchange code + code_verifier for tokens at token endpoint
- Validate state parameter matches original value
Step 2: Scope Design
- Define granular scopes:
read:users, write:orders, admin:settings
- Follow least-privilege: request minimum scopes needed
- Implement scope validation on resource server
- Document scope hierarchy and consent requirements
Step 3: Token Security
- Store tokens securely (httpOnly cookies for web, keychain for mobile)
- Implement token refresh with rotation (one-time-use refresh tokens)
- Set appropriate expiration: access tokens 5-15 min, refresh tokens 8-24 hrs
- Enable DPoP (Demonstration of Proof-of-Possession) for sender-constrained tokens
- Implement token revocation endpoint
Step 4: Client Credentials Flow
- Register service client with client_id and client_secret
- Request token: POST /oauth/token with grant_type=client_credentials
- Include scope for required permissions
- Store client_secret securely (vault, env vars, not code)
- Implement certificate-based client authentication for higher assurance
Step 5: Security Hardening
- Enforce PKCE for all authorization code flows
- Use exact redirect URI matching (no wildcards)
- Implement CSRF protection with state parameter
- Enable refresh token rotation and revocation on reuse detection
- Apply RFC 9700 security best practices
- Block implicit grant and ROPC (removed in OAuth 2.1)
Security Controls
| Control |
NIST 800-53 |
Description |
| Access Control |
AC-3 |
Token-based access enforcement |
| Authentication |
IA-5 |
Client credential management |
| Session Management |
SC-23 |
Token lifecycle management |
| Audit |
AU-3 |
Log all token issuance and revocation |
| Cryptographic Protection |
SC-13 |
PKCE and token signing |
Common Pitfalls
- Using implicit grant (removed in OAuth 2.1) instead of authorization code + PKCE
- Storing tokens in localStorage (XSS vulnerable) instead of httpOnly cookies
- Not validating state parameter enabling CSRF attacks
- Using wildcard redirect URIs allowing open redirect exploitation
- Not implementing refresh token rotation allowing token theft persistence
Verification
1---2name: configuring-oauth2-authorization-flow3description: Configure secure OAuth 2.0 authorization flows including Authorization Code with PKCE, Client Credentials, and Device Authorization Grant. This skill covers flow selection, PKCE implementation, token 4domain: cybersecurity5subdomain: identity-access-management6tags: [iam, identity, access-control, authentication, authorization, oauth2, oidc, pkce]7version: "1.0"8author: mahipal9license: Apache-2.010---1112# Configuring OAuth 2.0 Authorization Flow1314## Overview15Configure secure OAuth 2.0 authorization flows including Authorization Code with PKCE, Client Credentials, and Device Authorization Grant. This skill covers flow selection, PKCE implementation, token lifecycle management, scope design, and alignment with OAuth 2.1 security requirements.1617## Objectives18- Implement Authorization Code flow with PKCE for public and confidential clients19- Configure Client Credentials flow for machine-to-machine communication20- Design least-privilege scope hierarchies21- Implement secure token storage, refresh, and revocation22- Apply OAuth 2.1 best practices and RFC 9700 security recommendations23- Validate token integrity and prevent common OAuth attacks2425## Key Concepts2627### OAuth 2.0 Grant Types281. **Authorization Code + PKCE**: Recommended for all client types (web, mobile, SPA). PKCE is mandatory in OAuth 2.1.292. **Client Credentials**: Machine-to-machine authentication without user context.303. **Device Authorization Grant (RFC 8628)**: For input-constrained devices (smart TVs, CLI tools).314. **Refresh Token**: Long-lived token to obtain new access tokens without re-authentication.3233### PKCE (Proof Key for Code Exchange)34PKCE (RFC 7636) prevents authorization code interception attacks:351. Client generates random `code_verifier` (43-128 characters, unreserved URI chars)362. Client computes `code_challenge = BASE64URL(SHA256(code_verifier))`373. Authorization request includes `code_challenge` and `code_challenge_method=S256`384. Token request includes original `code_verifier`395. Server validates `SHA256(code_verifier)` matches stored `code_challenge`4041### Token Types42- **Access Token**: Short-lived (5-60 min), bearer or DPoP-bound43- **Refresh Token**: Long-lived, single-use with rotation44- **ID Token (OIDC)**: JWT containing user identity claims4546## Implementation Steps4748### Step 1: Authorization Code Flow with PKCE491. Generate cryptographically random code_verifier (min 43 chars)502. Compute code_challenge using S256 method513. Redirect user to authorization endpoint with parameters:52 - response_type=code53 - client_id, redirect_uri, scope, state54 - code_challenge, code_challenge_method=S256554. User authenticates and consents565. Authorization server redirects with authorization code576. Exchange code + code_verifier for tokens at token endpoint587. Validate state parameter matches original value5960### Step 2: Scope Design61- Define granular scopes: `read:users`, `write:orders`, `admin:settings`62- Follow least-privilege: request minimum scopes needed63- Implement scope validation on resource server64- Document scope hierarchy and consent requirements6566### Step 3: Token Security67- Store tokens securely (httpOnly cookies for web, keychain for mobile)68- Implement token refresh with rotation (one-time-use refresh tokens)69- Set appropriate expiration: access tokens 5-15 min, refresh tokens 8-24 hrs70- Enable DPoP (Demonstration of Proof-of-Possession) for sender-constrained tokens71- Implement token revocation endpoint7273### Step 4: Client Credentials Flow741. Register service client with client_id and client_secret752. Request token: POST /oauth/token with grant_type=client_credentials763. Include scope for required permissions774. Store client_secret securely (vault, env vars, not code)785. Implement certificate-based client authentication for higher assurance7980### Step 5: Security Hardening81- Enforce PKCE for all authorization code flows82- Use exact redirect URI matching (no wildcards)83- Implement CSRF protection with state parameter84- Enable refresh token rotation and revocation on reuse detection85- Apply RFC 9700 security best practices86- Block implicit grant and ROPC (removed in OAuth 2.1)8788## Security Controls89| Control | NIST 800-53 | Description |90|---------|-------------|-------------|91| Access Control | AC-3 | Token-based access enforcement |92| Authentication | IA-5 | Client credential management |93| Session Management | SC-23 | Token lifecycle management |94| Audit | AU-3 | Log all token issuance and revocation |95| Cryptographic Protection | SC-13 | PKCE and token signing |9697## Common Pitfalls98- Using implicit grant (removed in OAuth 2.1) instead of authorization code + PKCE99- Storing tokens in localStorage (XSS vulnerable) instead of httpOnly cookies100- Not validating state parameter enabling CSRF attacks101- Using wildcard redirect URIs allowing open redirect exploitation102- Not implementing refresh token rotation allowing token theft persistence103104## Verification105- [ ] Authorization Code + PKCE flow completes successfully106- [ ] PKCE code_challenge validated at token endpoint107- [ ] State parameter prevents CSRF108- [ ] Access tokens expire within configured lifetime109- [ ] Refresh token rotation issues new refresh token each use110- [ ] Token revocation invalidates both access and refresh tokens111- [ ] Client Credentials flow works for service-to-service calls112- [ ] Scopes correctly enforced at resource server