Designing Authentication Architecture

1. Designing JWT-Based Authentication

AspectDetail
Structureheader.payload.signature
AlgorithmRS256/ES256 (asymmetric) preferred over HS256
LifetimeAccess: 5–15 min; Refresh: days
StatelessNo server session lookup
PitfallsHard to revoke; keep small; verify exp/aud/iss

Example: JWT verification (Java)

JWT.require(Algorithm.RSA256(publicKey, null)) .withIssuer("auth.example.com") .withAudience("api") .build() .verify(token);

2. Designing OAuth2 / OpenID Connect Architecture

FlowUse Case
Authorization Code + PKCESPA, mobile, web (recommended)
Client CredentialsService-to-service
Device CodeTVs, CLIs
Implicit / PasswordDeprecated
OIDCAdds id_token (identity) on top of OAuth2

3. Designing Session-Based Authentication

ElementDetail
Session IDRandom 128-bit; HttpOnly Secure SameSite=Lax cookie
StoreRedis / DB
Sliding expiryRenew on activity
RevocationDelete server-side record
CSRF protectionRequired for cookie-based

4. Designing API Key Authentication

PracticeDetail
StorageHash (e.g., SHA-256) — never plain
Prefix"sk_live_" for scanability
ScopingLimit permissions per key
RotationEasy regeneration; overlap window
Rate limitingPer-key

5. Designing Single Sign-On (SSO)

ProtocolDetail
SAML 2.0Enterprise; XML-based
OIDCModern; JSON/JWT
IdPOkta, Azure AD/Entra, Auth0, Keycloak
JIT provisioningCreate user on first login
SCIMLifecycle sync

6. Designing Multi-Factor Authentication

FactorDetail
TOTP (RFC 6238)Google Authenticator, Authy
WebAuthn / PasskeysPhishing-resistant; recommended
SMS / Email OTPWeakest; SIM-swap risk
Push notificationsNumber matching to prevent fatigue
Recovery codesOne-time backup codes

7. Designing Token Refresh Mechanisms

StrategyDetail
Refresh token rotationNew refresh on each use; old invalid
Reuse detectionRevoke chain if old refresh reused
Storage (web)HttpOnly Secure cookie
Storage (mobile)Secure enclave / Keystore

8. Designing Token Revocation Strategy

ApproachDetail
Short-lived accessNatural revocation via expiry
Refresh blocklistDB / Redis set of revoked jti
Token introspectionOAuth2 RFC 7662 endpoint
User version stampInvalidate all tokens by bumping

9. Designing Passwordless Authentication

MethodDetail
Magic linkSigned URL via email; short TTL, single-use
Email/SMS OTP6-digit code
Passkeys (WebAuthn)Best UX + security
Push approvalConfirmed on trusted device

10. Designing Identity Provider Integration

ElementDetail
FederationTrust external IdPs (social, enterprise)
Account linkingSame email → merge accounts (verify)
Claims mappingIdP attributes → app user fields
DiscoveryEmail-domain → IdP routing

11. Designing Device Authentication

MechanismDetail
Device fingerprintDetect new device → step-up auth
Trusted device cookieSkip MFA on remembered device
Device flowOAuth2 device code for input-constrained
Mutual TLSCert per device for IoT/B2B

12. Designing Biometric Authentication

AspectDetail
Local matchFaceID/TouchID never leaves device
WebAuthn bindingBiometric unlocks platform authenticator
FallbackPIN / password
Don't store templatesServer only sees attestation