Grant a plan
Entitlements describes bundles, grants, and who holds them.
Before you start
- The
identityhelper, and a config whose catalog has thereports.exportentitlement, from IdentityHound quickstart. - Ana's workspace,
acme, andcheck, whose export check Acme's missing entitlement denies, from IdentityHound quickstart.
This page's code continues the script of the pages above: run its blocks after theirs, in the same file. To run them as a script of their own, first copy each helper from the page that defines it, and set each other value, such as a key or an ID, to one you already have.
1. Define the bundle
Your server creates the bundle once, with POST /v1/identity/bundles. Here pro holds the reports.export entitlement:
await identity('POST', '/bundles', {
key: 'pro',
name: 'Pro',
items: [{ entitlement: 'reports.export' }],
});
Creating the bundle can answer entitlement_unknown.
2. Grant it once per order
When Acme buys Pro, your server grants the pro bundle to the workspace with POST /v1/identity/grants. Send the order ID as the required Idempotency-Key header, so an authorized retry answers with the same grant, as Retrying a write describes:
const order = { 'idempotency-key': 'order-1001' };
const grant = {
target: { kind: 'bundle', bundle: 'pro' },
holder: { kind: 'workspace', id: acme.id },
provenance: { source: 'purchase', reference: 'order-1001' },
};
const first = await identity('POST', '/grants', grant, order);
const retry = await identity('POST', '/grants', grant, order);
console.log(first.grant.id === retry.grant.id); // true
Use your app’s own label for provenance.source; Entitlements explains the label. Use a new key for each order: the grant keeps its key with no time limit, and Create a grant has the rule in full. To end a plan on a date, send expiresAt with the grant.
Check it worked
Run the export check again:
const afterGrant = await check('export');
console.log(afterGrant.allowed, afterGrant.reason); // true workspace_role
The grants reference covers revoking a grant, and the entitlements reference lists and checks what a principal holds.