@vielzeug/ward
v26.10.17
Published
Ordered authorization rules with immutable policies and typed decisions
Readme
@vielzeug/ward
Ordered authorization rules with immutable policies and typed decisions
Installation
pnpm add @vielzeug/ward
npm install @vielzeug/ward
yarn add @vielzeug/wardQuick Start
import { ANONYMOUS, WILDCARD, allow, createWard, deny, predicate } from '@vielzeug/ward';
type Action = 'read' | 'update';
type Resource = 'posts';
type Attributes = { authorId: string };
const ward = createWard<Action, Resource, Attributes>([
deny('blocked', WILDCARD, [WILDCARD]),
allow(ANONYMOUS, 'posts', ['read']),
allow('viewer', 'posts', ['read']),
allow<Action, Resource, Attributes>('editor', 'posts', ['update'], {
when: predicate.owns<Attributes>('authorId'),
}),
]);
const principal = { id: 'u1', roles: ['editor'] };
const decision = ward.decide({
action: 'update',
attributes: { authorId: 'u1' },
principal,
resource: 'posts',
});
if (decision.effect === 'allow') {
console.log('update permitted');
}Documentation
License
MIT © Helmuth Saatkamp: part of the Vielzeug monorepo.
