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

  1. Login: the user sends their password over HTTPS. The server hashes it with the stored salt and compares the result with the stored hash.
  2. If it matches (and the second factor is valid), the server issues a short-lived access token and a longer-lived refresh token.
  3. Each request carries the access token. The gateway checks its signature and expiry without a database call.
  4. The service checks authorisation: does this user own account 123? This check happens on the server, every time.
  5. 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

Common mistakes

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.