Fine-grained authorization
Nod answers "may this user do this to that object?" from relationships you write: Zanzibar-style authorization, compatible with OpenFGA's modeling language (the one Auth0 FGA uses). Each realm has one authorization model and its own relationship tuples.
The model
model
schema 1.1
type user
type group
relations
define member: [user, group#member]
type folder
relations
define viewer: [user, group#member]
type document
relations
define parent: [folder]
define owner: [user]
define editor: [user, group#member] or owner
define viewer: ([user, user:*, group#member] or editor or viewer from parent) but not blocked
define blocked: [user]
| Construct | Means |
|---|---|
[user, user:*, group#member] | Who may be written directly: a user, every user, members of a group |
owner | Whoever has another relation on the same object |
viewer from parent | Whoever has viewer on the object's parent |
a or b, a and b, a but not b | Union, intersection, exclusion. Mixing or and and needs parentheses |
Modules
A model can be written in modules, as OpenFGA's modular models (schema 1.2), so each product or team keeps its own part. Put the module files one after another in the one text you PUT (in any order), each starting with module <name>:
module core
type user
type organization
relations
define member: [user]
define admin: [user]
module issue-tracker
extend type organization
relations
define can_create_project: admin
type project
relations
define organization: [organization]
define viewer: member from organization
extend type organizationadds relations to a type another module defines; the type may come later in the text.- A type is defined once, and a relation once on its type, whichever module adds it. Each refusal names the line and the modules.
- Every type and condition is in a module; a
model/schema 1.2header first is allowed, andschema 1.1with modules (orschema 1.2without) is refused. - The model is kept as written, and
GET …/fga/modelgives the text back; the composed model answers as the same model written whole.
Conditions
A relationship can hold only under a condition, a CEL expression over typed parameters, as in OpenFGA:
type document
relations
define viewer: [user, user with office_hours, user with from_office]
condition office_hours(hour: int, until: int) {
hour >= 9 && hour < until
}
condition from_office(ip: ipaddress) {
ip.in_cidr("10.0.0.0/8")
}
Write a conditional tuple with the condition's name, and optionally some of its parameters:
{ "object": "document:plan", "relation": "viewer", "user": "user:alice",
"condition": { "name": "office_hours", "context": { "until": 17 } } }
and pass the rest in the request's context:
{ "object": "document:plan", "relation": "viewer", "user": "user:alice", "context": { "hour": 10 } }
- Parameter types:
int,uint,double,bool,string,timestamp,duration,ipaddress,list<T>,map<T>andany. Values are checked against them, never coerced. - The tuple's values win: a parameter set when the tuple was written can't be overridden by the caller's
context. - Missing context is an error (
400 invalid_context), not a "no", when the answer depends on it. Check and ListObjects evaluate a condition only when its tuple would grant access, so context for other people's tuples isn't needed. ListUsers expands every tuple, so it needs every condition decidable. - Exclusions stay safe: if a
but notdepends on a condition that can't be decided, the answer is an error, never access. - Inline conditions:
[user with $expression]lets each tuple carry its own expression,"condition": {"name": "$expression", "context": {"expression": "dept == 'eng'", "parameters": {"dept": "string"}}}. Undeclared parameters areany.
Through the management API
Grant a machine client the fga: scopes it needs (Management API): fga:admin sets the model, fga:write writes tuples, fga:read reads them, and fga:check checks.
curl -X PUT $ISSUER/api/v1/fga/model -H "Authorization: Bearer $T" --data-binary @model.fga
curl -X POST $ISSUER/api/v1/fga/write -H "Authorization: Bearer $T" -H 'content-type: application/json' \
-d '{"writes":[{"object":"document:roadmap","relation":"viewer","user":"group:eng#member"},
{"object":"group:eng","relation":"member","user":"user:alice"}]}'
curl -X POST $ISSUER/api/v1/fga/check -H "Authorization: Bearer $T" -H 'content-type: application/json' \
-d '{"object":"document:roadmap","relation":"viewer","user":"user:alice"}'
# {"allowed":true}
- Writes are atomic, up to 100 tuples. Each must be allowed by the model. Writing a tuple that exists, or deleting one that doesn't, fails the whole write.
- A check sees one consistent snapshot.
contextual_tuplescount for that check only and are never stored. - Cycles in the relationships count as no access. A check that needs more than 25 steps is refused (
too_complex). - Changing the model keeps existing tuples. Those the new model no longer allows are ignored by checks.
- Many checks at once:
POST …/fga/batch-checkwith{"checks": [{"correlation_id", "object", "relation", "user", "contextual_tuples"?, "context"?}]}, up to 1,000, answers{"result": {"<id>": {"allowed": true}}}, or{"error": {"code", "message"}}for a check that is itself wrong, without failing the rest. Ids must be present and distinct. As in OpenFGA, the checks are independent: they're read 50 to a snapshot, a few snapshots at a time, so a page of 1,000 records is checked in one call (under a second on TwilightDB).
Listing
POST $ISSUER/api/v1/fga/list-objectswith{"type", "relation", "user"}answers which objects of that type the user has the relation to.POST …/fga/list-userswith{"object", "relation", "filters": ["user", "group#member"]}answers who has it: named users,user:*when everyone of the type does, and usersets.- Both return at most 1,000, sorted, and take
contextual_tupleslike Check does. - Not a query filter: list-objects checks every object of the type in turn, so its cost grows with the type, and it stops at 1,000. To filter a page of records, batch-check them.
- list-users follows the model back from the object (its tuples, usersets and their members, its other relations, its parents through a tupleset), so its cost grows with who's connected to that object, not with the realm; each one found is then checked. When a wildcard of the filtered type is in reach (anyone could have it), it checks every user of the type, as before.
How we know it's right
OpenFGA's own consolidated test suite (Apache-2.0) is in crates/fga/tests/data. On memory, PostgreSQL and TwilightDB, all 997 of its assertions pass: every Check, ListObjects and ListUsers one. Any failure fails the build.
OpenFGA's conditions suite (abac_tests.yaml, also Apache-2.0) runs the same way: all 335 of its assertions pass on all three stores.
ListUsers with wildcards follows OpenFGA's set expansion, not "everyone who passes Check". user:* is listed when a wildcard grants the relation. A named user is listed beside it only when a tuple path names them: through a union, a named branch; through an intersection, every operand admits them (a wildcard may) and one names them; through an exclusion, the base names them, or the excluded side names them without excluding them. So a public document whose other parent is a group lists user:* and that group's members, not every user the wildcard covers.