Authentication, authorization, and API security implementation. Use when building user systems, protecting APIs, or implementing access control...
Implement modern authentication, authorization, and API security across Python, Rust, Go, and TypeScript.
Use this skill when:
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
ā OAuth 2.1 MANDATORY REQUIREMENTS ā
ā (RFC 9798 - 2025) ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā¤
ā ā
ā ā
REQUIRED (Breaking Changes from OAuth 2.0) ā
ā āā PKCE (Proof Key for Code Exchange) MANDATORY ā
ā ā āā S256 method (SHA-256), minimum entropy 43 chars ā
ā āā Exact redirect URI matching ā
ā ā āā No wildcard matching, no substring matching ā
ā āā Authorization code flow ONLY for public clients ā
ā ā āā All other flows require confidential client ā
ā āā TLS 1.2+ required for all endpoints ā
ā ā
ā ā REMOVED (No Longer Supported) ā
ā āā Implicit grant (security vulnerabilities) ā
ā āā Resource Owner Password Credentials grant ā
ā ā āā Use OAuth 2.0 Device Flow (RFC 8628) instead ā
ā āā Bearer token in query parameters ā
ā āā Must use Authorization header or POST body ā
ā ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
Critical: PKCE is now mandatory for ALL OAuth flows, not just public clients.
EdDSA with Ed25519 (Recommended)
ES256 (ECDSA with P-256)
RS256 (RSA)
NEVER allow alg: none or algorithm switching attacks.
Refresh token rotation: Each refresh generates new access AND refresh tokens, invalidating the old refresh token.
{
"iss": "https://auth.example.com",
"sub": "user-id-123",
"aud": "api.example.com",
"exp": 1234567890,
"iat": 1234567890,
"jti": "unique-token-id",
"scope": "read:profile write:data"
}
Algorithm: Argon2id
Memory cost (m): 64 MB (65536 KiB)
Time cost (t): 3 iterations
Parallelism (p): 4 threads
Salt length: 16 bytes (128 bits)
Target hash time: 150-250ms
For concrete implementations, see references/password-hashing.md.
Key Points:
Passkeys provide phishing-resistant, passwordless authentication using FIDO2/WebAuthn.
For implementation guide, see references/passkeys-webauthn.md.
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
ā Authorization Model Selection ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā¤
ā ā
ā Simple Roles (<20 roles) ā
ā āā RBAC with Casbin (embedded, any language) ā
ā Example: Admin, User, Guest ā
ā ā
ā Complex Attribute Rules ā
ā āā ABAC with OPA or Cerbos ā
ā Example: "Allow if user.clearance >= doc.level ā
ā AND user.dept == doc.dept" ā
ā ā
ā Relationship-Based (Multi-Tenant, Collaborative) ā
ā āā ReBAC with SpiceDB (Zanzibar model) ā
ā Example: "Can edit if member of doc's workspace ā
ā AND workspace.plan includes feature" ā
ā Use cases: Notion-like, GitHub-like permissions ā
ā ā
ā Kubernetes / Infrastructure Policies ā
ā āā OPA (Gatekeeper for admission control) ā
ā Example: Enforce pod security policies ā
ā ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
For detailed comparison, see references/authorization-patterns.md.
| Use Case | Library | Context7 ID | Trust | Notes |
|---|---|---|---|---|
| Auth Framework | Auth.js v5 | /websites/authjs_dev |
87.4 | Multi-framework (Next, Svelte, Solid) |
| JWT | jose 5.x | - | - | EdDSA, ES256, RS256 support |
| Passkeys | @simplewebauthn/server 11.x | - | - | FIDO2 server |
| Validation | Zod 3.x | /colinhacks/zod |
90.4 | Schema validation |
| Policy Engine | Casbin.js 1.x | - | - | RBAC/ABAC embedded |
| Use Case | Library | Notes |
|---|---|---|
| Auth Framework | Authlib 1.3+ | OAuth/OIDC client + server |
| JWT | joserfc 1.x | Modern, maintained |
| Passkeys | py_webauthn 2.x | WebAuthn server |
| Password Hashing | argon2-cffi 24.x | OWASP parameters |
| Validation | Pydantic 2.x | FastAPI integration |
| Policy Engine | PyCasbin 1.x | RBAC/ABAC embedded |
| Use Case | Library | Notes |
|---|---|---|
| JWT | jsonwebtoken 10.x | EdDSA, ES256, RS256 |
| OAuth Client | oauth2 5.x | OAuth 2.1 flows |
| Passkeys | webauthn-rs 0.5.x | WebAuthn + attestation |
| Password Hashing | argon2 0.5.x | Native Argon2id |
| Policy Engine | Casbin-RS 2.x | RBAC/ABAC embedded |
| Use Case | Library | Notes |
|---|---|---|
| JWT | golang-jwt v5 | Community-maintained |
| OAuth Client | go-oidc v3 | OIDC client only |
| Passkeys | go-webauthn 0.11.x | Duo-maintained |
| Password Hashing | golang.org/x/crypto/argon2 | Standard library |
| Policy Engine | Casbin v2 | Original implementation |
| Service | Best For | Key Features |
|---|---|---|
| Clerk | Rapid development, startups | Prebuilt UI, Next.js SDK |
| Auth0 | Enterprise, established | 25+ social providers, SSO |
| WorkOS AuthKit | B2B SaaS, enterprise SSO | SAML/SCIM, admin portal |
| Supabase Auth | Postgres users | Built on Postgres, RLS |
For detailed comparison, see references/managed-auth-comparison.md.
| Solution | Language | Use Case |
|---|---|---|
| Keycloak | Java | Enterprise, on-prem |
| Ory | Go | Cloud-native, microservices |
| Authentik | Python | Modern, developer-friendly |
For setup guides, see references/self-hosted-auth.md.
// Tiered rate limiting (per IP + per user)
const rateLimits = {
anonymous: '10 requests/minute',
authenticated: '100 requests/minute',
premium: '1000 requests/minute',
}
Use sliding window algorithm (not fixed window) with Redis.
// Restrictive CORS (production)
const corsOptions = {
origin: ['https://app.example.com'],
credentials: true,
maxAge: 86400, // 24 hours
allowedHeaders: ['Content-Type', 'Authorization'],
methods: ['GET', 'POST', 'PUT', 'DELETE', 'PATCH'],
}
// NEVER use origin: '*' with credentials: true
const securityHeaders = {
'Strict-Transport-Security': 'max-age=63072000; includeSubDomains; preload',
'X-Frame-Options': 'DENY',
'X-Content-Type-Options': 'nosniff',
'Referrer-Policy': 'strict-origin-when-cross-origin',
'Permissions-Policy': 'geolocation=(), microphone=(), camera=()',
'Content-Security-Policy': "default-src 'self'; script-src 'self'",
}
For complete API security guide, see references/api-security.md.
// middleware.ts
import { withAuth } from 'next-auth/middleware'
export default withAuth({
callbacks: {
authorized: ({ token, req }) => {
if (req.nextUrl.pathname.startsWith('/dashboard')) {
return !!token
}
if (req.nextUrl.pathname.startsWith('/admin')) {
return token?.role === 'admin'
}
return true
},
},
})
export const config = {
matcher: ['/dashboard/:path*', '/admin/:path*'],
}
import { useSession } from 'next-auth/react'
export function AdminPanel() {
const { data: session } = useSession()
if (session?.user?.role !== 'admin') {
return null
}
return <div>Admin Controls</div>
}
See references/oauth21-guide.md for complete implementation.
scripts/generate_jwt_keys.pySee references/jwt-best-practices.md for detailed patterns.
See examples/passkeys-demo/ for runnable implementation.
See references/authorization-patterns.md for detailed comparison.
python scripts/generate_jwt_keys.py --algorithm EdDSA
Generates EdDSA or ES256 key pairs for JWT signing.
python scripts/validate_oauth_config.py --config oauth.json
Validates OAuth 2.1 compliance (PKCE enabled, exact redirect URIs, etc.).
Complete implementation with OAuth providers, credentials, and session management.
Location: examples/authjs-nextjs/
Self-hosted Keycloak with FastAPI integration via OIDC.
Location: examples/keycloak-fastapi/
Runnable passkeys implementation with @simplewebauthn.
Location: examples/passkeys-demo/
references/oauth21-guide.md - OAuth 2.1 implementation guidereferences/jwt-best-practices.md - JWT generation, validation, storagereferences/passkeys-webauthn.md - Passkeys/WebAuthn implementationreferences/authorization-patterns.md - RBAC, ABAC, ReBAC comparisonreferences/password-hashing.md - Argon2id parameters, migration