What Does The Configuration Language Look Like?
Below is a sample authorization model. The next sections discuss the basics of the OpenFGA configuration language.The authorization model describes four types of objects:
user, domain, folder and document.The domain type definition has a single relation called member that only allows direct relationships.The folder and document type definitions each have five relations: parent_folder, owner, writer, viewer and can_share.Direct Relationship Type Restrictions
When used at the beginning of a relation definition,[<string>, <string>, ...] allows direct relationships by the objects of these specified types, for example, [user, user:*, team#member]. The strings can be in one of three formats:
<type>: indicates that tuples relating objects of those types as users can be written. For example,group:marketingcan be related ifgroupis in the type restrictions.<type:*>: indicates that a tuple relating all objects of that type can be written. For example,user:*can be added ifuser:*is in the type restrictions.<type>#<relation>: indicates tuples with sets of users related to an object of that type by that particular relation. For example,group:marketing#membercan be added ifgroup#memberis in the type restrictions.
[<type1>, <type2>, ...] in the OpenFGA DSL translates to this in the OpenFGA API syntax.team type:
The team type definition above defines all the relations that users can have with an object of type team. In this example, the relation is member.
Because of the [user, team#member] direct relationship type restrictions used, a user in the system can have a direct relationship with the team type as a member for objects of:
- type
user - the
usertype bound public access (user:*) - usersets that have a
teamtype and amemberrelation (e.g.team:product#member)
anne is a member of team:product if any of the following relationship tuple sets exist:
-
Anne is directly related to the product team as a member
-
Everyone (
*) is directly related to the product team as a member -
Members of the contoso team are members of the product team
Anne is a member of the contoso team
Referencing Other Relations On The Same Object
The same object can also reference other relations. Below is a simplifieddocument type definition:
Above, document type definition defines all the relations that users can have with an object of type document. In this case, the relations are editor, viewer and can_rename. The viewer and can_rename relation definitions both reference editor, which is another relation of the same type.
can_rename does not reference the direct relationship type restrictions, which means a user cannot be directly assigned this relation and it must be inherited when the editor relation is assigned. Conversely, the viewer relation allows both direct and indirect relationships using the Union Operator.anne is a viewer of document:new-roadmap if any one of the following relationship tuple sets exists:
-
anne is an editor of document:new-roadmap
Anne is an editor of the new-roadmap document
-
anne is a viewer of document:new-roadmap
Anne is a viewer of the new-roadmap document
anne has a can_rename relationship with document:new-roadmap only if anne has an editor relationship with the document:
-
anne is an editor of document:new-roadmap
Anne is an editor of thew new-roadmap document
Referencing Relations On Related Objects
Another set of indirect relationships are made possible by referencing relations to other objects. The syntax isX from Y and requires that:
- the other object is related to the current object as
Y - the user is related to another object as
X
user:anne is a viewer of document:new-roadmap if any one of the following relationship tuples sets exists:
-
Anne is a viewer of the parent folder of the new-roadmap document
- planning folder is the parent folder of the new-roadmap document
- anne is a viewer of the planning folder
-
Anne is a viewer of the new-roadmap document (direct relationship)
anne is a viewer of the new-roadmap document
The Union Operator
The union operator (or in the DSL, union in the JSON syntax) indicates that a relationship exists if the user is in any of the sets of users (union).
In the type definition snippet above, user:anne is a viewer of document:new-roadmap if any of the following conditions are satisfied:
- there exists a direct relationship with anne as editor of document:new-roadmap
- anne is a viewer of document:new-roadmap
The above authorization model indicates that a user is related as a viewer if they are in any of the following:
- the userset of all users related to the object as “viewer”, indicating that a user can be assigned a direct
viewerrelation - the userset of all users related to the object as “editor”, indicating that a user who is an editor is also implicitly a viewer
anne is in at least one of those usersets, meaning anne is either an editor or a viewer, the check on {"user": "user:anne", "relation": "viewer", "object": "document:new-roadmap"} returns {"allowed": true}.The Intersection Operator
The intersection operator (and in the DSL, intersection in the JSON syntax) indicates that a relationship exists if the user is in all the sets of users.
In the type definition snippet above, user:anne is a viewer of document:new-roadmap if all of the following conditions are satisfied:
- anne is an editor of document:new-roadmap
AND
- anne is an authorized_user of document:new-roadmap:
The above authorization model indicates that a user is related as a viewer if they are in all of the following:
- the userset of all users related to the object as
authorized_user - the userset of all users related to the object as
editor
anne must be in the intersection of the usersets (meaning both an editor AND an authorized_user) for the check on {"user": "user:anne", "relation": "viewer", "object": "document:new-roadmap"} to return {"allowed": true}.anne is not a viewer for document:new-roadmap if either of the following is true:anneis not aneditortodocument:new-roadmap: no relationship tuple of{"user": "user:anne", "relation": "editor", "object": "document:new-roadmap"}anneis not anauthorized_useron thedocument:new-roadmap: no relationship tuple of{"user": "user:anne", "relation": "authorized_user", "object": "document:new-roadmap"}
The Exclusion Operator
The exclusion operator (but not in the DSL, difference in the JSON syntax) indicates that a relationship exists if the user is in the base userset but not in the excluded userset. This operator is particularly useful when modeling exclusion or block lists.
In the type definition snippet above, user:anne is a viewer of document:new-roadmap if and only if:
-
annehas a direct relationship asviewertodocument:new-roadmapAND -
anneis not blocked fromdocument:new-roadmap(i.e., the following relationship tuple must not exist):
The authorization model above indicates that a user is related as a viewer if they are in:
- the userset of all users related to the object as
viewer
- the userset of all users related to the object as
blocked
anne must be both a viewer and not blocked for the check on {"user": "user:anne", "relation": "viewer", "object": "document:new-roadmap"} to return {"allowed": true}.anne is not a viewer for document:new-roadmap if either of the following is true:anneis not assigned direct relationship as viewer to document:new-roadmap: no relationship tuple of{"user": "user:anne", "relation": "viewer", "object": "document:new-roadmap"}anneis blocked on the document:new-roadmap{"user": "user:anne", "relation": "blocked", "object": "document:new-roadmap"}
Grouping and nesting operators
You can define complex conditions by using parentheses to group and nest operators. Note that direct relationships can be included in an expression with parentheses.Conditional relationships
OpenFGA supports conditional relationships, which are only considered if a specific condition is met. You can learn more about Conditional Relationships in the Modeling: Conditional Relationships guide.Equivalent Zanzibar Concepts
The JSON syntax accepted by the OpenFGA API closely mirrors the syntax represented in the Zanzibar paper. The major modifications are a slight flattening and conversion of keys fromsnake_case to camelCase.
The Zanzibar paper presents this example:
-
The users with a viewer relationship to a certain doc are any of:
- the set of users who are directly related with this doc as
viewer - the set of users who are related to this doc as
editor - the set of users who are related to any object OBJ_1 as
viewer, where object OBJ_1 is any object related to this doc asparent(e.g. viewers of this doc’s parent folder, where the parent folder is OBJ_1)
- the set of users who are directly related with this doc as
Related Sections
Check the following sections for more on how to use the configuration language in modeling authorization.OpenFGA Concepts
Learn about the OpenFGA Concepts.
Modeling: Getting Started
Learn about how to get started with modeling your permission system in OpenFGA.
Direct Access
Learn about modeling user access to an object.