# Authorization: RBAC, scopes and object-level checks

> How would you design authorization for an API? Explain RBAC, OAuth scopes, and why object-level checks matter.

Canonical: https://www.internhack.in/learn/interview/api-design-interview/api-authorization-rbac-scopes
Last updated: 2026-11-28

Difficulty: Intermediate

## Question

How would you design authorization for an API? Explain RBAC, OAuth scopes, and why object-level checks matter.

## Answer

Authentication asks who are you; authorization asks what are you allowed to do. A useful analogy is an office building: your badge proves you work here (authentication), but which floors and rooms it opens depends on your role and on what has been granted (authorization). Most serious API breaches are not failed logins but broken authorization, which is why OWASP lists Broken Object Level Authorization as number one in its API Security Top 10.

Role-based access control, RBAC, assigns permissions to roles and roles to users. Example roles: customer, restaurant_owner, delivery_partner, admin. A role like admin maps to a set of permissions such as orders:read_all and refunds:create. RBAC is easy to reason about and audit, and fits most products. Limits: role explosion when you need finer distinctions, and inability to express context like 'only their own restaurant's orders'. Attribute-based access control, ABAC, evaluates attributes of user, resource and environment, such as department, owner id, time or IP, and relationship-based models like Google Zanzibar handle sharing graphs. Policy engines such as OPA or Cedar externalise these rules.

OAuth scopes are different: they limit what a specific token may do on behalf of a user, at the app grant level. A user can be an admin, but a token issued to a third-party analytics app with only the orders:read scope must not delete anything. Effective permission is the intersection of the user's permissions and the token's scopes. Scope naming is usually resource:action, such as orders:read and orders:write, and least privilege means requesting only what you need.

Object-level authorization is the check that people forget. Having role customer and scope orders:read does not entitle you to GET /orders/9001 unless order 9001 belongs to you. If the handler only checks that you are logged in and then fetches by id, an attacker just increments the id, an IDOR or BOLA attack. Always scope queries by ownership: WHERE id = :id AND user_id = :currentUser, and return 404 if nothing matches so as not to confirm existence. Do not rely on unguessable UUIDs as your authorization; they are defence in depth only.

Related vulnerabilities: function-level authorization, where a normal user can call an admin endpoint because only the UI hides the button; and property-level authorization or mass assignment, where a user can send role=admin in a PATCH body or see internal fields such as costPrice in a response. Use explicit allowlists for both input and output fields.

Implementation advice: centralise checks in middleware or a policy layer rather than scattering if statements, deny by default, keep permission checks on the server and never trust client-sent roles, and make claims in tokens minimal, because roles in a JWT go stale until it expires. Log authorization denials for auditing, and write tests that try cross-tenant access, since multi-tenant leaks are the worst class of bug.

Status codes: 401 when unauthenticated, 403 when authenticated but lacking permission, and 404 when hiding existence. Interviewers like to hear that you choose between 403 and 404 intentionally.

## Code examples

### Scope middleware plus ownership check

```
const requireScope = (scope) => (req, res, next) =>
  req.auth.scope.split(' ').includes(scope)
    ? next()
    : res.status(403).json({ title: 'Insufficient scope', required: scope });

app.get('/v1/orders/:id', requireScope('orders:read'), async (req, res) => {
  const order = await db.order.findFirst({
    where: { id: req.params.id, userId: req.auth.sub } // object-level check
  });
  if (!order) return res.status(404).json({ title: 'Order not found' });
  res.json(order);
});
```

Scope gates the endpoint, and the query is constrained to the caller so other users' orders are indistinguishable from missing ones.

## Key points

- RBAC maps users to roles to permissions; ABAC adds context attributes.
- Scopes limit a token; effective access is the intersection with user permissions.
- Object-level checks prevent IDOR/BOLA, the top API vulnerability.
- Guard against mass assignment with allowlists.
- Deny by default and centralise policy.

## Common follow-up questions

- How would you model multi-tenant authorization?
- Where should roles live, in the token or in the database?
- How do you test for BOLA?

## Interview tips

- Use the words BOLA/IDOR and give the ownership query as a fix.
- Separate scopes from roles clearly.

## Concepts covered

authorization, RBAC, scopes, BOLA

Previous: [Access tokens vs refresh tokens and token lifecycle](https://www.internhack.in/learn/interview/api-design-interview/api-access-refresh-tokens) · Next: [CORS: what it is and how preflight works](https://www.internhack.in/learn/interview/api-design-interview/api-cors) · [All of API Design](https://www.internhack.in/learn/interview/api-design-interview)

## More API Design questions

- [HTTP caching: Cache-Control, ETag and conditional requests](https://www.internhack.in/learn/interview/api-design-interview/api-caching-etag)
- [Preventing lost updates: optimistic concurrency with ETag and If-Match](https://www.internhack.in/learn/interview/api-design-interview/api-optimistic-concurrency)
- [REST vs GraphQL vs gRPC: when to pick each](https://www.internhack.in/learn/interview/api-design-interview/api-rest-vs-graphql-vs-grpc)
- [GraphQL N+1 problem and DataLoader batching](https://www.internhack.in/learn/interview/api-design-interview/api-graphql-n-plus-one-dataloader)
- [Designing webhooks: retries, signatures and idempotency](https://www.internhack.in/learn/interview/api-design-interview/api-webhooks-design)
- [Long-running operations: 202 Accepted and polling](https://www.internhack.in/learn/interview/api-design-interview/api-long-running-operations)

## Related

- [Learn](https://www.internhack.in/learn)
- [Interview Prep](https://www.internhack.in/learn/interview)
- [API Design](https://www.internhack.in/learn/interview/api-design-interview)
