Guides / Security / API keys

User-Restricted Access to Data

Sometimes, you don’t want your users to search your entire index, but only a subset that concerns them. That can be all the movies they added to their watch list, the items they added to their shopping cart, or the content shared by their friends.

This doesn’t mean you need one index per user. By generating a Secured API key for the current user, you can restrict the records they can retrieve.

Adding an attribute for filtering in your dataset

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.

Let’s take a dataset for a video streaming service. The index contains the entire list of available movies, but on a specific section of the app, users can search through the movies they added to their watch list.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[
  {
    "title": "The Shawshank Redemption",
    "watch_list_of": [1, 2, 3],
    "objectID": "myID1"
  },
  {
    "title": "The Godfather",
    "watch_list_of": [3],
    "objectID": "myID2"
  },
  {
    "title": "The Dark Knight",
    "watch_list_of": [1, 2],
    "objectID": "myID3"
  },
  {
    "title": "12 Angry Men",
    "watch_list_of": [2, 3],
    "objectID": "myID4"
  }
]

Each record contains a watch_list_of attribute, which contains a list of user IDs. Only listed users have added the movie on their watch list. When searching through it, they should only be able to find those movies.

Making the attribute filterable

To make your watch_list_of attribute filterable, you should add it in attributesForFaceting.

1
2
3
4
5
$index->setSettings([
  'attributesForFaceting' => [
    'filterOnly(watch_list_of)'
  ]
]);

In our case, we only want to filter on this attribute so that we can add the filterOnly modifier. It’s better for 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.

Adding and removing users

Whenever someone adds or removes a movie from their watch list, you need to update the watch_list_of attribute.

1
2
3
4
5
6
$index->partialUpdateObject(
    [
        'watch_list_of' => [1, 2],
        'objectID' => 'myID1'
    ]
);

The partialUpdateObjects allows you to partially update an attribute instead of replacing the entire record, or even the entire index.

Generating a Secured API key

With front-end search, malicious users can tweak the request to impersonate another user and see the movies on their watch list.

To prevent this, we can generate a Secured API key, a special key that you can generate on the fly, and within which you can embed a set of filters. End users can’t alter those filters.

1
2
3
4
5
6
7
8
$currentUserID = 1;

$securedApiKey = \Algolia\AlgoliaSearch\Client::generateSecuredApiKey(
  'YourSearchOnlyAPIKey', // A search key that you keep private
  [
    'filters' => 'watch_list_of:'.$currentUserID
  ]
);

Note that this needs to happen on the back end. For example, when a user successfully logs in to your application, you could generate the key from your back end and return it for the current session.

To invalidate Secured API keys, you need to invalidate the search API key you used to generate it.

Making sensitive attributes unretrievable

When using a Secured API key with an embedded filter, users can only retrieve movies from their own watch list. However, since the API returns the watch_list_of attribute for each record, they can find out what other users have the same movies in their watch list if they inspect the response.

To mitigate this privacy concern, you can leverage the unretrievableAttributes parameter. It allows you to ensure that the watch_list_of parameter is never part of the Algolia response, even though you use it for filtering on the engine side.

1
2
3
4
5
$index->setSettings([
  'unretrievableAttributes' => [
    'watch_list_of'
  ]
]);

Searching in the subset

You can now search on the front end using the API key generated from your back end. This API key has an embedded filter on the watch_list_of attribute, so you have a guarantee that the current user only sees results that they’re allowed to access.

Did you find this page helpful?