Course contents · Lesson 9 of 22

Authentication vs Authorization, Explained Simply

The Web and Staying Safe · Lesson 9 of 22 · 8 min Foundations

You sign in to your shop's back-office app, click "Approve refund" and see "You don't have permission". The app knows exactly who you are, and still says no. Two different checks ran. This lesson explains authentication vs authorization, how a site remembers you after login, and what the error codes 401 and 403 mean.

Two questions: who are you, and what may you do?

Authentication (often shortened to authn) answers "who are you?": you prove that you are the person an account belongs to. Authorization (authz) answers "what may you do?": the site's rules decide whether that person may open this page or take this action.

Authentication comes first, because the rules need to know who is asking. Public pages, such as a shop's home page, need neither check. In the diagram, notice that your ID is checked once, at the gate, while your band is checked at every door.

Concert diagram. At the gate a guard checks your ID and asks who are you: this is authentication. You then get one wristband whose colour is set by your ticket: green for general, purple for VIP, pink for crew. Staff check the band at every door, which is authorization: the general area lets in any band, the VIP lounge lets in a VIP or crew band, and backstage lets in the crew band only. Two cards below: no band at a door means back to the gate to show ID, like a 401; a band of the wrong colour means they know you, but no, like a 403
One ID check at the gate; after that, the band's colour decides every door.

Authentication: proving who you are

To prove who you are, you show something only the real account owner should have. Each kind of proof is a factor, and there are three kinds:

  • Something you know: a password or a PIN (personal identification number).
  • Something you have: your phone, which receives or makes a one-time code, or a security key you plug in or tap.
  • Something you are: your fingerprint or face, checked by your phone or laptop.

Multi-factor authentication (MFA) asks for two or more different kinds, such as a password plus a code from your phone, so a thief with your password still lacks your phone. Two passwords are still one factor: both are things you know.

The site checks a password by hashing what you typed and comparing it with the stored hash (lesson 7), and the password travels inside HTTPS (lesson 8).

Authorization: roles and permissions

Once the site knows who you are, it looks up what you may do. A permission is one allowed action, such as "view orders" or "approve refunds". Most systems group permissions into roles, such as staff or manager, and give each person one or more roles: role-based access control (RBAC).

Good rules follow least privilege: each person gets only the permissions their job needs, so a mistake or a stolen account can do less damage.

The server must check on every request. Hiding the "Approve refund" button from staff is a convenience, not protection: anyone can send the same request without the button, for example with curl.

How websites remember you after login

HTTP is stateless (lesson 6), so without help the site would need your password on every click. Instead, logging in hands you the website's version of a wristband: a cookie or a token, sent with every later request.

With a session cookie, a successful login makes the server create a session, a record on the server saying "this visitor is Asha", labelled with a long random session ID. The response hands that ID to your browser in a Set-Cookie header. On each later request, the server looks up the session to learn who you are, then checks your role.

Apps and scripts often send a token in the Authorization header, as the word Bearer and then the token: whoever bears, or holds, it is treated as you. Some tokens are random IDs; others carry your user ID, role and expiry time, signed by the site so that any change is detected.

Unlike a wristband, a cookie or token is only text: anyone who copies it can pass as you until it expires. So well-built sites send them only over HTTPS, make them expire, and end your session when you log out.

401 versus 403: "who are you?" or "no"

4xx status codes mean a problem with the request (lesson 6), and two of them match our two checks.

401 Unauthorized means the request lacks valid credentials, the proof of who you are: no cookie or token, or an expired one. Despite its name, it is about authentication. It comes with a WWW-Authenticate header naming how to log in. Many websites redirect your browser to a login page instead; APIs, the requests a server accepts from programs (lesson 4), answer with the 401 itself.

403 Forbidden means the server understood the request and refuses it, usually because your role lacks the permission. Logging in again with the same account changes nothing. Some sites answer 404 Not Found instead, so that people without access cannot even learn the page exists.

Notice in the flowchart that a 401 sends you back to log in, while a 403 is a final answer for that account.

Flowchart of a request for a protected page, which arrives with or without a cookie or token. First question, authentication: do we know who this is, from a valid cookie or token? No leads to 401 Unauthorized, we don't know who you are, log in first. Yes leads to the second question, authorization: is this user allowed to do it, by role and permissions? No leads to 403 Forbidden, we know you, but no, and the same login won't help. Yes leads to 200 OK, here is the page. A note says websites often send you to the login page instead of showing a 401. Another note says Asha, a staff member who tries to approve a refund, is known but not allowed, so gets 403. The server runs both checks on every request
401 means the first check failed; 403 means you passed it and failed the second.

Real example: who may approve a refund

Picture a shop's back-office app with two roles. Asha has the staff role, which can look up orders. Ravi has the manager role, which can also approve refunds. Both sign in with a password plus a phone code.

Asha signs in, so the app knows it is Asha, then sends a refund approval. The server finds the session, sees the staff role, finds no "approve refunds" permission and answers 403. Ravi's identical request gets 200 OK. Had Asha's session expired overnight, the click would get a 401, or the login page, instead. The table sums up both checks.

Table comparing authentication and authorization. Question: who are you, versus what may you do. What it checks: a password, a phone code or a fingerprint, versus roles and permissions. At the concert: the ID check at the gate, versus the wristband at each door. Example: Asha signs in with a password and a code from a phone, versus only managers may approve refunds. When it fails: 401 Unauthorized, log in first, versus 403 Forbidden, the same login won't help. A note says authentication comes first, because the rules need to know who is asking
Authentication answers who, authorization answers what, and each fails with its own code.

Check yourself

You can open your company's reports page, but the payroll page returns 403. Will logging out and back in fix it?

No. A 403 means authentication worked and authorization refused: your role lacks the permission. Only new permissions or a different account would change the answer.

A site asks for your password and then a second, different password. Is that multi-factor authentication?

No. Both are something you know, so a thief who learns both gets in. MFA needs different kinds, such as a password plus a code from your phone.

An app hides its "Delete user" button from ordinary users. Why is that not enough to stop them deleting users?

Anyone can send the delete request without the button, for example with curl. Only the server checking the user's role on every request makes it safe.