Security

Authentication

Understanding authentication patterns for system design.

3 min readConcepts · System Design

What is Authentication?

Authentication is the process of verifying the identity of a user or system. It answers the question: "Who are you?"


Authentication Methods

MethodDescriptionUse Case
PasswordTraditional username/passwordWeb applications
MFAMultiple verification factorsHigh-security apps
OAuthDelegated authorizationThird-party login
SSOSingle Sign-OnEnterprise systems
API KeysStatic tokensService-to-service
JWTJSON Web TokensStateless auth
CertificatesX.509 certificatesmTLS, IoT

Session-Based Authentication

Session-Based AuthenticationSequence diagram

Pros: Easy revocation, server control Cons: Stateful, session storage scaling


JWT (JSON Web Tokens)

Structure

Header.Payload.Signature

eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ1c2VyMTIzIn0.signature

┌─────────────────────────────────────────────────────┐
│                     Header                          │
│  { "alg": "HS256", "typ": "JWT" }                  │
├─────────────────────────────────────────────────────┤
│                     Payload                         │
│  {                                                  │
│    "sub": "user123",                               │
│    "name": "John Doe",                             │
│    "iat": 1516239022,                              │
│    "exp": 1516242622                               │
│  }                                                  │
├─────────────────────────────────────────────────────┤
│                    Signature                        │
│  HMACSHA256(                                        │
│    base64(header) + "." + base64(payload),         │
│    secret                                           │
│  )                                                  │
└─────────────────────────────────────────────────────┘

JWT Flow

JWT FlowSequence diagram

Pros: Stateless, scalable, self-contained Cons: Can't revoke easily, size overhead


OAuth 2.0

OAuth Roles

RoleDescription
Resource OwnerUser who owns the data
ClientApplication requesting access
Authorization ServerIssues access tokens
Resource ServerHosts protected resources

Authorization Code Flow

Authorization Code FlowSequence diagram

Single Sign-On (SSO)

Single Sign-On (SSO)Flowchart

Login once and access all applications.

SSO Protocols

ProtocolUse Case
SAML 2.0Enterprise SSO
OpenID ConnectConsumer apps (built on OAuth)
LDAPInternal directory

Token Refresh Flow

Access Token: Short-lived (15 min to 1 hour). Refresh Token: Long-lived (days to weeks).

Token Refresh FlowSequence diagram

Session vs JWT Comparison

AspectSessionJWT
StateStatefulStateless
StorageServer-sideClient-side
ScalabilityNeeds shared storeEasy horizontal scale
RevocationImmediateDifficult (need blocklist)
SizeSmall cookieLarger token
SecurityServer controlledSelf-contained

Best Practices

PracticeDescription
HTTPS onlyEncrypt all auth traffic
Secure cookiesHttpOnly, Secure, SameSite
Short token lifeMinimize exposure window
Refresh rotationRotate refresh tokens
Rate limitingPrevent brute force
MFAAdd second factor for sensitive ops

Interview Tips

  • Know OAuth 2.0 authorization code flow
  • Explain JWT structure and validation
  • Discuss session vs token trade-offs
  • Mention token refresh patterns
  • Cover SSO for enterprise systems

Finished reading?

Mark it done to track your progress in Concepts.