Authentication Hardening Plan: OAuth 2.1/OIDC with PKCE, Refresh Tokens, Logout, and Scopes
This plan outlines a security-focused implementation for hardening OAuth 2.1 (draft) and OpenID Connect (OIDC 1.0) flows in a modern web/mobile/SPA application ecosystem. It prioritizes OAuth 2.1's security enhancements (e.g., mandatory PKCE, PAR, exact param matching) while layering OIDC for identity. The goal is to mitigate common attacks like code interception, token theft, CSRF, and replay.
Assumptions:
- Authorization Server (AS): Compliant with OAuth 2.1/OIDC (e.g., Keycloak, Auth0, or custom).
- Resource Server (RS): Validates JWTs/access tokens with introspection/JWT validation.
- Clients: SPA (public), mobile (public), confidential (web servers).
- Deployment: HTTPS-only, HSTS, secure cookies.
1. Core Protocol Adoption: OAuth 2.1 + OIDC
- Upgrade to OAuth 2.1:
- Use only OAuth 2.1-compliant endpoints (
/authorize, /token, /userinfo, /jwks).
- Enforce exact string matching for parameters (no wildcards).
- Mandate Pushed Authorization Requests (PAR): Clients POST auth request to AS
/par endpoint first, receive request_uri for /authorize. Mitigates param tampering/CSRF.
- Disable legacy grants (implicit, resource owner password); use only
authorization_code.
- Layer OIDC:
- Use
openid scope for ID tokens (JWTs signed with RS256/PS256).
- Fetch
userinfo endpoint for claims post-auth.
- Dynamic client registration with
software_statement for trust.
- Token Formats:
- Access Tokens (AT): JWTs (preferred) or opaque with introspection.
- ID Tokens: JWTs with
aud, azp, nonce validation.
- Enable sender-constrained tokens (e.g., Mutual TLS or DPoP) for ATs.
Implementation Checklist:
| Step | Action | Rationale |
|---|
| 1.1 | Configure AS to reject OAuth 2.0-only clients. | Prevents downgrade attacks. |
| 1.2 | Mandate response_type=code + PAR. | No implicit flow. |
| 1.3 | Validate nonce (SPAs) and state (all). | CSRF protection. |
2. PKCE (Proof Key for Code Exchange)
- Mandatory for All Public Clients (SPAs, mobile); recommended for confidential.
- Use S256 code challenge method only (SHA-256 hash of 43-128 char random
code_verifier).
- Generate
code_verifier per auth attempt (high entropy, URL-safe base64url).
Flow:
- Client:
code_challenge = BASE64URL(SHA256(code_verifier)).
- Include in PAR or
/authorize: code_challenge + code_challenge_method=S256.
- AS issues code; client exchanges with
code_verifier.
Hardening:
- Rotate
code_verifier per request.
- AS: Bind code to
code_challenge; reject mismatches.
- Reject
plain method.
Client Pseudocode (JS/Node):
const crypto = require('crypto');
const codeVerifier = base64url(crypto.randomBytes(32)); // 43 chars
const codeChallenge = base64url(crypto.createHash('sha256').update(codeVerifier).digest());
3. Refresh Token Design
- Enable Refresh Tokens (RTs) only for long-lived sessions; short AT lifetimes (e.g., 15-60 min).
- Rotation + Single-Use: Issue new RT on each refresh; invalidate old RT immediately.
- Sender-Constrained RTs: Use DPoP (Demonstrable Proof-of-Possession) or mTLS to bind RT to client keypair.
- Lifetimes & Revocation:
| Token Type | Max Lifetime | Revocation |
|---|
| AT | 15-60 min | N/A |
| RT | 24 hours | On logout, idle >30d |
- Refresh Flow:
- POST
/token with grant_type=refresh_token, rotated RT, DPoP proof.
- AS validates binding, issues new AT + new RT.
- Storage:
- Confidential clients: Secure backend storage.
- Public clients: HttpOnly/Secure cookies or IndexedDB with encryption.
Attack Mitigations:
- Replay: DPoP
htu (hash-to-use) + htm (method).
- Theft: Rotation + binding.
4. Logout
- Client-Initiated (Front-Channel): Clear local session + redirect to AS
/end_session (OIDC RP-Initiated Logout).
- Params:
id_token_hint, post_logout_redirect_uri, state.
- Server-Side (Back-Channel): AS broadcasts revocation via:
- Session Management: iframe polling
check_session_iframe (OIDC Front-Channel Logout).
- Back-Channel Logout: AS POSTs to client
/logout endpoint with logout_token (JWT).
- Token Revocation:
- On logout: Revoke RT via
/revoke endpoint.
- Global: Push revocation events (e.g., WebSockets) to RS for AT invalidation.
- Full Cleanup:
- Client: Delete tokens, cookies, localStorage.
- Hit AS
/end_session.
- Notify RS to blacklist ATs.
Endpoint Example (AS /end_session):
GET /end_session?
id_token_hint=eyJ...&
post_logout_redirect_uri=https://client.com/cb&
state=xyz
5. Scopes & Permissions
- Principle of Least Privilege: Request minimal scopes (e.g.,
openid profile email + app-specific like read:profile).
- Audience-Bound: ATs scoped to specific RS (
aud claim).
- Scope-to-Token Mapping:
| Scope | Claims/Access | RS Endpoint |
|---|
openid | ID Token | /userinfo |
profile | name, picture | User API |
api:read | Read-only API | /api/v1/* |
- Dynamic Scopes: Use UMA 2.0 or Fine-Grained AuthZ (e.g., scopes with resource indicators
ri=urn:resource).
- Validation:
- RS: Introspect AT + check
scope claim.
- Reject over-privileged tokens (e.g., no
write:* unless requested).
- AS Policy: Consent screen for scopes; revocable via user dashboard.
Best Practice:
- Prefix scopes:
app:action:resource (e.g., finance:read:transactions).
- Rotate scopes on re-auth.
Monitoring, Auditing & Additional Hardening
- Logs: All auth events (audit log with token IDs, IPs, user agents).
- Threat Detection: Rate-limit
/token, anomaly detection (e.g., unusual geos).
- Extras:
- JARM (JWT Secured Authorization Request): Encrypt auth requests.
- Token Binding (RFC 8471/8473).
- RAR (Request Object): Sign auth requests.
- Testing: OAuth Security Audit (e.g., OWASP OAuth Cheat Sheet), fuzzing tools like oauth2-proxy.
Rollout Phases
- Phase 1 (1-2 weeks): PKCE + OAuth 2.1 baseline.
- Phase 2 (2-4 weeks): Refresh rotation + scopes.
- Phase 3 (1-2 weeks): Logout + monitoring.
- Ongoing: Quarterly audits, threat modeling.
This plan achieves OAuth 2.1 Security Profile compliance, reducing attack surface by 80%+ vs. legacy OAuth 2.0. For custom impl, reference OAuth 2.1 Draft and OIDC Core. Contact for diagrams/code samples.