Notes
AuthN vs AuthZ
Two separate questions. Authentication asks who you are: a password, a passkey, a one-time code. Authorisation asks what you're allowed to do: can user forty-two edit order nine? Authentication happens at login; authorisation must be checked on every single request, on the server, never trusted from the client.
- authentication (authN):
- who are you?
- password, passkey, OTP, SSO
- authorisation (authZ):
- what may you do?
- roles, permissions, ownership:
- can user 42 edit order 9?
- check authZ on EVERY request,
- on the server
Passwords
Never store passwords, not even encrypted. Store a slow, salted hash, using bcrypt, scrypt or argon2. The salt makes every hash unique, and the slowness, around a tenth of a second per guess, turns cracking a leaked database from minutes into years. Add rate limits on login and two-factor authentication.
never: plain text never: md5 / sha256 alone use a slow, salted hash: bcrypt, scrypt, argon2id hash = argon2(password, salt) slow = ~100 ms per guess → a leaked DB takes years to crack, not minutes + rate-limit logins, add 2FA
Tokens
After login, the client needs to prove who it is on each request. Either a random session id in a cookie, with the data on the server, easy to revoke. Or a signed token like a JWT that any service can verify without a lookup, but that's hard to revoke, so keep it short-lived, about fifteen minutes, with a long-lived refresh token you can revoke. Cookies should be HttpOnly, Secure and SameSite.
- session id in a cookie,
- data on the server
- + easy to revoke
- JWT: signed claims, no lookup
- + stateless, works across
- services
- − hard to revoke → keep it
- short (≈15 min) + a refresh
- token you CAN revoke
- cookies: HttpOnly, Secure, SameSite
OAuth
OAuth is how 'Sign in with Google' works without the app ever seeing your Google password. The app redirects you to Google, you log in and consent there, Google sends you back with a one-time code, and the app exchanges that code for tokens, server to server. OAuth handles delegated access; OpenID Connect adds identity on top.
- "Sign in with Google":
- 1. app redirects you to Google
- 2. you log in and consent there
- 3. Google redirects back with
- a one-time code
- 4. app exchanges the code for
- tokens (server to server)
- OAuth: delegated access
- OIDC: adds identity (id token)
- use PKCE for mobile / SPA
Encryption
Encrypt data in transit with TLS everywhere, including between your own services with mutual TLS. Encrypt it at rest: disks, databases, backups and object storage, with keys held in a key management service and rotated. For extra-sensitive fields like card numbers, use field-level encryption or tokenisation.
- in transit: TLS everywhere
- (HTTPS, also between services:
- mTLS)
- at rest: disks, DB, backups,
- object storage encrypted (AES)
- keys in a KMS, rotated
- extra-sensitive fields
- (card numbers, Aadhaar):
- field-level encryption or
- tokenisation
Secrets
Database passwords and API keys never belong in code, Git or container images. Keep them in a secrets manager and inject them at runtime, with rotation. Apply least privilege: each service gets only the permissions it needs, production access is limited and every access is logged.
- no API keys in code, Git,
- or Docker images
- → secrets manager (Vault, AWS
- Secrets Manager), injected
- at runtime, rotated
- least privilege: each service
- gets only the permissions
- it needs
- separate prod access, audit logs
Attacks
Know the common attacks and their fixes. SQL injection: always use parameterised queries. Cross-site scripting: escape output and set a content security policy. Cross-site request forgery: SameSite cookies or tokens. Broken access control, the most common one: check ownership on every request. And rate limits, a firewall and a CDN against brute force and floods.
- SQL injection → parameterised
- queries, never string concat
- XSS → escape output, CSP
- CSRF → SameSite cookies, tokens
- broken access control →
- check ownership every request
- brute force → rate limits, 2FA
- DDoS → CDN, WAF, rate limits
- outdated libraries → patch
Say it like this
In an interview: an auth service issues short-lived access tokens with refresh tokens, or you use single sign-on via OpenID Connect. The API gateway verifies tokens, and each service checks permissions on the resource. Hash passwords with argon2 or bcrypt, use TLS everywhere, encrypt at rest with managed keys, keep secrets in a manager, and rate-limit.
- auth service issues short JWT
- + refresh token; or OIDC SSO
- API gateway verifies tokens
- each service checks permissions
- passwords: argon2/bcrypt
- TLS everywhere, mTLS inside
- encryption at rest, KMS keys
- secrets manager, least privilege
- rate limits, audit logs