Authentication vs authorisation
35 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)A shop API receives a request to change a price. Before it does anything, it must ask who is calling and whether that caller is allowed. Mixing the two questions up is the root of most access bugs.
Authentication: proving who you are
Authentication is the check that proves a caller is who they claim to be. The caller shows a secret that only the real person should have, and the server checks it.
ShopLite must know who is calling before it trusts a request. First a request with no credentials is refused with 401, then one with a user name and password passes, then a real login returns a token.
Example 1 · Authentication fails, then succeeds
curl -i https://httpbin.org/basic-auth/asha/campus123curl -i -u asha:campus123 https://httpbin.org/basic-auth/asha/campus123HTTP/1.1 401 UNAUTHORIZED
Date: Fri, 16 Oct 2026 05:27:00 GMT
Content-Length: 0
Connection: keep-alive
Server: gunicorn/19.9.0
WWW-Authenticate: Basic realm="Fake Realm"
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
HTTP/1.1 200 OK
Date: Fri, 16 Oct 2026 05:27:03 GMT
Content-Type: application/json
Content-Length: 47
Connection: keep-alive
Server: gunicorn/19.9.0
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
{
"authenticated": true,
"user": "asha"
}What this code does
httpbin.orgis a public test service. This URL accepts only the user nameashawith the passwordcampus123. The first request sends no credentials, so the server answers 401 and itsWWW-Authenticateheader says how to log in.-u asha:campus123makes curl send the user name and password. Now the check passes:200 OKand the body names the principal,"user": "asha". The HTTP Basic & Sessions module shows exactly how curl packs the password into the request.
Example 2 · ShopLite login: a wrong password, then a token
Runs in: a demo of the secured ShopLite API on localhost:8080 (you build the real one later in this section)
curl -i http://localhost:8080/api/v1/auth/login -H "Content-Type: application/json" -d '{"email": "asha.rao@example.com", "password": "mango-kite-rain-2026"}'curl -i http://localhost:8080/api/v1/auth/login -H "Content-Type: application/json" -d '{"email": "asha.rao@example.com", "password": "Mango-Kite-Rain-2026"}'HTTP/1.1 401 Unauthorized
Connection: close
Content-Type: application/problem+json
Content-Length: 186
{
"type": "https://shoplite.example/problems/unauthorized",
"title": "Unauthorized",
"status": 401,
"detail": "Email or password is wrong.",
"instance": "/api/v1/auth/login"
}
HTTP/1.1 200 OK
Connection: close
Content-Type: application/json
Content-Length: 101
{
"accessToken": "7f3a9c2e5b8d41f0a6c3e9b2d5f8a1c4",
"tokenType": "Bearer",
"expiresIn": 900
}What this code does
- Passwords are case-sensitive, so the lower-case
mango-kite-rain-2026fails. The error says "Email or password is wrong" and never which one was wrong, so a stranger cannot use it to find out which emails have accounts. - The correct password returns an access token. From now on the client sends this token instead of the password;
expiresIn: 900means it stops working after 900 seconds (15 minutes).
The proof comes in three kinds: something you know (a password or a UPI PIN), something you have (a phone that receives a one-time code, or a token the server gave you) and something you are (a fingerprint or face). Using two kinds together, like a password plus an OTP, is called two-factor authentication.
For an API, authentication usually happens once at login. The server gives back a token: a long random string that the client sends with every later request instead of the password, to show it is the same person who logged in a few minutes ago.
Authorisation: deciding what you may do
Authorisation is the check that decides whether a known caller may do an action on some data. It runs only after authentication, because a rule like "only admins may change prices" needs to know who is asking.
ShopLite's rules: anyone may read the catalogue, signed-in customers place and read their own orders, and only admins change products. The same URL gets two different answers for two callers.
Example 3 · Same person, different permissions
Runs in: a demo of the secured ShopLite API on localhost:8080 (you build the real one later in this section)
TOKEN=7f3a9c2e5b8d41f0a6c3e9b2d5f8a1c4 # Asha, a customerADMIN_TOKEN=2d9f6a1c8e4b73f05a2c9e6b1d8f4a7c # the shop admincurl -i -X PATCH http://localhost:8080/api/v1/products/6 -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" -d '{"price": 1.00}'curl -i -X PATCH http://localhost:8080/api/v1/products/6 -H "Authorization: Bearer $ADMIN_TOKEN" -H "Content-Type: application/json" -d '{"price": 1199.00}'HTTP/1.1 403 Forbidden
Connection: close
Content-Type: application/problem+json
Content-Length: 190
{
"type": "https://shoplite.example/problems/forbidden",
"title": "Forbidden",
"status": 403,
"detail": "You do not have permission to do this.",
"instance": "/api/v1/products/6"
}
HTTP/1.1 200 OK
Connection: close
Content-Type: application/json
Content-Length: 175
{
"id": 6,
"sku": "ACC-AD141",
"name": "boAt Airdopes 141",
"categoryId": 4,
"categoryName": "Accessories",
"price": 1199.00,
"stock": 120,
"isActive": true
}What this code does
- Asha's token is valid, so authentication passes. Authorisation then fails: changing a price needs the ADMIN role, so the answer is
403 Forbiddenand the price stays ₹1,299. - The admin sends the same request with a different token and gets
200 OKwith the new price. Same URL, same body: only the principal's permissions changed the result.
Authorisation rules come from the business, not from the login. A correct password never means "may do anything": Asha can log in perfectly and still be refused when she tries to set a price, because her account is a customer account.
Order of execution
Order a protected request
Put the steps in the order the server runs them.
Two real pictures: an airport and a shop API
At an airport, the passport check at the entry gate is authentication: the officer compares your face with the photo and your name with the ticket. The boarding pass is authorisation: it lets you onto one flight and one seat, not into the cockpit and not onto another flight.
In a shop API, logging in is the passport check. After that, every request is like walking up to a gate with your boarding pass. Reading products needs no pass at all, reading your orders needs a customer pass, and changing a price needs an admin pass.
- Passport check = login with email and password: it proves who you are.
- Boarding pass = the access token sent with each request: it says what you may do.
- Public:
GET /api/v1/products/6works for everyone, signed in or not. - Admin only:
PATCH /api/v1/products/6(change a price) needs the ADMIN role.
Identity, credentials, principal and permissions
The principal is the caller the server has accepted after a successful check, for example "customer 1, Asha Rao". Its role, such as CUSTOMER or ADMIN, names the permissions it has.
One request, taken apart: the server turns the token it receives into a principal and a role, and only then decides.
Example 4 · Taking one request apart
PATCH /api/v1/products/6 HTTP/1.1Host: localhost:8080Authorization: Bearer 7f3a9c2e5b8d41f0a6c3e9b2d5f8a1c4Content-Type: application/json{"price": 1.00}Identity asha.rao@example.com (who she said she was at login) Credentials the token 7f3a9c2e...a1c4 (her proof on this request) Principal customer 1, Asha Rao (who the server now knows) Role CUSTOMER Permissions read own orders, place orders (nothing that changes products) Decision 403 Forbidden (authorisation failed)
What this code does
- The token is the credential. The server looks it up and finds the principal it was issued to, customer 1, together with her role.
- The role CUSTOMER does not include changing products, so the decision is 403. The same request with the admin token finds a principal whose role allows it.
An identity is who someone claims to be, such as the email asha.rao@example.com. Credentials are the proof for that claim: a password, a one-time code or a token. Permissions (also called authorities) are what the principal may do, and a role is a named group of permissions, so you give a person one role instead of twenty separate permissions.
Where each check happens: 401 vs 403
401 Unauthorized is the HTTP status for a request with no valid credential. Despite its name it means "not authenticated"; a valid login may fix it, and the response carries a WWW-Authenticate header.
Four callers hit one admin-only endpoint, then the two error responses are compared side by side.
Example 5 · Four callers, one admin endpoint
Runs in: a demo of the secured ShopLite API on localhost:8080 (you build the real one later in this section)
URL=http://localhost:8080/api/v1/customerscurl -s -o /dev/null -w "%{http_code}\n" $URLcurl -s -o /dev/null -w "%{http_code}\n" $URL -H "Authorization: Bearer 7f3a9c2e5b8d41f0a6c3e9b2d5f8a1c5"curl -s -o /dev/null -w "%{http_code}\n" $URL -H "Authorization: Bearer $TOKEN"curl -s -o /dev/null -w "%{http_code}\n" $URL -H "Authorization: Bearer $ADMIN_TOKEN"401 401 403 200
What this code does
-o /dev/nullthrows the body away and-w "%{http_code}\n"prints only the status code, which makes the four results easy to compare.- No token: 401. A token with its last character changed: still 401, because no valid credential means no known caller. Asha's real token: 403, she is known but the customer list is admin-only. The admin's token: 200.
Example 6 · Reading the difference in the headers
Runs in: a demo of the secured ShopLite API on localhost:8080 (you build the real one later in this section)
curl -i http://localhost:8080/api/v1/customerscurl -i http://localhost:8080/api/v1/customers -H "Authorization: Bearer $TOKEN"HTTP/1.1 401 Unauthorized
Connection: close
WWW-Authenticate: Bearer
Content-Type: application/problem+json
Content-Length: 187
{
"type": "https://shoplite.example/problems/unauthorized",
"title": "Unauthorized",
"status": 401,
"detail": "Sign in to use this endpoint.",
"instance": "/api/v1/customers"
}
HTTP/1.1 403 Forbidden
Connection: close
Content-Type: application/problem+json
Content-Length: 190
{
"type": "https://shoplite.example/problems/forbidden",
"title": "Forbidden",
"status": 403,
"detail": "You do not have permission to do this.",
"instance": "/api/v1/customers"
}What this code does
- The 401 carries
WWW-Authenticate: Bearer: "send me a bearer token and try again". The 403 has no such header, because a new login would not change the answer. - Both errors use the same Problem Details shape as every other ShopLite error, so a client can show
detailto the user in one place. A mobile app would open the login screen on 401 and show "not allowed" on 403.
403 Forbidden means "I know exactly who you are, and the answer is no". Logging in again with the same account will not help; only a different account or a change of permissions would. The name 401 is a historic mistake in the HTTP standard, which is why the two codes are so often mixed up.
A server checks every protected request in this order:
- Authentication: is there a credential, and is it valid? If not, stop with 401 Unauthorized.
- Authorisation: may this principal do this action on this data? If not, stop with 403 Forbidden.
- Only now does the endpoint run and answer 200, 201 or 204.
Predict the output
Which status code?
Read each request and pick the status the server sends.
Round 1 of 3
An admin-only endpoint, called with no token.
Next step
Check your understanding
12 quick questions