Skip to content

@chamvi/access ​

Permission checks over authup permission bindings, the same in Node.js and the browser. The public API of packages/access/src/index.ts; @chamvi/core, @chamvi/http, @chamvi/app (which re-exports it) and the dashboard use it.

bash
npm install @chamvi/access --save

It depends on @authup/access and on no other @chamvi/* package. See Permissions and authup for the names, their attributes and the authup setup.

Checks ​

ts
function can(holder: PermissionHolder, name: string, options?: PermissionCheckOptions): Promise<boolean>;
function canSome(holder: PermissionHolder, name: string, options?: PermissionCheckOptions): Promise<boolean>;

can is true when the holder's bindings or its authorization pass for the resource in options.attributes; without attributes a policy over them fails (the check asks about every resource). Both are evaluated by createAuthorizationSnapshotEvaluator of @authup/access: the bindings as a snapshot without grants (bindings of one name are alternatives), the authorization as authup's two-layer snapshot with the holder's identity and realm as the resource realm. canSome counts a policy whose data is missing as passed (may the holder do this to some resource?). options.now feeds the date and time policies (default the real clock), the holder's identity the identity policies. Both never reject: an entry that is not a binding, a binding with a realmMatch node, a policy that cannot be evaluated, a malformed authorization or one without an identity grants nothing. Built evaluators are cached per holder object; treat a holder as immutable.

Names and helpers ​

ts
const Permission: {
    AGENT_READ: 'chamvi_agent_read';
    AGENT_RUN: 'chamvi_agent_run';
    AGENT_APPROVE: 'chamvi_agent_approve';
    RUN_READ: 'chamvi_run_read';
    RUN_CANCEL: 'chamvi_run_cancel';
    STATE_READ: 'chamvi_state_read';
    STATE_WRITE: 'chamvi_state_write';
    CONNECTION_READ: 'chamvi_connection_read';
    CONNECTION_WRITE: 'chamvi_connection_write';
    CONNECTION_CREATE: 'chamvi_connection_create';
    TOOL_USE: 'chamvi_tool_use';
    KNOWLEDGE_READ: 'chamvi_knowledge_read';
    KNOWLEDGE_WRITE: 'chamvi_knowledge_write';
    COMPONENT_READ: 'chamvi_component_read';
};
const PERMISSION_NAMES: readonly string[];                     // the fourteen, in this order

function isPermissionBinding(value: unknown): value is PermissionBinding;            // upstream's isPermissionPolicyBinding
function isPermissionAuthorization(value: unknown): value is PermissionAuthorization; // shallow shape check
function allPermissions(): PermissionBinding[];                // one unrestricted binding per name
function permissionNames(holder: PermissionHolder): string[];  // names of the bindings and of the snapshot's grants
ts
/*
 * Copyright (c) 2026.
 * Author Peter Placzek (tada5hi)
 * For the full copyright and license information,
 * view the LICENSE file that was distributed with this source code.
 */

import type { AuthorizationSnapshot, PermissionPolicyBinding } from '@authup/access';

/**
 * One grant: a permission name and the policies that must pass for it,
 * authup's own binding shape with resolved policy trees (plain data).
 */
export type PermissionBinding = PermissionPolicyBinding;

/** What an `identity` policy needs; set by authup(), absent for tokens. */
export type PermissionIdentity = {
    type: 'user' | 'client';
    id: string;
    realmId?: string | null;
    realmName?: string | null;
    clientId?: string | null;
};

/**
 * authup's two-layer authorization of an identity: the definitions a check
 * may ask for and the identity's grants (plain JSON, `buildAuthorizationSnapshot`).
 */
export type PermissionAuthorization = {
    snapshot: AuthorizationSnapshot;
    /** The realm chamvi's resources belong to; every check reaches into it. */
    realm: string;
    /**
     * The owner client id. Present, every check asks only that client's
     * definition in `realm`; absent, the global then the realm-owned one.
     */
    client?: string;
};

/** Anything that holds grants: a principal, a run's invoker. */
export type PermissionHolder = {
    /** Self-contained bindings (static tokens, oidc mappers, direct dispatch). */
    bindings: readonly PermissionBinding[];
    /** Set by authup(): the identity's snapshot, evaluated together with `identity`. */
    authorization?: PermissionAuthorization;
    identity?: PermissionIdentity;
};

export type PermissionCheckOptions = {
    /** The resource. Absent: the check asks about every resource of that kind. */
    attributes?: Record<string, unknown>;
    /** Feeds the date and time policies. Default: the real clock. */
    now?: Date;
};