Skip to main content

Blocklists

note
OpenFGA is an open source Fine-Grained Authorization solution based on Google's Zanzibar. We welcome community contribution to this project.

In this guide you'll see how to model preventing users from accessing objects using OpenFGA. For example, blocking users from accessing a document, even if it has been already shared with them.

When to use

Exclusion is useful while building applications. You may need to support access patterns like granting access to some users, but excluding specific people or groups, similar to how users can block others from following them on social media, or prevent them from sharing documents on Google Drive.

This is useful when:

  • Implementing the "blocking" feature, such as the profile blocking commonly present on social media platforms (e.g. Instagram and Twitter).
  • Reduce a user's access if they are part of a particular group (e.g. restricting access to members who are also guests, or restricting access to users in a certain locality).

Before You Start

Before you start this guide, make sure you're familiar with some OpenFGA Concepts and know how to develop the things listed below.

You will start with the authorization model below, it represents a document type that can have users related as editor, and team type that can have users related as member.

Let us also assume that we have a document called "planning", shared for editing within the product team (comprised of becky and carl).

type document
relations
define editor as self
type team
relations
define member as self

The current state of the system is represented by the following relationship tuples being in the system already:

[
// Members of the product team can edit the planning document
{
"user": "team:product#member",
"relation": "editor",
"object": "document:planning",
},
// Becky is a member of the product team
{
"user": "becky",
"relation": "member",
"object": "team:product",
},
// Carl is a member of the product team
{
"user": "carl",
"relation": "member",
"object": "team:product",
},
]

In addition, you will need to know the following:

Modeling User Groups

You need to know how to add users to groups and grant groups access to resources. 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
  • Exclusion Operator: the exclusion operator can be used to exclude certain usersets from being related to an object

Step By Step

With the above authorization model and relationship tuples, OpenFGA will correctly respond with {"allowed":true} when check is called to see if Carl and Becky can edit this document.

We can verify that by issuing two check requests:

Initialize the SDK
// ApiTokenIssuer, ApiAudience, ClientId and ClientSecret are optional.
// import the SDK
const { OpenFgaApi } = require('@openfga/sdk');

// Initialize the SDK with no auth - see "How to setup SDK client" for more options
const fgaClient = new OpenFgaApi({
apiScheme: process.env.FGA_API_SCHEME, // Either "http" or "https", defaults to "https"
apiHost: process.env.FGA_API_HOST, // required, define without the scheme (e.g. api.openfga.example instead of https://api.openfga.example)
storeId: process.env.FGA_STORE_ID, // Either "http" or "https", defaults to "https"
});

// Run a check
const { allowed } = await fgaClient.check({
tuple_key: {
user: 'becky',
relation: 'editor',
object: 'document:planning',
},
});

// allowed = true
Initialize the SDK
// ApiTokenIssuer, ApiAudience, ClientId and ClientSecret are optional.
// import the SDK
const { OpenFgaApi } = require('@openfga/sdk');

// Initialize the SDK with no auth - see "How to setup SDK client" for more options
const fgaClient = new OpenFgaApi({
apiScheme: process.env.FGA_API_SCHEME, // Either "http" or "https", defaults to "https"
apiHost: process.env.FGA_API_HOST, // required, define without the scheme (e.g. api.openfga.example instead of https://api.openfga.example)
storeId: process.env.FGA_STORE_ID, // Either "http" or "https", defaults to "https"
});

// Run a check
const { allowed } = await fgaClient.check({
tuple_key: {
user: 'carl',
relation: 'editor',
object: 'document:planning',
},
});

// allowed = true

We want to share a document with the product team and also have the ability to deny certain users access, even if they have the document shared with them already. We can verify this by blocking Carl (who we have seen already has edit access) from editing the document.

In order to do that, we need to:

  1. Modify our model to allow indicating that users can be blocked from accessing a document

  2. Modify our model to indicate that users who are blocked can no longer edit the document

  3. Verify that our solution works:

    a. Indicate that Carl is blocked from the planning document

    b. Carl (now blocked) can no longer edit the document

    c. Becky still has edit access

01. Modify Our Model So Users Can Be Blocked From Accessing A Document

To allow users to be "blocked" from accessing a document, we first need to allow this relation. We'll update our store model to add a blocked relation to the document type.

The authorization model becomes this:

type document
relations
define blocked as self
define editor as self
type team
relations
define member as self

Now we can add relationship tuples indicating that a certain user is blocked from editing a document.

02. Modify Our Model So Users Who Are Blocked Can No Longer Edit The Document

Now that we can mark users as blocked from editing documents, we need to support denying the editor relationship when a user is blocked. We do that by modifying the relation definition of editor, and making use of the the exclusion operator to exclude the set of blocked users, as we can see here:

type document
relations
define blocked as self
define editor as self but not blocked
type team
relations
define member as self

03. Verify Our Solution Works

To check if our new model works, we'll add a relationship tuple with Carl as blocked from document:planning and then verify that Carl no longer has editor access to that document.

a. Indicate That Carl Is Blocked From The Planning Document

With our modified authorization model, we can indicate that Carl is blocked by adding this relationship tuple.

Initialize the SDK
// ApiTokenIssuer, ApiAudience, ClientId and ClientSecret are optional.
// import the SDK
const { OpenFgaApi } = require('@openfga/sdk');

// Initialize the SDK with no auth - see "How to setup SDK client" for more options
const fgaClient = new OpenFgaApi({
apiScheme: process.env.FGA_API_SCHEME, // Either "http" or "https", defaults to "https"
apiHost: process.env.FGA_API_HOST, // required, define without the scheme (e.g. api.openfga.example instead of https://api.openfga.example)
storeId: process.env.FGA_STORE_ID, // Either "http" or "https", defaults to "https"
});

await fgaClient.write({
writes: {
tuple_keys: [
// Carl is blocked from editing the planning document
{ user: 'carl', relation: 'blocked', object: 'document:planning'}
]
}
});

b. Carl (now blocked) Can No Longer Edit The Document

We have modified the authorization model and added relationship tuples to indicate that Carl is blocked. Now let's make sure our solution works as expected.

To check if Carl still has access to the document, we can issue a check request with Carl as the user.

Initialize the SDK
// ApiTokenIssuer, ApiAudience, ClientId and ClientSecret are optional.
// import the SDK
const { OpenFgaApi } = require('@openfga/sdk');

// Initialize the SDK with no auth - see "How to setup SDK client" for more options
const fgaClient = new OpenFgaApi({
apiScheme: process.env.FGA_API_SCHEME, // Either "http" or "https", defaults to "https"
apiHost: process.env.FGA_API_HOST, // required, define without the scheme (e.g. api.openfga.example instead of https://api.openfga.example)
storeId: process.env.FGA_STORE_ID, // Either "http" or "https", defaults to "https"
});

// Run a check
const { allowed } = await fgaClient.check({
tuple_key: {
user: 'carl',
relation: 'editor',
object: 'document:planning',
},
});

// allowed = false

The response is false, so our solution is working as expected.

c. Becky Still Has Edit Access

To check if Becky still has access to the document, we'll issue another check request with Becky as the user.

Initialize the SDK
// ApiTokenIssuer, ApiAudience, ClientId and ClientSecret are optional.
// import the SDK
const { OpenFgaApi } = require('@openfga/sdk');

// Initialize the SDK with no auth - see "How to setup SDK client" for more options
const fgaClient = new OpenFgaApi({
apiScheme: process.env.FGA_API_SCHEME, // Either "http" or "https", defaults to "https"
apiHost: process.env.FGA_API_HOST, // required, define without the scheme (e.g. api.openfga.example instead of https://api.openfga.example)
storeId: process.env.FGA_STORE_ID, // Either "http" or "https", defaults to "https"
});

// Run a check
const { allowed } = await fgaClient.check({
tuple_key: {
user: 'becky',
relation: 'editor',
object: 'document:planning',
},
});

// allowed = true

The response is true, indicating our model change did not inadvertently deny access for users who have access but are not blocked.

caution

Note: When creating tuples for OpenFGA make sure to use unique ids for each object and user within your application domain. We are using first names and human-readable identifiers to make this task easier to read.

Modeling: Getting Started

Learn about how to get started with modeling.

Configuration Language

Learn about OpenFGA Configuration Language.

Public Access

Learn about model public access.