SkillSprout
Chapter 33 · Lesson 1
Beginner
3 min read

Authentication and Authorization

Overview

Two questions guard every system. Authentication asks "who are you?" and authorization asks "what are you allowed to do?". They are different checks and both are needed.

Detailed Notes

What are authentication and authorization

Authentication (AuthN) proves identity. Authorization (AuthZ) decides what that identity may do. At an airport, your passport check is authentication; your boarding pass deciding which plane you may enter is authorization.

The problem it solves

Without authentication anyone can pretend to be anyone. Without authorization, a logged-in user can read other people's data by changing an ID in the URL. Check both on every request.

How it works

Authentication

Proof comes from three kinds of factors: something you know (password), have (phone, hardware key) or are (fingerprint). Multi-factor authentication (MFA) requires two or more, so a stolen password alone is not enough.

Storing passwords. Never store the password itself. Store a hash: a one-way fingerprint (as in the Checksums chapter, but built to resist attack). Add a random salt per user so identical passwords get different hashes, and use a deliberately slow algorithm such as bcrypt, scrypt or Argon2 so guessing is expensive.

Keeping you logged in. After login the server must recognise you on later requests:

  • Session: the server stores your login state and gives the browser a random session ID in a cookie. Easy to revoke, but the server must keep state.
  • Token (such as a JWT): the server issues a signed token holding your identity and an expiry time. Any server can verify the signature without a lookup, which suits many servers behind a load balancer. The trade-off: a token is hard to revoke before it expires, so keep lifetimes short.

OAuth 2.0 and OpenID Connect let an app log you in through another provider ("Sign in with Google") without ever seeing your password. Single sign-on (SSO) means one login works across many apps.

Authorization

  • RBAC (role-based access control): permissions attach to roles (admin, editor, viewer) and users get roles. Simple and common.
  • ABAC (attribute-based): rules use attributes, like "doctors may read records of patients in their own hospital". More flexible.
  • Least privilege: give each user and service only the access it needs, nothing more, so a stolen credential does limited damage.

Enforce authorization on the server for every request, not just by hiding buttons in the UI.

A simple example

A user logs in with a password and an MFA code. The identity provider returns a short-lived token. The app sends it with each request. The order service checks the signature (authenticated), then checks that the user's role may view orders and that this order belongs to them (authorized).

System design example

A web app delegates login to an identity provider (SSO, MFA, OAuth/OIDC). The API gateway verifies the token and rejects bad ones early. Each service checks roles and ownership, and service-to-service calls use their own least-privilege credentials.

Summary

  • Authentication proves who you are; authorization decides what you can do.
  • Use MFA; store only salted, slow hashes of passwords.
  • Sessions keep state on the server; tokens like JWTs are stateless but harder to revoke.
  • RBAC (roles) is the common model; ABAC (attributes) is more flexible.
  • Apply least privilege and check authorization on the server every time.

Architecture Diagram

Token-based login with an identity provider
Architecture
User
Identity provider
API gatewayverify token
Service

Authenticate once, then authorize every request.

  1. 1The user logs in with a password and a second factor.
  2. 2The identity provider returns a short-lived signed token.
  3. 3The gateway verifies the token on every request.
  4. 4The service checks the user's role and ownership before acting.

Common Interview Questions

Finished this lesson?

0 of 91 lessons done · 0% of the course