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.
When to Use
- When deploying or configuring configuring oauth2 authorization flow capabilities in your environment
- When establishing security controls aligned to compliance requirements
- When building or improving security architecture for this domain
- When conducting security assessments that require this implementation
Prerequisites
- Familiarity with identity access management concepts and tools
- Access to a test or lab environment for safe execution
- Python 3.8+ with required dependencies installed
- Appropriate authorization for any testing activities
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
Workflow
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: Configures secure OAuth 2.0 authorization flows, including4version: '1.0'5author: mahipal (vendored by SOCIS)6license: Apache-2.07platforms:8- linux9- macos10- windows11category: security12metadata:13 socis:14 tags:15 - iam16 - identity17 - access-control18 - authentication19 - authorization20 - oauth221 - oidc22 - pkce23 subdomain: identity-access-management24 upstream: mukul975/Anthropic-Cybersecurity-Skills25 nist_csf:26 - PR.AA-0127 - PR.AA-0228 - PR.AA-0529 - PR.AA-0630 mitre_attack:31 - T152832 - T1550.00133 - T153934 - T1606.00135 - T121236 mitre_f3:37 version: '1.1'38 tactics:39 - initial-access40 - positioning41 techniques:42 - id: T1550.00143 name: 'Use Alternate Authentication Material: Application Access Token'44 tactic: initial-access45 source: https://skillmd.com/skills/socisio/configuring-oauth2-authorization-flow46 - id: F100447 name: Access with Stolen Session Cookie48 tactic: initial-access49 source: f350 - id: F100651 name: Account Takeover52 tactic: initial-access53 source: f354 - id: T153955 name: Steal Web Session Cookie56 tactic: positioning57 source: attack58---5960# Configuring OAuth 2.0 Authorization Flow6162## Overview63Configure 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.646566## When to Use6768- When deploying or configuring configuring oauth2 authorization flow capabilities in your environment69- When establishing security controls aligned to compliance requirements70- When building or improving security architecture for this domain71- When conducting security assessments that require this implementation7273## Prerequisites7475- Familiarity with identity access management concepts and tools76- Access to a test or lab environment for safe execution77- Python 3.8+ with required dependencies installed78- Appropriate authorization for any testing activities7980## Objectives81- Implement Authorization Code flow with PKCE for public and confidential clients82- Configure Client Credentials flow for machine-to-machine communication83- Design least-privilege scope hierarchies84- Implement secure token storage, refresh, and revocation85- Apply OAuth 2.1 best practices and RFC 9700 security recommendations86- Validate token integrity and prevent common OAuth attacks8788## Key Concepts8990### OAuth 2.0 Grant Types911. **Authorization Code + PKCE**: Recommended for all client types (web, mobile, SPA). PKCE is mandatory in OAuth 2.1.922. **Client Credentials**: Machine-to-machine authentication without user context.933. **Device Authorization Grant (RFC 8628)**: For input-constrained devices (smart TVs, CLI tools).944. **Refresh Token**: Long-lived token to obtain new access tokens without re-authentication.9596### PKCE (Proof Key for Code Exchange)97PKCE (RFC 7636) prevents authorization code interception attacks:981. Client generates random `code_verifier` (43-128 characters, unreserved URI chars)992. Client computes `code_challenge = BASE64URL(SHA256(code_verifier))`1003. Authorization request includes `code_challenge` and `code_challenge_method=S256`1014. Token request includes original `code_verifier`1025. Server validates `SHA256(code_verifier)` matches stored `code_challenge`103104### Token Types105- **Access Token**: Short-lived (5-60 min), bearer or DPoP-bound106- **Refresh Token**: Long-lived, single-use with rotation107- **ID Token (OIDC)**: JWT containing user identity claims108109## Workflow110111### Step 1: Authorization Code Flow with PKCE1121. Generate cryptographically random code_verifier (min 43 chars)1132. Compute code_challenge using S256 method1143. Redirect user to authorization endpoint with parameters:115 - response_type=code116 - client_id, redirect_uri, scope, state117 - code_challenge, code_challenge_method=S2561184. User authenticates and consents1195. Authorization server redirects with authorization code1206. Exchange code + code_verifier for tokens at token endpoint1217. Validate state parameter matches original value122123### Step 2: Scope Design124- Define granular scopes: `read:users`, `write:orders`, `admin:settings`125- Follow least-privilege: request minimum scopes needed126- Implement scope validation on resource server127- Document scope hierarchy and consent requirements128129### Step 3: Token Security130- Store tokens securely (httpOnly cookies for web, keychain for mobile)131- Implement token refresh with rotation (one-time-use refresh tokens)132- Set appropriate expiration: access tokens 5-15 min, refresh tokens 8-24 hrs133- Enable DPoP (Demonstration of Proof-of-Possession) for sender-constrained tokens134- Implement token revocation endpoint135136### Step 4: Client Credentials Flow1371. Register service client with client_id and client_secret1382. Request token: POST /oauth/token with grant_type=client_credentials1393. Include scope for required permissions1404. Store client_secret securely (vault, env vars, not code)1415. Implement certificate-based client authentication for higher assurance142143### Step 5: Security Hardening144- Enforce PKCE for all authorization code flows145- Use exact redirect URI matching (no wildcards)146- Implement CSRF protection with state parameter147- Enable refresh token rotation and revocation on reuse detection148- Apply RFC 9700 security best practices149- Block implicit grant and ROPC (removed in OAuth 2.1)150151## Security Controls152| Control | NIST 800-53 | Description |153|---------|-------------|-------------|154| Access Control | AC-3 | Token-based access enforcement |155| Authentication | IA-5 | Client credential management |156| Session Management | SC-23 | Token lifecycle management |157| Audit | AU-3 | Log all token issuance and revocation |158| Cryptographic Protection | SC-13 | PKCE and token signing |159160## Common Pitfalls161- Using implicit grant (removed in OAuth 2.1) instead of authorization code + PKCE162- Storing tokens in localStorage (XSS vulnerable) instead of httpOnly cookies163- Not validating state parameter enabling CSRF attacks164- Using wildcard redirect URIs allowing open redirect exploitation165- Not implementing refresh token rotation allowing token theft persistence166167## Verification168- [ ] Authorization Code + PKCE flow completes successfully169- [ ] PKCE code_challenge validated at token endpoint170- [ ] State parameter prevents CSRF171- [ ] Access tokens expire within configured lifetime172- [ ] Refresh token rotation issues new refresh token each use173- [ ] Token revocation invalidates both access and refresh tokens174- [ ] Client Credentials flow works for service-to-service calls175- [ ] Scopes correctly enforced at resource server