Course map

Topic 039 / Core

API authentication and authorization basics: API keys, sessions, tokens, resource-level checks (full detail → 159–163)

Chapter 3.6 · API Access Basics

What and why

An API designed so far (→ 31–38) is open to anyone who can reach it; before it goes near real data it needs to know who is calling, and whether that caller may touch the specific object requested. Authentication establishes who (or which application) is calling; authorization decides what that caller may do. This topic covers three mechanisms enough to ship safely — API keys (identify a calling application, not a person), sessions (server-side state referenced by a cookie), tokens (self-contained credentials sent per request) — plus resource-level checks: does this specific caller have rights to this specific object, not just "is anyone logged in." Full identity-lifecycle, OAuth/OIDC, JWT mechanics, and RBAC/ABAC depth is → 159–163. Builds on: → 31.

Diagram

Mechanism Identifies Typical caller Where state lives
API key a calling application third-party integration, server-to-server none needed beyond the key itself
Session a logged-in user traditional server-rendered app server-side, referenced by a cookie
Token a logged-in user or service stateless API, mobile, SPA self-contained in the token itself

How it works

  1. Pick a mechanism matching the caller type: API keys for machine-to-machine or third-party callers, sessions for server-rendered apps, tokens for stateless APIs and mobile or single-page clients.
  2. Every request carries the credential — header, cookie, or bearer token.
  3. The server authenticates the credential before doing any work.
  4. Authentication alone only proves identity; a separate resource-level check must confirm this identity may access this specific object (e.g. "does order 42 belong to the calling user").
  5. The missing step is almost always step 4 — checking "is logged in" without checking "may touch this object" is the gap behind most real-world access-control bugs.

Practical example

(illustrative). GET /orders/42 authenticates the caller as user 7 via their token (step 3), then must separately check that order 42 belongs to user 7 before returning it (step 4). Skipping that second check, and returning the order just because the token was valid, lets user 7 read anyone's order by guessing IDs.

When to use it

  • Use when: shipping Projects 1, 3 and 4 — this basic layer is enough before the full identity stack (→ 159–163) is needed.
  • Avoid when: treating "authentication passed" as sufficient on its own — always add the resource-level check from step 4.
  • Failure scenarios: an API key leaked in client-side code (keys identify an application, not a user, and aren't safe to embed in a browser); a missing resource-level check letting one user read or modify another user's object by guessing or incrementing an ID — the single most common real-world instance of this bug.
  • Simpler alternative: a single shared API key for a low-stakes internal tool with no sensitive per-user data; real user-level access control is where the resource-level check becomes mandatory.

Practice

(pattern kind — implement minimal, spot the misuse; 20–30 min, no lab services). Add authentication and per-object authorization to Project 1's (task management) API: pick one mechanism from the table above, and implement a resource-level check on at least one endpoint. Write down, for each check added, which specific attack it stops — for example, "the ownership check on GET /tasks/{id} stops user A from reading user B's tasks by guessing IDs." Done when every mutating and every per-object-read endpoint has a documented check and a named attack it defeats.

Key takeaway

Authentication answers "who is calling"; authorization answers "may they touch this object" — and the second question is the one most often skipped. Done when you can: add a resource-level check to an endpoint and name the exact attack it prevents. Used next in → 159–163 (full identity, OAuth/OIDC, JWT, RBAC/ABAC depth); Project 4 (checklist: auth forwarding, topic 39).

References

None — this topic makes no external claims.

Source directory

Search the guide