Security Foundations
Intermediate 15 min read Security Foundations

Authentication and Authorization

Who you are vs what you're allowed to do

Authentication proves who you are; authorization decides what you may do. Getting the two mixed up — or using weak authN — is the root of most access-control breaches.

authenticationauthorizationpasswordsMFASSOsessionstokens
Before we start

What you will learn

  • Separate authentication from authorization and name an incident of each
  • Explain MFA and why passwords alone are insufficient
  • Describe session and token lifecycle basics securely
Introduction

What is this?

Authentication (authN) verifies identity claims — the password matches, the key verifies, the biometric matches. Authorization (authZ) decides, once identity is established, which resources and actions that identity may perform. Modern systems add factors (MFA) and move identity to central providers (SSO/OIDC), while authorization is expressed in roles, scopes and policies. Weaknesses at either step cause most access-control incidents in the OWASP Top 10.

Motivation

Why do you need it?

  • Password reuse and phishing defeat password-only authN at scale; MFA is the standard mitigator.
  • AuthZ mistakes (horizontal/vertical privilege escalation, IDOR) are quietly the #1 web flaw.
  • Sessions and tokens are the glue: a stolen session token is an authN bypass that no password strength fixes.
  • Separating the concepts lets you debug precisely: 'they authenticated fine but couldn't act' is an authZ bug.
Think about it

Mental model

Imagine

Showing your ID at the door vs the bouncer's guest list

Think of

Authentication vs authorization

Your ID proves who you are (authentication). The door list decides whether you're allowed into the VIP room tonight (authorization). Knowing someone's name is worth nothing if they're not on the list — and being on the list proves nothing about who they are.

Mechanics

How it works

  • On login, the app verifies credentials (password hash, TOTP, WebAuthn key) — authentication completes.
  • A session/token is issued and bound to that identity: cookie, opaque session ID, or signed JWT.
  • Every subsequent request carries proof of the session; the app validates it (still authN: 'is this session valid for this user?').
  • Authorization then checks the user's roles/claims against the resource's policy ('must be admin of org 42 to PATCH this').
  • MFA adds a second factor (something you have/are) so a stolen password alone can't authenticate.
  • Federation (SSO/OIDC/SAML) delegates authN to an identity provider, giving central control and logout.

Login flow with the authN/authZ boundary marked.

Credentials
Verify identity
Issue session
Request resource
Policy check
Allow or deny
Real world

In production

  • An app enforces MFA for all logins and then applies row-level authZ per organization in every query — a support rep sees only their org's rows.
  • A microservice gateway validates the JWT (authN) then reads the tenant claim to scope database queries (authZ).
  • A company moves to SSO via OIDC: password resets, lockouts and MFA enrollments consolidate at the IdP.
Guidelines

Best practices

  • Never store plaintext passwords: hash with a memory-hard algorithm (argon2id/bcrypt) and salt.
  • Enable MFA everywhere sensitive, and require it for admins and remote access.
  • Enforce authZ server-side on every action — never trust client-side 'user is admin' flags.
  • Bind sessions to the session token's origin, rotate on privilege change, and expire them.
  • Log authN and authZ events (success, failure, escalation) for triage.
Watch out

Common mistakes

  • Treating authZ as 'the front end hides the delete button' — an attacker calls the API directly.
  • Accepting a JWT 'because it's signed' without checking scopes/audience/expiry — a valid-but-sloppy token is a free pass.
  • Using a weak password policy but no MFA — the cheap hard-policy is bypassed by breaches anyway.
  • Mixing authN errors ('wrong password') with authZ ('not allowed') in one response, leaking account existence.
When things break

Debugging tips

  • 401 vs 403: 401 = authN failed (no/expired session); 403 = authN succeeded but authZ denied.
  • A user 'can see things they shouldn't' almost always means an authZ check is missing or uses the wrong tenant id.
  • Check token expiry and issuer claims first when sessions 'randomly' fail after a deploy.
Security

Security considerations

  • Phishing defeats password MFA-absent, and even OTP is phishable — hardware keys are strongest.
  • AuthZ bugs (IDOR, path/object-level confusion) are heavily probed; enforce object-level checks, not just route-level.
  • Session fixation and token leakage (in URLs, referrers, logs) are classic authN breaks to test for in your own apps.
Interviews

Interview checkpoint

Practice

Hands-on lab

Hash and Verify Passwords Like a Real App
30 min · intermediate
In progress
Objectives
  • Hash passwords with a memory-hard algorithm instead of storing plaintext
  • Verify login attempts and measure why hashing beats hashing-once
  • Compare unsalted vs salted+slow hashing and see a cheap brute force fail

Hash and Verify Passwords Like a Real App

30 min · intermediate

0/5 steps
0%
sandbox · Linux sandbox
$
Try these commands
Test yourself

Quick quiz

0/2
1

A user logs in successfully but receives 403 when deleting a record. What failed?

2

Requiring a complex password policy removes the need for MFA.

Go deeper

Official documentation