Difficulty: Intermediate
How would you design authorization for an API? Explain RBAC, OAuth scopes, and why object-level checks matter.
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.
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.
authorization, RBAC, scopes, BOLA