Online admissions open for the SEO, web development and networking & cybersecurity courses. Enquire for the next batch.

Authentication and Authorisation: MFA, Sessions, OAuth and RBAC

5 min read Updated By the SkillNewton team

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:

FactorMeaningExamples
Something you knowA secret in your memoryPassword, PIN
Something you haveA device or key you possessAuthenticator app, hardware security key, phone
Something you areA biometric characteristicFingerprint, 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.

AspectServer-side sessionSelf-contained token (JWT)
What the client holdsA random session ID in a cookieA signed token containing claims
Where state livesOn the server or in a shared storeInside the token
Logout and revocationDelete the session: immediateHard before expiry; needs short lifetimes or a deny list
Typical useTraditional web applicationsAPIs, 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.

  1. The application redirects the user to the authorisation server with its client ID, scopes, a state value and a PKCE code challenge.
  2. The user signs in there and approves the requested access.
  3. The authorisation server redirects back with a short-lived authorisation code.
  4. The application exchanges the code and its PKCE verifier for tokens over a back channel.
  5. 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/1043 to /invoices/1044 shows 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 role field 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.

  1. Create two ordinary user accounts and one admin account in the app.
  2. As user A, note the URL or API request for one of your own records.
  3. Log in as user B and replay that request; record whether user A's data is returned.
  4. Try opening each admin URL as user B.
  5. 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.

Want to Learn This Properly?

The Networking & Cybersecurity: Foundation & Practical Implementation (Online) turns guides like this into practised skills, with projects on real websites.

WhatsApp Call Us Now

Free counselling

Send an Enquiry

Tell us which course you are interested in. We reply by email or phone with the next batch and how to enrol.

Protected by reCAPTCHA. Google Privacy and Terms apply.

We reply by email or phone within working hours. No spam.