Skip to the page

OAuth, tokens and login providers · mid

Validating tokens, scopes and API keys

An API must verify the signature, issuer, audience and expiry of every token, accept access tokens and not ID tokens, and still check that the caller may touch this object. API keys are random secrets stored as hashes.

The notes

What you must know

  • Never use decodeJwt as a fallback. A decode does not check the signature, so anyone can craft a token with any sub and role. On failure, reject the request.
  • Verify with the provider's public keys (JWKS): signature, an allowed algorithm list, iss, aud (your API), exp and nbf, with a small clock tolerance.
  • Audience check: without it, any token from the same issuer (the admin tool, a partner app) is accepted. Require the API's audience.
  • ID tokens tell the client who logged in (aud is the client id). The API must receive an access token issued for its audience, with scopes. Where the provider issues RFC 9068 access tokens, check typ is at+jwt.
  • Scopes say what kind of action a client may take, not which objects. A token with orders:read must still pass a check that this partner is linked to this merchant. Missing it is broken object-level authorization.
  • API keys: a prefix with the environment (pk_live_), a public id, and 32 or more random bytes. Show once at creation, store a SHA-256 hash (fine for long random keys) next to the public id used for lookup, compare in constant time.
  • Send keys in a header, never in the URL, where it reaches logs and history. Add per-key rate limits and quotas, rotation with two active keys, and revocation that works within seconds (short cache).
  • After a leak through missing object checks: hotfix the check, size the leak from gateway and access logs, then inform security and legal teams, since Indian rules (CERT-In, DPDP Act) set reporting duties.

The red pen

Common traps: mistake → fix

  • Mistake: If the signature check fails, I can still read the claims

    Fix: Reject. Unverified claims are attacker input.

  • Mistake: An ID token can be sent to the API

    Fix: It is for the client. The API wants an access token for its audience.

  • Mistake: A valid scope is enough

    Fix: Scopes do not say which objects. Check ownership or links.

Easy to hard

Questions interviewers ask, easy to hard

  1. What must you check when verifying a JWT?
  2. Why is the audience check important?
  3. What is the difference between an ID token and an access token?
  4. How do you store and verify an API key?
  5. A partner token reads other merchants' orders. What was wrong and what do you do in the first hour?

Practise

Practise with these questions

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview