Skip to main content
From Cptn Fizzbin
KeyCard Logo

KeyCard

Cross-language Permissions

Define in
Java
Serialize to
ProtoBuf
Enforce in
JavaScript

Pre-alpha: the API and policy format may still change without notice.

I

Define

Build a policy from a fluent, type-safe PolicyBuilder API to create a Policy and/or a PolicyDefinition

// UserPolicy.java
Action Create = new Action("Create");
Action Update = new Action("Update");
Action Delete = new Action("Delete");

Subject<Article> ArticleSubject = new Subject<>("Article");

Policy policy = new PolicyBuilder()
.allow(Create, ArticleSubject)
.allow(Update, ArticleSubject, Condition.field(Article::getOwnerId).eq(1))
.deny(Delete, ArticleSubject, Condition.field(Article::getStatus).ne("archived"))
.build();

if (policy.can(Create, ArticleSubject))
createArticle();

if (policy.can(Update, ArticleSubject.wrap(article)))
updateArticle(article);

PolicyDefinition policyDef = policy.getDefinition();
II

Seralize

A PolicyDefinition is a simple JSON encodeable object. Ship it to a browser, a mobile client, or a service in another language

# policy.yaml
version: 1.0
rules:
- [ allow, Create, Article ],
- [ allow, Update, Article, { ownerId: 1 } ],
- [ deny, Delete, Article, { status: { $ne: archived } } ]
III

Enforce

Read the PolicyDefinition into a Policy and quickly check permissions

// policy.ts
const res = await fetch("/api/user/me/policy.yaml")
const policyDef = await res.json() as PolicyDefinition
const policy = new Policy(policyDef);

// check if user can create an article
if (policy.can(Actions.Create, Subjects.Article)) {
// create article
}

// check if user can update an article
if (policy.can(Actions.Update, Subjects.Article.wrap(article))) {
// update article
}

// throws if the user is not allowed to delete the article
policy.require(Actions.Delete, Subjects.Article.wrap(article));