Authentication and Authorisation: MFA, Sessions, OAuth and RBAC
What is the difference between authentication and authorisation?
Authentication verifies who a user or system is, for example by checking a password and a one-time code. Authorisation decides what that verified identity is allowed to do, such as reading an invoice or deleting a user. Authentication always comes first, but both must be enforced on the server for every request.
Key terms and authentication factors
- Identity: the account or service being represented, such as a user ID.
- Credential: what proves the identity: a password, key, token or certificate.
- Principal: the authenticated identity a request runs as.
- Permission: a specific allowed action on a resource, such as
invoice:read.
Authentication factors fall into three categories:
| Factor | Meaning | Examples |
|---|---|---|
| Something you know | A secret in your memory | Password, PIN |
| Something you have | A device or key you possess | Authenticator app, hardware security key, phone |
| Something you are | A biometric characteristic | Fingerprint, face recognition |
Multi-factor authentication and passkeys
Multi-factor authentication (MFA) requires factors from at least two different categories. A password plus a security question is not MFA, because both are something you know. MFA stops most attacks that rely on stolen or reused passwords.
- SMS codes: better than nothing, but exposed to SIM swap and interception.
- Authenticator apps (TOTP, RFC 6238): generate a code from a shared secret and the current time; stronger, but a user can still type the code into a phishing page.
- Push approval: convenient, but vulnerable to "MFA fatigue" unless number matching is used.
- Passkeys and security keys (FIDO2/WebAuthn): the private key never leaves the device and is bound to the real website, so they are phishing-resistant.
Protecting the password step
- Allow long passphrases and check new passwords against lists of known breached passwords instead of forcing complex character rules.
- Do not force periodic password changes without a reason; require a change when a compromise is suspected.
- Slow down repeated failed logins with rate limiting, and alert on many failures across different accounts.
- Store passwords only as salted, slow hashes, never in a readable form.
Why phishing resistance matters is explained in our phishing guide.
Sessions vs tokens
HTTP is stateless, so after login the server needs a way to recognise later requests.
| Aspect | Server-side session | Self-contained token (JWT) |
|---|---|---|
| What the client holds | A random session ID in a cookie | A signed token containing claims |
| Where state lives | On the server or in a shared store | Inside the token |
| Logout and revocation | Delete the session: immediate | Hard before expiry; needs short lifetimes or a deny list |
| Typical use | Traditional web applications | APIs, mobile apps, service-to-service calls |
For session cookies, set the HttpOnly, Secure and SameSite attributes, and issue a new session ID at login to prevent session fixation. A JWT (RFC 7519) is signed, not encrypted by default, so never put secrets in it, and always verify its signature, expiry, issuer and audience.
<?php
// After the password and second factor are verified:
session_regenerate_id(true); // new ID, old session destroyed
$_SESSION['user_id'] = $user['id'];
$_SESSION['role'] = $user['role']; // loaded from the database, never from the request OAuth 2.0 vs OpenID Connect
OAuth 2.0 (RFC 6749) is an authorisation framework. It lets a user grant an application limited access to their data on another service, such as reading a calendar, without sharing their password. The application receives an access token with specific scopes. OAuth on its own does not tell the application who the user is.
OpenID Connect (OIDC) is an identity layer built on top of OAuth 2.0. When the application requests the openid scope, it also receives an ID token, a signed JWT stating who the user is and when they authenticated. "Sign in with Google" or Microsoft is OIDC.
- The application redirects the user to the authorisation server with its client ID, scopes, a
statevalue and a PKCE code challenge. - The user signs in there and approves the requested access.
- The authorisation server redirects back with a short-lived authorisation code.
- The application exchanges the code and its PKCE verifier for tokens over a back channel.
- The application validates the ID token and uses the access token to call the API.
Authorisation models: RBAC and least privilege
Role-based access control (RBAC) assigns permissions to roles, such as viewer, editor and admin, and roles to users. It is easy to audit. Attribute-based access control (ABAC) adds conditions, such as "managers can approve expenses for their own department". Most applications combine roles with ownership checks.
The principle of least privilege means every user, service account and process gets only the access it needs, for only as long as it needs it. Apply it to database accounts, cloud roles, Linux users and firewall rules alike, as described in network security fundamentals. Review access regularly and remove it promptly when people change roles or leave.
Broken access control: real examples
Broken access control is the first category in the OWASP Top 10. Common patterns include:
- Insecure direct object reference (IDOR): changing
/invoices/1043to/invoices/1044shows another customer's invoice. - Missing function-level checks: an admin page is hidden from the menu but its URL works for any logged-in user.
- Trusting the client: the role or price is read from a hidden form field or a cookie the user can edit.
- Privilege escalation: a user changes their own
rolefield through a profile update API.
<?php
// Vulnerable: any logged-in user can read any invoice
$stmt = $pdo->prepare('SELECT * FROM invoices WHERE id = ?');
$stmt->execute([$_GET['id']]);
// Fixed: the query also checks ownership, using the ID from the session
$stmt = $pdo->prepare('SELECT * FROM invoices WHERE id = ? AND user_id = ?');
$stmt->execute([$_GET['id'], $_SESSION['user_id']]);
$invoice = $stmt->fetch();
if (!$invoice) {
http_response_code(404); // do not reveal whether the invoice exists
exit;
} Practical exercise: audit access in a small app you own
Use a local application you wrote, or a deliberately vulnerable app such as OWASP Juice Shop running on your own machine.
- Create two ordinary user accounts and one admin account in the app.
- As user A, note the URL or API request for one of your own records.
- Log in as user B and replay that request; record whether user A's data is returned.
- Try opening each admin URL as user B.
- Fix each failure with a server-side ownership or role check, then repeat the tests.
You are done when every cross-user and admin request is refused and you have a table of each test, its result before and after the fix. Secure access design is part of the Networking Foundation course.
Common mistakes
- Hiding buttons in the interface instead of checking permissions on the server.
- Treating an OAuth access token as proof of the user's identity instead of validating an OIDC ID token.
- Keeping the same session ID before and after login.
- Storing sensitive data in a JWT payload, which anyone can decode.
- Giving service accounts administrator rights because it is quicker than defining the permissions they need.
Frequently asked questions
Is a fingerprint on my phone a second factor?
It releases a key stored on the device, so a passkey protected by a fingerprint combines something you have with something you are. The fingerprint itself never leaves the phone.
Should I store JWTs in localStorage?
It is readable by any script on the page, so a cross-site scripting flaw can steal it. Many teams prefer an HttpOnly, Secure cookie for browser apps, with CSRF protection.
What is single sign-on?
Single sign-on lets one login at an identity provider grant access to several applications, usually through OIDC or SAML. It centralises MFA and makes removing access easier when someone leaves.