Skip to main content

Share a resource with relationships

Model owner, editor and viewer of a document as relationships, with folders that pass access down.

RecipeReadyView source
Works with
JavaScript / TypeScriptPythonGoPHPJavaC# / .NETPlain HTTP

What it shows​

  1. Two namespaces define the rules. An owner of a document is also an editor, and an editor is also a viewer. A viewer of a folder is a viewer of each document in it.
  2. Four relationship tuples say who is related to what. Maya owns a report, Noah edits it, and Priya may view the folder of the report.
  3. Your backend asks about a relation before it returns the document. LumoAuth follows the rules, so Priya may view the report with no tuple of her own for it.
  4. expand lists everyone who can view the report, for a "Shared with" dialog.

Before you start​

  • An API key with the scope authz:check. Create it in the portal: open API keys, under Applications & developer tools. The person who creates the key must hold the permission policies.view.
  • Check that your plan includes fine-grained authorization in the plans matrix. Without it, every check answers "denied".
  • Two namespaces and four tuples. You create them in the portal:
  1. In the sidebar, under Access control, open Relationships, and click Create namespace.
  2. Enter folder as the Namespace name. Open Schema JSON, replace its text with the schema of the folder below, and click Create namespace.
  3. Create the namespace document in the same way, with the schema of the document.
  4. Open the Tuples tab and click Add relationship. Fill in the first row of the table below, and click Add tuple. Leave User relation empty.
  5. Add the other three rows in the same way.

The schema of the folder:

{
"relations": {
"owner": { "_this": { "relation": "owner" } },
"viewer": {
"union": [
{ "computed_userset": { "relation": "owner" } },
{ "_this": { "relation": "viewer" } }
]
}
}
}

The schema of the document. The last rule of viewer reads the parent tuple of the document, and takes the viewers of that folder:

{
"relations": {
"parent": { "_this": { "relation": "parent" } },
"owner": { "_this": { "relation": "owner" } },
"editor": {
"union": [
{ "computed_userset": { "relation": "owner" } },
{ "_this": { "relation": "editor" } }
]
},
"viewer": {
"union": [
{ "computed_userset": { "relation": "editor" } },
{ "_this": { "relation": "viewer" } },
{ "tuple_to_userset": { "tupleset": { "relation": "parent" }, "computed_userset": { "relation": "viewer" } } }
]
}
}
}

The four tuples:

Object namespaceObject IDRelationUser namespaceUser ID
documentq4-reportownerusermaya
documentq4-reporteditorusernoah
documentq4-reportparentfolderfinance
folderfinancevieweruserpriya

The names maya, noah and priya keep the example short. In your app, use the ID of the LumoAuth user: the sub claim of the token.

VariableValue
LUMOAUTH_URLThe address of LumoAuth: https://app.lumoauth.dev, or https://eu.app.lumoauth.dev for the EU region.
LUMOAUTH_ORG_IDYour organization, for example acme-corp.
LUMOAUTH_API_KEYAn API key with the scope authz:check.

Code​

Run each command from the recipes folder of the examples repository. Set the folder up once for your language. Every version below was run against a LumoAuth organization. The curl version prints the JSON answers of the same six requests.

authorization/share-with-relationships/js/share.mjs
// Ask LumoAuth who is related to a document, and list everyone who can view it.
//
// node authorization/share-with-relationships/js/share.mjs
import { LumoAuthBackend } from '@lumoauth/backend';

const lumo = new LumoAuthBackend({
baseUrl: process.env.LUMOAUTH_URL,
orgId: process.env.LUMOAUTH_ORG_ID,
secretKey: process.env.LUMOAUTH_API_KEY,
});

const document = 'document:q4-report';

for (const [subject, relation] of [
['user:maya', 'editor'], // Maya is the owner. An owner is also an editor.
['user:noah', 'viewer'], // Noah is an editor. An editor is also a viewer.
['user:noah', 'owner'],
['user:priya', 'viewer'], // Priya is a viewer of the folder of the document.
['user:priya', 'editor'],
]) {
const allowed = await lumo.zanzibar.check({ object: document, relation, subject });
console.log(`${subject} ${relation} of ${document} -> ${allowed ? 'allowed' : 'denied'}`);
}

// The answer of expand is a tree. Its leaves hold the subjects.
const subjects = (node) => [...(node.subjects ?? []), ...(node.children ?? []).flatMap(subjects)];
const tree = await lumo.zanzibar.expand({ object: document, relation: 'viewer' });
console.log(`can view ${document}: ${[...new Set(subjects(tree))].join(', ')}`);
node authorization/share-with-relationships/js/share.mjs

Check the result​

Output
user:maya editor of document:q4-report -> allowed
user:noah viewer of document:q4-report -> allowed
user:noah owner of document:q4-report -> denied
user:priya viewer of document:q4-report -> allowed
user:priya editor of document:q4-report -> denied
can view document:q4-report: user:maya, user:noah, user:priya
  • Share the folder with one more person: add the tuple folder:finance#viewer@user:omar in the portal, and run the program again. The last line now names user:omar too.
  • The Tester tab of the Relationships page answers the same questions in the portal. It also shows the rule that gave the answer.

Limits​

  • LumoAuth has no API for writing a tuple or a namespace. You write them in the portal, so your app cannot add a tuple when a person clicks "Share".
  • There is no operation that lists the objects of a person, such as "every document that Priya can view". Ask about each object.
  • A namespace cannot exclude subjects, as in "every viewer except blocked users".
  • expand lists a userset such as team:finance#member as it is. It does not list the members of the team.
  • When the subject is a LumoAuth user who is blocked or deleted, the answer is "denied", whatever the tuples say.
  • An API key may ask about any subject. A signed-in person who asks with an access token may ask only about themselves, unless they hold the permission authz.check.