Skip to main content

Ask a person for a permission, for one task

An agent that lacks a permission asks for it with a reason. A person approves, and the agent gets a token for that task only.

RecipeReadyView source
Works with
JavaScript / TypeScriptPythonGoPHPJavaC# / .NETPlain HTTP
Standards
RFC 9396

What it shows​

  1. The agent opens a task, "Pay invoice INV-7741".
  2. It asks for a permission with structured detail: the type invoices, the action approve and the record INV-7741. It adds a reason for the person who decides.
  3. The agent may only read invoices, so the request waits for a person. It appears in the approvals inbox of the portal.
  4. A person approves, and the agent collects a token that carries exactly the approved detail.
  5. The agent completes the task, and LumoAuth revokes the token.

The agent never holds the permission to approve invoices. It holds one token, for one invoice, for a few minutes.

Before you start​

  • An agent whose capabilities do not cover the request. The setup script registers the agent "Invoice agent" with the single capability invoices:read.
  • A person who decides. This person needs the permission jit.approve, which the administrators of an organization hold. The owner of an agent cannot decide the requests of that agent, and the owner is the person who registered it. The setup script therefore makes a second user, Maya Chen, the owner of the agent. You can then approve as the administrator.
  • Check that your plan includes just-in-time permissions in the plans matrix.
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_KEYFor the setup script only: an API key with the scopes admin:agents:read, admin:agents:write and admin:users:write. The agent itself needs no API key.
AGENT_CLIENT_IDThe client ID of the agent, which is its agent ID (agt_…). The setup script prints it.
AGENT_CLIENT_SECRETThe client secret of the agent. The setup script prints it.

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

node approvals/just-in-time-permission/setup.mjs
setup.mjs
// Creates what the recipe needs: an agent that may read invoices, and a user
// who owns it. Run it again at any time.
//
// node approvals/just-in-time-permission/setup.mjs
//
// The API key needs the scopes admin:agents:read, admin:agents:write and
// admin:users:write.
import { randomBytes } from 'node:crypto';
import { call, ensure, printEnv } from '../../_lib/admin.mjs';

const agent = await ensure('/admin/agents', (a) => a.name === 'Invoice agent', {
name: 'Invoice agent',
description: 'Reads invoices, and asks a person before it approves one.',
identityType: 'client_secret',
// The agent may read invoices. To approve one, it must ask.
capabilities: ['invoices:read'],
});

// LumoAuth shows a secret once, in the answer that creates it. On a second
// run the script therefore asks for a new secret, and the old one stops.
const secret =
agent.clientSecret ??
(await call('POST', `/admin/agents/${agent.agentId}/rotate-credentials`)).data.clientSecret;

// The owner of an agent cannot decide its requests, and the agent belongs to
// the person who created it: you. Give it to another person, so that you can
// approve in the portal.
const owner = await ensure('/admin/users', (u) => u.email === 'maya.chen@example.com', {
email: 'maya.chen@example.com',
name: 'Maya Chen',
// The recipe never signs in as this user, so nobody needs the password.
password: `${randomBytes(18).toString('base64url')}#Aa1`,
emailVerified: true,
});
await call('PATCH', `/admin/agents/${agent.agentId}`, { ownerId: owner.id });

console.log(`Agent "${agent.name}" is ready. Its owner is ${owner.email}.`);
printEnv({ AGENT_CLIENT_ID: agent.agentId, AGENT_CLIENT_SECRET: secret });

LumoAuth shows a secret once. A second run of the setup script therefore asks for a new secret, and the old one stops at once.

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. Each program was run, and the request was approved in the approvals inbox of the portal. The JavaScript program was also run with a denial.

approvals/just-in-time-permission/js/agent.mjs
// An agent asks a person for a permission that it lacks, for one task.
//
// node approvals/just-in-time-permission/js/agent.mjs
import { LumoAgent } from '@lumoauth/agent';

// Reads LUMOAUTH_URL, LUMOAUTH_ORG_ID, AGENT_CLIENT_ID and AGENT_CLIENT_SECRET.
const agent = new LumoAgent();

// A task is the frame of the permission: the token works for this task only.
const task = await agent.jit.createTask({ name: 'Pay invoice INV-7741', type: 'finance' });
console.log(`task: ${task.taskId}`);

// The agent may read invoices. Approval is outside its capabilities, so a
// person decides. The call waits for the decision, for 5 minutes at most.
console.log('request: waiting for a person');
const request = await agent.jit.requestPermission(
{ type: 'invoices', actions: ['approve'], identifier: 'INV-7741' },
{ justification: 'The vendor Northwind Labs asked for payment of INV-7741.' },
);
console.log(`request: ${request.status}`);

if (request.status === 'approved') {
// The token carries exactly the approved detail. Send it to the API that approves invoices.
const token = await agent.jit.getToken(request.request_id);
console.log(`token: ${token.slice(0, 12)}… valid for ${request.granted_ttl} seconds`);
}

// The end of the task ends the permission: LumoAuth revokes the token.
await agent.jit.completeTask();
console.log('task: completed');
node approvals/just-in-time-permission/js/agent.mjs

Each program stops at "waiting for a person" until somebody decides:

  1. Open the portal and go to AI Agents, then Approvals.
  2. The tab Waiting shows the request of "Invoice agent", with the reason, the type invoices and the action approve.
  3. Open the menu … at the end of the row, and click Approve. To refuse, click Deny and give a reason.

Check the result​

Output
task: task_c101cda8d1e5265a8a5fb36c31a05cbe
request: waiting for a person
request: approved
token: eyJ0eXAiOiJK… valid for 300 seconds
task: completed
  • After Deny, the second request: line says denied. The program gets no token, and it still completes the task.
  • When nobody decides within 5 minutes, the line says pending or expired. A request that has expired cannot be approved.
  • In the portal, the tab Approved of the Approvals page shows the request and the name of the person who approved it.
  • Open Audit logs under Security. The run adds the events jit.permission_approved and jit.task_completed.

Limits​

  • The token is valid for 300 seconds here. A request can ask for up to 900 seconds, and a token never lives longer than its task.
  • The detail has a fixed form: type, identifier, and the lists actions, privileges, locations and datatypes. LumoAuth refuses any other member, so an amount cannot be part of the detail.
  • A request inside the capabilities of the agent, with read-only actions, is approved at once and no person sees it. Approve routine requests by rule shows which requests a rule can approve.
  • The token says what was approved. The API that receives it must still compare the detail with the call: see Accept a just-in-time token in your API.
  • A token of the agent cannot approve a request. The decision needs a person: in the portal, or in your own app with an approvals inbox.