You can use secured API keys to restrict the records users can retrieve.
Sometimes, you don’t want your users to search your entire , but only a subset that concerns them.
You can restrict content to a specific user, a set of users, a group, or everyone.
Handling access within an index allows to have a fine-grained control over who can search and view what content.This doesn’t mean you need one index per user.
By generating a secured API key for the current user, you can restrict the they can retrieve.
Algolia is schemaless and doesn’t have any concept of relationships between objects, so you need to put all the relevant information in each record.Take a dataset for corporate documents as an example. The index contains the entire list of documents for the company, but has dedicated access control to restrict who can view content.Consider different users in this company: Angela, Mike, and Ruth. Angela is an executive, Mike the accountant, and Ruth is an engineer.
Each record has a visible_by attribute, which has a list of users, or groups. Only listed users and groups can see the specific record, with the group Everybody visible by anyone. When searching through it, only allowed people should be able to find those records.
To restrict access, configure an attribute for filtering, generate secured API keys with embedded filters, and optionally hide the attribute from API responses.
In this case, you only want to filter on this attribute, and not use it for facet counts.
To do this, add the filterOnly modifier.
This improves performance because the engine doesn’t have to compute the count for each value.If you need faceting on this attribute, you can remove the filterOnly modifier.
Frontend search can be vulnerable to malicious users who can tweak the request to impersonate another user and see content they shouldn’t have access to.To prevent this, generate a secured API key on the backend with filters (users can’t alter these filters).
var response = client.GenerateSecuredApiKey( "2640659426d5107b6e47d75db9cbaef8", new SecuredApiKeyRestrictions { Filters = "visible_by:group/Finance" });
When using a secured API key with an embedded filter, users can only retrieve content they’re allowed to access to.
Since the API returns the visible_by attribute for each record, they can find out what other users have the same access for this record if they inspect the response.To mitigate this privacy concern, use the unretrievableAttributes parameter.
It ensures that the visible_by parameter is never part of the Algolia response, even though you use it for filtering on the engine side.
var response = await client.SetSettingsAsync( "INDEX_NAME", new IndexSettings { UnretrievableAttributes = new List<string> { "visible_by" } });
You can now search on the frontend using the API key generated from your backend.
This API key has an embedded filter on the visible_by attribute, so you have a guarantee that the current user only sees results that they’re allowed to access.