@xtandard/permissions 0.1.0 → 0.2.0
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.
- package/README.md +5 -3
- package/dist/{better-auth-NO40ZqSM.mjs → better-auth-BEmX8qyz.mjs} +28 -8
- package/dist/better-auth-BEmX8qyz.mjs.map +1 -0
- package/dist/{better-auth-Bp9YVyQa.d.cts → better-auth-DQET1sXa.d.mts} +24 -5
- package/dist/{better-auth-DHyW5-yt.cjs → better-auth-MR7dBtpo.cjs} +28 -8
- package/dist/better-auth-MR7dBtpo.cjs.map +1 -0
- package/dist/{better-auth-NSXgt6ZU.d.mts → better-auth-vgRYgjYK.d.cts} +24 -5
- package/dist/cli.cjs +12 -6
- package/dist/cli.cjs.map +1 -1
- package/dist/cli.mjs +12 -6
- package/dist/cli.mjs.map +1 -1
- package/dist/{contract-BMiOmg2f.d.cts → contract-BAisPHBw.d.cts} +30 -2
- package/dist/{contract-Yc5AZuvv.mjs → contract-BganM0fx.mjs} +34 -2
- package/dist/contract-BganM0fx.mjs.map +1 -0
- package/dist/{contract-CPmJVoRx.cjs → contract-Brnu9XXo.cjs} +45 -1
- package/dist/contract-Brnu9XXo.cjs.map +1 -0
- package/dist/{contract-Dk6d2ylD.d.cts → contract-CXPMGuHk.d.cts} +18 -2
- package/dist/{contract-D32H1t0u.d.mts → contract-D6j-yrBL.d.mts} +30 -2
- package/dist/{contract-DUzgB-He.d.mts → contract-DhS23Pcp.d.mts} +18 -2
- package/dist/{create-fetch-handler-ojjpk1Iw.mjs → create-fetch-handler-B0twltQL.mjs} +43 -6
- package/dist/create-fetch-handler-B0twltQL.mjs.map +1 -0
- package/dist/{create-fetch-handler-BHDU4chu.d.mts → create-fetch-handler-B711wb64.d.mts} +4 -4
- package/dist/{create-fetch-handler-C-WyaRCR.cjs → create-fetch-handler-BHT4vSZs.cjs} +43 -6
- package/dist/create-fetch-handler-BHT4vSZs.cjs.map +1 -0
- package/dist/{create-fetch-handler-BtX64_rS.d.cts → create-fetch-handler-DxhvXuBA.d.cts} +4 -4
- package/dist/{elysia-shared-BUTFTeal.cjs → elysia-shared-BFcWzfc9.cjs} +3 -3
- package/dist/{elysia-shared-BUTFTeal.cjs.map → elysia-shared-BFcWzfc9.cjs.map} +1 -1
- package/dist/{elysia-shared-BM6cPoCU.mjs → elysia-shared-BwUYg0Ln.mjs} +3 -3
- package/dist/{elysia-shared-BM6cPoCU.mjs.map → elysia-shared-BwUYg0Ln.mjs.map} +1 -1
- package/dist/{elysia-shared-DtKViycb.d.cts → elysia-shared-C2LnDOj3.d.cts} +5 -5
- package/dist/{elysia-shared-g8zsb_pU.d.mts → elysia-shared-WuDr0jco.d.mts} +5 -5
- package/dist/{engine-bMoz3iW-.cjs → engine--C7zo-4M.cjs} +93 -13
- package/dist/engine--C7zo-4M.cjs.map +1 -0
- package/dist/{engine-CGGVDCqi.d.cts → engine-BTgwbHeO.d.cts} +49 -9
- package/dist/{engine-DpFIcbgh.d.mts → engine-C3xHfi3q.d.mts} +49 -9
- package/dist/{engine-Bufv0TbD.mjs → engine-CR75K253.mjs} +93 -13
- package/dist/engine-CR75K253.mjs.map +1 -0
- package/dist/entry-auth-better-auth.cjs +6 -3
- package/dist/entry-auth-better-auth.d.cts +4 -4
- package/dist/entry-auth-better-auth.d.mts +4 -4
- package/dist/entry-auth-better-auth.mjs +4 -4
- package/dist/entry-auth-jwt.cjs +6 -2
- package/dist/entry-auth-jwt.cjs.map +1 -1
- package/dist/entry-auth-jwt.d.cts +9 -3
- package/dist/entry-auth-jwt.d.mts +9 -3
- package/dist/entry-auth-jwt.mjs +4 -3
- package/dist/entry-auth-jwt.mjs.map +1 -1
- package/dist/entry-auth-session.cjs +6 -2
- package/dist/entry-auth-session.cjs.map +1 -1
- package/dist/entry-auth-session.d.cts +8 -3
- package/dist/entry-auth-session.d.mts +8 -3
- package/dist/entry-auth-session.mjs +4 -3
- package/dist/entry-auth-session.mjs.map +1 -1
- package/dist/entry-authorization-permissions.cjs +1 -1
- package/dist/entry-authorization-permissions.d.cts +1 -1
- package/dist/entry-authorization-permissions.d.mts +1 -1
- package/dist/entry-authorization-permissions.mjs +1 -1
- package/dist/entry-better-auth-client.d.cts +2 -2
- package/dist/entry-better-auth-client.d.mts +2 -2
- package/dist/entry-better-auth.cjs +1 -1
- package/dist/entry-better-auth.d.cts +2 -2
- package/dist/entry-better-auth.d.mts +2 -2
- package/dist/entry-better-auth.mjs +1 -1
- package/dist/entry-bridges-audit.d.cts +1 -1
- package/dist/entry-bridges-audit.d.mts +1 -1
- package/dist/entry-bridges-webhooks.d.cts +1 -1
- package/dist/entry-bridges-webhooks.d.mts +1 -1
- package/dist/entry-bun.cjs +2 -2
- package/dist/entry-bun.d.cts +4 -4
- package/dist/entry-bun.d.mts +4 -4
- package/dist/entry-bun.mjs +2 -2
- package/dist/entry-elysia.cjs +1 -1
- package/dist/entry-elysia.d.cts +4 -4
- package/dist/entry-elysia.d.mts +4 -4
- package/dist/entry-elysia.mjs +1 -1
- package/dist/entry-elysia2.cjs +1 -1
- package/dist/entry-elysia2.d.cts +4 -4
- package/dist/entry-elysia2.d.mts +4 -4
- package/dist/entry-elysia2.mjs +1 -1
- package/dist/entry-express.cjs +2 -2
- package/dist/entry-express.d.cts +4 -4
- package/dist/entry-express.d.mts +4 -4
- package/dist/entry-express.mjs +2 -2
- package/dist/entry-hono.cjs +2 -2
- package/dist/entry-hono.d.cts +4 -4
- package/dist/entry-hono.d.mts +4 -4
- package/dist/entry-hono.mjs +2 -2
- package/dist/entry-hooks-log.d.cts +1 -1
- package/dist/entry-hooks-log.d.mts +1 -1
- package/dist/entry-next.cjs +2 -2
- package/dist/entry-next.d.cts +5 -5
- package/dist/entry-next.d.mts +5 -5
- package/dist/entry-next.mjs +2 -2
- package/dist/entry-react.d.cts +2 -2
- package/dist/entry-react.d.mts +2 -2
- package/dist/{errors-bL3XQLfE.mjs → errors-CG_OLEEv.mjs} +12 -2
- package/dist/errors-CG_OLEEv.mjs.map +1 -0
- package/dist/{errors-vVYic8gz.cjs → errors-CjpymzFj.cjs} +17 -1
- package/dist/errors-CjpymzFj.cjs.map +1 -0
- package/dist/{guard-BOx5cnMP.mjs → guard-Bj8zk23F.mjs} +2 -2
- package/dist/{guard-BOx5cnMP.mjs.map → guard-Bj8zk23F.mjs.map} +1 -1
- package/dist/{guard-nwdOYe90.cjs → guard-DvaoUgjk.cjs} +2 -2
- package/dist/{guard-nwdOYe90.cjs.map → guard-DvaoUgjk.cjs.map} +1 -1
- package/dist/{guard-C3o7V14m.d.cts → guard-VEK0W8ug.d.cts} +4 -4
- package/dist/{guard-DCbTnqPz.d.mts → guard-k7A71SWC.d.mts} +4 -4
- package/dist/index.cjs +9 -6
- package/dist/index.d.cts +28 -10
- package/dist/index.d.mts +28 -10
- package/dist/index.mjs +7 -7
- package/dist/{kit-DMVBLBI9.d.mts → kit-51JtMxsn.d.mts} +8 -3
- package/dist/{kit-fzXHYb1l.mjs → kit-BM3uosfV.mjs} +23 -8
- package/dist/kit-BM3uosfV.mjs.map +1 -0
- package/dist/{kit-C7tepDB2.cjs → kit-BrqnCcDw.cjs} +28 -7
- package/dist/kit-BrqnCcDw.cjs.map +1 -0
- package/dist/{kit-qAROuhOI.d.cts → kit-DLF7DZrp.d.cts} +8 -3
- package/dist/{permissions-C9PjftY6.mjs → permissions-CeV6M3v7.mjs} +6 -3
- package/dist/permissions-CeV6M3v7.mjs.map +1 -0
- package/dist/{permissions-Cxri_P_k.cjs → permissions-DDEkHNvN.cjs} +6 -3
- package/dist/permissions-DDEkHNvN.cjs.map +1 -0
- package/dist/react-embed.js +1880 -1855
- package/dist/relationships-BLYEJ9tc.cjs.map +1 -1
- package/dist/relationships-BfLonEMI.mjs.map +1 -1
- package/dist/{routes-irNuuZqo.cjs → routes-B2LrYpT0.cjs} +15 -9
- package/dist/routes-B2LrYpT0.cjs.map +1 -0
- package/dist/{routes-DtRb3EO0.mjs → routes-Cx-kl4PY.mjs} +15 -9
- package/dist/routes-Cx-kl4PY.mjs.map +1 -0
- package/dist/{server-CJ7AIJpO.d.mts → server-Bn643UOy.d.mts} +4 -4
- package/dist/{server-C2F4H6bR.d.cts → server-YdH25OEA.d.cts} +4 -4
- package/dist/testing.cjs +1 -1
- package/dist/testing.d.cts +2 -2
- package/dist/testing.d.mts +2 -2
- package/dist/testing.mjs +1 -1
- package/dist/types-elysia2/auth-provider/contract.d.ts +28 -0
- package/dist/types-elysia2/auth-provider/kit.d.ts +5 -0
- package/dist/types-elysia2/engine.d.ts +49 -8
- package/dist/types-elysia2/errors.d.ts +8 -0
- package/dist/types-elysia2/schema.d.ts +16 -0
- package/dist/types-elysia2/server/routes.d.ts +16 -5
- package/dist/types-react/engine.d.ts +49 -8
- package/dist/types-react/errors.d.ts +8 -0
- package/dist/types-react/schema.d.ts +16 -0
- package/dist/types-react/ui/App.d.ts +6 -1
- package/dist/types-react/ui/components/perm-bits.d.ts +5 -1
- package/dist/types-react/ui/types.d.ts +11 -2
- package/dist/ui/assets/index-DdvxpphI.js +104 -0
- package/dist/ui/index.html +1 -1
- package/package.json +1 -1
- package/dist/better-auth-DHyW5-yt.cjs.map +0 -1
- package/dist/better-auth-NO40ZqSM.mjs.map +0 -1
- package/dist/contract-CPmJVoRx.cjs.map +0 -1
- package/dist/contract-Yc5AZuvv.mjs.map +0 -1
- package/dist/create-fetch-handler-C-WyaRCR.cjs.map +0 -1
- package/dist/create-fetch-handler-ojjpk1Iw.mjs.map +0 -1
- package/dist/engine-Bufv0TbD.mjs.map +0 -1
- package/dist/engine-bMoz3iW-.cjs.map +0 -1
- package/dist/errors-bL3XQLfE.mjs.map +0 -1
- package/dist/errors-vVYic8gz.cjs.map +0 -1
- package/dist/kit-C7tepDB2.cjs.map +0 -1
- package/dist/kit-fzXHYb1l.mjs.map +0 -1
- package/dist/permissions-C9PjftY6.mjs.map +0 -1
- package/dist/permissions-Cxri_P_k.cjs.map +0 -1
- package/dist/routes-DtRb3EO0.mjs.map +0 -1
- package/dist/routes-irNuuZqo.cjs.map +0 -1
- package/dist/ui/assets/index-uXGOtyuj.js +0 -104
|
@@ -3,7 +3,8 @@
|
|
|
3
3
|
*
|
|
4
4
|
* Writes (grant, revoke, role assignment, subject records) go to the storage
|
|
5
5
|
* under the `perm/` key root (ADR 0003) and fire the hooks. Checks load the
|
|
6
|
-
* subject's stored role and grants (async, cached per subject),
|
|
6
|
+
* subject's stored role and grants (async, cached per subject), resolve the
|
|
7
|
+
* effective role from the `roleSource` (ADR 0014), then evaluate
|
|
7
8
|
* the catalog's rules **synchronously** with `@xtandard/lib/policy/permix`
|
|
8
9
|
* (ADR 0005).
|
|
9
10
|
*
|
|
@@ -25,7 +26,7 @@ export type { SubjectIdentity } from "./catalog.ts";
|
|
|
25
26
|
import { type HookErrorReporter, type PermissionsHooksInput } from "./hooks/contract.ts";
|
|
26
27
|
import type { Ability, DataAtPath, Definition, RelationSchema, RulesPaths } from "./policy.ts";
|
|
27
28
|
import { type RelationshipsApi, type RelationsOptions } from "./relationships.ts";
|
|
28
|
-
import { type CheckResult, type Grant, type GrantInput, type HistoryPage, type HistoryQuery, type ListQuery, type PermissionsSnapshot, type RevokeInput, type SetRoleInput, type SubjectRecord, type UpsertSubjectInput } from "./schema.ts";
|
|
29
|
+
import { type CheckResult, type Grant, type GrantInput, type HistoryPage, type HistoryQuery, type ListQuery, type PermissionsSnapshot, type RevokeInput, type RoleSource, type RoleSourceMode, type SetRoleInput, type SubjectRecord, type UpsertSubjectInput } from "./schema.ts";
|
|
29
30
|
import { type PermissionsStorage } from "./storage/contract.ts";
|
|
30
31
|
/** Options for {@link createPermissions}. */
|
|
31
32
|
export interface PermissionsOptions<D extends Definition = Definition, R extends string = string, P extends string = string, X = undefined, S extends RelationSchema = RelationSchema> {
|
|
@@ -38,7 +39,8 @@ export interface PermissionsOptions<D extends Definition = Definition, R extends
|
|
|
38
39
|
/** Receives errors thrown by `after` hooks. Default: a prefixed `console.warn`. */
|
|
39
40
|
onHookError?: HookErrorReporter;
|
|
40
41
|
/**
|
|
41
|
-
* Per-subject cache of the stored role and grants used by checks
|
|
42
|
+
* Per-subject cache of the stored role and grants used by checks (and of `lookupSubject`
|
|
43
|
+
* results). Entries expire after `ttlMs`
|
|
42
44
|
* (default 5 000), are dropped on this engine's writes and, when the storage is watchable, on
|
|
43
45
|
* every change it reports. `false` disables it (every check reads the storage).
|
|
44
46
|
*/
|
|
@@ -56,7 +58,26 @@ export interface PermissionsOptions<D extends Definition = Definition, R extends
|
|
|
56
58
|
};
|
|
57
59
|
/** ReBAC tuning (the catalog's `relations`): `maxDepth` of checks and listing walks. */
|
|
58
60
|
relations?: RelationsOptions;
|
|
61
|
+
/**
|
|
62
|
+
* Who owns roles (ADR 0014). `"stored"` (default, the 0.1 behaviour): a role set with
|
|
63
|
+
* `setRole()` wins over the auth provider's. `"external"`: the auth provider owns roles; stored
|
|
64
|
+
* roles are ignored and `setRole()` throws {@link ExternalRolesError}.
|
|
65
|
+
*/
|
|
66
|
+
roleSource?: RoleSourceMode;
|
|
67
|
+
/**
|
|
68
|
+
* Resolve a subject by id when a check (or the panel) only has the id, not a session: returns
|
|
69
|
+
* the auth provider's identity (`role`, `name`, `email`). Used in both role-source modes, only
|
|
70
|
+
* when the identity passed carries no `role`; cached with the per-subject cache. Needed for the
|
|
71
|
+
* panel to show the provider's role of subjects other than the caller. Every bundled auth
|
|
72
|
+
* provider can supply one (`provider.lookupSubject`).
|
|
73
|
+
*/
|
|
74
|
+
lookupSubject?: SubjectLookup | undefined;
|
|
59
75
|
}
|
|
76
|
+
/**
|
|
77
|
+
* Looks a subject up by id in the auth provider (see {@link PermissionsOptions.lookupSubject}).
|
|
78
|
+
* `null` = unknown subject (the catalog's `defaultRole` applies). A throw fails the check.
|
|
79
|
+
*/
|
|
80
|
+
export type SubjectLookup = (id: string) => SubjectIdentity | null | undefined | Promise<SubjectIdentity | null | undefined>;
|
|
60
81
|
/** A subject id, an identity, or `null` / `undefined` for a guest (no session). */
|
|
61
82
|
export type SubjectInput = string | SubjectIdentity | null | undefined;
|
|
62
83
|
/** The app context argument of checks: required when the catalog's context type is. */
|
|
@@ -104,16 +125,27 @@ export interface SubjectView<R extends string = string, P extends string = strin
|
|
|
104
125
|
id: string;
|
|
105
126
|
/** The stored record, `null` when the subject has none. */
|
|
106
127
|
record: SubjectRecord | null;
|
|
107
|
-
/**
|
|
128
|
+
/**
|
|
129
|
+
* The stored role assignment when it is a known, assignable role; else `null`. Ignored by checks
|
|
130
|
+
* when the engine's `roleSource` is `"external"`.
|
|
131
|
+
*/
|
|
108
132
|
role: R | null;
|
|
133
|
+
/** The role checks use: see {@link roleSource} for what decided it. */
|
|
134
|
+
effectiveRole: R;
|
|
135
|
+
/** Which source decided {@link effectiveRole}. */
|
|
136
|
+
roleSource: RoleSource;
|
|
109
137
|
/** The subject's grants (non-expired unless `includeExpired`). */
|
|
110
138
|
grants: Grant<P>[];
|
|
111
139
|
}
|
|
112
140
|
/** One row of `listSubjects()`. */
|
|
113
141
|
export interface SubjectSummary {
|
|
114
142
|
id: string;
|
|
115
|
-
/** The stored role assignment, if any. */
|
|
143
|
+
/** The stored role assignment, if any (ignored by checks when `roleSource` is `"external"`). */
|
|
116
144
|
role: string | null;
|
|
145
|
+
/** The role checks use (the stored role, the auth provider's, or `defaultRole`). */
|
|
146
|
+
effectiveRole: string;
|
|
147
|
+
/** Which source decided {@link effectiveRole}. */
|
|
148
|
+
roleSource: RoleSource;
|
|
117
149
|
name?: string;
|
|
118
150
|
email?: string;
|
|
119
151
|
/** Stored grants (expired ones included). */
|
|
@@ -127,6 +159,8 @@ export interface Permissions<D extends Definition = Definition, R extends string
|
|
|
127
159
|
readonly catalog: PermissionsCatalog<D, R, P, X, S>;
|
|
128
160
|
readonly storage: PermissionsStorage;
|
|
129
161
|
readonly readonly: boolean;
|
|
162
|
+
/** Who owns roles: `"stored"` (default) or `"external"` (the auth provider; ADR 0014). */
|
|
163
|
+
readonly roleSource: RoleSourceMode;
|
|
130
164
|
/**
|
|
131
165
|
* Grant a permission. Idempotent: re-granting returns the existing grant with `created: false`
|
|
132
166
|
* (no history entry, no hooks). An **expired** existing grant is replaced (`created: true`).
|
|
@@ -148,14 +182,21 @@ export interface Permissions<D extends Definition = Definition, R extends string
|
|
|
148
182
|
/**
|
|
149
183
|
* Store a role assignment (overrides the auth provider's role); `role: null` clears it.
|
|
150
184
|
*
|
|
151
|
-
* @throws {UnknownRoleError} not one of `roles`, or the guest role.
|
|
185
|
+
* @throws {UnknownRoleError} not one of `roles`, or the guest role. {@link ExternalRolesError}
|
|
186
|
+
* when `roleSource` is `"external"` (the auth provider owns roles).
|
|
152
187
|
*/
|
|
153
188
|
setRole(input: SetRoleInput<R>): Promise<SetRoleResult>;
|
|
154
|
-
/**
|
|
189
|
+
/**
|
|
190
|
+
* A subject's stored record and grants (an empty view when nothing is stored), with its effective
|
|
191
|
+
* role and the source that decided it (the auth provider's role comes from `lookupSubject`).
|
|
192
|
+
*/
|
|
155
193
|
getSubject(id: string, options?: {
|
|
156
194
|
includeExpired?: boolean;
|
|
157
195
|
}): Promise<SubjectView<R, P>>;
|
|
158
|
-
/**
|
|
196
|
+
/**
|
|
197
|
+
* Every subject with a record or a grant, sorted by id, with effective roles. A failing
|
|
198
|
+
* `lookupSubject` is logged and reported as `roleSource: "default"` (the list still loads).
|
|
199
|
+
*/
|
|
159
200
|
listSubjects(): Promise<SubjectSummary[]>;
|
|
160
201
|
/** Create or update a subject's display fields (`null` clears one). Keeps the stored role. */
|
|
161
202
|
upsertSubject(input: UpsertSubjectInput): Promise<SubjectRecord>;
|
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
* | `ForbiddenError` | the caller may not manage permissions |
|
|
11
11
|
* | `NotFoundError` | a referenced record does not exist |
|
|
12
12
|
* | `ReadonlyError` | a mutation on a `readonly` engine |
|
|
13
|
+
* | `ExternalRolesError` | `setRole()` with `roleSource: "external"` (the provider owns roles) |
|
|
13
14
|
* | `ValidationError` | malformed input (the kit's, with `errors` / `issues`) |
|
|
14
15
|
* | `AsyncRuleError` | a rule returned a Promise (the lib's; the check denied) |
|
|
15
16
|
*
|
|
@@ -45,3 +46,10 @@ export declare class NotFoundError extends Error {
|
|
|
45
46
|
export declare class ReadonlyError extends Error {
|
|
46
47
|
constructor(operation: string);
|
|
47
48
|
}
|
|
49
|
+
/**
|
|
50
|
+
* `setRole()` on an engine whose `roleSource` is `"external"`: roles are managed by the auth
|
|
51
|
+
* provider, so a stored role would be ignored (ADR 0014). HTTP 409 `ROLES_EXTERNAL`.
|
|
52
|
+
*/
|
|
53
|
+
export declare class ExternalRolesError extends Error {
|
|
54
|
+
constructor(message?: string);
|
|
55
|
+
}
|
|
@@ -100,15 +100,31 @@ export interface CheckResult<R extends string = string, P extends string = strin
|
|
|
100
100
|
subject: string | null;
|
|
101
101
|
/** The effective role the rules ran as. */
|
|
102
102
|
role: R;
|
|
103
|
+
/** Which source decided {@link role} (absent for a guest). See {@link RoleSource}. */
|
|
104
|
+
roleSource?: RoleSource;
|
|
103
105
|
reason: CheckReason;
|
|
104
106
|
/** The lib's `explain()` detail of the rule alone (role, permissions seen, what decided). */
|
|
105
107
|
explanation: CheckExplanation<R, P>;
|
|
106
108
|
}
|
|
109
|
+
/**
|
|
110
|
+
* Who owns role assignments (the engine's `roleSource` option, ADR 0014). `"stored"` (default): a
|
|
111
|
+
* role set with `setRole()` wins over the auth provider's. `"external"`: the auth provider owns
|
|
112
|
+
* roles; stored roles are ignored and `setRole()` throws `ExternalRolesError`.
|
|
113
|
+
*/
|
|
114
|
+
export type RoleSourceMode = "stored" | "external";
|
|
115
|
+
/**
|
|
116
|
+
* Which source decided a subject's effective role: a stored assignment (`"stored"`), the auth
|
|
117
|
+
* provider's role — passed with the identity or from `lookupSubject` (`"provider"`), or the
|
|
118
|
+
* catalog's `defaultRole` because neither gave a known role (`"default"`).
|
|
119
|
+
*/
|
|
120
|
+
export type RoleSource = "stored" | "provider" | "default";
|
|
107
121
|
/** The serializable state a client needs to rebuild a subject's ability (React, SSR). */
|
|
108
122
|
export interface PermissionsSnapshot<R extends string = string, P extends string = string> {
|
|
123
|
+
/** The subject and its effective role; `roleSource` is absent for a guest. */
|
|
109
124
|
subject: {
|
|
110
125
|
id: string | null;
|
|
111
126
|
role: R;
|
|
127
|
+
roleSource?: RoleSource;
|
|
112
128
|
};
|
|
113
129
|
/** The non-expired, assignable granted permissions. */
|
|
114
130
|
permissions: P[];
|
|
@@ -27,7 +27,7 @@ import { type AuthorizationProvider, type PermissionsAction, type PermissionsRes
|
|
|
27
27
|
import type { AnyPermissionsCatalog, SubjectIdentity } from "../catalog.ts";
|
|
28
28
|
import type { AnyPermissions, SubjectSummary, SubjectView } from "../engine.ts";
|
|
29
29
|
import { type RelationTypeView } from "../relationships.ts";
|
|
30
|
-
import type { CheckResult } from "../schema.ts";
|
|
30
|
+
import type { CheckResult, RoleSource, RoleSourceMode } from "../schema.ts";
|
|
31
31
|
/** Everything the API router needs. */
|
|
32
32
|
export interface ApiContext {
|
|
33
33
|
engine: AnyPermissions;
|
|
@@ -55,11 +55,17 @@ export interface PanelConfig {
|
|
|
55
55
|
logoUrl?: string;
|
|
56
56
|
basePath: string;
|
|
57
57
|
readonly: boolean;
|
|
58
|
+
/** Who owns roles (ADR 0014): `"external"` hides role changes (the auth provider owns them). */
|
|
59
|
+
roleSource: RoleSourceMode;
|
|
58
60
|
authenticated: boolean;
|
|
59
|
-
/**
|
|
61
|
+
/**
|
|
62
|
+
* The caller, with its effective catalog role and the source that decided it; `null` when not
|
|
63
|
+
* authenticated.
|
|
64
|
+
*/
|
|
60
65
|
principal: {
|
|
61
66
|
id: string;
|
|
62
67
|
role: string;
|
|
68
|
+
roleSource?: RoleSource;
|
|
63
69
|
name?: string;
|
|
64
70
|
email?: string;
|
|
65
71
|
} | null;
|
|
@@ -84,6 +90,8 @@ export interface CatalogView {
|
|
|
84
90
|
defaultRole: string;
|
|
85
91
|
managerRoles: string[];
|
|
86
92
|
directGrants: boolean;
|
|
93
|
+
/** Who owns roles: `"stored"` (role changes allowed) or `"external"` (the auth provider's). */
|
|
94
|
+
roleSource: RoleSourceMode;
|
|
87
95
|
/** Every leaf permix path of every role's rules tree, sorted (for the playground). */
|
|
88
96
|
paths: string[];
|
|
89
97
|
/** The relation schema (ReBAC), `null` when the catalog declares none. */
|
|
@@ -95,7 +103,10 @@ export interface SubjectList {
|
|
|
95
103
|
}
|
|
96
104
|
/** `GET /api/subjects/:id`: the stored view plus the effective role and active permissions. */
|
|
97
105
|
export interface SubjectDetail extends SubjectView {
|
|
98
|
-
/**
|
|
106
|
+
/**
|
|
107
|
+
* The role checks use: the stored role (`roleSource: "stored"` mode only), else the auth
|
|
108
|
+
* provider's (`lookupSubject`), else the catalog's `defaultRole`; `roleSource` says which.
|
|
109
|
+
*/
|
|
99
110
|
effectiveRole: string;
|
|
100
111
|
/** The non-expired, assignable granted permissions. */
|
|
101
112
|
permissions: string[];
|
|
@@ -116,8 +127,8 @@ export type RouteSpec = KitRouteSpec<PermissionsAction, RouteTag> & {
|
|
|
116
127
|
* placeholder subject and no grants; a role whose rules can't be built that way is skipped.
|
|
117
128
|
*/
|
|
118
129
|
export declare function catalogPaths(catalog: AnyPermissionsCatalog): string[];
|
|
119
|
-
/** The catalog view served at `/api/catalog
|
|
120
|
-
export declare function catalogView(catalog: AnyPermissionsCatalog): CatalogView;
|
|
130
|
+
/** The catalog view served at `/api/catalog` (`roleSource`: the engine's, default `"stored"`). */
|
|
131
|
+
export declare function catalogView(catalog: AnyPermissionsCatalog, roleSource?: RoleSourceMode): CatalogView;
|
|
121
132
|
/** A check result as JSON: `explanation.error` (a thrown value) becomes its message. */
|
|
122
133
|
export declare function serializeCheck(result: CheckResult): CheckResult;
|
|
123
134
|
/** The documented route table (every API route, in match order). */
|
|
@@ -3,7 +3,8 @@
|
|
|
3
3
|
*
|
|
4
4
|
* Writes (grant, revoke, role assignment, subject records) go to the storage
|
|
5
5
|
* under the `perm/` key root (ADR 0003) and fire the hooks. Checks load the
|
|
6
|
-
* subject's stored role and grants (async, cached per subject),
|
|
6
|
+
* subject's stored role and grants (async, cached per subject), resolve the
|
|
7
|
+
* effective role from the `roleSource` (ADR 0014), then evaluate
|
|
7
8
|
* the catalog's rules **synchronously** with `@xtandard/lib/policy/permix`
|
|
8
9
|
* (ADR 0005).
|
|
9
10
|
*
|
|
@@ -25,7 +26,7 @@ export type { SubjectIdentity } from "./catalog.ts";
|
|
|
25
26
|
import { type HookErrorReporter, type PermissionsHooksInput } from "./hooks/contract.ts";
|
|
26
27
|
import type { Ability, DataAtPath, Definition, RelationSchema, RulesPaths } from "./policy.ts";
|
|
27
28
|
import { type RelationshipsApi, type RelationsOptions } from "./relationships.ts";
|
|
28
|
-
import { type CheckResult, type Grant, type GrantInput, type HistoryPage, type HistoryQuery, type ListQuery, type PermissionsSnapshot, type RevokeInput, type SetRoleInput, type SubjectRecord, type UpsertSubjectInput } from "./schema.ts";
|
|
29
|
+
import { type CheckResult, type Grant, type GrantInput, type HistoryPage, type HistoryQuery, type ListQuery, type PermissionsSnapshot, type RevokeInput, type RoleSource, type RoleSourceMode, type SetRoleInput, type SubjectRecord, type UpsertSubjectInput } from "./schema.ts";
|
|
29
30
|
import { type PermissionsStorage } from "./storage/contract.ts";
|
|
30
31
|
/** Options for {@link createPermissions}. */
|
|
31
32
|
export interface PermissionsOptions<D extends Definition = Definition, R extends string = string, P extends string = string, X = undefined, S extends RelationSchema = RelationSchema> {
|
|
@@ -38,7 +39,8 @@ export interface PermissionsOptions<D extends Definition = Definition, R extends
|
|
|
38
39
|
/** Receives errors thrown by `after` hooks. Default: a prefixed `console.warn`. */
|
|
39
40
|
onHookError?: HookErrorReporter;
|
|
40
41
|
/**
|
|
41
|
-
* Per-subject cache of the stored role and grants used by checks
|
|
42
|
+
* Per-subject cache of the stored role and grants used by checks (and of `lookupSubject`
|
|
43
|
+
* results). Entries expire after `ttlMs`
|
|
42
44
|
* (default 5 000), are dropped on this engine's writes and, when the storage is watchable, on
|
|
43
45
|
* every change it reports. `false` disables it (every check reads the storage).
|
|
44
46
|
*/
|
|
@@ -56,7 +58,26 @@ export interface PermissionsOptions<D extends Definition = Definition, R extends
|
|
|
56
58
|
};
|
|
57
59
|
/** ReBAC tuning (the catalog's `relations`): `maxDepth` of checks and listing walks. */
|
|
58
60
|
relations?: RelationsOptions;
|
|
61
|
+
/**
|
|
62
|
+
* Who owns roles (ADR 0014). `"stored"` (default, the 0.1 behaviour): a role set with
|
|
63
|
+
* `setRole()` wins over the auth provider's. `"external"`: the auth provider owns roles; stored
|
|
64
|
+
* roles are ignored and `setRole()` throws {@link ExternalRolesError}.
|
|
65
|
+
*/
|
|
66
|
+
roleSource?: RoleSourceMode;
|
|
67
|
+
/**
|
|
68
|
+
* Resolve a subject by id when a check (or the panel) only has the id, not a session: returns
|
|
69
|
+
* the auth provider's identity (`role`, `name`, `email`). Used in both role-source modes, only
|
|
70
|
+
* when the identity passed carries no `role`; cached with the per-subject cache. Needed for the
|
|
71
|
+
* panel to show the provider's role of subjects other than the caller. Every bundled auth
|
|
72
|
+
* provider can supply one (`provider.lookupSubject`).
|
|
73
|
+
*/
|
|
74
|
+
lookupSubject?: SubjectLookup | undefined;
|
|
59
75
|
}
|
|
76
|
+
/**
|
|
77
|
+
* Looks a subject up by id in the auth provider (see {@link PermissionsOptions.lookupSubject}).
|
|
78
|
+
* `null` = unknown subject (the catalog's `defaultRole` applies). A throw fails the check.
|
|
79
|
+
*/
|
|
80
|
+
export type SubjectLookup = (id: string) => SubjectIdentity | null | undefined | Promise<SubjectIdentity | null | undefined>;
|
|
60
81
|
/** A subject id, an identity, or `null` / `undefined` for a guest (no session). */
|
|
61
82
|
export type SubjectInput = string | SubjectIdentity | null | undefined;
|
|
62
83
|
/** The app context argument of checks: required when the catalog's context type is. */
|
|
@@ -104,16 +125,27 @@ export interface SubjectView<R extends string = string, P extends string = strin
|
|
|
104
125
|
id: string;
|
|
105
126
|
/** The stored record, `null` when the subject has none. */
|
|
106
127
|
record: SubjectRecord | null;
|
|
107
|
-
/**
|
|
128
|
+
/**
|
|
129
|
+
* The stored role assignment when it is a known, assignable role; else `null`. Ignored by checks
|
|
130
|
+
* when the engine's `roleSource` is `"external"`.
|
|
131
|
+
*/
|
|
108
132
|
role: R | null;
|
|
133
|
+
/** The role checks use: see {@link roleSource} for what decided it. */
|
|
134
|
+
effectiveRole: R;
|
|
135
|
+
/** Which source decided {@link effectiveRole}. */
|
|
136
|
+
roleSource: RoleSource;
|
|
109
137
|
/** The subject's grants (non-expired unless `includeExpired`). */
|
|
110
138
|
grants: Grant<P>[];
|
|
111
139
|
}
|
|
112
140
|
/** One row of `listSubjects()`. */
|
|
113
141
|
export interface SubjectSummary {
|
|
114
142
|
id: string;
|
|
115
|
-
/** The stored role assignment, if any. */
|
|
143
|
+
/** The stored role assignment, if any (ignored by checks when `roleSource` is `"external"`). */
|
|
116
144
|
role: string | null;
|
|
145
|
+
/** The role checks use (the stored role, the auth provider's, or `defaultRole`). */
|
|
146
|
+
effectiveRole: string;
|
|
147
|
+
/** Which source decided {@link effectiveRole}. */
|
|
148
|
+
roleSource: RoleSource;
|
|
117
149
|
name?: string;
|
|
118
150
|
email?: string;
|
|
119
151
|
/** Stored grants (expired ones included). */
|
|
@@ -127,6 +159,8 @@ export interface Permissions<D extends Definition = Definition, R extends string
|
|
|
127
159
|
readonly catalog: PermissionsCatalog<D, R, P, X, S>;
|
|
128
160
|
readonly storage: PermissionsStorage;
|
|
129
161
|
readonly readonly: boolean;
|
|
162
|
+
/** Who owns roles: `"stored"` (default) or `"external"` (the auth provider; ADR 0014). */
|
|
163
|
+
readonly roleSource: RoleSourceMode;
|
|
130
164
|
/**
|
|
131
165
|
* Grant a permission. Idempotent: re-granting returns the existing grant with `created: false`
|
|
132
166
|
* (no history entry, no hooks). An **expired** existing grant is replaced (`created: true`).
|
|
@@ -148,14 +182,21 @@ export interface Permissions<D extends Definition = Definition, R extends string
|
|
|
148
182
|
/**
|
|
149
183
|
* Store a role assignment (overrides the auth provider's role); `role: null` clears it.
|
|
150
184
|
*
|
|
151
|
-
* @throws {UnknownRoleError} not one of `roles`, or the guest role.
|
|
185
|
+
* @throws {UnknownRoleError} not one of `roles`, or the guest role. {@link ExternalRolesError}
|
|
186
|
+
* when `roleSource` is `"external"` (the auth provider owns roles).
|
|
152
187
|
*/
|
|
153
188
|
setRole(input: SetRoleInput<R>): Promise<SetRoleResult>;
|
|
154
|
-
/**
|
|
189
|
+
/**
|
|
190
|
+
* A subject's stored record and grants (an empty view when nothing is stored), with its effective
|
|
191
|
+
* role and the source that decided it (the auth provider's role comes from `lookupSubject`).
|
|
192
|
+
*/
|
|
155
193
|
getSubject(id: string, options?: {
|
|
156
194
|
includeExpired?: boolean;
|
|
157
195
|
}): Promise<SubjectView<R, P>>;
|
|
158
|
-
/**
|
|
196
|
+
/**
|
|
197
|
+
* Every subject with a record or a grant, sorted by id, with effective roles. A failing
|
|
198
|
+
* `lookupSubject` is logged and reported as `roleSource: "default"` (the list still loads).
|
|
199
|
+
*/
|
|
159
200
|
listSubjects(): Promise<SubjectSummary[]>;
|
|
160
201
|
/** Create or update a subject's display fields (`null` clears one). Keeps the stored role. */
|
|
161
202
|
upsertSubject(input: UpsertSubjectInput): Promise<SubjectRecord>;
|
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
* | `ForbiddenError` | the caller may not manage permissions |
|
|
11
11
|
* | `NotFoundError` | a referenced record does not exist |
|
|
12
12
|
* | `ReadonlyError` | a mutation on a `readonly` engine |
|
|
13
|
+
* | `ExternalRolesError` | `setRole()` with `roleSource: "external"` (the provider owns roles) |
|
|
13
14
|
* | `ValidationError` | malformed input (the kit's, with `errors` / `issues`) |
|
|
14
15
|
* | `AsyncRuleError` | a rule returned a Promise (the lib's; the check denied) |
|
|
15
16
|
*
|
|
@@ -45,3 +46,10 @@ export declare class NotFoundError extends Error {
|
|
|
45
46
|
export declare class ReadonlyError extends Error {
|
|
46
47
|
constructor(operation: string);
|
|
47
48
|
}
|
|
49
|
+
/**
|
|
50
|
+
* `setRole()` on an engine whose `roleSource` is `"external"`: roles are managed by the auth
|
|
51
|
+
* provider, so a stored role would be ignored (ADR 0014). HTTP 409 `ROLES_EXTERNAL`.
|
|
52
|
+
*/
|
|
53
|
+
export declare class ExternalRolesError extends Error {
|
|
54
|
+
constructor(message?: string);
|
|
55
|
+
}
|
|
@@ -100,15 +100,31 @@ export interface CheckResult<R extends string = string, P extends string = strin
|
|
|
100
100
|
subject: string | null;
|
|
101
101
|
/** The effective role the rules ran as. */
|
|
102
102
|
role: R;
|
|
103
|
+
/** Which source decided {@link role} (absent for a guest). See {@link RoleSource}. */
|
|
104
|
+
roleSource?: RoleSource;
|
|
103
105
|
reason: CheckReason;
|
|
104
106
|
/** The lib's `explain()` detail of the rule alone (role, permissions seen, what decided). */
|
|
105
107
|
explanation: CheckExplanation<R, P>;
|
|
106
108
|
}
|
|
109
|
+
/**
|
|
110
|
+
* Who owns role assignments (the engine's `roleSource` option, ADR 0014). `"stored"` (default): a
|
|
111
|
+
* role set with `setRole()` wins over the auth provider's. `"external"`: the auth provider owns
|
|
112
|
+
* roles; stored roles are ignored and `setRole()` throws `ExternalRolesError`.
|
|
113
|
+
*/
|
|
114
|
+
export type RoleSourceMode = "stored" | "external";
|
|
115
|
+
/**
|
|
116
|
+
* Which source decided a subject's effective role: a stored assignment (`"stored"`), the auth
|
|
117
|
+
* provider's role — passed with the identity or from `lookupSubject` (`"provider"`), or the
|
|
118
|
+
* catalog's `defaultRole` because neither gave a known role (`"default"`).
|
|
119
|
+
*/
|
|
120
|
+
export type RoleSource = "stored" | "provider" | "default";
|
|
107
121
|
/** The serializable state a client needs to rebuild a subject's ability (React, SSR). */
|
|
108
122
|
export interface PermissionsSnapshot<R extends string = string, P extends string = string> {
|
|
123
|
+
/** The subject and its effective role; `roleSource` is absent for a guest. */
|
|
109
124
|
subject: {
|
|
110
125
|
id: string | null;
|
|
111
126
|
role: R;
|
|
127
|
+
roleSource?: RoleSource;
|
|
112
128
|
};
|
|
113
129
|
/** The non-expired, assignable granted permissions. */
|
|
114
130
|
permissions: P[];
|
|
@@ -9,8 +9,13 @@ export interface ShellContext {
|
|
|
9
9
|
go: (path: string, params?: Record<string, string>) => void;
|
|
10
10
|
/** The principal may run `action` (always `true` when `/config` lists no actions). */
|
|
11
11
|
can: (action: PanelAction) => boolean;
|
|
12
|
-
/**
|
|
12
|
+
/**
|
|
13
|
+
* `can(action)` and the panel is not read-only: show the write UI. `roles:write` is also `false`
|
|
14
|
+
* when the auth provider owns roles (`roleSource: "external"`).
|
|
15
|
+
*/
|
|
13
16
|
canWrite: (action: "grants:write" | "roles:write" | "relationships:write") => boolean;
|
|
17
|
+
/** The auth provider owns roles (`roleSource: "external"`): roles are read-only here. */
|
|
18
|
+
externalRoles: boolean;
|
|
14
19
|
}
|
|
15
20
|
/** The path of a subject's detail view. */
|
|
16
21
|
export declare const subjectPath: (id: string) => string;
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
*/
|
|
7
7
|
import React from "react";
|
|
8
8
|
import type { CatalogIndex } from "../lib/queries.ts";
|
|
9
|
-
import type { HistoryEntry } from "../types.ts";
|
|
9
|
+
import type { HistoryEntry, RoleSource } from "../types.ts";
|
|
10
10
|
/** An edge-to-edge table inside a card. */
|
|
11
11
|
export declare function DataTable({ head, children, label, }: {
|
|
12
12
|
head: (string | {
|
|
@@ -27,6 +27,10 @@ export declare function RoleChip({ role, catalog, muted, }: {
|
|
|
27
27
|
catalog?: CatalogIndex;
|
|
28
28
|
muted?: boolean;
|
|
29
29
|
}): React.ReactElement;
|
|
30
|
+
/** What decided an effective role, as a short muted label. */
|
|
31
|
+
export declare function RoleSourceLabel({ source }: {
|
|
32
|
+
source: RoleSource;
|
|
33
|
+
}): React.ReactElement;
|
|
30
34
|
/** A permission: label over its key. */
|
|
31
35
|
export declare function PermissionName({ permission, catalog, }: {
|
|
32
36
|
permission: string;
|
|
@@ -5,7 +5,8 @@
|
|
|
5
5
|
*
|
|
6
6
|
* @module
|
|
7
7
|
*/
|
|
8
|
-
export type { CheckReason, CheckResult, Grant, HistoryEntry, HistoryPage, HistoryType, PermissionsSnapshot, Relationship, RelationshipPage, SubjectRecord, } from "../schema.ts";
|
|
8
|
+
export type { CheckReason, CheckResult, Grant, HistoryEntry, HistoryPage, HistoryType, PermissionsSnapshot, Relationship, RelationshipPage, RoleSource, RoleSourceMode, SubjectRecord, } from "../schema.ts";
|
|
9
|
+
import type { RoleSource, RoleSourceMode } from "../schema.ts";
|
|
9
10
|
export type { RelationTypeView, RelationView } from "../relationships.ts";
|
|
10
11
|
import type { RelationTypeView } from "../relationships.ts";
|
|
11
12
|
export type { SubjectSummary, SubjectView } from "../engine.ts";
|
|
@@ -24,12 +25,18 @@ export interface PanelConfig extends ShellConfigBase {
|
|
|
24
25
|
readonly: boolean;
|
|
25
26
|
basePath?: string;
|
|
26
27
|
logoUrl?: string;
|
|
28
|
+
/**
|
|
29
|
+
* Who owns roles: `"external"` (the auth provider) hides every role change. Absent (an older
|
|
30
|
+
* server) = `"stored"`.
|
|
31
|
+
*/
|
|
32
|
+
roleSource?: RoleSourceMode;
|
|
27
33
|
/** Whether the request carried an authenticated principal. */
|
|
28
34
|
authenticated?: boolean;
|
|
29
35
|
/** The caller, with its effective catalog role; `null` when not authenticated. */
|
|
30
36
|
principal?: {
|
|
31
37
|
id: string;
|
|
32
38
|
role: string;
|
|
39
|
+
roleSource?: RoleSource;
|
|
33
40
|
name?: string;
|
|
34
41
|
email?: string;
|
|
35
42
|
} | null;
|
|
@@ -41,7 +48,7 @@ export interface PanelConfig extends ShellConfigBase {
|
|
|
41
48
|
}
|
|
42
49
|
/** `GET /api/subjects/:id`: the stored view plus the effective role and active permissions. */
|
|
43
50
|
export interface SubjectDetail extends SubjectView {
|
|
44
|
-
/** The
|
|
51
|
+
/** The role checks use; `roleSource` says what decided it. */
|
|
45
52
|
effectiveRole: string;
|
|
46
53
|
/** The non-expired, assignable granted permissions. */
|
|
47
54
|
permissions: string[];
|
|
@@ -61,6 +68,8 @@ export interface CatalogView {
|
|
|
61
68
|
managerRoles: string[];
|
|
62
69
|
assignable: LabeledKey[];
|
|
63
70
|
directGrants: boolean;
|
|
71
|
+
/** Who owns roles; absent (an older server) = `"stored"`. */
|
|
72
|
+
roleSource?: RoleSourceMode;
|
|
64
73
|
/** Every leaf rule path (`"post.publish"`) of every role, sorted, for the playground. */
|
|
65
74
|
paths: string[];
|
|
66
75
|
/** The relation schema (ReBAC); `null` (or absent, an older server) when there is none. */
|