Chapter 10: Authorization and Access Control
Authorization answers one question: can subject do action on resource, in context? Every model you will meet in this chapter (RBAC, ABAC, ReBAC, NGAC) is a different way to compute that boolean. The question looks simple, but it is really a language-recognition problem: given a request, decide whether it is in the set of permitted requests. How hard that is depends entirely on the structure of your permissions.
Concepts from other sections (7)
This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.
- Retrieval-augmented generation (RAG): retrieving relevant evidence at query time and placing it in the context window, so a model answers from fresh, private, citable facts instead of only its parametric memory. Read more: Chapter 27: Retrieval and RAG in AI Engineering
- API key: a credential
tl_<id>_<secret>whose public id locates a record and whose secret proves possession. Read more: 10.1 API Keys, Scopes, Model Allowlists, and Tenants - Model allowlist: a per-key list of models it may use; empty means all. Read more: 10.1 API Keys, Scopes, Model Allowlists, and Tenants
- RPM / TPM: requests per minute and tokens per minute, the two per-key budgets. Read more: 10.2 Rate Limiting: RPM Token Buckets and TPM Reserve/Settle
- AI gateway / LLM gateway: the single front door to a fleet of model servers, where authentication, rate limits, usage policy, caching, routing, and metering live, so the engines behind it stay simple and keyless; its defining constraint is forwarding a token stream without delaying the first token. Read more: Chapter 25: The AI Gateway in AI Engineering
- BM25: a ranking function that extends TF-IDF with term-frequency saturation () and document-length normalization (). Read more: Chapter 8: Search and Indexing in Systems Engineering
- HMAC: a keyed hash construction, , resistant to length extension. Read more: 10.1 API Keys, Scopes, Model Allowlists, and Tenants
If permissions are flat (this role, these actions), recognizing them is a finite lookup: a regular language, decidable by a DFA. The moment permissions become recursive (groups contain groups, folders contain folders, roles inherit roles, delegation chains), you can no longer decide membership with finite state. You need a stack to track how deep you have descended, which is the jump from a finite automaton to a pushdown automaton. That is why real authorization systems are graph-traversal engines, not permission tables, and recognizing it saves you from the classic mistake of bolting recursion onto a model that cannot express it.
The chapter has two halves. The first surveys the models on that complexity ladder, the architecture that separates deciding from enforcing, and the place where authorization meets retrieval: secure RAG. The second builds the gateway’s own access control in Go: per-client API keys with scopes, model allowlists, and tenants, and then per-key rate limits that keep one tenant from starving the rest.
Learning objectives
- Distinguish authentication from authorization and name the PEP, PDP, PAP, and PIP roles.
- Compare RBAC, ABAC, ReBAC, and NGAC, and place each on the complexity ladder from lookup to graph reachability.
- Explain why recursive permissions need a relationship graph and how a Zanzibar-style
checkworks.- Design permission-aware retrieval that filters during the search, not after it.
- Build API-key authentication, scope and allowlist authorization, and RPM/TPM rate limiting for an LLM gateway.
Before you begin
Section titled “Before you begin”The secure-RAG discussion assumes Retrieval & RAG, which explains why retrieval must be permission-aware. The architecture assumes some System Design from Systems Engineering, and the ReBAC material assumes graph reachability from Graphs in Programming and Algorithms. For the formal-languages side (the Chomsky hierarchy and pushdown automata), see Discrete Math 2 in Mathematics or Sipser. The two build sections plug into the gateway, so the server skeleton is a prerequisite for them.
The vocabulary
Section titled “The vocabulary”Start by separating two words that are constantly confused. Authentication (“authn”) is who are you: identity. Authorization (“authz”) is what may you do. This chapter is about authz, though its first build section does both, and the difference shows up directly as the difference between an HTTP 401 and a 403.
A request has a subject or principal (a user or a service), an action (read, write), a resource or object (a doc, a row, an endpoint), and a context (time, IP, device). The standard XACML architecture splits the machinery into four roles: the Policy Enforcement Point (PEP) intercepts requests and enforces decisions; the Policy Decision Point (PDP) evaluates policy; the Policy Administration Point (PAP) manages policy; and the Policy Information Point (PIP) supplies attributes. The key idea is to separate deciding from enforcing. Two principles apply everywhere: least privilege (grant the minimum) and deny by default (the absence of a grant is a denial).
RBAC: permissions through roles
Section titled “RBAC: permissions through roles”In role-based access control, users are assigned roles and roles carry permissions: user, then role, then permission. Add role hierarchies, where a senior role inherits a junior one, and you have NIST RBAC. It is simple, auditable, and matches org charts, which is why it is the industry default.
Its limit is role explosion. When you need per-resource or conditional access (“editors, but only for their own team’s docs in business hours”), you end up minting a role per combination. Through the complexity lens, flat RBAC is essentially a finite lookup, a regular language. Hierarchical RBAC introduces inheritance, recursion up the role tree, and quietly steps up the ladder.
ABAC: permissions through rules over attributes
Section titled “ABAC: permissions through rules over attributes”Attribute-based access control evaluates policies over attributes of the subject, resource, action, and context, for example allow if subject.dept == resource.dept and context.time in hours and resource.clearance <= subject.clearance. It is expressive, fine-grained, and context-aware, and it avoids role explosion because one rule covers many cases.
The price is auditability. Answering “who can access X?” requires evaluating the rules over all attributes, and you need a trustworthy attribute source, the PIP. XACML is the classic instance; modern policy engines such as OPA/Rego, Cedar, and Casbin are the current ones. A policy language is essentially how you express an ABAC decision function.
ReBAC and Zanzibar: permissions through a graph
Section titled “ReBAC and Zanzibar: permissions through a graph”Relationship-based access control stores relationship tuples of the form (object, relation, subject), such as (doc:1, viewer, group:eng#member), and answers check(user, relation, object) by traversing the relationship graph. This is Google’s Zanzibar, the model behind Drive, YouTube, and Cloud, with open-source implementations in OpenFGA and SpiceDB. It naturally expresses nesting and inheritance: groups of groups, folders of folders, “a viewer of the parent folder is a viewer of the child.” That is exactly the recursive structure RBAC and ABAC struggle with.
Here the pushdown insight becomes concrete. check() is graph reachability: a traversal that descends through nested groups and resource hierarchies, using a stack or frontier to remember where it is. You are recognizing membership in a recursively defined set, a context-free, pushdown-style computation, not a finite table lookup. It connects directly to graph BFS and DFS. At scale, Zanzibar adds consistency tokens (“zookies”) and caching so that a globally distributed authorization graph stays correct and fast, which is itself a distributed data and caching problem.
NGAC: NIST’s unifying policy graph
Section titled “NGAC: NIST’s unifying policy graph”NIST’s Next Generation Access Control (the “Policy Machine”) represents users, objects, and attributes as nodes in a directed policy graph with assignment and association edges. A decision asks whether a privilege is derivable by traversing that graph under the policy. It matters because it is general: RBAC and ABAC are special cases of NGAC. It unifies attributes (like ABAC) and relationship evaluation (like ReBAC) under one formalism, and like ReBAC its evaluation is graph reachability over a recursively structured policy, pushdown-style again. Reach for it when you need both attribute conditions and relationship inheritance, with formal, analyzable semantics.
The complexity ladder
Section titled “The complexity ladder”The four models line up by the structure of the permissions they handle:
| Model | Permission structure | Computation | Recognizes |
|---|---|---|---|
| Flat RBAC / ACL | role/user → permission | Lookup | Regular (DFA) |
| Hierarchical RBAC | + role inheritance | Bounded tree walk | (Steps up the ladder) |
| ABAC | rules over attributes | Evaluate a decision function | Depends on the language |
| ReBAC / NGAC | recursive relationship/policy graph | Graph reachability (stack-driven) | Context-free / pushdown |
The lesson is to pick the model whose native shape matches your permissions, not the one in fashion. If you find yourself adding “nested”, “inherited”, or “transitive” to an RBAC system, you have outgrown finite state; move to a relationship or graph model instead of piling up nesting hacks. It is the same “match the structure to the computation” instinct as choosing a data engine by access pattern.
Separating policy from enforcement
Section titled “Separating policy from enforcement”Whatever the model, keep the decision in one place. Externalized authorization runs a PDP as a library or service (an OPA sidecar, an OpenFGA or SpiceDB service), and applications call it at their PEPs. That gives one source of truth, testable policies, and a central audit, instead of if user.role == "admin" scattered across the codebase. Policy as code means policies are versioned, reviewed, and tested like any other code, in the spirit of Software Engineering. And decision logging records every allow and deny with its reason, for audit and debugging: the authorization analogue of observability.
Secure RAG: authorization applied to retrieval
Section titled “Secure RAG: authorization applied to retrieval”This is where the chapter meets Retrieval & RAG. The requirement is that a user may retrieve, and be grounded on, only content they are authorized to see. An LLM that quotes a document the user cannot access is a data breach, not a hallucination. Authorization is therefore part of retrieval, not a wrapper around it.
The pattern is to filter at retrieval time. Attach ACLs or permission labels to every chunk’s metadata at ingestion. At query time, resolve the user’s permissions with an RBAC, ABAC, or ReBAC check and pre-filter or constrain the vector and BM25 searches to authorized chunks. Prefer pre-filtering, or a permission-aware index: post-filtering after ranking leaks existence and ranking signal and skews results, and naive post-filtering can starve the top , the same “filter before the cut” rule the retrieval section enforces with its keep predicate.
Multi-tenancy is the coarsest permission filter: partition or namespace the index so one tenant can never retrieve another’s data (compare sharding). Isolate by tenant before you rank. And tools and agents must enforce authorization too: an agent acting on behalf of a user inherits that user’s permissions, not the service’s.
Choosing a model
Section titled “Choosing a model”The survey reduces to a lookup from situation to approach:
| Situation | Reach for |
|---|---|
| Simple, org-chart-shaped permissions | RBAC |
| Context/attribute conditions, no role explosion | ABAC (OPA/Rego, Cedar) |
| Nested groups, folder hierarchies, sharing | ReBAC (Zanzibar / OpenFGA / SpiceDB) |
| Need attributes and graph inheritance, formal | NGAC |
Scattered if role == checks | Externalize to a PDP |
| RAG over private/multi-tenant data | Permission-filter at retrieval (secure RAG) |
| “Why can this user see X?” | Decision logging + an auditable model |
Industry practice follows the same map. Google runs ReBAC at planetary scale with Zanzibar (Drive, YouTube, Cloud IAM). Auth0 and Okta combine RBAC with fine-grained ReBAC in FGA and OpenFGA. AWS evaluates ABAC policies in IAM and in Cedar and Verified Permissions. Airbnb and Carta use policy-as-code authorization with OPA/Rego and Oso. Glean and other enterprise AI products depend on permission-aware retrieval for secure RAG over corporate data. And NIST’s NGAC Policy Machine is the standard’s unifying policy-graph model.
To try the ideas yourself, model one application three ways (RBAC roles to permissions, ABAC attribute rules, ReBAC relationship graph) and note which questions each makes easy and which it makes painful. Implement a tiny Zanzibar-style check(user, relation, object) by graph traversal, then add nested groups and watch it become a reachability problem. Write one policy in OPA/Rego and the same policy as ReBAC tuples to feel the declarative versus relational trade-off. And wire permission filters into the RAG retriever so a query can return only authorized chunks.
In this chapter
Section titled “In this chapter”The build sections apply the simplest rung of the ladder, done carefully, to the LLM gateway. The route-to-scope table and per-key model allowlist are flat lookups, which is exactly right for their structure, and the tenant is the coarse isolation boundary that later stages, such as the response cache, rely on.
- API Keys, Scopes, Model Allowlists, and Tenants replaces the tracer’s shared static key with per-client
tl_<id>_<secret>keys stored as peppered HMACs, a short-TTL lookup cache that still honors revocation, and a pure authorization table that answers 403 for scope and allowlist failures. - Rate Limiting: RPM Token Buckets and TPM Reserve/Settle gives each key a requests-per-minute and a tokens-per-minute budget, reserving an estimated token cost before a request runs and settling the actual usage afterwards, with exact
Retry-Afterandx-ratelimit-*headers.
Key terms
Section titled “Key terms”- Authentication (authn): establishing who a caller is, its identity; a failure is an HTTP 401.
- Authorization (authz): deciding what an authenticated caller may do, whether a subject may perform an action on a resource in a context; a failure is an HTTP 403.
- Policy Enforcement Point (PEP): the component that intercepts a request and enforces an allow or deny, after asking a Policy Decision Point; keeping enforcement separate from deciding keeps policy in one place.
- Policy Decision Point (PDP): the component that evaluates policy and returns allow or deny for a request, fed attributes by a Policy Information Point (PIP) and managed through a Policy Administration Point (PAP).
- Least privilege: granting each subject the minimum permissions it needs, so a compromised or buggy caller can do as little damage as possible.
- Role-based access control (RBAC): users are assigned roles and roles carry permissions; simple, auditable, and the industry default, with optional role hierarchies where a senior role inherits a junior one.
- Role explosion: the failure of RBAC under per-resource or conditional rules, where a new role must be minted for every combination of conditions.
- Attribute-based access control (ABAC): authorization by policies evaluated over attributes of the subject, resource, action, and context (as in OPA/Rego or Cedar); expressive and fine-grained, but harder to audit.
- Relationship-based access control (ReBAC): authorization from stored relationship tuples
(object, relation, subject), answeringcheck(user, relation, object)by graph reachability; Google’s Zanzibar, OpenFGA, and SpiceDB use it because it expresses nested groups and folder inheritance naturally. - Next Generation Access Control (NGAC): NIST’s model of users, objects, and attributes as nodes in a directed policy graph, where a privilege holds if it is derivable by traversal; RBAC and ABAC are special cases of it.
- Externalized authorization: running the policy decision as a separate library or service (an OPA sidecar, OpenFGA, SpiceDB) that applications call, giving one source of truth, testable policies, and a central audit log.
- Policy as code: keeping authorization policies in version control, reviewed and tested like any other code.
- Secure RAG / permission-aware retrieval: retrieval that returns only chunks the user is authorized to see, by attaching permission labels at ingestion and filtering during the search; post-filtering after ranking leaks existence and can starve the top .
Further reading
Section titled “Further reading”- NIST, Role-Based Access Control (free).
- NIST, SP 800-162, Attribute Based Access Control (free).
- NIST, Next Generation Access Control (Policy Machine) (free).
- Pang et al., Zanzibar: Google’s Consistent, Global Authorization System (free).
- Open Policy Agent (OPA/Rego) (free) and OpenFGA (free).
- Casbin, Oso, and SpiceDB.