API & BACKEND ARCHITECTURE / SYSTEM CONCEPT BRIEF

Authentication

Authentication (AuthN) answers 'who are you?'.

BeginnerPhase 02 / Topic 5 of 20RequirementsTrade-offsFailure modes
01

Overview

Authentication (AuthN) answers 'who are you?'. The user or service proves its identity with something it knows (password), has (phone, security key), or is (biometrics), and the system then issues a credential, such as a session cookie or token, that later requests present instead of the password.

Good authentication design stores passwords only as slow salted hashes, supports multi-factor authentication, protects against brute force and credential stuffing, and prefers delegating to proven identity providers (OAuth 2.0 / OpenID Connect) over building everything yourself.

Checking in at a hotel

At check-in you show your passport once (authentication). The hotel gives you a key card (session or token). For the rest of your stay, the card opens doors without showing your passport again, and it expires when you check out.

02

When to use it

  • Any system with user accounts or private data.
  • Service-to-service calls that must prove the caller's identity.
  • Choosing between sessions, JWTs, and an identity provider.
  • Adding MFA and passwordless login (passkeys).
03

Where it shows up in interviews

Login system design

Recognize it when: design sign-up, login, and session handling.

  • Design an authentication service
  • Design login for a banking app
Machine identity

Recognize it when: services and jobs authenticate to each other.

  • Design service-to-service auth in microservices
  • Design CI/CD access to cloud
04

Where it is used in real software

Identity providers

Okta, Auth0, Microsoft Entra ID, Amazon Cognito, and Keycloak handle login, MFA, and federation so apps do not store passwords.

Passkeys (WebAuthn)

Apple, Google, and Microsoft support passkeys: phishing-resistant public-key credentials that replace passwords.

Credential stuffing

Attackers replay leaked passwords from other breaches; rate limiting, MFA, and breached-password checks defend against it.

05

Key terms

Credential
Proof of identity: password, key, token, certificate.
Password hashing
Store bcrypt / scrypt / Argon2 hashes with a unique salt, never plain text.
MFA
Multi-factor authentication: combine two factor types.
OIDC
OpenID Connect: an identity layer on top of OAuth 2.0 that returns an ID token.
mTLS
Mutual TLS: services authenticate each other with certificates.
06

How it works, step by step

  1. 1
    User submits credentials

    Over HTTPS only.

  2. 2
    Rate limit and check

    Throttle attempts per account and IP; compare the password with the stored hash using a constant-time check.

  3. 3
    Second factor

    TOTP code, push approval, or WebAuthn challenge.

  4. 4
    Issue a credential

    Create a session (cookie) or tokens (access + refresh).

  5. 5
    Later requests present the credential

    The server validates it on every request, and revokes it on logout or suspicion.

07

Authentication methods compared

Stronger options resist phishing and reuse

Step 1 / 5
MethodSecurityUser frictionNotes
Password onlyLowLowVulnerable to reuse and phishing
Password + SMS codeMediumMediumSIM swapping risk
Password + TOTP appHighMediumOffline codes
Passkey / security keyVery highLowPhishing resistant, no shared secret
SSO via identity providerDepends on IdPLowCentralized policy and MFA

NOWMethod: Password only | Security: Low | User friction: Low | Notes: Vulnerable to reuse and phishing

For most products: delegate to an identity provider, enable MFA, and offer passkeys.

08

Implementation

import argon2 from "argon2"; // Sign-up: store only a salted, slow hashasync function register(email: string, password: string) {  const hash = await argon2.hash(password, { type: argon2.argon2id });  await db.users.insert({ email: email.toLowerCase(), passwordHash: hash });} // Login: same response for unknown user and wrong password (no user enumeration)async function login(email: string, password: string, ip: string) {  if (await attempts.tooMany(email, ip)) throw new HttpError(429, "Too many attempts");  const user = await db.users.findByEmail(email.toLowerCase());  const valid = user ? await argon2.verify(user.passwordHash, password) : false;  if (!valid) {    await attempts.record(email, ip);    throw new HttpError(401, "Invalid email or password");  }  if (user.mfaEnabled) return { mfaRequired: true, challengeId: await mfa.start(user.id) };  return sessions.create(user.id); // sets an HttpOnly, Secure cookie}
09

Complexity and performance

Password hash time~50-250 ms

Deliberately slow to resist brute force.

Session check per request~1 ms

Cache lookup or signature verify.

10

Trade-offs

Build vs buy

Building auth is easy to start and hard to secure. Identity providers cost money but handle MFA, recovery, breach detection, and compliance.

Security vs friction

Step-up authentication (asking for MFA only for sensitive actions) balances safety and usability.

11

Variants and related techniques

Sessions

Server-side state referenced by a cookie; easy revocation.

JWT access tokens

Self-contained, verified without a database lookup; harder to revoke.

API keys

Simple long-lived secrets for server-to-server integrations; rotate and scope them.

12

Common mistakes

  • Storing passwords with fast hashes (MD5, SHA-256).

    Fix: Use Argon2id, bcrypt, or scrypt with per-user salts.

  • Different errors for unknown user vs wrong password.

    Fix: Return one generic message to prevent account enumeration.

  • No brute-force protection.

    Fix: Rate limit per account and IP, add MFA, and check breached passwords.

13

Interview questions

What is the difference between authentication and authorization?

Authentication verifies identity (who you are). Authorization decides what that identity may do (what you are allowed to access). Authentication comes first.

How should passwords be stored?

As slow, salted hashes using Argon2id, bcrypt, or scrypt. Never encrypted or plain text, so a database leak does not reveal passwords.

14

Practice problems

ProblemDifficultyWhat it trains
Design sign-up and login with MFAMediumFlows and storage.
Design password reset securelyMediumTokens, expiry, enumeration.
Design an authentication service for 100M usersHardScale, sessions, IdP.