Skip to content
Tech Interview Prep
Technical interview guide

API Authentication & Authorization

Verifying who's calling an API (authentication) and what they're allowed to do (authorization): API keys, OAuth, and JWTs.

Minutes to read
4
Practice MCQs
25
Interview Q&A
25
Edition
v4
Editorial status
Reviewed

Scope: HTTP Semantics RFC 9110 and OpenAPI Specification 3.2.

Overview

Curated: · Written: · Reviewed:

API Authentication & Authorization

In one line

Authentication establishes who is calling; authorization decides whether that caller may touch this specific object or function. Use OAuth 2.0 with PKCE when a user delegates access to a client, client credentials when a service acts as itself, and an object-level check in the data layer whenever an identifier arrives in a request.

Key definitions

  • Authentication (AuthN): verifying the caller's identity from a credential.
  • Authorization (AuthZ): deciding whether the authenticated subject may perform this action on this object.
  • BOLA: broken object-level authorization (OWASP API1:2023), where a valid token is used against another user's identifier.
  • Audience (aud): the resource server a token is meant for; verifiers reject tokens issued for other audiences.
  • PKCE: binds an authorization code to the client that started the flow, via a verifier only that client holds.
  • Refresh-token rotation: each refresh issues a new refresh token; presenting an already-rotated token signals theft and should revoke the family.

Formulas and complexity

PKCE challenge, sent with the authorization request:

code_challenge=BASE64URL(SHA256(code_verifier))code\_challenge = \mathrm{BASE64URL}(\mathrm{SHA256}(code\_verifier))

How it works

  • The gateway or middleware verifies the credential and returns 401 when it is missing, malformed or expired. It populates a caller context that travels with the request.
  • The authenticated caller is not yet authorized for any row. The check is "may this subject read this invoice", enforced in the data layer or the database.
  • A 403 means authenticated but not permitted. Re-authenticating will not help, so clients must not refresh tokens on 403.
  • In PKCE, the challenge goes out with the authorization request and the verifier comes back at the token exchange. The server recomputes the hash and compares.
yes no Client creates verifier and challenge Authorize request with code_challenge User signs in, code returned to redirect URI Token request with code and code_verifier SHA256 of verifier equals challenge? Access token issued invalid_grant

When to use and when not to

Credential or flowUse whenAvoid when
API keyServer-to-server integration that must be identifiable and revocableActing for an end user; a key proves possession only
OAuth code + PKCEA user delegates access to a browser or native clientA server with no user present (use client credentials)
Client credentialsA service acts as itselfAny action on behalf of a user
Self-issued JWTFirst-party or internal APIs with short lifetimesYou need instant revocation with no refresh or check path
Opaque tokenYou need instant revocation and private claimsMany services cannot afford a lookup per request
Password grantAlmost never; deprecated for new designsAny app that would handle the user's password

Traps interviewers probe

  1. Checking the token but not the object. Fix: scope every query to the caller's tenant or owner, or enforce it with row-level security keyed on a session variable set from the verified token.
  2. Trusting the header's alg. Fix: pin the algorithm server-side, reject alg none, and never verify an HS256 token with an RSA public key as the secret.
  3. Sending the ID token to the API. Fix: the API takes an access token whose aud equals the API. The ID token is for the client to learn who the user is.
  4. Claiming logout on stateless tokens. Fix: short access-token lifetime, server-held rotating refresh tokens, and a per-request revocation check if the revocation must take effect at once.
  5. Pooled connections keeping session state. Fix: set tenant context with SET LOCAL inside the transaction that runs the query. A session-level SET survives on a pooled connection and can leak to the next caller.

Interview tips

Open with the split: "Authentication tells me who is calling; authorization tells me whether they may touch this object. I enforce the second in the data layer, not in each handler."

Expect these follow-ups:

  • How do you revoke a JWT? Answer with the residual window stated explicitly.
  • Why PKCE for a single-page app? It binds the code to a verifier without needing a client secret.
  • Where should the browser keep the token? Compare XSS and CSRF exposure per storage option.
  • Why return 404 rather than 403 for another tenant's object? It avoids confirming the object exists, at the cost of more confusing errors for legitimate callers.

30-second outline:

  1. Name the two questions and which one a valid token answers.
  2. Pick the credential and flow for the caller type.
  3. Put the object check where handlers cannot skip it.
  4. State the revocation window and how short it is.
  5. Name one failure you would test for, such as a valid token used on another tenant's ID.

Feedback on this overview

Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.