Skip to main content

Decide by attributes

Allow an action by the attributes of the person and of the resource: department, clearance, working hours, network.

RecipeDraftView source
This page is a draft. The code exists, and not all of it has been run for this page yet. The page says what was checked.
Works with
JavaScript / TypeScriptPythonGoPHPJavaC# / .NETPlain HTTP

What it shows​

  1. The setup script defines three attributes: the department of a person, the department of a record, and a legal hold on a record.
  2. It writes two policies. A person of cardiology may read a record of cardiology. Nobody may read a record on legal hold.
  3. The program stores the values: the department of the caller, and the attributes of three records.
  4. It asks about each record. The request names only the resource and the action, and LumoAuth reads the stored values.
  5. The record on legal hold is refused, because a deny policy wins over every allow policy.

LumoAuth also adds the time and the address of the request itself. A policy can read them, for example as environment.hour and environment.ip.

Before you start​

  • Check that your plan includes fine-grained authorization in the plans matrix.
  • An API key with the scope admin:abac:write, for the setup script. Create it in the portal: open API keys, under Applications & developer tools.
  • An application that may ask for the scopes abac:read and abac:write. In the portal, open Applications, your application, Configuration, and add both to Allowed scopes.
  • The access token of an administrator who signed in through that application with both scopes. The Express sign-in recipe has an application that you can use for this.

The checks answer for the person who holds the token, and an API key cannot make them. Storing a value needs the permission abac.manage. So in this recipe one administrator stores the values and asks the questions. In your app, an administrator stores the values, and each person asks with their own token and the scope abac:read.

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 admin:abac:write. Only the setup script uses it.
USER_ACCESS_TOKENThe access token of a signed-in administrator, with the scopes abac:read and abac:write.

The setup script creates what the recipe needs, through the Admin API. You can run it again at any time.

node authorization/attribute-policies/setup.mjs
setup.mjs
// Creates what the recipe needs: three attribute definitions and two
// policies. Run it again at any time.
//
// node authorization/attribute-policies/setup.mjs
//
// The API key needs the scope admin:abac:write. The person who created the
// key must be an administrator of the organization.
import { ensure } from '../../_lib/admin.mjs';

const attribute = (attributeType, slug, name, dataType, extra = {}) =>
ensure('/abac/attributes', (a) => a.slug === slug && a.attributeType === attributeType, {
name,
slug,
attributeType,
dataType,
...extra,
});

await attribute('user', 'department', 'Department', 'string', {
description: 'The department that the person works in.',
});
await attribute('resource', 'record_department', 'Department of the record', 'string', {
description: 'The department that the record belongs to.',
});
// A record with no stored value is not on legal hold.
await attribute('resource', 'legal_hold', 'Legal hold', 'boolean', {
description: 'Nobody may read a record on legal hold.',
defaultValue: false,
});

// A condition compares one attribute with a fixed value, so each department
// gets its own policy.
await ensure('/abac/policies', (p) => p.name === 'Cardiology reads its records', {
name: 'Cardiology reads its records',
description: 'A person of cardiology may read a record of cardiology.',
resourceType: 'record',
action: 'read',
effect: 'allow',
priority: 100,
conditions: {
operator: 'AND',
conditions: [
{ subject: 'user.department', operator: 'equals', value: 'cardiology' },
{ subject: 'resource.record_department', operator: 'equals', value: 'cardiology' },
],
},
});

// A deny policy wins over every allow policy.
await ensure('/abac/policies', (p) => p.name === 'No reading on legal hold', {
name: 'No reading on legal hold',
description: 'Nobody may read a record on legal hold.',
resourceType: 'record',
action: 'read',
effect: 'deny',
priority: 200,
conditions: {
operator: 'AND',
conditions: [{ subject: 'resource.legal_hold', operator: 'equals', value: true }],
},
});

console.log('Three attribute definitions and two policies are ready.');

Code​

Run each command from the recipes folder of the examples repository. Set the folder up once for your language. The curl version prints the JSON answers of the three checks and of the bulk check.

authorization/attribute-policies/js/records.mjs
// Set attributes on a person and on three records, then ask LumoAuth which
// records the person may read.
//
// node authorization/attribute-policies/js/records.mjs
import { LumoAuthBackend } from '@lumoauth/backend';

// These calls take the access token of a signed-in person, not an API key.
const lumo = new LumoAuthBackend({
baseUrl: process.env.LUMOAUTH_URL,
orgId: process.env.LUMOAUTH_ORG_ID,
secretKey: process.env.USER_ACCESS_TOKEN,
});

const department = process.env.DEPARTMENT ?? 'cardiology';

// The policies read stored values. An administrator sets them.
const me = await lumo.abac.getMyAttributes();
await lumo.abac.setUserAttribute(me.user_id, 'department', department);
await lumo.abac.setResourceAttribute('record', 'rec-1001', 'record_department', 'cardiology');
await lumo.abac.setResourceAttribute('record', 'rec-2001', 'record_department', 'oncology');
await lumo.abac.setResourceAttribute('record', 'rec-1002', 'record_department', 'cardiology');
await lumo.abac.setResourceAttribute('record', 'rec-1002', 'legal_hold', true);
console.log(`department of the caller: ${department}`);

const records = ['rec-1001', 'rec-2001', 'rec-1002'];

// The request names the resource and the action. The answer is about the caller.
for (const resourceId of records) {
const decision = await lumo.abac.check({ resourceType: 'record', resourceId, action: 'read' });
console.log(`read ${resourceId} -> ${decision.allowed ? 'allowed' : 'denied'} (${decision.reason})`);
}

// One request for the three records.
const bulk = await lumo.abac.checkBulk({
checks: records.map((resourceId) => ({ resourceType: 'record', resourceId, action: 'read' })),
});
console.log(`bulk: ${bulk.results.map((r) => `${r.resourceId} ${r.allowed ? 'allowed' : 'denied'}`).join(', ')}`);
node authorization/attribute-policies/js/records.mjs

Run against a LumoAuth organization.

Check the result​

Output
department of the caller: cardiology
read rec-1001 -> allowed (Access allowed by policy: Cardiology reads its records)
read rec-2001 -> denied (No matching policies - default deny)
read rec-1002 -> denied (Access denied by policy: No reading on legal hold)
bulk: rec-1001 allowed, rec-2001 denied, rec-1002 denied
  • Change the department of the caller: run the program again with DEPARTMENT=oncology. The first record is now refused too, because no policy allows oncology anything.
  • In the portal, open Attribute rules under Access control. The Policy tester shows which policies matched for a person and a request.

Limits​

  • A condition compares one attribute with a fixed value. It cannot compare two attributes, such as "the department of the person equals the department of the record". Write one policy for each department.
  • A check answers only for the person who holds the token. An API key cannot ask, and it cannot store a value on a person or on a resource.
  • A check cannot send the attributes of the resource. LumoAuth reads the stored values. A request can send only the environment attributes that you defined.
  • When no policy matches, the answer is "denied".
  • environment.ip is the address of the request that LumoAuth received. When your backend makes the call, that is the address of your backend.
  • Only a full administrator may store an attribute on their own account.