Authentication in practice
- API key: a fixed secret that identifies an app or partner, usually sent in a header such as
x-api-key. It says which app is calling, not which person. - Bearer token: a token you get after logging in and send on every request as
Authorization: Bearer <token>. Whoever holds (“bears”) it is treated as you. - JWT (JSON Web Token): a common token format with three dot-separated parts,
header.payload.signature. The payload is just base64url-encoded JSON, so anyone can decode it and read the user id, role and expiry time (exp). Decoding is not verifying: only the server, with its secret key, can check the signature. If you edit the payload, the signature no longer matches and the server must reject it. - Expiry: access tokens expire on purpose (often after 5–60 minutes). An expired token must get 401 on every endpoint.
401 vs 403 vs 404
| You send | Expected |
|---|---|
| No token, broken token, expired token | 401 Unauthorized (“who are you?”) |
| A valid token, but your role isn’t allowed | 403 Forbidden (“I know you, the answer is no”) |
| A valid token for someone else’s order | 404 (or 403), never their data |
AuthenticationAuthenticationChecking who a user is, usually with a password, a code or biometrics.Open in glossary → answers “who are you?”. APIs don’t have login screens, so every request has to carry proof. You’ll meet three kinds.
API keys
An API keyAPI keyA fixed secret string that identifies an app or partner calling an API, usually sent in a header.Open in glossary → is a long, fixed secret string issued to an application, a partner or a developer account, for example a restaurant’s point-of-sale system that pushes menu updates to FoodDash. It’s usually sent in a header:
GET /v1/partner/menus HTTP/1.1
Host: api.fooddash.test
x-api-key: test_pk_7Hq2...What to test:
- No key, a wrong key, and a revoked key all return 401.
- A key for partner A can’t change partner B’s menu (403 or 404).
- Keys are not accepted in the URL (
?api_key=...) if the docs say header only. URLs end up in logs and browser history. - The key never appears in responses, error messages or the mobile app’s public code (ask developers how it’s protected).
Bearer tokens
After a user logs in (for example POST /auth/login with email and password), the server returns an access token, sent as a bearer tokenBearer tokenAn access token sent as Authorization: Bearer <token>; whoever holds it is treated as the user.Open in glossary →. The app sends it on every request:
GET /v1/users/me HTTP/1.1
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ1XzEwMDEi...“Bearer” means whoever holds this token is treated as the owner. Like a concert ticket, nobody checks your face, they check the ticket. That’s why tokens are treated like passwords: never in screenshots, public tickets or shared collections without masking. Tokens can be opaque (a random string the server looks up in a database, much like a session tokenSession tokenA string the server issues after login that proves who you are on later requests.Open in glossary →) or self-contained, which usually means a JWT.
JWT: what’s inside
A JWTJWTJSON Web Token: a signed token with three base64url parts (header, payload, signature) that APIs use to identify the caller.Open in glossary → (JSON Web Token, say “jot”) is three base64url-encoded parts joined by dots:
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ1XzEwMDEiLCJyb2xlIjoiY3VzdG9tZXIiLCJleHAiOjE3OTIyMzA2MDB9.x3Fh9...Qe8
Header
Which algorithm signed it.
{ "alg": "HS256",
"typ": "JWT" }Payload (claims)
Who, what role, until when.
{ "sub": "u_1001",
"role": "customer",
"exp": 1792230600 }Signature
Made from the header, the payload and a secret key only the server knows (or a private key).
The payload holds claims. The standard ones you’ll see most:
| Claim | Meaning | Example |
|---|---|---|
sub | Subject: the user id | "u_1001" |
exp | Expires at (seconds since 1 Jan 1970, UTC) | 1792230600 |
iat | Issued at | 1792227000 |
iss / aud | Issuer / intended audience | "https://auth.fooddash.test" |
Apps add their own claims too, like role, name or scope.
How to decode one. Copy the token from Postman or from DevToolsDevToolsDeveloper tools built into browsers for inspecting pages, network traffic and errors.Open in glossary → › Network › a request › Headers › Authorization. Then paste it into a JWT decoder. The sandbox below has one built in. Online decoders such as jwt.io decode in your browser, but company policy may still forbid pasting tokens into external sites, so use test tokens only and ask first. Things to look at:
- Is
expthere, and is the lifetime what the team agreed (15 minutes, not 15 years)? - Is the
roleorscoperight for this test user? - Is there anything that shouldn’t be readable by the user, such as an email of another person, an internal flag or a password? A JWT is encoded, not encrypted. Anyone holding it can read it.
Token expiry and refresh
Access tokens are short-lived so a stolen token stops working soon. To avoid logging users out every 15 minutes, many apps also get a refresh tokenRefresh tokenA longer-lived token used only to get a new access token when the old one expires.Open in glossary →: a longer-lived token used only to ask the auth server for a new access token (POST /auth/refresh).
- Expired access token → 401. Use an expired token (ask developers for a test token with a short lifetime, or wait). Every protected endpoint must return 401, not just most of them. Check the response: per the HTTP spec a 401 comes with a
WWW-Authenticateheader, often witherror="invalid_token". - The app refreshes quietly. In the app, wait until the access token expires, then tap something. The app should get a new token in the background and retry once. The user shouldn’t see an error or get logged out (watch it in a proxy tool or DevTools).
- Refresh after logout fails. Log out, then try the old refresh token. Expected: 401. Logout must revoke it on the server, not just delete it on the device.
- Refresh token rotation. If the team uses rotation, each refresh returns a new refresh token and the old one stops working. Try the old one again.
- Password change. After changing the password on another device, do old tokens stop working (now, or when the access token expires)? Ask what the requirement is.
401 vs 403, tested properly
| Test | Request | Expected |
|---|---|---|
| No credentials | No Authorization header | 401 + WWW-Authenticate |
| Malformed | Authorization: Bearer abc, or Token xyz | 401 |
| Expired | A token whose exp is in the past | 401, on every endpoint |
| Tampered | Edited payload, old signature | 401 |
| Valid, wrong role | Customer token on GET /admin/users | 403 |
| Valid, not the owner | A’s token on B’s order | 404 (or 403, see below) |
Repeating the request with the same credentials won’t fix a 403. A 401 means “authenticate (again)”, so apps usually react by refreshing the token or sending the user to the login screen. If the app logs users out on a 403, that’s a client bug worth reporting.
Check this part
You decode a JWT and change
"role": "customer"to"admin", keeping the old signature. What should the API return when you call an admin endpoint?A valid customer token calls an endpoint that only admins may use. What is the expected status?
Who can read the payload of a JWT?