Policies and access
Problem. Some operations need a signed-in user, some need ownership, some need a tenant. You want that stated once, in meaning.
Pattern. Declare a policy (what it requires and provides, and optionally a rule) and attach it to operations, groups or the app.
import {
context,
dryv,
expression,
field,
operation,
policy,
property,
schema,
} from "@dryvcode/author";
const users = dryv.group({ name: "users" });
const values = users.properties({ UserId: property.string.uuid() });
const schemas = users.schemas({
User: schema({ id: field(values.UserId).system() }),
});
// A reusable boolean rule from a closed vocabulary
const expressions = users.expressions({
IsOwner: expression.equal(
context("actor", "id"),
context("resource", "ownerId"),
),
});
const policies = users.policies({
// Authentication: provides the current user's id to operations that require it
CurrentUser: policy()
.role("authentication")
.provides({ userId: values.UserId }),
// Authorization: what the caller and the record must look like, and the rule that decides
CanRead: policy()
.requires({ actor: schemas.User, resource: schemas.User })
.provides({ id: values.UserId })
.rule(expressions.IsOwner),
});
const GetMe = operation()
.role("detail")
.input({ id: values.UserId })
.output(schemas.User)
.requires(policies.CurrentUser, policies.CanRead);
Scope a policy to many things
users.policies(policies.CurrentUser); // every declaration in the group
dryv.app({ name: "example" }).groups(users).policies(policies.CurrentUser); // the whole application
Authoring records only the declared scope. Working out what is effectively inherited is the runtime's job.
What Dryv does and does not do
- Dryv records the requirement and the rule, so packs can generate guards, checks or documentation.
- Dryv does not evaluate policies at runtime. Your application obtains trusted values and enforces the rule.
- Expressions are a closed vocabulary:
equal, date and time helpers,value(...), andcontext(...). There is no arbitrary-code escape hatch, which keeps rules portable.
See Policies for the full contract.