TechCoder.io / System Design

Authentication & Authorization

Master the difference between authentication and authorization, session-based vs token-based auth, JWT structure and signing, OAuth 2.0 flows (Authorization Code, PKCE, Client Credentials), OpenID Connect, MFA, and SSO architecture.

By TechCoder TeamLast updated: 2026-10-17
In a Nutshell

Master the difference between authentication and authorization, session-based vs token-based auth, JWT structure and signing, OAuth 2.0 flows (Authorization Code, PKCE, Client Credentials), OpenID Connect, MFA, and SSO architecture. This hands-on tutorial focuses on practical implementation of authentication & authorization concepts.

Authentication & Authorization

Authentication (AuthN) answers: "Who are you?" — verifying identity.
Authorization (AuthZ) answers: "What are you allowed to do?" — verifying permissions.

[!IMPORTANT] These two concepts are often confused but operate independently. A valid JWT token (authenticated) doesn't mean the user can access admin endpoints (authorized). Always enforce both independently.

Session-Based vs Token-Based Auth

PYTHON PLAYGROUND
⏳ Loading editor…

OAuth 2.0 — Authorization Code Flow

PYTHON PLAYGROUND
⏳ Loading editor…

Quiz

Quiz

Question 1 of 2

A user's JWT access token is stolen from localStorage by an XSS attack. The token has a 24-hour expiry. What is the impact?

No impact — JWT tokens are encrypted and cannot be used by attackers
The attacker has full access as that user for 24 hours — there's no way to revoke a stateless JWT before expiry
The server detects the theft because the IP address changed

Key Takeaways

✅ Authentication = verify identity. Authorization = verify permissions. They are independent.
✅ JWT is stateless — verify by signature only, no DB lookup. Trade-off: cannot revoke before expiry.
✅ Store tokens in httpOnly cookies — XSS attacks can't read them (unlike localStorage).
✅ OAuth 2.0: Authorization Code + PKCE for user login; Client Credentials for service-to-service.
✅ RBAC — assign permissions to roles, roles to users. Never assign permissions directly to individual users.
✅ Short access tokens (15 min) + long refresh tokens (30 days in httpOnly cookie) is the industry standard.

Authentication Methods Comparison

MethodHow It WorksStateless?Best For
Session CookieServer stores session ID → cookie❌ StatefulTraditional web apps
JWTSigned token carries claims✅ YesMobile/SPA APIs
OAuth 2.0Delegated access via tokens✅ YesThird-party integrations
API KeyStatic key in header✅ YesServer-to-server, simple APIs
mTLSMutual certificate auth✅ YesService-to-service (zero trust)
SAMLXML-based SSO❌ StatefulEnterprise SSO
Passkey / WebAuthnFIDO2 biometric✅ YesPasswordless consumer apps

Official Resources

What's Next?

Users are authenticated — but how does data travel securely between them and your servers?
Next: HTTPS, TLS & Web Security — TLS handshake, certificate authorities, and the OWASP Top 10 attacks.