Search With Permissions
Once you implement fine-grained authorization to protect your resources, search becomes a more complex problem, because the user's access to each resource now has to be validated before the resource can be shown.
The search problem can then be summarized as:
"Given a particular search filter and a sort order, what objects can the user access"?
The OpenFGA service does not store object metadata (names of files, creation dates, time of last update, etc), which means completing any search request by filtering and sorting according to certain criteria will require data from your database.
The services responsible for performing these actions are:
- Filter: Your database
- Sort: Your database
- Authorize: OpenFGA
To return the set of results that match the user's search query, you will need to get the intersection of the results from the services above.
Possible options
There are three possible ways to do this:
Option 1: Search, then check
Pre-filter, then call OpenFGA Batch Check endpoint.
- Filter and sort on your database.
- Call
/batch-checkto check access for multiple objects in a single request. - Filter out objects the user does not have access to.
- Return the filtered result to the user.
Option 2: Build a local index from changes endpoint, search, then check
Consume the GET /changes endpoint to create a local index you can use to do an intersection on the two sets of results.
- Call the OpenFGA changes API.
- For the particular authorization model version(s) you are using in production, flatten/expand the changes (e.g.
user:anne, writer, doc:planningbecomes two tuples:user:anne, writer, doc:planninganduser:anne, reader, doc:planning). - Build the intersection between the objects in your database and the flattened/expanded state you created.
- You can then call
/checkon each resource in the resulting set before returning the response to filter out any resource with permissions revoked but whose authorization data has not made it into your index yet.