@protobase/cli 0.28.4 → 0.29.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
1
1
  //#region package.json
2
- var version = "0.28.4";
2
+ var version = "0.29.1";
3
3
  //#endregion
4
4
  export { version };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@protobase/cli",
3
- "version": "0.28.4",
3
+ "version": "0.29.1",
4
4
  "description": "The protobase command: scaffold, doctor, dev, build and serve a Protobase project.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -35,11 +35,11 @@
35
35
  "dependencies": {
36
36
  "@hono/node-server": "^2.1.3",
37
37
  "@hono/vite-dev-server": "^0.26.1",
38
- "@protobase/layout": "0.28.4",
39
- "@protobase/query": "0.28.4",
40
- "@protobase/schema": "0.28.4",
41
- "@protobase/server": "0.28.4",
42
- "@protobase/ui": "0.28.4",
38
+ "@protobase/layout": "0.29.1",
39
+ "@protobase/query": "0.29.1",
40
+ "@protobase/schema": "0.29.1",
41
+ "@protobase/server": "0.29.1",
42
+ "@protobase/ui": "0.29.1",
43
43
  "@react-email/render": "^2.1.0",
44
44
  "@scalar/hono-api-reference": "^0.12.10",
45
45
  "@tailwindcss/vite": "^4.3.3",
@@ -96,7 +96,9 @@ A refusal or a missing record rejects with the problem the REST API would answer
96
96
 
97
97
  ### Roles
98
98
 
99
- `defineFunction({ roles: ['mechanic', 'desk'] }, handler)` lets in callers with at least one of those roles. Anyone else gets a `403 function-forbidden` problem before the handler runs. Finer rules belong in the resources' [access rules](../reference/access.md)), which `records` already applies.
99
+ `defineFunction({ roles: ['mechanic', 'desk'] }, handler)` lets in callers with at least one of those roles. `defineFunction({ capability: 'vms.update' }, handler)` lets in callers whose roles hold all of that capability ([`roles.holds`](../reference/access.md#wildcards-in-has-can-and-holds))): `*`, `vms.*` or a plain `vms.update`, but not `vms.update.own` or a `vms.update` limited to fields or rows. A wildcard means all of it, so `vms.*` needs a role that holds `vms.*` or `*`. Anyone else gets a `403 function-forbidden` problem before the handler runs. Finer rules belong in the resources' [access rules](../reference/access.md)), which `records` already applies.
100
+
101
+ In an app with [roles](../reference/access.md)) (`options: { roles }`), a function with neither is closed like a resource without a rule: only callers whose roles hold `*`, such as `admin: ['*']`, may call it, unless `defaultAccess` is `allow`. A function for everyone signed in says so with its roles. In an app without roles, every signed-in caller may call it. A `capability` needs the app's roles, and a function takes `roles` or `capability`, not both: the server refuses to start otherwise.
100
102
 
101
103
  ## Public functions and webhooks
102
104
 
@@ -12,6 +12,7 @@ Roles are defined in code ([`defineRoles`](https://docs.protobase.net/api/schema
12
12
  - A project without roles (the single-admin case) is **default allow**.
13
13
  - `defaultAccess: 'allow'` together with `roles` is an explicit opt-in. Print `accessWarnings(options)` at startup.
14
14
  - Every request needs a token, unless the roles define [`anonymous`](#anonymous-access).
15
+ - An [API function](../guides/api-functions.md#roles)) without `roles` or a `capability` is closed the same way: with roles, only a caller holding `*` may call it.
15
16
 
16
17
  ## Capabilities
17
18
 
@@ -33,6 +34,13 @@ A field is readable when the role's list allows it and the field's own [rule](#f
33
34
 
34
35
  **Several roles add up.** A user gets every field one of their roles lists (all of them when a role holds the plain capability) and the rows any of their roles allows. Fields and rows add up separately: someone holding both `erp_sync` and `reseller` above reads `name` on unpublished products too. With Better Auth a session acts with one [active role](auth.md#active-role)) at a time, so this only comes up with an authenticator that hands out several.
35
36
 
37
+ ### Wildcards in has, can and holds
38
+
39
+ A wildcard in a role grants everything it stands for: `*.read` reads every resource. What a check asks for is different, so the checks take wildcards in one place only:
40
+
41
+ - `roles.has(user, 'vms.update')` and `roles.can('vms.update')` ask for one capability, in any scope or limit the role holds it. They refuse a wildcard, at compile time and when called, since `vms.*` would pass for a role holding only `vms.read`: ask for each capability instead.
42
+ - `roles.holds(user, 'vms.*')` asks for all of it, in every row and field: a role holding `*` or `vms.*` passes, one holding `vms.read`, `*.read`, `vms.update.own` or a `vms.update` limited to fields or rows does not. A scope asks for those rows at least: `orders.read.own` passes for a role holding `orders.read.own`, `orders.read` or `*.read`. API functions' `capability` is checked this way.
43
+
36
44
  ## Anonymous access
37
45
 
38
46
  The role `anonymous` is what a request without a token acts as. A project without it answers such requests `401`, as always.
@@ -254,7 +254,7 @@ The guardrails:
254
254
  - **Permission:** the ID token's `groups` claim must hold the configured group (default `protobase-staff-access`).
255
255
  - **A strong, recent sign-in:** the provider must report a passkey or a second step (`amr` with `mfa`, `hwk` or `swk`, or an `acr` from `acrValues`), at most 15 minutes old (`auth_time`; the app asks with `max_age`). The person's own two-factor and passkey requirements do not apply to staff.
256
256
  - **A reason**, kept in the log with who signed in as whom.
257
- - **Short and stoppable:** the session lasts 30 minutes (`sessionMinutes`, at most 240), is never extended, ends with the browser, and "Stop staff session" (`POST /api/auth/staff/stop`) ends it and signs the browser out. An API token it got before keeps working until it expires, at most 15 minutes later, like any other.
257
+ - **Short and stoppable:** the session lasts 30 minutes (`sessionMinutes`, at most 240), is never extended, ends with the browser, and "Stop staff session" (`POST /api/auth/staff/stop`) ends it and signs the browser out. An API token it got before stops working on its next request, like that of any [ended session](#when-a-token-stops-working).
258
258
  - **No takeover:** a staff session cannot change the person's password, email, passkeys, two-factor authentication or sessions, nor save the sign-in policy (`403`, `STAFF_CANNOT_CHANGE_SIGN_IN`).
259
259
  - **The app decides:** admins turn staff sign-in off, or have the person emailed each time, with the [sign-in policy](#sign-in-policy).
260
260
  - **One way in:** Better Auth's own `/admin/impersonate-user` and `/admin/stop-impersonating` are off (`404`), so app admins cannot sign in as their people.
@@ -424,7 +424,7 @@ It is Better Auth's organization plugin, with its tables named in snake_case (`o
424
424
 
425
425
  **Who gives which role.** Someone may give or take away a member's app role only if they hold it, in that organization or globally, or hold a role whose `grants` list it (`manager` above gives `accountant` without holding it); grants do not chain, and a superuser may give any. Only a superuser changes global roles. The creator of an organization becomes its owner with every app role (`creatorAppRoles`, default `'all'`), so they can give them on; for a role added later, list it in a role owners hold, or run `protobase organizations add-app-role <role> --to owners`.
426
426
 
427
- **The organization in the token.** A new session starts in the organization the person last worked in, else their oldest membership. `/api/auth/token` adds `org` (the organization), `org_role` and `app_roles` (the membership's, narrowed to the active role), read when the token is issued, so a removed member or a changed role shows within its 15 minutes. `betterAuthAuthenticator` makes `org` the tenant and `ctx.organization.id`, and adds `app_roles` to the global roles. `POST /api/auth/organization/switch` with `{ organizationId }` moves the session; the next token names the new organization, without signing in again. Someone with a global role may switch into any organization, without being a member, and sees there what those roles allow; each such entry is an `organization.entered` [audit event](api.md#audit-events)). `GET /api/auth/organization/search?query=&offset=` lists every organization for them, ordered by slug, 20 at a time with the `total`, narrowed by text in the name or slug; the Organizations page shows it as All organizations. A list across organizations stays a resource without `.tenant`, readable by the global role.
427
+ **The organization in the token.** A new session starts in the organization the person last worked in, else their oldest membership. `/api/auth/token` adds `org` (the organization), `org_role` and `app_roles` (the membership's, narrowed to the active role), read when the token is issued; a removed member's token, or one carrying an app role taken away, [stops working](#when-a-token-stops-working) on its next request. `betterAuthAuthenticator` makes `org` the tenant and `ctx.organization.id`, and adds `app_roles` to the global roles. `POST /api/auth/organization/switch` with `{ organizationId }` moves the session; the next token names the new organization, without signing in again. Someone with a global role may switch into any organization, without being a member, and sees there what those roles allow; each such entry is an `organization.entered` [audit event](api.md#audit-events)). `GET /api/auth/organization/search?query=&offset=` lists every organization for them, ordered by slug, 20 at a time with the `total`, narrowed by text in the name or slug; the Organizations page shows it as All organizations. A list across organizations stays a resource without `.tenant`, readable by the global role.
428
428
 
429
429
  **Invitations.** Better Auth's `POST /api/auth/organization/invite-member` with `{ organizationId, email, role, appRoles }`; inviting again replaces the earlier invitation, and it works for `invitationDays` (default 7). The email links to `{baseURL}/-/invitation?token=…` (`invitationUrl`), with a token signed with `secret`, since members can read invitation ids; without a mailer, the answer carries the `link` to pass on. The link page shows `GET /api/auth/invitation?token=`, accepts with `POST /api/auth/invitation/accept` for someone signed in as the invited address, and makes an account with `POST /api/auth/invitation/sign-up` (`{ token, name, password? }`) for an address without one. The link page and its sign-up serve [invitations to the app](#users-page) too. [Open sign-up](#open-sign-up) is not available with organizations: an invitation is the address someone vouched for.
430
430
 
@@ -444,6 +444,22 @@ Sign in at `POST /api/auth/sign-in/email` (or with a code or passkey), then `GET
444
444
 
445
445
  `jwtAuthenticator` accepts asymmetric signature algorithms only unless you pass `algorithms` explicitly (the tests do, for HS256), so a token cannot be forged with a public key used as an HMAC secret; Better Auth signs with its own key pair and publishes the public keys at `/api/auth/jwks`. An expired, tampered, wrongly signed or missing token is a `401` problem with `WWW-Authenticate: Bearer`; with an [anonymous role](access.md#anonymous-access)), a missing one on the resources API is served as that role instead.
446
446
 
447
+ ### When a token stops working
448
+
449
+ With `auth`, `betterAuthAuthenticator` checks on every request that the token still stands, so these take effect on the next request every route that authenticates sees (the resources API, `/api/meta`, batches, functions, files and new `/live` streams), not when the token expires:
450
+
451
+ | Change | The token |
452
+ | --- | --- |
453
+ | A ban (`/admin/ban-user`, `protobase users disable`) | is refused until the ban runs out or is lifted; a ban ends the sessions too, so a new token takes signing in again. |
454
+ | A role taken away (`/admin/set-role`, `setUserRole`, a membership's app roles) | carrying that role is refused; the client's retry gets a token from its session with the roles left. A token carrying fewer roles than held keeps working. |
455
+ | The user deleted | is refused. |
456
+ | Signing out, a revoked session, a stopped staff session | of that session is refused; the person's other sessions keep theirs. |
457
+ | Removal from the organization | naming that organization is refused, unless a global role still enters it. |
458
+
459
+ A refused token is a `401`, as an expired one is, and the client retries the request once with a new token from `/api/auth/token`. Tokens carry their session's id in `sid`; one from [`issueToken`](#tokens-for-scripts-and-curl) names none, and is checked against the user alone.
460
+
461
+ The cost is two or three lookups by key in the auth store per request: the session by `sid`, the user by `sub`, and with organizations the membership. Nothing is cached, so every server process sees a change at once. An open `/live` stream was admitted when it connected and keeps running until it closes. With `jwksUrl` instead of `auth`, the API cannot read the auth store and a token works until it expires, as do tokens of [other apps](#users-of-another-app).
462
+
447
463
  ## Users of another app
448
464
 
449
465
  An API can admit the signed-in users of another Protobase app with the tokens that app issues, so that app's admin app can draw its resources as [remote resources](remote-resources.md)). `betterAuthAuthenticator`'s `apps` lists each such app by its `baseURL`, the issuer of its tokens, with the roles its users' global roles become here:
@@ -479,7 +495,7 @@ import { issueToken } from '@protobase/server'
479
495
  const { token, expiresAt } = await issueToken(auth, { email: 'you@example.com', ttlSeconds: 3600 }) // default 15 minutes, at most 24 hours
480
496
  ```
481
497
 
482
- It is signed with the same keys and carries the same claims as a token from `/api/auth/token`, with the role the user last chose, else their first, so the API treats it as any other. Banned and unknown users get an error. There is no HTTP route for this.
498
+ It is signed with the same keys and carries the same claims as a token from `/api/auth/token`, with the role the user last chose, else their first, so the API treats it as any other, and a ban, a role taken away or the user deleted ends it before it expires. Banned and unknown users get an error. There is no HTTP route for this.
483
499
 
484
500
  ## Several origins
485
501
 
@@ -423,7 +423,7 @@ Run inside a project whose `protobase.config.ts` exports `auth` (Better Auth). T
423
423
 
424
424
  The password is typed at a hidden prompt with confirmation, read from stdin with `--password-stdin` (for scripts, e.g. `pass show erp | protobase users create me@example.com --password-stdin`), or generated with `--generate-password` and printed once. `--no-password` makes an account without one, which signs in by [emailed code](auth.md#emailed-sign-in-codes)), passkey or a sign-in provider, as with an app whose [sign-in policy](auth.md#sign-in-policy)) turns passwords off. A password is never accepted as an argument, since it would end up in shell history. `users list` prints email, role and creation date (and `disabled` for banned users), never credentials.
425
425
 
426
- `delete` asks you to type the email back (or pass `--yes`), and needs a terminal otherwise. `delete`, `set-role` and `disable` refuse to touch the last admin, so the project cannot lock itself out. `disable` bans the user and revokes their sessions; tokens already issued stop working within their lifetime (15 minutes by default), and `protobase token` refuses disabled users. `enable` lifts the ban. `protobase dev` prints "No users yet. Create the first admin with: protobase users create you@example.com" at startup while the user table is empty.
426
+ `delete` asks you to type the email back (or pass `--yes`), and needs a terminal otherwise. `delete`, `set-role` and `disable` refuse to touch the last admin, so the project cannot lock itself out. `disable` bans the user and revokes their sessions; tokens already issued stop working on their next request, and `protobase token` refuses disabled users. `enable` lifts the ban. `protobase dev` prints "No users yet. Create the first admin with: protobase users create you@example.com" at startup while the user table is empty.
427
427
 
428
428
  With [organizations](auth.md#organizations)), `--role` is the user's global role (none by default) and `--organization` adds the new user to an organization, with `--org-role` (`owner`, `admin`, or `member`, the default) and `--app-roles` there.
429
429