Skip to content
AI Engineer Guide.

concept · basics · intermediate · concept 6 of 24

AuthN vs AuthZ

0 of 24 complete

Why it matters

A system that knows who you are has not decided what you may see. The common failure is an endpoint that authenticates the caller and then trusts an identifier from the request, so changing a number returns someone else's data. Authorisation has to be evaluated against the specific object and the caller's current entitlements, which is a different code path from verifying a token and needs its own tests.

Mental model

Authentication is the passport check at the border; authorisation is the visa that says where you may travel and for how long. The analogy breaks because a visa is checked once, while permissions change during a session and must be re-evaluated for every object a request touches.

How it works

Mechanism, step by stepInteractive
  1. 1Separate the concerns in code: one middleware resolves identity, another resolves permission for a named action on a named resource.
  2. 2Model permissions as actions on resources, then map roles to permissions rather than checking role names inline.
  3. 3Enforce object-level checks in the data access layer, so a query cannot return rows the caller may not see.
  4. 4Deny by default and require an explicit allow for every route, including internal and admin endpoints.
  5. 5Keep the decision close to the data, and log it with subject, action, resource and outcome.
  6. 6Test with a matrix of identities against resources, including a second tenant and an expired entitlement.

Step 1 of 6

Separate the concerns in code: one middleware resolves identity, another resolves permission for a named action on a named resource.

Step 1/6 · step 1

Figure. The mechanism in this page's own steps. The figure adds sequence, not new claims.

What to notice. A step can be read alone, but the order is the mechanism. The control lets you stop on any one.

Text version

The page's steps: 1. Separate the concerns in code: one middleware resolves identity, another resolves permiss… 2. Model permissions as actions on resources, then map roles to permissions rather than chec… 3. Enforce object-level checks in the data access layer, so a query cannot return rows the c… 4. Deny by default and require an explicit allow for every route, including internal and adm… 5. Keep the decision close to the data, and log it with subject, action, resource and outcom… 6. Test with a matrix of identities against resources, including a second tenant and an expi…

  1. Separate the concerns in code: one middleware resolves identity, another resolves permission for a named action on a named resource.
  2. Model permissions as actions on resources, then map roles to permissions rather than checking role names inline.
  3. Enforce object-level checks in the data access layer, so a query cannot return rows the caller may not see.
  4. Deny by default and require an explicit allow for every route, including internal and admin endpoints.
  5. Keep the decision close to the data, and log it with subject, action, resource and outcome.
  6. Test with a matrix of identities against resources, including a second tenant and an expired entitlement.

Trade-offs

ChoiceGood forCostFails when
Role-based access controlSimple to reason about and auditRole explosion as the product growsPermissions depend on attributes or ownership
Attribute-based access controlFine-grained and contextualComplex policy and harder debuggingThe policy language is unmanaged
Permissions in the tokenNo lookup per requestStale until the token expiresRevocation must be immediate
Central policy serviceConsistent decisionsA dependency on the request pathThe latency budget is tight
Trade-off mapInteractive

Good for

Simple to reason about and audit

Cost

Role explosion as the product grows

Fails when

Permissions depend on attributes or ownership

  1. Permissions depend on attributes or ownership
  2. The policy language is unmanaged
  3. Revocation must be immediate
  4. The latency budget is tight

Figure. Each choice with what it buys, what it costs, and the case where it loses.

What to notice. The fourth column is the decision: if the losing case describes your situation, the choice is wrong.

Text version

Four trade-offs: Role-based access control is good for Simple to reason about and audit, costs Role explosion as the product grows, and fails when Permissions depend on attributes or ownership. Attribute-based access control is good for Fine-grained and contextual, costs Complex policy and harder debugging, and fails when The policy language is unmanaged. Permissions in the token is good for No lookup per request, costs Stale until the token expires, and fails when Revocation must be immediate. Central policy service is good for Consistent decisions, costs A dependency on the request path, and fails when The latency budget is tight

Failure modes

  • Insecure direct object reference. A caller changes an id and reads another tenant's record. Debug: run the endpoint with a second tenant's identifier and assert a not-found or forbidden response.
  • Role check instead of permission. A role name implies access to a resource it should not include. Debug: enumerate routes by required permission and diff against the role map.
  • Stale token permissions. A revoked user keeps access until the token expires. Debug: measure the window between revocation and rejection, and shorten it with a session check.

In production

Log every authorisation decision with subject, action and resource, and alert on a spike in denials for one subject, which often means a broken policy rather than an attack. Review role assignments on a schedule, not only at creation.

Common misconceptions

People think: Authentication is enough if the interface hides the button.

Actually: Hidden interface elements are not a control; the endpoint is reachable and must decide for itself.

People think: A signed token is an authorisation system.

Actually: A token carries claims; the service still has to decide whether those claims permit this action on this resource.

Interview angle

Keep the two checks separate and put the authorisation decision next to the data, with an object-level test. Explain why a token claim is not a decision. Naming a role and trusting a hidden interface element is the weak answer.

Sources