RolebookDocs

Resource sharing

@rolebook/sharing: tenant-scoped membership, ownership, invitations, and audit, decided by Rolebook.

Most products need more than workspace roles: a project shared with three people, a folder handed to a new owner, an invitation that expires, a contractor whose access ends on a date. @rolebook/sharing owns that layer. It manages per-resource membership, ownership transfer, resource invitations, revocation, audit events, and accessible-resource listing, and it delegates every final decision to a Rolebook authorizer built from the exact policy in its definition.

The division of labour

LayerOwns
Identity provider (Better Auth, Clerk, WorkOS, your own)Users, organizations or workspaces, workspace members and roles, workspace invitations.
@rolebook/sharingPer-resource ownership, memberships, resource invitations, audit, listing.
Storage adapter (PostgreSQL, SQLite)The sharing tables, locks, fences, and atomic commits in your own database.
The Rolebook engine (rolebook)Every allow or deny.

Workspace membership is eligibility, never access. A member of the workspace sees a private project only when they own it, hold a membership on it (directly or through a team), or hold a workspace role your identity adapter explicitly maps to Rolebook assignments.

How a request is decided

For every operation, the module:

  1. Captures the input as immutable data, with an explicit tenant ID and actor.
  2. Asks the identity adapter whether the actor is an eligible member of that tenant, and for any mapped workspace assignments and current groups.
  3. Asks the resource type’s storage adapter for an authoritative snapshot of the resource, its owner, and the memberships of the actor and its groups, fencing the actor’s eligibility in the same read.
  4. Projects ownership and memberships into Rolebook assignments, binds them with the identity adapter’s assignments, and asks Rolebook.
  5. For a mutation, applies it in the storage adapter’s transaction, conditional on the resource version and sharing revision it read, with an audit event in the same commit. A concurrent change produces a conflict, and the module re-runs the whole sequence.

What you can ask

Beyond commands that change access, the module answers the questions your screens and jobs ask. Every access decision is made by the Rolebook engine; a database query only ever narrows candidates.

QuestionCall
May the actor do this to this resource?authorize()
What may the actor do to this resource, action by action?permissions()
Who owns this resource, who are its members, which invitations are pending?describeAccess()
Which resources of a type may the actor perform one action on?listAccessible()
Which resources of a type may the actor do anything on, and what exactly?listAccess()
Which rows of my own table, filtered and sorted my way, may the actor open?Named authorized queries
Why can or can’t a named subject do this, right now?explain(), opt-in
Who changed access to this resource, and when?auditHistory()

Every call runs in the named tenant under the bound actor’s authority, and all but one answer about that actor. The exception is explain(), the opt-in privileged inspection: the actor must hold the resource’s inspect permission, and the answer explains the subject named in the request. describeAccess() reports what is stored on one resource (its owner, members, and pending invitations); it does not enumerate everyone whom a workspace role, a group, or a condition would also allow.

Each page of listAccessible() and listAccess() is consistent within the page. Its candidate scan, the principal, each examined resource, the policy runtime, and the instant used for every expiry comparison belong to one coherent read point, so both the entries and the exhausted flag describe a state that existed. Every page takes a fresh snapshot: a cursor records a position and does not freeze the result set across pages, and a page is not kept current after it returns.

That depends on configuration. The storage adapter has to hold one snapshot for the page: the in-memory adapter holds its own, and a SQL adapter needs a supported executor, which is the package’s checked executor on PostgreSQL and the worker-backed executor over a database file on SQLite. The identity source has to supply evidence for the same point. The identity adapters for Better Auth, Clerk, and WorkOS, and the withTeams() wrapper, answer from the provider and do not supply that evidence. For listing, the identity adapter you pass to createSharing() must itself resolve principals from a complete local identity projection in the same database, kept synchronized as authoritative data, and that same adapter object is named with the projection in the storage adapter’s pageIdentity option; or it must be an adapter of your own that implements resolvePagePrincipal over a source with a non-repeating revision. See listing page evidence. When the configured adapter, executor, or identity source cannot provide a consistent page, the call rejects with AccessListingUnavailableError and a reason; it does not return a partial or weaker page. Listing accessible resources has the requirements and the reasons.

To give other services a plain Core check over HTTP, see the decision service; to serve these Sharing operations to a browser or an SDK, see the HTTP API.

Packages

PackageRole
@rolebook/sharingThe module, the adapter contracts, an in-memory adapter for tests.
@rolebook/sharing-postgresORM-neutral PostgreSQL adapter: SERIALIZABLE transactions, row-lock fences, migrations.
@rolebook/sharing-sqliteDriver-neutral SQLite adapter: BEGIN IMMEDIATE fencing, migrations, bun:sqlite and libSQL executors.
@rolebook/sharing-better-authIdentity from Better Auth organizations.
@rolebook/better-authDrop-in Better Auth plugin: endpoints, migrations, client plugin, and React bindings.
@rolebook/sharing-clerkIdentity from Clerk organizations.
@rolebook/sharing-workosIdentity from WorkOS User Management, plus Directory Sync groups.
@rolebook/teamsTeam principals: flat directories, an identity wrapper, PostgreSQL fences, and an optional nested-lineage directory.
@rolebook/sharing-testkitThe security contract every adapter must pass.
@rolebook/http, server, clientThe sharing operations over HTTP, plus signed access-change webhooks.
@rolebook/reactAccessible React sharing components and headless state over @rolebook/client.

Invariants the module enforces

  • Every operation names its tenant explicitly. A session’s active workspace is never tenancy evidence. Malformed tenant or actor IDs fail closed before any callback runs.
  • Resources are loaded by (tenantId, resourceId) from the storage adapter. A client-supplied ID of the right type is not provenance.
  • Ordinary member roles never include ownership. Only transferOwnership replaces the owner, atomically.
  • An actor cannot grant, change, or revoke its own membership, and a manager other than the owner can hand out only access it holds itself: the delegation ceiling.
  • A denial does not confirm that a resource exists: a caller with no relationship to it gets the same answer as for a missing resource.
  • Invitation secrets are returned once. Storage keeps only a keyed digest, the recipient binding, expiry, and consumption or revocation state.
  • Sharing state changes and their audit events commit atomically. Access changes can be delivered to other systems after commit as signed webhooks; other consumers drain the audit table themselves.