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
Common Misconfigurations & Verification
- Implicit/hybrid grant still enabled:
response_type=token or id_token token returns tokens in the URL fragment where they leak via history/referrer. Verify the authorization server rejects implicit and hybrid responses and that only response_type=code is allowed (OAuth 2.1 removes implicit).
- redirect_uri wildcards / loose matching:
https://app.example.com/* or scheme-relative entries enable token theft via open redirect. Confirm exact-string registration only — test redirect_uri=https://app.example.com.evil.com and an open-redirect on a whitelisted host; both must be rejected.
- PKCE missing or downgradeable: public clients without PKCE, or servers that accept
code_challenge_method=plain, are open to code interception. Verify S256 is required and that a token request omitting code_verifier fails.
- State/nonce not validated: missing
state enables CSRF; missing nonce enables ID-token replay. Confirm both are bound to the session and single-use.
- Refresh-token handling: non-rotating or non-revoked-on-reuse refresh tokens give attackers persistence; confirm rotation and reuse-detection revoke the whole chain.
- Verification: decode the access token at the resource server and assert
aud, iss, exp, and scope are checked (not just signature); replay a one-time refresh token and confirm the family is revoked; attempt a plain PKCE downgrade and confirm rejection.
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: 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, token4license: Apache-2.05---6# Configuring OAuth 2.0 Authorization Flow78## Overview9Configure 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.101112## When to Use1314- When deploying or configuring configuring oauth2 authorization flow capabilities in your environment15- When establishing security controls aligned to compliance requirements16- When building or improving security architecture for this domain17- When conducting security assessments that require this implementation1819## Common Misconfigurations & Verification2021- **Implicit/hybrid grant still enabled:** `response_type=token` or `id_token token` returns tokens in the URL fragment where they leak via history/referrer. Verify the authorization server rejects implicit and hybrid responses and that only `response_type=code` is allowed (OAuth 2.1 removes implicit).22- **redirect_uri wildcards / loose matching:** `https://app.example.com/*` or scheme-relative entries enable token theft via open redirect. Confirm exact-string registration only — test `redirect_uri=https://app.example.com.evil.com` and an open-redirect on a whitelisted host; both must be rejected.23- **PKCE missing or downgradeable:** public clients without PKCE, or servers that accept `code_challenge_method=plain`, are open to code interception. Verify `S256` is required and that a token request omitting `code_verifier` fails.24- **State/nonce not validated:** missing `state` enables CSRF; missing `nonce` enables ID-token replay. Confirm both are bound to the session and single-use.25- **Refresh-token handling:** non-rotating or non-revoked-on-reuse refresh tokens give attackers persistence; confirm rotation and reuse-detection revoke the whole chain.26- **Verification:** decode the access token at the resource server and assert `aud`, `iss`, `exp`, and scope are checked (not just signature); replay a one-time refresh token and confirm the family is revoked; attempt a `plain` PKCE downgrade and confirm rejection.2728## Prerequisites2930- Familiarity with identity access management concepts and tools31- Access to a test or lab environment for safe execution32- Python 3.8+ with required dependencies installed33- Appropriate authorization for any testing activities3435## Objectives36- Implement Authorization Code flow with PKCE for public and confidential clients37- Configure Client Credentials flow for machine-to-machine communication38- Design least-privilege scope hierarchies39- Implement secure token storage, refresh, and revocation40- Apply OAuth 2.1 best practices and RFC 9700 security recommendations41- Validate token integrity and prevent common OAuth attacks4243## Key Concepts4445### OAuth 2.0 Grant Types461. **Authorization Code + PKCE**: Recommended for all client types (web, mobile, SPA). PKCE is mandatory in OAuth 2.1.472. **Client Credentials**: Machine-to-machine authentication without user context.483. **Device Authorization Grant (RFC 8628)**: For input-constrained devices (smart TVs, CLI tools).494. **Refresh Token**: Long-lived token to obtain new access tokens without re-authentication.5051### PKCE (Proof Key for Code Exchange)52PKCE (RFC 7636) prevents authorization code interception attacks:531. Client generates random `code_verifier` (43-128 characters, unreserved URI chars)542. Client computes `code_challenge = BASE64URL(SHA256(code_verifier))`553. Authorization request includes `code_challenge` and `code_challenge_method=S256`564. Token request includes original `code_verifier`575. Server validates `SHA256(code_verifier)` matches stored `code_challenge`5859### Token Types60- **Access Token**: Short-lived (5-60 min), bearer or DPoP-bound61- **Refresh Token**: Long-lived, single-use with rotation62- **ID Token (OIDC)**: JWT containing user identity claims6364## Workflow6566### Step 1: Authorization Code Flow with PKCE671. Generate cryptographically random code_verifier (min 43 chars)682. Compute code_challenge using S256 method693. Redirect user to authorization endpoint with parameters:70 - response_type=code71 - client_id, redirect_uri, scope, state72 - code_challenge, code_challenge_method=S256734. User authenticates and consents745. Authorization server redirects with authorization code756. Exchange code + code_verifier for tokens at token endpoint767. Validate state parameter matches original value7778### Step 2: Scope Design79- Define granular scopes: `read:users`, `write:orders`, `admin:settings`80- Follow least-privilege: request minimum scopes needed81- Implement scope validation on resource server82- Document scope hierarchy and consent requirements8384### Step 3: Token Security85- Store tokens securely (httpOnly cookies for web, keychain for mobile)86- Implement token refresh with rotation (one-time-use refresh tokens)87- Set appropriate expiration: access tokens 5-15 min, refresh tokens 8-24 hrs88- Enable DPoP (Demonstration of Proof-of-Possession) for sender-constrained tokens89- Implement token revocation endpoint9091### Step 4: Client Credentials Flow921. Register service client with client_id and client_secret932. Request token: POST /oauth/token with grant_type=client_credentials943. Include scope for required permissions954. Store client_secret securely (vault, env vars, not code)965. Implement certificate-based client authentication for higher assurance9798### Step 5: Security Hardening99- Enforce PKCE for all authorization code flows100- Use exact redirect URI matching (no wildcards)101- Implement CSRF protection with state parameter102- Enable refresh token rotation and revocation on reuse detection103- Apply RFC 9700 security best practices104- Block implicit grant and ROPC (removed in OAuth 2.1)105106## Security Controls107| Control | NIST 800-53 | Description |108|---------|-------------|-------------|109| Access Control | AC-3 | Token-based access enforcement |110| Authentication | IA-5 | Client credential management |111| Session Management | SC-23 | Token lifecycle management |112| Audit | AU-3 | Log all token issuance and revocation |113| Cryptographic Protection | SC-13 | PKCE and token signing |114115## Common Pitfalls116- Using implicit grant (removed in OAuth 2.1) instead of authorization code + PKCE117- Storing tokens in localStorage (XSS vulnerable) instead of httpOnly cookies118- Not validating state parameter enabling CSRF attacks119- Using wildcard redirect URIs allowing open redirect exploitation120- Not implementing refresh token rotation allowing token theft persistence121122## Verification123- [ ] Authorization Code + PKCE flow completes successfully124- [ ] PKCE code_challenge validated at token endpoint125- [ ] State parameter prevents CSRF126- [ ] Access tokens expire within configured lifetime127- [ ] Refresh token rotation issues new refresh token each use128- [ ] Token revocation invalidates both access and refresh tokens129- [ ] Client Credentials flow works for service-to-service calls130- [ ] Scopes correctly enforced at resource server