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: none-113description: 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: MIT10metadata:11 source: https://skillmd.com/skills/njones17/none-1112---1314# Configuring OAuth 2.0 Authorization Flow1516## Overview17Configure 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.1819## Objectives20- Implement Authorization Code flow with PKCE for public and confidential clients21- Configure Client Credentials flow for machine-to-machine communication22- Design least-privilege scope hierarchies23- Implement secure token storage, refresh, and revocation24- Apply OAuth 2.1 best practices and RFC 9700 security recommendations25- Validate token integrity and prevent common OAuth attacks2627## Key Concepts2829### OAuth 2.0 Grant Types301. **Authorization Code + PKCE**: Recommended for all client types (web, mobile, SPA). PKCE is mandatory in OAuth 2.1.312. **Client Credentials**: Machine-to-machine authentication without user context.323. **Device Authorization Grant (RFC 8628)**: For input-constrained devices (smart TVs, CLI tools).334. **Refresh Token**: Long-lived token to obtain new access tokens without re-authentication.3435### PKCE (Proof Key for Code Exchange)36PKCE (RFC 7636) prevents authorization code interception attacks:371. Client generates random `code_verifier` (43-128 characters, unreserved URI chars)382. Client computes `code_challenge = BASE64URL(SHA256(code_verifier))`393. Authorization request includes `code_challenge` and `code_challenge_method=S256`404. Token request includes original `code_verifier`415. Server validates `SHA256(code_verifier)` matches stored `code_challenge`4243### Token Types44- **Access Token**: Short-lived (5-60 min), bearer or DPoP-bound45- **Refresh Token**: Long-lived, single-use with rotation46- **ID Token (OIDC)**: JWT containing user identity claims4748## Implementation Steps4950### Step 1: Authorization Code Flow with PKCE511. Generate cryptographically random code_verifier (min 43 chars)522. Compute code_challenge using S256 method533. Redirect user to authorization endpoint with parameters:54 - response_type=code55 - client_id, redirect_uri, scope, state56 - code_challenge, code_challenge_method=S256574. User authenticates and consents585. Authorization server redirects with authorization code596. Exchange code + code_verifier for tokens at token endpoint607. Validate state parameter matches original value6162### Step 2: Scope Design63- Define granular scopes: `read:users`, `write:orders`, `admin:settings`64- Follow least-privilege: request minimum scopes needed65- Implement scope validation on resource server66- Document scope hierarchy and consent requirements6768### Step 3: Token Security69- Store tokens securely (httpOnly cookies for web, keychain for mobile)70- Implement token refresh with rotation (one-time-use refresh tokens)71- Set appropriate expiration: access tokens 5-15 min, refresh tokens 8-24 hrs72- Enable DPoP (Demonstration of Proof-of-Possession) for sender-constrained tokens73- Implement token revocation endpoint7475### Step 4: Client Credentials Flow761. Register service client with client_id and client_secret772. Request token: POST /oauth/token with grant_type=client_credentials783. Include scope for required permissions794. Store client_secret securely (vault, env vars, not code)805. Implement certificate-based client authentication for higher assurance8182### Step 5: Security Hardening83- Enforce PKCE for all authorization code flows84- Use exact redirect URI matching (no wildcards)85- Implement CSRF protection with state parameter86- Enable refresh token rotation and revocation on reuse detection87- Apply RFC 9700 security best practices88- Block implicit grant and ROPC (removed in OAuth 2.1)8990## Security Controls91| Control | NIST 800-53 | Description |92|---------|-------------|-------------|93| Access Control | AC-3 | Token-based access enforcement |94| Authentication | IA-5 | Client credential management |95| Session Management | SC-23 | Token lifecycle management |96| Audit | AU-3 | Log all token issuance and revocation |97| Cryptographic Protection | SC-13 | PKCE and token signing |9899## Common Pitfalls100- Using implicit grant (removed in OAuth 2.1) instead of authorization code + PKCE101- Storing tokens in localStorage (XSS vulnerable) instead of httpOnly cookies102- Not validating state parameter enabling CSRF attacks103- Using wildcard redirect URIs allowing open redirect exploitation104- Not implementing refresh token rotation allowing token theft persistence105106## Verification107- [ ] Authorization Code + PKCE flow completes successfully108- [ ] PKCE code_challenge validated at token endpoint109- [ ] State parameter prevents CSRF110- [ ] Access tokens expire within configured lifetime111- [ ] Refresh token rotation issues new refresh token each use112- [ ] Token revocation invalidates both access and refresh tokens113- [ ] Client Credentials flow works for service-to-service calls114- [ ] Scopes correctly enforced at resource server