Skip to content
learn
  1. Learn
  2. High Level System Design
  3. Security in System Design

System Design HLD-16.01

Authentication vs authorization

Who you are versus what you may do: sessions and tokens, role-based and attribute-based access control, and least privilege in every service.

Beginner 10 min Last reviewed

The big idea

Two questions sit in front of every protected request, and confusing them causes real breaches. Who is this? and Are they allowed to do this? AuthenticationVerifying who a user or service is, for example with a password, passkey, certificate or single sign-on. (often shortened to authN) answers the first: it checks a password, a passkey, a certificate or a login at a trusted identity provider. AuthorizationDeciding what an authenticated user or service is allowed to do, such as reading a document or deleting an account. (authZ) answers the second: given that this is Ana, may she edit invoice 42?

Many incidents come from systems that authenticate carefully and then authorize sloppily: any logged-in user can read any other user's order by changing a number in the URL. Authentication is mostly a solved problem you should not build yourself; authorization is specific to your domain and must be checked on every request, in every service, for every object.

Think of an office building. The badge at the front door proves who you are (authentication). Whether your badge opens the server room or the finance office depends on your role and the room's rules (authorization). Being inside the building does not mean you can open every door.

How it works

Step 1: Two gates on every request

A request passing an authentication gate and then an authorization gate A request carrying a token arrives at an authentication gate, which verifies the token and establishes the identity Ana, glowing. It then reaches an authorization gate, which checks whether Ana may perform edit on invoice 42 using roles and attributes, also glowing, and finally the request reaches the invoice service. Failure at the first gate returns 401, failure at the second returns 403. Request token: eyJ... AuthN who? Ana (verified) AuthZ may Ana edit invoice 42? Invoice service 401 Unauthorized no valid identity 403 Forbidden identity ok, not allowed Figure 1. First prove who the caller is (fail: 401 Unauthorized), then decide whether that caller may perform this action on this resource (fail: 403 Forbidden). Only then does the request reach the business logic.

HTTP even has separate status codes for the two failures: 401 Unauthorized really means "not authenticated", and 403 Forbidden means "authenticated, but not allowed".

Step 2: Sessions and tokens

After a user logs in, the system must remember who they are on later requests:

  • Server-side sessions: the server stores session data and gives the browser a random session id in a cookie (marked HttpOnly and Secure). Easy to revoke (delete the session), but every request needs a session lookup in a shared store ( Stateless vs stateful services ).
  • Tokens: the server issues a signed token, often a JWT, containing the user id and some claims. Any service can verify the signature without a lookup, which suits distributed systems, but a token stays valid until it expires, so tokens are kept short-lived ( OAuth 2.0, OpenID Connect and JWT ).

Delegate login itself to a proven identity provider or single sign-on (SSO) rather than storing passwords yourself; if you must store passwords, use a slow, salted password hash such as bcrypt, scrypt or Argon2, and support multi-factor authentication or passkeys.

Step 3: RBAC and ABAC

access.pyruns with python3 (standard library)
# Authentication happened earlier: we already know WHO the caller is.
users = {"ana": {"roles": {"editor"}, "dept": "sales", "region": "eu"},
         "raj": {"roles": {"viewer"}, "dept": "sales", "region": "us"}}

# RBAC: permissions come from roles.
ROLE_PERMS = {"viewer": {"doc:read"}, "editor": {"doc:read", "doc:write"}, "admin": {"doc:read", "doc:write", "doc:delete"}}
def rbac(user, action):
    return any(action in ROLE_PERMS[r] for r in users[user]["roles"])

# ABAC: a rule over attributes of the user, the resource and the context.
def abac(user, action, doc, hour):
    u = users[user]
    return (rbac(user, action)                        # still needs the base permission
            and u["dept"] == doc["dept"]              # same department only
            and u["region"] == doc["region"]          # data residency rule
            and (action == "doc:read" or 8 <= hour < 20))   # writes only in working hours

doc = {"id": 42, "dept": "sales", "region": "eu"}
for user, action, hour in [("ana", "doc:write", 10), ("ana", "doc:write", 23), ("raj", "doc:read", 10), ("raj", "doc:write", 10)]:
    print(f"{user} {action:<9} at {hour:>2}h  RBAC {'allow' if rbac(user, action) else 'deny':<5}  ABAC {'allow' if abac(user, action, doc, hour) else 'deny'}")

Output

ana doc:write at 10h  RBAC allow  ABAC allow
ana doc:write at 23h  RBAC allow  ABAC deny
raj doc:read  at 10h  RBAC allow  ABAC deny
raj doc:write at 10h  RBAC deny   ABAC deny
  • Role-based access control (RBAC)Role-based access control: users are assigned roles such as viewer or admin, and each role grants a set of permissions. grants permissions through roles: viewers read, editors read and write, admins delete. Simple to understand and audit, and enough for many products.
  • Attribute-based access control (ABAC)Attribute-based access control: access decisions evaluate rules over attributes of the user, the resource, the action and the context, such as department, region or time. evaluates rules over attributes of the user, the resource and the context: same department, same region for data residency, writes only during working hours. More expressive, harder to reason about and audit.

In the sample, Ana's role allows editing, but ABAC refuses a late-night write; Raj can read by role, but not a document from another region. Many systems combine them: roles for coarse permissions, attribute rules or per-object ownership checks for the fine-grained ones.

Step 4: Least privilege, everywhere

Grant every user, service and credential only the permissions it needs, for only as long as it needs them. Check authorization on every request at the service that owns the data, not just at the edge, and always check access to the specific object ("does this invoice belong to this user's company?"), not only the type of action. Services calling each other need identities and permissions too ( Service mesh ).

Trade-offs

OptionWins whenLoses when
Server-side sessions Web apps needing instant logout and revocation A session store lookup on every request
Short-lived signed tokens Distributed services verifying identity without a lookup Revocation before expiry needs extra machinery
RBAC Clear, auditable permissions for most products Role explosion when rules depend on context
ABAC / policy engines Fine-grained rules over attributes and context Complexity; harder to audit who can do what

Common mistakes

  • Checking that a user is logged in but not that they may access this specific object.
  • Authorizing only at the API gateway and trusting every internal call.
  • Building your own password storage or login flows instead of using a proven provider.
  • Giving services and users broad admin permissions "to make it work".
  • Returning 404 versus 403 inconsistently, leaking which resources exist.

In the wild

Identity providers such as Okta, Microsoft Entra ID, Auth0 and Google provide authentication and single sign-on for many companies, while authorization often lives in application code, in policy engines such as Open Policy Agent, or in relationship-based systems inspired by Google's Zanzibar paper. Broken object-level authorization, letting users access other users' objects by changing an id, is consistently ranked among the top API security risks by OWASP.

Check yourself

  1. A logged-in user requests another customer's invoice by changing the id in the URL and gets it. What failed?

    Show answer

    B. Authorization: the object-level access check was missing The user was correctly identified; the system never checked whether that user may see that invoice.

  2. Which status code means 'authenticated, but not allowed'?

    Show answer

    B. 403 401 means no valid identity; 403 means the identity is known but lacks permission.

  3. A rule says 'editors may only change documents in their own region during working hours'. Which model expresses it naturally?

    Show answer

    B. ABAC It depends on attributes of the user, resource and context, not just a role.

  4. What does least privilege mean for a service account?

    Show answer

    B. It holds only the permissions it needs, for as long as it needs them Minimal permissions limit what an attacker can do with a compromised credential.

Recap

  • Authentication proves who the caller is; authorization decides what they may do.
  • 401 means unauthenticated, 403 means authenticated but forbidden.
  • Sessions are easy to revoke; signed tokens avoid lookups but must be short-lived.
  • RBAC grants permissions through roles; ABAC evaluates rules over attributes and context.
  • Apply least privilege and check object-level access in every service, on every request.

Get new lessons by email

Get new posts on backend and AI internals. No spam, unsubscribe anytime.

Your email is only used to send new posts. Privacy