Security for Engineers/Authentication & Authorization

OAuth2, OIDC & JWT Flows

Authorization code + PKCE, client credentials, JWT structure and pitfalls, refresh tokens, and what to draw on a whiteboard.

3/5Overview: 35m

Authentication vs authorization

  • AuthN — who are you? (login, token, cert)
  • AuthZ — what may you do? (roles, scopes, resource checks)

Fail AuthN → 401. Fail AuthZ → 403. Don't conflate.

OAuth2 grant types (know 3)

GrantUseInterview note
Authorization code + PKCEUser login (SPA, mobile)Default for public clients
Client credentialsService-to-serviceMachine identity, no user
Refresh tokenLong-lived sessionsRotate, revoke, bind to client

Avoid: implicit flow (deprecated), resource owner password (legacy only).

OIDC adds identity

OAuth2 = authorization. OpenID Connect adds id_token with user claims (sub, email).

Login button → OIDC provider (Google, Okta, Auth0).

JWT structure

header.payload.signature

Validate on every request:

  1. Signature (issuer's JWKS)
  2. exp not passed
  3. aud matches your API
  4. iss trusted

Never trust payload without signature verify.

JWT pitfalls (senior favorites)

PitfallAttack
alg: noneStrip signature check
Confusion RS256/HS256Use public key as HMAC secret
Long expStolen token valid for days
Sensitive data in payloadJWT is base64, not encrypted

Prefer opaque tokens + introspection for high-security; JWT for stateless scale.

Sessions vs tokens

Session cookieBearer JWT
StorageServer-side session storeClient holds token
RevokeInstantHard until expiry
ScaleSticky session or shared storeStateless verification

HttpOnly, Secure, SameSite cookies for session IDs (Web Security topic).

Where validation happens

  • Gateway — signature, expiry, coarse scopes
  • Service — resource-level AuthZ (always)

Cross-reference: Microservices → API Gateway for edge termination; Communication → REST for 401/403 semantics.

Further Reading

Hands-On Tasks (Optional)

Security design drills — threat modeling, auth flows, and incident playbooks. Assumes Networking (TLS) fundamentals.

  • Sketch authorization code + PKCE

    SPA login to your API. Draw: redirect to IdP, code exchange, access token, API call. Where store tokens? What stops code interception?

    15m