Skip to main content

Ask with the AuthZEN API

Send an authorization question in the standard AuthZEN shape, alone or as a batch.

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
Plain HTTP
Standards
AuthZEN

What it shows​

  1. A role holds the permission report.publish. Maya holds the role, and Noah does not.
  2. One evaluation asks with a subject, an action and a resource: may Maya publish the report q4?
  3. A batch asks the same question for both people in one request. The answers come in the order of the questions.
  4. The permission check POST /api/v1/authz/check gives the same answer for report.publish.

LumoAuth reads the resource type and the action as the permission <resource type>.<action>. So a product that speaks AuthZEN can ask LumoAuth with no code that is special to LumoAuth.

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.
  • A permission, a role that holds it, and two users. The setup script creates them. Its API key also needs admin:permissions:write, admin:roles:write and admin:users:write.
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.
PUBLISHER_USER_IDThe ID of a user who holds the permission. The setup script prints it.
INTERN_USER_IDThe ID of a user who does not hold it.

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

node authorization/authzen/setup.mjs
setup.mjs
// Creates what the recipe needs: a permission, a role that holds it, and two
// test users. One user gets the role. Run it again at any time.
//
// node authorization/authzen/setup.mjs
//
// The API key needs the scopes admin:permissions:write, admin:roles:write
// and admin:users:write.
import { randomBytes } from 'node:crypto';
import { call, ensure, printEnv } from '../../_lib/admin.mjs';

// AuthZEN asks with a resource type and an action. LumoAuth reads the two as
// the permission "<resource type>.<action>".
const permission = await ensure('/admin/permissions', (p) => p.slug === 'report.publish', {
name: 'Publish a report',
slug: 'report.publish',
description: 'Make a report visible to every reader.',
});

const role = await ensure('/admin/roles', (r) => r.slug === 'report-publisher', {
name: 'Report publisher',
slug: 'report-publisher',
description: 'May publish reports.',
});
await call('PUT', `/admin/roles/${role.id}/permissions`, { permissions: [permission.slug] });

const user = (email, name) =>
ensure('/admin/users', (u) => u.email === email, {
email,
name,
// The recipe never signs in as these users, so nobody needs the password.
password: `${randomBytes(18).toString('base64url')}#Aa1`,
emailVerified: true,
});

const publisher = await user('report-publisher@example.com', 'Maya Chen');
const intern = await user('report-intern@example.com', 'Noah Kim');
await call('PUT', `/admin/users/${publisher.id}/roles`, { roles: [role.slug] });

console.log(`Permission ${permission.slug}, role ${role.slug}, and two users are ready.`);
printEnv({ PUBLISHER_USER_ID: publisher.id, INTERN_USER_ID: intern.id });

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 script was run with a built-in permission. On a new organization the setup script stops at the step that adds the new permission to the role.

authorization/authzen/http/evaluate.sh
#!/bin/sh
# Ask LumoAuth an authorization question in the AuthZEN shape: one evaluation,
# then a batch, then the same question at the permission check.
#
# sh authorization/authzen/http/evaluate.sh
set -eu

RESOURCE_TYPE="${RESOURCE_TYPE:-report}"
ACTION="${ACTION:-publish}"

# These endpoints are not under /orgs/{org}: the API key names the organization.
authz() {
curl --silent --show-error "$LUMOAUTH_URL/api/v1/authz/$1" \
--header "X-API-Key: $LUMOAUTH_API_KEY" \
--header "Content-Type: application/json" \
--data "$2"
echo
}

# One evaluation: may the publisher publish the report q4?
authz v1/evaluation "{
\"subject\": {\"type\": \"user\", \"id\": \"$PUBLISHER_USER_ID\"},
\"action\": {\"name\": \"$ACTION\"},
\"resource\": {\"type\": \"$RESOURCE_TYPE\", \"id\": \"q4\"}
}"

# A batch. The action and the resource at the top apply to every entry, and
# each entry names its own subject. The answers come in the same order.
authz v1/evaluations "{
\"action\": {\"name\": \"$ACTION\"},
\"resource\": {\"type\": \"$RESOURCE_TYPE\", \"id\": \"q4\"},
\"evaluations\": [
{\"subject\": {\"type\": \"user\", \"id\": \"$PUBLISHER_USER_ID\"}},
{\"subject\": {\"type\": \"user\", \"id\": \"$INTERN_USER_ID\"}}
]
}"

# The same question at the permission check gives the same answer.
authz check "{
\"permission\": \"$RESOURCE_TYPE.$ACTION\",
\"subject\": {\"type\": \"user\", \"id\": \"$PUBLISHER_USER_ID\"}
}"
sh authorization/authzen/http/evaluate.sh

Check the result​

Output
{"decision":true}
{"evaluations":[{"decision":true},{"decision":false,"context":{"id":"c527e67f2c83a0bd","reason_admin":{"en":"No matching policies or relationships"}}}]}
{"allowed":true,"permission":"report.publish","subject_type":"user","subject_id":"019d6190-9bba-7b5f-a0a5-07a6d10184d2","user_id":"019d6190-9bba-7b5f-a0a5-07a6d10184d2","context":{}}
  • The first line is the single evaluation, the second line is the batch, and the third line is the permission check.
  • A refusal has a context with the reason only when the caller holds the permission policies.view. Every other caller gets the plain {"decision": false}.
  • Remove the role from Maya in the portal, and run the script again: every answer is now false.

Limits​

  • A batch takes at most 100 evaluations.
  • An evaluation also reads relationships and attribute policies. A deny policy refuses first. Then a relationship <resource type>:<resource id>#<action>, a permission, or an allow policy can allow. These two models need fine-grained authorization: check that your plan includes it in the plans matrix.
  • The subject is a user (the ID is a UUID) or an agent (agt_…). A subject that LumoAuth does not find gets "decision": false, not an error.
  • A caller may always ask about itself. To ask about another subject, the caller needs the scope authz:check or the permission authz.check.
  • LumoAuth implements the two evaluation endpoints of AuthZEN: evaluation and evaluations.