Access

Version 0.0.1 · Owners: @benallfree

Contracts say what each operation needs: a scope (billing, billing:refunds), and for an operation on someone's data, whose data it is. They never say who holds a scope. This extension does: it's the app's policy, a list of rows, each naming an auth collection, optionally a filter on the caller's own record, and the grants the row gives.

_superusers                         => *@all             (fixed)
users                               => billing           (Billing's default: owners manage their own)
*                                   => llm:usage         (LLM's default: every account reads its own usage)
users where role = 'billing_admin'  => billing:refunds, billing:read@all

A caller holds the union of the grants of every row it matches; matching none, nothing.

  • A grant is a scope and a reach. billing lets an owner act on their own items; billing@all on anyone's. A scope covers its :read half, and * covers every scope.
  • Superusers hold *@all. That row is fixed: no layer can change or remove it, so a superuser can't lock themselves out.
  • Tokens narrow, never widen. An API token or an MCP connection does what its scopes allow, within what its account holds; the reach comes from the account.
  • Changes apply on the next call. Grants are cached per account for ten seconds and dropped when that account's record changes or the policy is saved, so a role taken away stops working at once.

It provides the access/v1 contract.

Install · Configure · Use · Integrations · Example

Install

It's built into pocketone and on in every app: a member of the standard set, so a Go build made with standard.New() has it too. On its own:

extkit.Setup(app, []extkit.Extension{access.New()}, nil, nil)

The module is pocketone.dev/ext/access. Without it (or with access: {enabled: false}), only superusers hold anything.

Configure

Three layers, each on top of the one before:

  1. Extension defaults, registered in Go (accessv1.AddDefault).
  2. pocketone.yaml, under access: rows:.
  3. Settings → Access, in the app's dashboard (/_/): a switch for each default and pocketone.yaml row, and Rows, one per line.
access:
  rows:
    - id: refunds
      who: users
      where: "role = 'billing_admin'"
      grant: [billing:refunds, billing:read@all]
      description: Billing admins refund and see every customer's items.
Key What it does
who An auth collection's name, or * for every auth collection. Not _superusers: they hold everything already.
where Optional. A PocketBase filter run against the caller's own record, the syntax of API rules: role = 'admin', roles.name ?= 'billing_admin', verified = true. Needs one collection, not *.
grant Scopes, each optionally ending in @all.
id Optional; names the row so Settings can switch it off (default yaml.<n>).
description Shown in Settings.

In Settings, a row is a line: who [where filter] => grant, grant, such as users where role = 'support' => kv:read. A row whose collection isn't an auth collection, whose filter doesn't run or whose grants aren't scopes is refused when saved, and a bad pocketone.yaml row stops the app at start.

pocketone.yaml reference

access:

The app's policy: which auth collections (and which of their records, by a filter) hold which scopes. Superusers always hold everything. Settings → Access adds rows and switches these off.

Key Type Default Description
enabled bool On unless false. Off, only superusers hold anything.
rows list of object Rows of the policy, after the extensions' defaults: who (an auth collection, or * for all of them), where (a filter on the caller's own record, optional) and grant (scopes, each optionally ending in @all to reach every owner's data). A superuser may switch any of them off in Settings → Access.

Use

Ask what a caller holds:

gs, err := extkit.GrantsOf(ctx, app, auth) // nil for anonymous; "*@all" for superusers
ok, all := gs.Allows("billing:refunds", false)

Contract operations don't need this: the gate in extkit.Run checks every call before its provider sees it. List the policy with accessv1.Contract.Of(app) and Rows(ctx).

An extension whose users should hold one of its scopes out of the box ships a default:

accessv1.AddDefault(app, "billing", accessv1.Default{
	Name:        "owners",
	Who:         "users",
	Grant:       []string{"billing"},
	Description: "Owners manage their own billing.",
})

Integrations

  • Settings: the Access section.
  • KV: Settings → Access is stored at _access/settings.
  • API tokens: an OAuth consent screen offers only the contract scopes the account holds.
  • MCP: tools/list shows each caller only the contract tools its grants allow.

Example

The example app gives users with role = 'billing_admin' refunds, on top of the defaults.

// Command example is a PocketBase app whose policy gives billing admins refunds. Run `go run . serve`, then open
// Settings → Access in the dashboard.
package main

import (
	"log"

	"ganigeorgiev.com/pocketbase/pocketbase"

	"pocketone.dev/ext/access"
	accessv1 "pocketone.dev/ext/contracts/access/v1"
	"pocketone.dev/ext/extkit"
)

func main() {
	app := pocketbase.New()
	if _, err := extkit.Setup(app, []extkit.Extension{access.New()}, nil, nil); err != nil {
		log.Fatal(err)
	}
	accessv1.AddDefault(app, "example", accessv1.Default{
		Name:        "refunds",
		Who:         "users",
		Where:       "role = 'billing_admin'",
		Grant:       []string{"billing:refunds"},
		Description: "Billing admins may refund.",
	})
	if err := app.Start(); err != nil {
		log.Fatal(err)
	}
}

License

MIT