Last updated: April 29, 2026
Authentication and authorization are where most API security work lives. Get them right and the rest of your defenses operate from a solid foundation. Get them wrong and every endpoint is potentially compromised. This module covers JWT pitfalls, OAuth 2.0 flows for APIs, session management patterns, and the things to test in each.
JWT — the pitfalls
JSON Web Tokens are everywhere. They are also full of footguns:
- “none” algorithm bypass: some libraries accept
{"alg":"none"}and skip signature verification. Older bug; still appears in custom verifiers - Algorithm confusion (RS256 vs HS256): server expects RS256 (asymmetric); attacker submits HS256 token signed with the public key (which they obtained legitimately). HS256 verifier uses the public key as HMAC secret → signature passes
- Weak HMAC secret: JWT signed with
secret123; offline crack via hashcat in seconds - kid injection: kid header pointing to attacker-controlled key
- jku/x5u abuse: JWT claims a key URL; some verifiers fetch and trust it
- Expiration not enforced: token from last year still works
- Token reuse after logout: JWT is stateless; without revocation infrastructure, “logout” just means client deletes token
- Secrets in JWT body: JWT base64-encodes — does not encrypt. Anything in the body is readable
JWT testing checklist
# Test 1: alg=none
echo -n '{"alg":"none","typ":"JWT"}' | base64 -w0
echo -n '{"sub":"admin"}' | base64 -w0
# Final token: HEADER.PAYLOAD. (note trailing dot, empty signature)
# Test 2: HS256 confusion using known RS256 public key
# (Use jwt_tool by ticarpi)
jwt_tool TOKEN -X k -pk public.pem
# Test 3: Crack weak HS256 secret offline
hashcat -a 0 -m 16500 token.txt rockyou.txt
# Test 4: Replay expired token; verify rejection
# Test 5: Decode body — any sensitive fields you can see?
OAuth 2.0 — the flows that matter for APIs
- Authorization Code with PKCE — standard for first-party + native apps. PKCE prevents code interception
- Client Credentials — service-to-service auth; no user involved
- Resource Owner Password Credentials (ROPC) — deprecated; user gives username/password directly to client. Security anti-pattern
- Implicit flow — deprecated; access tokens in URL fragment. Replaced by Auth Code + PKCE
- Device code — for input-constrained devices (TVs, IoT)
Anything still using ROPC or Implicit in 2026 is a finding by itself.
OAuth-specific bugs
- Open redirect in redirect_uri: attacker registers
https://evil.comand tricks user; auth server should validate redirect_uri against pre-registered allow-list - State parameter missing: CSRF on the OAuth callback; state should be unguessable per request
- Authorization code reuse: codes should be single-use; reuse should fail and revoke any tokens issued from that code
- PKCE not enforced: on auth-code flow without PKCE, intercepted code can be exchanged for token
- Refresh token without rotation: long-lived refresh tokens that don’t rotate are persistent compromise risk
- Scope inflation: token grants more scopes than requested
Session management for stateful APIs
Not every API uses JWT. Cookie/session APIs have their own checklist:
- Session ID generated by cryptographic RNG (not predictable)
- Set-Cookie has
HttpOnly,Secure,SameSite=StrictorLax - Session invalidated on logout server-side
- Session invalidated on password change for ALL of user’s sessions
- Concurrent session limits where applicable
- Idle timeout enforced server-side, not just client
- Re-authentication required for sensitive actions (changing email, payment methods)
API key auth — when and how
API keys (static, long-lived strings) are appropriate for:
- Internal service-to-service when service mesh / OAuth client credentials would be heavier than warranted
- Webhook signing
- Public read-only APIs with rate limiting
What to verify:
- Keys are scoped (read vs write, narrow resource grants)
- Keys can be rotated without service interruption (multiple active keys per consumer)
- Key usage is logged and auditable
- Compromise revocation is fast (under 5 minutes)
- Keys never appear in URL paths/query strings (use Authorization header)
- Keys never logged (cookie-redaction, header-redaction in log pipelines)
mTLS — the highest-trust API auth
Mutual TLS authenticates both client and server with certificates. Used in:
- Service mesh (Istio, Linkerd) for in-cluster auth
- B2B APIs with stable client base (banks, payment networks)
- IoT device-to-cloud trust
What to verify when testing mTLS APIs:
- Self-signed certs rejected; only your CA-issued certs accepted
- Certificate revocation list (CRL) or OCSP checked
- Client cert subject mapped to identity correctly (no impersonation via cert SAN manipulation)
- Cert lifetime is short (90 days or less)
Authorization patterns — beyond authn
Authentication says “who you are.” Authorization says “what you can do.” Patterns:
- Role-Based Access Control (RBAC): users → roles → permissions. Simple, well-understood, but coarse-grained
- Attribute-Based Access Control (ABAC): policy combines user attrs + resource attrs + context. Flexible, complex
- Relationship-Based Access Control (ReBAC): Google Zanzibar-style; “user X can read document Y because X is a member of group Z which has read permission on Y.” Industry trend in 2026
- Policy-as-code: OPA (Open Policy Agent) — externalize authorization decisions to a policy engine; uniform across services
Testing authorization at scale
Manual testing every endpoint × every role is impractical. Tools:
- Autorize (Burp extension): replays requests as different users; flags differences
- Authz scanners (commercial): same idea, integrated with API spec
- Custom matrix: spreadsheet of endpoints × roles × expected access; script that tests the matrix and reports deltas
The audit log
Every authentication and authorization decision should be loggable:
- Who (user, service)
- What (endpoint, action)
- When (timestamp)
- Outcome (allow/deny)
- Why-deny (which policy)
Without this, post-incident investigation is guesswork. Most APIs log half of this. The “deny + why” is often missing.
What the next module covers
Module 3 dives into GraphQL — distinct query language, distinct security model. Authorization, query depth limiting, introspection, batching attacks, all GraphQL-specific.
Module Quiz · 15 questions
Pass with 80%+ to mark this module complete. Unlimited retries. Each question shows an explanation.
Custom team training + practitioner advisory
Beyond the free academy — we run private workshops, vCISO advisory, and red-team exercises tailored to your stack. For Indian SMBs scaling past their first hire.