Authentication and authorization

Proving who a user is (sessions, JWTs, OAuth) and deciding what they may do (roles, ownership checks, row-level security), without the classic holes.

L2 · Mid-level engineer≈ 5 min read
Key points · the two-minute version
  1. Authentication asks who you are; authorization asks what you may do. Most breaches are authorization bugs.
  2. IDOR: check object ownership on every access, and return the same 404 for missing and not yours.
  3. Sessions are simple and revocable. JWTs are stateless, so keep them short-lived with rotating refresh tokens.
  4. OAuth/OIDC uses the authorization code flow with PKCE. Hash passwords with Argon2id or bcrypt.
  5. Never trust a client-sent tenant ID. Add RLS as a second wall. Prompts never grant permissions.
You should be able to
  • Choose between server sessions and JWTs
  • Explain the OAuth authorization code flow with PKCE
  • Prevent IDOR with object-level authorization checks

Why this matters

Auth bugs are the most damaging bugs: one missing check exposes every customer's data. AI features add a new twist, because a model must never be the thing that decides who may see what.

Core ideas

  • Authentication (authn): who are you? Passwords, OAuth/OIDC login, passkeys, API keys.
  • Authorization (authz): what may you do? Roles (RBAC), attributes (ABAC), ownership ("is this your order?").
  • Session cookie: the server stores session state; the browser holds an opaque ID in an HttpOnly cookie. Easy to revoke.
  • JWT: a signed token carrying claims (user id, expiry). Servers verify it without a lookup; hard to revoke before expiry, so keep access tokens short-lived and use rotating refresh tokens.
  • OAuth 2.0 / OpenID Connect: delegate login or access to another provider ("Sign in with Google", "connect your HubSpot"). The authorization code flow with PKCE is the safe default.
  • Password storage: a slow, salted hash (Argon2id, bcrypt). Never encryption, never plain hashing.

How to do it

Object-level authorization (the check people forget)

@router.get("/v1/orders/{order_id}")
async def get_order(order_id: int, user: CurrentUser, repo: OrderRepo):
    order = await repo.get(order_id)
    if order is None or order.customer_id != user.id:   # same 404 for "missing" and "not yours"
        raise HTTPException(404)
    return order

Without the ownership check, any logged-in user can read any order by changing the number. That's IDOR (insecure direct object reference), number one on most API security lists.

Defence in depth for multi-tenant data

Filter by tenant in every query and enable Postgres row-level security as a second wall, so a forgotten filter returns nothing instead of another tenant's data (C4).

Authorization in AI features

  • Retrieval must apply the user's permissions before documents reach the model (Q8).
  • A tool the agent calls runs with the user's permissions, checked in code, never because the prompt says so (Q25).
  • "A system prompt is not a security boundary."

Why this way

Sessions suit classic web apps on one domain (simple, revocable). JWTs suit many services verifying identity without a shared session store, or mobile clients; pay for that with short expiry and refresh rotation. OAuth avoids handling other providers' passwords at all.

Common mistakes

  • Checking that a user is logged in but not that they own the resource.
  • Long-lived JWTs stored in localStorage (readable by any injected script).
  • Rolling your own crypto or password hashing.
  • Trusting a tenant_id sent by the client.

Interview follow-ups

  • "How do you log out a user who has a JWT?" Short access-token expiry plus revoking the refresh token (stored server-side with rotation and reuse detection); for immediate revocation, keep a deny-list of token IDs until they expire.
  • "Where do authorization checks live?" In one place in the service or repository layer (e.g. repo.get_for_user(id, user)), not scattered in routes, plus database-level RLS as a backstop and tests that try cross-tenant access.
  • "How should an AI agent call your API on behalf of a user?" With a token scoped to that user and the minimum permissions (OAuth scopes), never a service account with full access; the tool checks permissions like any other client.
  • "RBAC isn't enough. Now what?" Add attribute or relationship checks (owner, team, document ACLs); for complex rules, use a policy engine (OPA, Cedar, OpenFGA).

Check yourself

What is IDOR and how do you prevent it?

Insecure direct object reference: accessing another user's object by changing an ID. Prevent it with an ownership or permission check on every object access, in server code, ideally centralised in the repository layer, plus RLS as a backstop.

Why is PKCE used in the OAuth code flow?

It binds the authorization code to the client that started the flow (via a one-time secret), so an intercepted code can't be exchanged by an attacker. It's required for public clients like SPAs and mobile apps and recommended for all.

Build it

Add login with hashed passwords (Argon2id) and session cookies to your FastAPI service, then write a test proving user A gets 404 on user B's order.

What comes next

3.5 Caching and Redis.

Build it to remember

Two hands-on projects that exercise this lesson. Pick one, build it end to end, and keep it in your portfolio.

Project 1

Multi-tenant SaaS starter

Sign-up, login, organisations and roles; every query is tenant-filtered with an RLS backstop; tests prove user A can't read user B's data.

  • FastAPI
  • Postgres RLS
  • Argon2
  • HttpOnly cookies
  • pytest

Practises Object-level authorization and defence in depth.

Project 2

Sign in with Google + API keys

OAuth login plus per-user API keys (hashed, scoped, revocable) for programmatic access.

  • Next.js
  • Auth.js
  • Postgres
  • Prisma

Practises OAuth flows, key hygiene, scopes.

Can you answer every check-yourself question out loud without opening the answer?