LogoMasst Docs

Authentication

Understanding authentication patterns for 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

┌────────┐         ┌────────────┐         ┌─────────────┐
│ Client │         │   Server   │         │Session Store│
└───┬────┘         └─────┬──────┘         └──────┬──────┘
    │                    │                       │
    │  1. Login (creds)  │                       │
    │───────────────────>│                       │
    │                    │  2. Create session    │
    │                    │──────────────────────>│
    │                    │     Session ID        │
    │                    │<──────────────────────│
    │  3. Set-Cookie:    │                       │
    │     session_id     │                       │
    │<───────────────────│                       │
    │                    │                       │
    │  4. Request +      │                       │
    │     Cookie         │                       │
    │───────────────────>│  5. Validate session  │
    │                    │──────────────────────>│
    │                    │      User data        │
    │                    │<──────────────────────│
    │  6. Response       │                       │
    │<───────────────────│                       │

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

┌────────┐                    ┌────────────┐
│ Client │                    │   Server   │
└───┬────┘                    └─────┬──────┘
    │                               │
    │  1. Login (credentials)       │
    │──────────────────────────────>│
    │                               │ 2. Verify credentials
    │                               │    Generate JWT
    │  3. Return JWT                │
    │<──────────────────────────────│
    │                               │
    │  4. Request + JWT             │
    │     (Authorization: Bearer)   │
    │──────────────────────────────>│
    │                               │ 5. Verify signature
    │                               │    Check expiration
    │  6. Response                  │
    │<──────────────────────────────│

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

┌──────────┐    ┌──────────┐    ┌─────────────┐    ┌────────────┐
│   User   │    │  Client  │    │  Auth Server│    │  Resource  │
│ (Browser)│    │   App    │    │   (Google)  │    │   Server   │
└────┬─────┘    └────┬─────┘    └──────┬──────┘    └─────┬──────┘
     │               │                 │                 │
     │ 1. Click      │                 │                 │
     │   "Login"     │                 │                 │
     │──────────────>│                 │                 │
     │               │                 │                 │
     │  2. Redirect to Auth Server     │                 │
     │<──────────────────────────────────────────────────│
     │               │                 │                 │
     │  3. Login &   │                 │                 │
     │     Consent   │                 │                 │
     │─────────────────────────────────>                 │
     │               │                 │                 │
     │  4. Redirect with Auth Code     │                 │
     │<─────────────────────────────────                 │
     │               │                 │                 │
     │  5. Send Auth Code              │                 │
     │──────────────>│                 │                 │
     │               │                 │                 │
     │               │ 6. Exchange code│                 │
     │               │    for tokens   │                 │
     │               │────────────────>│                 │
     │               │                 │                 │
     │               │ 7. Access +     │                 │
     │               │    Refresh Token│                 │
     │               │<────────────────│                 │
     │               │                 │                 │
     │               │ 8. API call with Access Token     │
     │               │─────────────────────────────────> │
     │               │                                   │
     │               │ 9. Protected Resource             │
     │               │<──────────────────────────────────│

Single Sign-On (SSO)

┌────────────────────────────────────────────────────────┐
│                    Identity Provider                    │
│                       (IdP)                            │
│              ┌─────────────────────┐                   │
│              │   User Directory    │                   │
│              │   Session Store     │                   │
│              └─────────────────────┘                   │
└──────────────────────┬─────────────────────────────────┘

       ┌───────────────┼───────────────┐
       │               │               │
       ▼               ▼               ▼
  ┌─────────┐    ┌─────────┐    ┌─────────┐
  │  App A  │    │  App B  │    │  App C  │
  │ (Gmail) │    │ (Drive) │    │ (Docs)  │
  └─────────┘    └─────────┘    └─────────┘

Login once → 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 - 1 hour)
Refresh Token: Long-lived (days - weeks)

┌────────┐                    ┌────────────┐
│ Client │                    │   Server   │
└───┬────┘                    └─────┬──────┘
    │                               │
    │  Request + Expired Token      │
    │──────────────────────────────>│
    │                               │
    │  401 Unauthorized             │
    │<──────────────────────────────│
    │                               │
    │  Refresh Token Request        │
    │──────────────────────────────>│
    │                               │
    │  New Access Token             │
    │<──────────────────────────────│
    │                               │
    │  Retry Request                │
    │──────────────────────────────>│

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