abac-wasm: Browser & Node.js
abac-wasm provides WebAssembly bindings for the abac-rs engine, bringing attribute-based access control to browser and Node.js environments through a JSON-based API.
Quick Start
import init, { AbacPolicyWasm } from 'abac-wasm';
await init();
const policy = new AbacPolicyWasm();
// Add a rule
policy.addRuleFromJson(JSON.stringify({
name: "allow-engineers-read",
dimensions: {
role: [{ type: "String", value: "engineer" }],
action: [{ type: "String", value: "read" }],
resource: "all"
},
rule_type: "Allow",
enabled: true
}));
// Evaluate a request
const result = policy.evaluateJson(JSON.stringify({
dimensions: {
role: { value: { type: "String", value: "engineer" } },
action: { value: { type: "String", value: "read" } },
resource: { value: { type: "String", value: "dashboard" } }
}
}));
const decision = JSON.parse(result);
console.log(decision.allowed); // true
Rule Builder
Use AbacRuleBuilderWasm to construct rules without manual JSON:
import { AbacRuleBuilderWasm } from 'abac-wasm';
const builder = new AbacRuleBuilderWasm("allow-admin-all");
builder.addDimension("role", JSON.stringify([
{ type: "String", value: "admin" }
]));
builder.addDimensionAll("resource"); // wildcard — matches any resource
builder.setEnabled(true);
const ruleJson = builder.buildJson();
policy.addRuleFromJson(ruleJson);
Custom Matchers
Register JavaScript callbacks for custom matching logic on specific dimensions. This is useful for threshold comparisons, range checks, or any matching that goes beyond exact equality:
policy.registerMatcher("temperature_high", (ruleValue, requestValue, requestGroups) => {
if (ruleValue === "all") return true;
if (!Array.isArray(ruleValue)) return false;
const actual = requestValue.type === "Float" ? requestValue.value : null;
if (actual == null) return false;
return ruleValue.some(t => {
const threshold = t.type === "Float" ? t.value : null;
return threshold != null && actual >= threshold;
});
});
Error Handling
All errors are structured JavaScript objects with type and message fields:
try {
policy.addRuleFromJson("invalid json");
} catch (e) {
console.log(e.type); // "JsonError"
console.log(e.message); // parsing error details
}
Error types: JsonError (malformed JSON), PolicyError (rule limit or
conflict), ValidationError (invalid field values).
API Reference
See the full API listing in API Documentation.
For build instructions, see WebAssembly Builds.