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
Source: mukul975/Anthropic-Cybersecurity-Skills → skills/configuring-oauth2-authorization-flow/SKILL.md
1---
2name: configuring-oauth2-authorization-flow
3description: "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"
4category: security-and-compliance
5source_repo: mukul975/Anthropic-Cybersecurity-Skills
6source_path: "skills/configuring-oauth2-authorization-flow/SKILL.md"
7source_url: https://github.com/mukul975/Anthropic-Cybersecurity-Skills/blob/HEAD/skills/configuring-oauth2-authorization-flow/SKILL.md
8metadata:
9 source: https://skillmd.com/skills/thedixitjain/configuring-oauth2-authorization-flow
10---
11
12# Configuring OAuth 2.0 Authorization Flow
13
14## Overview
15Configure 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.
16
17
18## When to Use
19
20- When deploying or configuring configuring oauth2 authorization flow capabilities in your environment
21- When establishing security controls aligned to compliance requirements
22- When building or improving security architecture for this domain
23- When conducting security assessments that require this implementation
24
25## Prerequisites
26
27- Familiarity with identity access management concepts and tools
28- Access to a test or lab environment for safe execution
29- Python 3.8+ with required dependencies installed
30- Appropriate authorization for any testing activities
31
32## Objectives
33- Implement Authorization Code flow with PKCE for public and confidential clients
34- Configure Client Credentials flow for machine-to-machine communication
35- Design least-privilege scope hierarchies
36- Implement secure token storage, refresh, and revocation
37- Apply OAuth 2.1 best practices and RFC 9700 security recommendations
38- Validate token integrity and prevent common OAuth attacks
39
40## Key Concepts
41
42### OAuth 2.0 Grant Types
431. **Authorization Code + PKCE**: Recommended for all client types (web, mobile, SPA). PKCE is mandatory in OAuth 2.1.
442. **Client Credentials**: Machine-to-machine authentication without user context.
453. **Device Authorization Grant (RFC 8628)**: For input-constrained devices (smart TVs, CLI tools).
464. **Refresh Token**: Long-lived token to obtain new access tokens without re-authentication.
47
48### PKCE (Proof Key for Code Exchange)
49PKCE (RFC 7636) prevents authorization code interception attacks:
501. Client generates random `code_verifier` (43-128 characters, unreserved URI chars)
512. Client computes `code_challenge = BASE64URL(SHA256(code_verifier))`
523. Authorization request includes `code_challenge` and `code_challenge_method=S256`
534. Token request includes original `code_verifier`
545. Server validates `SHA256(code_verifier)` matches stored `code_challenge`
55
56### Token Types
57- **Access Token**: Short-lived (5-60 min), bearer or DPoP-bound
58- **Refresh Token**: Long-lived, single-use with rotation
59- **ID Token (OIDC)**: JWT containing user identity claims
60
61## Workflow
62
63### Step 1: Authorization Code Flow with PKCE
641. Generate cryptographically random code_verifier (min 43 chars)
652. Compute code_challenge using S256 method
663. Redirect user to authorization endpoint with parameters:
67 - response_type=code
68 - client_id, redirect_uri, scope, state
69 - code_challenge, code_challenge_method=S256
704. User authenticates and consents
715. Authorization server redirects with authorization code
726. Exchange code + code_verifier for tokens at token endpoint
737. Validate state parameter matches original value
74
75### Step 2: Scope Design
76- Define granular scopes: `read:users`, `write:orders`, `admin:settings`
77- Follow least-privilege: request minimum scopes needed
78- Implement scope validation on resource server
79- Document scope hierarchy and consent requirements
80
81### Step 3: Token Security
82- Store tokens securely (httpOnly cookies for web, keychain for mobile)
83- Implement token refresh with rotation (one-time-use refresh tokens)
84- Set appropriate expiration: access tokens 5-15 min, refresh tokens 8-24 hrs
85- Enable DPoP (Demonstration of Proof-of-Possession) for sender-constrained tokens
86- Implement token revocation endpoint
87
88### Step 4: Client Credentials Flow
891. Register service client with client_id and client_secret
902. Request token: POST /oauth/token with grant_type=client_credentials
913. Include scope for required permissions
924. Store client_secret securely (vault, env vars, not code)
935. Implement certificate-based client authentication for higher assurance
94
95### Step 5: Security Hardening
96- Enforce PKCE for all authorization code flows
97- Use exact redirect URI matching (no wildcards)
98- Implement CSRF protection with state parameter
99- Enable refresh token rotation and revocation on reuse detection
100- Apply RFC 9700 security best practices
101- Block implicit grant and ROPC (removed in OAuth 2.1)
102
103## Security Controls
104| Control | NIST 800-53 | Description |
105|---------|-------------|-------------|
106| Access Control | AC-3 | Token-based access enforcement |
107| Authentication | IA-5 | Client credential management |
108| Session Management | SC-23 | Token lifecycle management |
109| Audit | AU-3 | Log all token issuance and revocation |
110| Cryptographic Protection | SC-13 | PKCE and token signing |
111
112## Common Pitfalls
113- Using implicit grant (removed in OAuth 2.1) instead of authorization code + PKCE
114- Storing tokens in localStorage (XSS vulnerable) instead of httpOnly cookies
115- Not validating state parameter enabling CSRF attacks
116- Using wildcard redirect URIs allowing open redirect exploitation
117- Not implementing refresh token rotation allowing token theft persistence
118
119## Verification
120- [ ] Authorization Code + PKCE flow completes successfully
121- [ ] PKCE code_challenge validated at token endpoint
122- [ ] State parameter prevents CSRF
123- [ ] Access tokens expire within configured lifetime
124- [ ] Refresh token rotation issues new refresh token each use
125- [ ] Token revocation invalidates both access and refresh tokens
126- [ ] Client Credentials flow works for service-to-service calls
127- [ ] Scopes correctly enforced at resource server
128
129---
130
131**Source:** [`mukul975/Anthropic-Cybersecurity-Skills`](https://github.com/mukul975/Anthropic-Cybersecurity-Skills) → `skills/configuring-oauth2-authorization-flow/SKILL.md`