Security Basics

Enough security to design responsibly. Authentication, authorization, encryption, secrets, and the perimeter โ€” interview depth, not a course.

AuthN vs AuthZ Sessions vs JWT TLS / mTLS Hashing Secrets
๐Ÿชช

Authentication

Authentication proves identity. After login, the server issues something the client presents on later requests, and the two dominant approaches differ in where the state lives.

  SESSION + COOKIE                       JWT (JSON Web Token)
  server stores session, sends an ID     server signs a token holding the claims
     client โ”€cookie(id)โ”€โ–ถ server            client โ”€tokenโ”€โ–ถ server
     server looks up id in session store    server verifies the SIGNATURE, no lookup
     โ–ถ state on the SERVER                   โ–ถ state in the TOKEN (stateless)
     โ–ถ revoke = delete the session (easy)    โ–ถ revoke = hard (valid till it expires)
SessionsJWT
StateServer-side storeIn the token, stateless
ScalingNeeds a shared session storeAny server verifies alone
RevocationEasy โ€” delete the sessionHard โ€” valid until expiry
Best forClassic web appsAPIs, microservices, mobile
๐Ÿ’ก
The JWT revocation problem is the tell. Because a signed token is valid until it expires, you can't instantly revoke it โ€” so keep access tokens short-lived (minutes) and pair them with a longer-lived refresh token you can revoke at the store. OAuth2 / OIDC is the standard for delegated authorization โ€” "sign in with Google" hands you a token without the app ever seeing the password.
๐Ÿšฆ

Authorization

Authentication is who you are; authorization is what you're allowed to do. Different problems, often conflated. A valid login (authN) doesn't mean you can delete another user's data (authZ). RBAC โ€” role-based access control โ€” is the default model: assign users roles, grant permissions to roles, check the role on each action.

โ„น๏ธ
Enforce at two levels. The gateway does coarse checks โ€” is this token valid, does this role reach this service at all โ€” cheaply at the edge. The service does fine-grained checks it alone can make: does this user own this specific record. The gateway can't know the second; never rely on it for object-level authorization (Discovery).
๐Ÿ”

Encryption

Two states to protect, plus one thing that isn't encryption at all.

In transit TLS everywhere

TLS on every connection, including service-to-service. In a zero-trust design, internal calls use mTLS โ€” both sides present certificates, so services mutually authenticate and the internal network isn't assumed safe (TLS).

At rest disk and field

Disk and database encryption protect data if the storage is stolen. For the truly sensitive โ€” SSNs, card numbers โ€” add field-level encryption so a single leaked field is useless without the key.

โš ๏ธ
Hashing is not encryption. Encryption is reversible with a key; hashing is one-way. Passwords must be hashed, never encrypted โ€” with a slow, salted algorithm like bcrypt or argon2 that resists brute force. If your system can decrypt a stored password back to plaintext, that's the bug. You verify a login by hashing the input and comparing, never by decrypting.
๐Ÿ—

Secrets Management

API keys, database passwords, and signing keys are secrets, and they do not belong in code or committed env files โ€” one leaked repo and they're public forever. Store them in a dedicated secret store โ€” Vault, or a cloud KMS โ€” that services fetch from at runtime with tight access control, and rotate them periodically so a leaked secret has a short useful life.

โš ๏ธ
A hardcoded secret is a breach waiting to happen โ€” it lives in git history even after you delete it, and anyone with repo access has it. "Secrets in a vault, rotated, never in code" is a one-line answer that signals you've operated real systems, not just built them.
๐Ÿงฑ

The Perimeter

No single control is enough โ€” you layer them so one failure doesn't mean a breach. That's defense in depth.

ControlGuards against
Rate limitingAbuse, credential stuffing, scraping (Scaling)
Input validation at the edgeInjection, malformed payloads โ€” reject early
Least privilege between servicesA compromised service can't reach everything
Defense in depthOne bypassed layer isn't game over
โ„น๏ธ
Least privilege is the principle underneath most of this: every service, token, and key gets the minimum access it needs and nothing more. When a component is inevitably compromised, least privilege bounds the blast radius to what that one thing could touch (Resilience).
๐Ÿ“‹

Quick Reference

SessionsJWT
StateServer-sideIn the token
RevocationEasy (delete session)Hard (short expiry + refresh)
ScalingShared session storeStateless verify
FitsWeb appsAPIs, mobile, microservices
Security lines to say during a design
TLS everywhere, mTLS between services in zero-trust
Passwords hashed with bcrypt/argon2, never encrypted
AuthZ at the gateway (coarse) and service (fine-grained)
Secrets in a vault, rotated, never in code
Rate limiting + input validation at the edge; least privilege throughout
โ„น๏ธ
The one-liners to keep ready. AuthN is who you are, authZ is what you can do โ€” enforce authZ coarse at the gateway and fine at the service. Sessions store state server-side and revoke easily; JWTs are stateless but hard to revoke, so keep them short with refresh tokens. TLS everywhere, hash passwords with bcrypt/argon2, keep secrets in a vault, and layer controls for defense in depth.