Notes
Two questions
There are two separate questions. Authentication asks: who are you? If the API cannot tell, it answers four oh one, unauthorized, despite the confusing name. Authorisation asks: may you do this? If you are known but not allowed, the answer is four oh three, forbidden, or sometimes 404, to hide that the thing even exists, which is what ShopRung does for other customers' orders. Test both questions, separately.
- authentication (authN) who are you? wrong → 401 Unauthorized
- authorisation (authZ) may you do THIS? wrong → 403 Forbidden (or 404 to hide that it exists: ShopRung ORD-7)
- test both, separately
The options
APIs identify callers in a few common ways. An API key: a secret string sent in a header, usually for one application calling another. Basic authentication: a user name and password in a header, acceptable only over HTTPS. A session cookie: the browser logs in and the server remembers the session. A bearer token: an authorization header with the word bearer and a token, very often a JSON web token. And OAuth two, a standard way to obtain such tokens.
- API key: a secret string in a header, for apps and services
- basic auth: user:password in a header (only over HTTPS)
- session cookie: browser logs in, server remembers
- bearer token: "Authorization: Bearer <token>", often a JWT
- OAuth 2.0: a standard way to GET those tokens
ShopRung login
ShopRung uses bearer tokens. Post Alice's email and password to the login endpoint, and you get back a token and expires in, seven thousand two hundred seconds, which is two hours. From then on, every protected call carries an authorization header: the word bearer, a space, and the token. Get the cart with it: two hundred. Without it: four oh one, log in first. In tests, log in once per test or per user, through the API, never through the login page.
POST /api/auth/login
{"email":"alice@shoprung.test",
"password":"Alice123!"}
→ 200 {"token":"eyJhbGciOi...",
"expiresIn":7200, ...}
GET /api/cart
Authorization: Bearer eyJhbGci...
→ 200 { "items": [...] }
no header → 401 unauthorized Inside a JWT
A JSON web token has three parts separated by dots: a header, a payload and a signature, each base64 url encoded. Decode ShopRung's and the header says the algorithm, H S two five six. The payload holds claims: the subject, user one, Alice's email, her role, customer, when it was issued and when it expires. Anyone can decode this, so a token must never contain secrets. Its safety comes from the signature, which only the server can create.
eyJhbGciOiJIUzI1NiIs... .
eyJzdWIiOiIxIiwiZW1h... .
kH3n8... (signature)
header {"alg":"HS256",
"typ":"JWT"}
payload {"sub":"1","email":
"alice@shoprung.test","role":
"CUSTOMER","iat":...,"exp":...}
readable by anyone: not secret! Token tests
Every protected endpoint needs negative token tests. No header: four oh one. A garbage token: four oh one. Change one character of a real token: four oh one, the token is not valid, because the signature no longer matches. An expired token: four oh one, token expired. Edit the payload to make yourself an admin: the signature fails, four oh one. The answer must never be two hundred, and never a five hundred crash.
no header 401 unauthorized "Bearer abc" 401 invalid_token change 1 char 401 invalid_token "The token is not valid" expired (>2h) 401 token_expired user deleted 401 invalid_token role edited in payload → signature fails → 401 never 200, never 500
OAuth 2.0
OAuth two is a standard for getting tokens. Two flows are worth knowing. Authorization code with PKCE: the user logs in on the provider's page, like sign in with Google, the app receives a short code and swaps it for tokens; used by web and mobile apps. Client credentials: one service calls another with a client id and secret and gets a token, with no user involved. In automated tests, use client credentials or a dedicated test user, not the login screen.
- authorization code + PKCE a user logs in on the provider's page, the app gets a code, swaps it for tokens (web and mobile apps)
- client credentials service to service: client id + secret → token
- tests: use client credentials or a test user, not the UI
Secrets in tests
Handle test credentials carefully too. Never commit passwords, keys or tokens to Git; keep them in CI secret variables or a local dot env file that Git ignores. Use dedicated test accounts with the least privilege they need. Mask tokens in logs and test reports, because reports are often shared widely. And if a secret leaks, rotate it. ShopRung's practice passwords are public on purpose; real ones never are.
- never commit passwords, keys or tokens to Git
- CI secret variables or a local .env (git-ignored)
- dedicated test accounts with least privilege
- mask tokens in logs and reports
- rotate anything that leaked
Common mistakes
Common gaps. Testing everything with a valid admin token, so authorisation bugs never show. Never using a second user. Logging in through the UI in every test, which is slow. Secrets in code or in reports. Never testing expiry. And treating 404 and four oh three as interchangeable: check which one your API is supposed to return, because the choice is often deliberate, as with ShopRung's privacy rule.
- only testing with a valid admin token
- never testing a second user
- logging in via the UI in every test
- secrets in code or reports
- not testing expiry
- treating 404 vs 403 as "close enough" without checking the agreed rule
Say it like this
In the interview: I separate authentication and authorisation. For authentication: missing, malformed, tampered and expired tokens must all get four oh one, never data and never a crash. For authorisation: each role can do only its own actions, otherwise four oh three, and a second user can never read or change the first user's data. I get tokens through the API, keep secrets in CI variables, and mask them in logs.
- "I separate authN and authZ.
- AuthN: missing, malformed, tampered and expired tokens
- all give 401. AuthZ: each role
- can only do its actions (403), and a second user can never
- read or change the first
- user's data. I get tokens via the API, keep secrets in CI
- variables, and mask logs."