Security basics: authentication, authorisation, OAuth and JWT
Authentication checks who you are. Authorisation checks what you're allowed to do.
Why it matters
Every system must know who is calling and what they may do, and must keep data safe even if something leaks. Mistakes here are the most expensive kind.
The idea
A user logs in once and gets a session ID or a token that proves who they are on later requests. A JWT is a signed token that services can check without a database lookup. OAuth 2.0 lets one app act on another service for a user without seeing their password; 'Sign in with Google' adds OpenID Connect on top.
Also essential: HTTPS everywhere, passwords stored with a slow salted hash, least privilege, encryption at rest, and no secrets in code.
A hotel: reception checks your passport (authentication) and gives you a key card that opens only your room and the gym (authorisation).
A key card can be switched off at reception instantly. A JWT stays valid until it expires, because services only check its signature, not a central list. So keep JWTs short-lived and use refresh tokens.
How it works
- Login: the user sends their password over HTTPS. The server hashes it with the stored salt and compares the result with the stored hash.
- If it matches (and the second factor is valid), the server issues a short-lived access token and a longer-lived refresh token.
- Each request carries the access token. The gateway checks its signature and expiry without a database call.
- The service checks authorisation: does this user own account 123? This check happens on the server, every time.
- When the access token expires, the app swaps the refresh token for a new one. Logging out revokes the refresh token.
Example
Take a banking app. You log in with a password plus a one-time code (multi-factor), and get a 15-minute access token and a refresh token. The gateway checks the token's signature. The payments service checks that the user owns the account before moving any money.
Key points
- Never store plain passwords: use a slow, salted hash (bcrypt, scrypt, Argon2).
- Session cookies (state kept on the server, easy to revoke) or JWTs (stateless, hard to revoke).
- OAuth 2.0 = delegated authorisation; OpenID Connect = login and identity on top of it.
- Authorisation models: role-based (RBAC) or attribute-based (ABAC). Check on every request, on the server.
- Defence in depth: TLS, input validation, rate limiting, least privilege, audit logs.
- Common attacks: SQL injection, cross-site scripting (XSS), cross-site request forgery (CSRF), credential stuffing.
Common mistakes
- Checking permissions only in the user interface.
- Long-lived JWTs that can't be revoked.
- Storing passwords with a fast hash (MD5, SHA-256) or without a salt.
- Committing secrets such as passwords or API keys to Git, where they stay in the history for ever.
Not true: Hiding a button in the user interface stops a user from doing that action.
In fact: Anyone can send a request straight to the server, without the interface. Check permissions on the server, for every request.
Not true: Storing passwords: SHA-256 is a good choice, because it is fast.
In fact: Fast is the problem: an attacker can try billions of guesses a second. Use a slow, salted hash made for passwords, such as bcrypt or Argon2.
In the interview
Authentication goes through an identity provider with multi-factor login, issuing short-lived access tokens and refresh tokens. The gateway verifies tokens, and each service enforces authorisation on every request — for example, that the user owns the account. Passwords are stored with a slow hash, everything runs over TLS, and sensitive data is encrypted at rest.
Part of Unit 4: Distributed systems and security in the free System Design course.