Before you start
In order to understand this guide correctly you must be familiar with some OpenFGA Concepts and know how to develop the things that we will list below. Assume that you have the following authorization model.You have a type called
document that can have a reader
and writer. All writers are readers. bob has a writer relationship with document:planning.
In addition, you will need to know the following:
Direct access
You need to know how to create an authorization model and create a relationship tuple to grant a user access to an object. Learn more →OpenFGA concepts
- A Type: a class of objects that have similar characteristics
- A User: an entity in the system that can be related to an object
- A Relation: is a string defined in the type definition of an authorization model that defines the possibility of a relationship between an object of the same type as the type definition and a user in the system
- An Object: represents an entity in the system. Users’ relationships to it can be define through relationship tuples and the authorization model
- A Relationship Tuple: a grouping consisting of a user, a relation and an object stored in OpenFGA
Check
What is it for?
The Check API is an API endpoint that returns whether the user has a certain relationship with an object. OpenFGA will resolve all prerequisite relationships to establish whether a relationship exists.When to use?
Check can be called if you need to establish whether a particular user has a specific relationship with a particular object. For example, you can call check to determine whetherbob has a reader relationship with document:planning.
The OpenFGA API will return true because there is an implied relationship as
- every
writeris also areader bobis awriterfordocument:planning
Caveats and when not to use it
Check is designed to answer the question “Does user:X have relationship Y with object:Z?”. It is not designed to answer the following questions:- “Who has relationship Y with object:Z?”
- “What are the objects that userX has relationship Y with?”
- “Why does user:X have relationship Y with object:Z?”
Batch Check
What is it for?
The Batch Check API is an API endpoint that allows you to check multiple user-object-relationship combinations in a single request.When to use?
Batching authorization checks together in a single request significantly reduces overall network latency. Two scenarios are common to use Batch Check:- When determining if the user has access to a list of objects (such as Option 1 in Search with Permissions), filter and sort on your database, then call
/batch-check. Repeat to perform pagination. - When determining fields on a web page the user has access to, call
/batch-checkfor every relation necessary to show/hide each field.
bob has can_view_name, can_view_dob, and can_view_ssn relationships with patient_record:1.
The OpenFGA API will return true depending on the level of access assigned to that user and the implied relationships inherited in the authorization model.
Caveats and when not to use it
If you are making less than 10 checks, it may be faster to call the Check API in parallel instead of Batch Check.Read
What Is It For?
The Read API is an API endpoint that returns the relationship tuples that are stored in the system that satisfy a query.When to use?
Read can be called if you need to get all the stored relationship tuples that relate:- user + relation + object
- user + relation + object type
- user + object with any relation
- user + object type with any relation
- relation + object for any user
- object with any user and relation
- all with any user, relation, or object
Initialize the SDK for Read and Expand
Initialize the SDK for Read and Expand
Set
FGA_API_URL for your service (for example, https://api.fga.example) and FGA_STORE_ID for your store. Initialize once, then place the request snippets below in the same scope as the client. For Python, run requests inside the async with block; for Go, .NET, and Java, use the method that initializes the client. See SDK client setup for authentication options.1. Read a tuple related to a particular user, relation, and object
For example, to query ifbob has a writer relationship on document:planning (essentially finding out if a tuple exists), one can ask
Response
2. Read all tuples related to a particular user, relation, and object type
For example, to query all the stored relationship tuplesbob has a writer relationship with on type document:, one can ask
Response
3. Read all tuples related to a particular user and object with any relation
For example, to query all the stored relationship tuplesbob has on type document:planning, one can ask
Response
4. Read all tuples related to a particular user and object type with any relation
For example, to query all the stored relationship tuplesbob has on object type document:, one can ask
Response
5. Read all tuples related to a particular relation and object for any user
For example, to query all the stored relationship tuples which have thewriter relation on object document:planning, one can ask
Response
6. Read all tuples related to a particular object with any user or relation
For example, to query all the stored relationship tuples for objectdocument:planning, one can ask
Response
7. Read all tuples for any user, relation, or object
For example, to query all stored relationship tuples, one can askResponse
Caveats and when not to use it
The Read API will only return all the stored relationships that match the query specification. It does not expand or traverse the graph by taking the authorization model into account. For example, if you specify thatwriters are viewers in the authorization model, the Read API will ignore that and it will return tuples where a user is a viewer if and only if the (user_id, "viewer", object_type:object_id) relationship tuple exists in the system.
In the following case, although all writers have reader relationships for document objects and bob is a writer for document:planning, if you query for all objects that bob has reader relationships, it will not return document:planning.
Response
Although bob is a writer to document:planning and every writer is also a reader, the Read API will return an empty list because there are no stored relationship tuples that relate bob to document:planning as reader.
Expand
What is it for?
The Expand API returns all users (including users and usersets) that have a specific relationship with an object. The response is represented as a tree of users or usersets. To build the full graph of access, you would need to recursively call expand on the leaves returned from the previous expand call.When to use?
Expand is used for debugging and to understand why a user has a particular relationship with a specific object. Reuse the SDK initialization from the Read examples. Replace the example authorization model ID below with the ID of the model you want to expand. For example, to understand whybob can have a reader relationship with document:planning, one could first call
Response
writer, for which we will call
Response
- those related to
document:planningasreaderare all those who are related to that document aswriter bobis related todocument:planningaswriter
ListObjects
What is it for?
The ListObjects API is an API endpoint that returns the list of all the objects of a particular type that a specific user has a specific relationship with. It provides a solution to the Search with Permissions (Option 3) use case for access-aware filtering on small object collections.When to use?
Use the ListObjects API to get what objects a user can see based on the relationships they have. See Search with Permissions for more guidance. There’s two variations of the List Objects API.- The standard version, which waits until all results are ready and sends them in one response.
- The streaming version, which should be used if you want the individual results as soon as they become available.
Caveats
ListObjects will return the results found within the time allotted (listObjectsDeadline, default: 3s) up to the maximum number of results configured (listObjectsMaxResults, default: 1000). See Configuring the Server) for more on how to change the default configuration.
- If you set
listObjectsDeadlineto1s, the server will spend at most 1 second finding results. - If you set
listObjectsMaxResultsto10, the server will return, at most, 10 objects.
listObjectsDeadline. If the number of objects of that type the user could have access to is high, you should set a high value for listObjectsMaxResults.
ListUsers
What is it for?
The ListUsers API is an API endpoint that that returns all users of a given type that have a specified relationship with an object.When to use?
Use the ListUsers API to get which users have a relation to a specific object.Caveats
ListUsers will return the results found within the time allotted (listUsersDeadline, default: 3s) up to the maximum number of results configured (listUsersMaxResults, default: 1000). See Configuring the Server) for more on how to change the default configuration.
- If you set
listUsersDeadlineto1s, the server will spend at most 1 second finding results. - If you set
listUsersMaxResultsto10, the server will return, at most, 10 objects.
listUsersDeadline. If the number of users matching that filter that could have that relation with the object is high, you should set a high value for listUsersMaxResults.
Summary
Related Sections
Check out this additional content for more information on how to query relationships.Check API Reference
Official reference guide for the Check API
Read API Reference
Official reference guide for the Read API
Expand API Reference
Official reference guide for the Expand API
ListObjects API Reference
Official reference guide for the ListObjects API
ListUsers API Reference
Official reference guide for the ListUsers API