TL;DR
- 目的:Configures secure OAuth 2.0 authorization flows, including Authorization Code with PKCE, Client Credentials, and Device Authorization Grant…
- 适用:认证/授权/ATO 测试
- 输入:OAuth2 client_id + redirect_uri
- 输出:配置审计报告 + 修复建议
- 红线:仅测自己有权限的 App;禁止上传样本到公网扫描器
- 关联:上游:003-src-session-start → 下游:073-hunt-rag-vector, 072-hunt-llm-ai
Configuring OAuth 2.0 Authorization Flow
Quick Start
# 验证 OAuth 端点
curl -v "http://target/oauth/authorize?response_type=code&client_id=xxx&redirect_uri=xxx&scope=read"
# 检查 token 端点
curl -X POST -d "grant_type=authorization_code&code=xxx&client_id=xxx&client_secret=xxx" http://target/oauth/token
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
Validation Criteria
1---2name: configuring-oauth2-authorization-flow3description: Configures secure OAuth 2.0 authorization flows, including Authorization Code with PKCE, Client Credentials, and Device Authorization Grant…4license: Apache-2.05---67## TL;DR89- **目的**:Configures secure OAuth 2.0 authorization flows, including Authorization Code with PKCE, Client Credentials, and Device Authorization Grant…10- **适用**:认证/授权/ATO 测试11- **输入**:OAuth2 client_id + redirect_uri12- **输出**:配置审计报告 + 修复建议13- **红线**:仅测自己有权限的 App;禁止上传样本到公网扫描器14- **关联**:上游:003-src-session-start → 下游:073-hunt-rag-vector, 072-hunt-llm-ai1516# Configuring OAuth 2.0 Authorization Flow1718## Quick Start1920```bash21# 验证 OAuth 端点22curl -v "http://target/oauth/authorize?response_type=code&client_id=xxx&redirect_uri=xxx&scope=read"23# 检查 token 端点24curl -X POST -d "grant_type=authorization_code&code=xxx&client_id=xxx&client_secret=xxx" http://target/oauth/token25```2627## Overview28Configure 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.293031## When to Use3233- When deploying or configuring configuring oauth2 authorization flow capabilities in your environment34- When establishing security controls aligned to compliance requirements35- When building or improving security architecture for this domain36- When conducting security assessments that require this implementation3738## Prerequisites3940- Familiarity with identity access management concepts and tools41- Access to a test or lab environment for safe execution42- Python 3.8+ with required dependencies installed43- Appropriate authorization for any testing activities4445## Objectives46- Implement Authorization Code flow with PKCE for public and confidential clients47- Configure Client Credentials flow for machine-to-machine communication48- Design least-privilege scope hierarchies49- Implement secure token storage, refresh, and revocation50- Apply OAuth 2.1 best practices and RFC 9700 security recommendations51- Validate token integrity and prevent common OAuth attacks5253## Key Concepts5455### OAuth 2.0 Grant Types561. **Authorization Code + PKCE**: Recommended for all client types (web, mobile, SPA). PKCE is mandatory in OAuth 2.1.572. **Client Credentials**: Machine-to-machine authentication without user context.583. **Device Authorization Grant (RFC 8628)**: For input-constrained devices (smart TVs, CLI tools).594. **Refresh Token**: Long-lived token to obtain new access tokens without re-authentication.6061### PKCE (Proof Key for Code Exchange)62PKCE (RFC 7636) prevents authorization code interception attacks:631. Client generates random `code_verifier` (43-128 characters, unreserved URI chars)642. Client computes `code_challenge = BASE64URL(SHA256(code_verifier))`653. Authorization request includes `code_challenge` and `code_challenge_method=S256`664. Token request includes original `code_verifier`675. Server validates `SHA256(code_verifier)` matches stored `code_challenge`6869### Token Types70- **Access Token**: Short-lived (5-60 min), bearer or DPoP-bound71- **Refresh Token**: Long-lived, single-use with rotation72- **ID Token (OIDC)**: JWT containing user identity claims7374## Workflow7576### Step 1: Authorization Code Flow with PKCE771. Generate cryptographically random code_verifier (min 43 chars)782. Compute code_challenge using S256 method793. Redirect user to authorization endpoint with parameters:80 - response_type=code81 - client_id, redirect_uri, scope, state82 - code_challenge, code_challenge_method=S256834. User authenticates and consents845. Authorization server redirects with authorization code856. Exchange code + code_verifier for tokens at token endpoint867. Validate state parameter matches original value8788### Step 2: Scope Design89- Define granular scopes: `read:users`, `write:orders`, `admin:settings`90- Follow least-privilege: request minimum scopes needed91- Implement scope validation on resource server92- Document scope hierarchy and consent requirements9394### Step 3: Token Security95- Store tokens securely (httpOnly cookies for web, keychain for mobile)96- Implement token refresh with rotation (one-time-use refresh tokens)97- Set appropriate expiration: access tokens 5-15 min, refresh tokens 8-24 hrs98- Enable DPoP (Demonstration of Proof-of-Possession) for sender-constrained tokens99- Implement token revocation endpoint100101### Step 4: Client Credentials Flow1021. Register service client with client_id and client_secret1032. Request token: POST /oauth/token with grant_type=client_credentials1043. Include scope for required permissions1054. Store client_secret securely (vault, env vars, not code)1065. Implement certificate-based client authentication for higher assurance107108### Step 5: Security Hardening109- Enforce PKCE for all authorization code flows110- Use exact redirect URI matching (no wildcards)111- Implement CSRF protection with state parameter112- Enable refresh token rotation and revocation on reuse detection113- Apply RFC 9700 security best practices114- Block implicit grant and ROPC (removed in OAuth 2.1)115116## Security Controls117| Control | NIST 800-53 | Description |118|---------|-------------|-------------|119| Access Control | AC-3 | Token-based access enforcement |120| Authentication | IA-5 | Client credential management |121| Session Management | SC-23 | Token lifecycle management |122| Audit | AU-3 | Log all token issuance and revocation |123| Cryptographic Protection | SC-13 | PKCE and token signing |124125## Common Pitfalls126- Using implicit grant (removed in OAuth 2.1) instead of authorization code + PKCE127- Storing tokens in localStorage (XSS vulnerable) instead of httpOnly cookies128- Not validating state parameter enabling CSRF attacks129- Using wildcard redirect URIs allowing open redirect exploitation130- Not implementing refresh token rotation allowing token theft persistence131132## Validation Criteria133- [ ] Authorization Code + PKCE flow completes successfully134- [ ] PKCE code_challenge validated at token endpoint135- [ ] State parameter prevents CSRF136- [ ] Access tokens expire within configured lifetime137- [ ] Refresh token rotation issues new refresh token each use138- [ ] Token revocation invalidates both access and refresh tokens139- [ ] Client Credentials flow works for service-to-service calls140- [ ] Scopes correctly enforced at resource server