@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
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { f as PermissionsSubject, h as SubjectIdentity } from "./catalog-DJcJ3ycq.mjs";
|
|
2
|
-
import { t as PermissionsAuthProvider } from "./contract-
|
|
2
|
+
import { t as PermissionsAuthProvider } from "./contract-D6j-yrBL.mjs";
|
|
3
3
|
import { AuthorizationProvider } from "@xtandard/kit/authorization";
|
|
4
4
|
import { AuthProvider, Principal } from "@xtandard/kit/auth";
|
|
5
5
|
|
|
@@ -38,6 +38,11 @@ interface KitAuth {
|
|
|
38
38
|
}
|
|
39
39
|
/** The `PermissionsSubject` a kit principal built by {@link toKitAuth} carries, if any. */
|
|
40
40
|
declare function subjectOf(principal: Principal | null | undefined): PermissionsSubject | null;
|
|
41
|
+
/**
|
|
42
|
+
* The identity the auth provider reported for a kit principal built by {@link toKitAuth} (its raw
|
|
43
|
+
* role, before the engine resolved it), if any.
|
|
44
|
+
*/
|
|
45
|
+
declare function identityOf(principal: Principal | null | undefined): SubjectIdentity | null;
|
|
41
46
|
/**
|
|
42
47
|
* Build the kit `auth` + `authorization` pair from a provider and the engine.
|
|
43
48
|
*
|
|
@@ -53,5 +58,5 @@ declare function subjectOf(principal: Principal | null | undefined): Permissions
|
|
|
53
58
|
*/
|
|
54
59
|
declare function toKitAuth(provider: PermissionsAuthProvider, perms: KitAuthEngine, options?: ToKitAuthOptions): KitAuth;
|
|
55
60
|
//#endregion
|
|
56
|
-
export {
|
|
57
|
-
//# sourceMappingURL=kit-
|
|
61
|
+
export { subjectOf as a, identityOf as i, KitAuthEngine as n, toKitAuth as o, ToKitAuthOptions as r, KitAuth as t };
|
|
62
|
+
//# sourceMappingURL=kit-51JtMxsn.d.mts.map
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { r as NoSubjectError } from "./errors-CG_OLEEv.mjs";
|
|
2
2
|
//#region src/auth-provider/kit.ts
|
|
3
3
|
const DEFAULT_MUTATING = new Set(["grants:write", "roles:write"]);
|
|
4
4
|
/** The `PermissionsSubject` a kit principal built by {@link toKitAuth} carries, if any. */
|
|
@@ -6,6 +6,13 @@ function subjectOf(principal) {
|
|
|
6
6
|
return (principal?.metadata)?.subject ?? null;
|
|
7
7
|
}
|
|
8
8
|
/**
|
|
9
|
+
* The identity the auth provider reported for a kit principal built by {@link toKitAuth} (its raw
|
|
10
|
+
* role, before the engine resolved it), if any.
|
|
11
|
+
*/
|
|
12
|
+
function identityOf(principal) {
|
|
13
|
+
return (principal?.metadata)?.identity ?? null;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
9
16
|
* Build the kit `auth` + `authorization` pair from a provider and the engine.
|
|
10
17
|
*
|
|
11
18
|
* - `auth.authenticate` → principal `{ id, name, email, roles: [effectiveRole], metadata: { subject } }`.
|
|
@@ -20,7 +27,7 @@ function subjectOf(principal) {
|
|
|
20
27
|
*/
|
|
21
28
|
function toKitAuth(provider, perms, options = {}) {
|
|
22
29
|
const isMutating = options.isMutating ?? ((action) => DEFAULT_MUTATING.has(action));
|
|
23
|
-
const
|
|
30
|
+
const caller = async (request) => {
|
|
24
31
|
const found = await provider.getSubject(request);
|
|
25
32
|
if (found === null || found === void 0) return null;
|
|
26
33
|
if (typeof found.id !== "string" || found.id === "") throw new NoSubjectError("the auth provider returned an identity with an empty id");
|
|
@@ -32,18 +39,26 @@ function toKitAuth(provider, perms, options = {}) {
|
|
|
32
39
|
if (found.name !== void 0) out.name = found.name;
|
|
33
40
|
if (found.email !== void 0) out.email = found.email;
|
|
34
41
|
if (found.attributes !== void 0) out.attributes = found.attributes;
|
|
35
|
-
return
|
|
42
|
+
return {
|
|
43
|
+
subject: out,
|
|
44
|
+
identity: found
|
|
45
|
+
};
|
|
36
46
|
};
|
|
47
|
+
const subject = async (request) => (await caller(request))?.subject ?? null;
|
|
37
48
|
const canManage = async (s, request) => provider.canManage ? await provider.canManage(s, request) === true : perms.catalog.isManagerRole(s.role);
|
|
38
49
|
const canRead = async (s, request) => options.canRead ? await options.canRead(s, request) === true : canManage(s, request);
|
|
39
50
|
return {
|
|
40
51
|
auth: { async authenticate(request) {
|
|
41
|
-
const
|
|
42
|
-
if (!
|
|
52
|
+
const found = await caller(request);
|
|
53
|
+
if (!found) return null;
|
|
54
|
+
const s = found.subject;
|
|
43
55
|
const principal = {
|
|
44
56
|
id: s.id,
|
|
45
57
|
roles: [s.role],
|
|
46
|
-
metadata: {
|
|
58
|
+
metadata: {
|
|
59
|
+
subject: s,
|
|
60
|
+
identity: found.identity
|
|
61
|
+
}
|
|
47
62
|
};
|
|
48
63
|
if (s.name !== void 0) principal.name = s.name;
|
|
49
64
|
if (s.email !== void 0) principal.email = s.email;
|
|
@@ -60,6 +75,6 @@ function toKitAuth(provider, perms, options = {}) {
|
|
|
60
75
|
};
|
|
61
76
|
}
|
|
62
77
|
//#endregion
|
|
63
|
-
export {
|
|
78
|
+
export { subjectOf as n, toKitAuth as r, identityOf as t };
|
|
64
79
|
|
|
65
|
-
//# sourceMappingURL=kit-
|
|
80
|
+
//# sourceMappingURL=kit-BM3uosfV.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"kit-BM3uosfV.mjs","names":[],"sources":["../src/auth-provider/kit.ts"],"sourcesContent":["/**\n * Adapt a {@link PermissionsAuthProvider} to the kit's `AuthProvider` + `AuthorizationProvider`, so\n * the panel, the router and the framework adapters use it unchanged (ADR 0006).\n *\n * @module\n */\n\nimport type { AuthProvider, Principal } from \"@xtandard/kit/auth\";\nimport type { AuthorizationProvider, AuthorizeInput } from \"@xtandard/kit/authorization\";\nimport { NoSubjectError } from \"../errors.ts\";\nimport type { PermissionsAuthProvider, PermissionsSubject, SubjectIdentity } from \"./contract.ts\";\n\n/** What {@link toKitAuth} needs from the engine (structural, so a test double works too). */\nexport interface KitAuthEngine {\n readonly catalog: { isManagerRole(role: unknown): boolean };\n /** Resolves the effective role: the stored role if any, else the provider's, parsed. */\n snapshot(subject: SubjectIdentity): Promise<{ subject: { id: string | null; role: string } }>;\n}\n\n/** Options for {@link toKitAuth}. */\nexport interface ToKitAuthOptions {\n /**\n * May this subject use the panel's read actions (catalog, subjects, grants, history, the check\n * playground)? Default: the same answer as `canManage` — the panel shows every subject's grants.\n */\n canRead?(subject: PermissionsSubject, request: Request): boolean | Promise<boolean>;\n /** Which actions write state. Default: `grants:write` and `roles:write` (ADR 0008). */\n isMutating?(action: string): boolean;\n}\n\n/** The kit providers built by {@link toKitAuth}. */\nexport interface KitAuth {\n auth: AuthProvider;\n authorization: AuthorizationProvider;\n /** Resolve the caller as a {@link PermissionsSubject} (effective role), or `null`. */\n subject(request: Request): Promise<PermissionsSubject | null>;\n /** The resolved manage decision for a subject (provider `canManage`, else `managerRoles`). */\n canManage(subject: PermissionsSubject, request: Request): Promise<boolean>;\n}\n\nconst DEFAULT_MUTATING: ReadonlySet<string> = new Set([\"grants:write\", \"roles:write\"]);\n\n/** The `PermissionsSubject` a kit principal built by {@link toKitAuth} carries, if any. */\nexport function subjectOf(principal: Principal | null | undefined): PermissionsSubject | null {\n const meta = principal?.metadata as { subject?: PermissionsSubject } | undefined;\n return meta?.subject ?? null;\n}\n\n/**\n * The identity the auth provider reported for a kit principal built by {@link toKitAuth} (its raw\n * role, before the engine resolved it), if any.\n */\nexport function identityOf(principal: Principal | null | undefined): SubjectIdentity | null {\n const meta = principal?.metadata as { identity?: SubjectIdentity } | undefined;\n return meta?.identity ?? null;\n}\n\n/**\n * Build the kit `auth` + `authorization` pair from a provider and the engine.\n *\n * - `auth.authenticate` → principal `{ id, name, email, roles: [effectiveRole], metadata: { subject } }`.\n * - `authorization.authorize` → no principal: deny; mutating actions: `canManage`; read actions:\n * `canRead` (default `canManage`).\n *\n * @example\n * ```ts\n * const { auth, authorization } = toKitAuth(betterAuthProvider(auth), perms);\n * createFetchHandler({ permissions: perms, auth, authorization });\n * ```\n */\nexport function toKitAuth(\n provider: PermissionsAuthProvider,\n perms: KitAuthEngine,\n options: ToKitAuthOptions = {},\n): KitAuth {\n const isMutating = options.isMutating ?? ((action: string) => DEFAULT_MUTATING.has(action));\n\n const caller = async (\n request: Request,\n ): Promise<{ subject: PermissionsSubject; identity: SubjectIdentity } | null> => {\n const found = await provider.getSubject(request);\n if (found === null || found === undefined) return null;\n if (typeof found.id !== \"string\" || found.id === \"\") {\n throw new NoSubjectError(\"the auth provider returned an identity with an empty id\");\n }\n const { subject: resolved } = await perms.snapshot(found);\n const out: PermissionsSubject = { id: found.id, role: resolved.role };\n if (found.name !== undefined) out.name = found.name;\n if (found.email !== undefined) out.email = found.email;\n if (found.attributes !== undefined) out.attributes = found.attributes;\n return { subject: out, identity: found };\n };\n\n const subject = async (request: Request): Promise<PermissionsSubject | null> =>\n (await caller(request))?.subject ?? null;\n\n const canManage = async (s: PermissionsSubject, request: Request): Promise<boolean> =>\n provider.canManage\n ? (await provider.canManage(s, request)) === true\n : perms.catalog.isManagerRole(s.role);\n\n const canRead = async (s: PermissionsSubject, request: Request): Promise<boolean> =>\n options.canRead ? (await options.canRead(s, request)) === true : canManage(s, request);\n\n const auth: AuthProvider = {\n async authenticate(request) {\n const found = await caller(request);\n if (!found) return null;\n const s = found.subject;\n const principal: Principal = {\n id: s.id,\n roles: [s.role],\n metadata: { subject: s, identity: found.identity },\n };\n if (s.name !== undefined) principal.name = s.name;\n if (s.email !== undefined) principal.email = s.email;\n return principal;\n },\n };\n\n const authorization: AuthorizationProvider = {\n async authorize({ principal, action, request }: AuthorizeInput) {\n if (!principal) return false;\n const s = subjectOf(principal) ?? (await subject(request));\n if (!s) return false;\n return isMutating(action) ? canManage(s, request) : canRead(s, request);\n },\n };\n\n return { auth, authorization, subject, canManage };\n}\n"],"mappings":";;AAwCA,MAAM,mBAAwC,IAAI,IAAI,CAAC,gBAAgB,aAAa,CAAC;;AAGrF,SAAgB,UAAU,WAAoE;CAE5F,QADa,WAAW,WACX,WAAW;AAC1B;;;;;AAMA,SAAgB,WAAW,WAAiE;CAE1F,QADa,WAAW,WACX,YAAY;AAC3B;;;;;;;;;;;;;;AAeA,SAAgB,UACd,UACA,OACA,UAA4B,CAAC,GACpB;CACT,MAAM,aAAa,QAAQ,gBAAgB,WAAmB,iBAAiB,IAAI,MAAM;CAEzF,MAAM,SAAS,OACb,YAC+E;EAC/E,MAAM,QAAQ,MAAM,SAAS,WAAW,OAAO;EAC/C,IAAI,UAAU,QAAQ,UAAU,KAAA,GAAW,OAAO;EAClD,IAAI,OAAO,MAAM,OAAO,YAAY,MAAM,OAAO,IAC/C,MAAM,IAAI,eAAe,yDAAyD;EAEpF,MAAM,EAAE,SAAS,aAAa,MAAM,MAAM,SAAS,KAAK;EACxD,MAAM,MAA0B;GAAE,IAAI,MAAM;GAAI,MAAM,SAAS;EAAK;EACpE,IAAI,MAAM,SAAS,KAAA,GAAW,IAAI,OAAO,MAAM;EAC/C,IAAI,MAAM,UAAU,KAAA,GAAW,IAAI,QAAQ,MAAM;EACjD,IAAI,MAAM,eAAe,KAAA,GAAW,IAAI,aAAa,MAAM;EAC3D,OAAO;GAAE,SAAS;GAAK,UAAU;EAAM;CACzC;CAEA,MAAM,UAAU,OAAO,aACpB,MAAM,OAAO,OAAO,IAAI,WAAW;CAEtC,MAAM,YAAY,OAAO,GAAuB,YAC9C,SAAS,YACJ,MAAM,SAAS,UAAU,GAAG,OAAO,MAAO,OAC3C,MAAM,QAAQ,cAAc,EAAE,IAAI;CAExC,MAAM,UAAU,OAAO,GAAuB,YAC5C,QAAQ,UAAW,MAAM,QAAQ,QAAQ,GAAG,OAAO,MAAO,OAAO,UAAU,GAAG,OAAO;CA2BvF,OAAO;EAAE,MAAA,EAxBP,MAAM,aAAa,SAAS;GAC1B,MAAM,QAAQ,MAAM,OAAO,OAAO;GAClC,IAAI,CAAC,OAAO,OAAO;GACnB,MAAM,IAAI,MAAM;GAChB,MAAM,YAAuB;IAC3B,IAAI,EAAE;IACN,OAAO,CAAC,EAAE,IAAI;IACd,UAAU;KAAE,SAAS;KAAG,UAAU,MAAM;IAAS;GACnD;GACA,IAAI,EAAE,SAAS,KAAA,GAAW,UAAU,OAAO,EAAE;GAC7C,IAAI,EAAE,UAAU,KAAA,GAAW,UAAU,QAAQ,EAAE;GAC/C,OAAO;EACT,EAYU;EAAG,eAAA,EARb,MAAM,UAAU,EAAE,WAAW,QAAQ,WAA2B;GAC9D,IAAI,CAAC,WAAW,OAAO;GACvB,MAAM,IAAI,UAAU,SAAS,KAAM,MAAM,QAAQ,OAAO;GACxD,IAAI,CAAC,GAAG,OAAO;GACf,OAAO,WAAW,MAAM,IAAI,UAAU,GAAG,OAAO,IAAI,QAAQ,GAAG,OAAO;EACxE,EAGyB;EAAG;EAAS;CAAU;AACnD"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const require_errors = require("./errors-
|
|
1
|
+
const require_errors = require("./errors-CjpymzFj.cjs");
|
|
2
2
|
//#region src/auth-provider/kit.ts
|
|
3
3
|
const DEFAULT_MUTATING = new Set(["grants:write", "roles:write"]);
|
|
4
4
|
/** The `PermissionsSubject` a kit principal built by {@link toKitAuth} carries, if any. */
|
|
@@ -6,6 +6,13 @@ function subjectOf(principal) {
|
|
|
6
6
|
return (principal?.metadata)?.subject ?? null;
|
|
7
7
|
}
|
|
8
8
|
/**
|
|
9
|
+
* The identity the auth provider reported for a kit principal built by {@link toKitAuth} (its raw
|
|
10
|
+
* role, before the engine resolved it), if any.
|
|
11
|
+
*/
|
|
12
|
+
function identityOf(principal) {
|
|
13
|
+
return (principal?.metadata)?.identity ?? null;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
9
16
|
* Build the kit `auth` + `authorization` pair from a provider and the engine.
|
|
10
17
|
*
|
|
11
18
|
* - `auth.authenticate` → principal `{ id, name, email, roles: [effectiveRole], metadata: { subject } }`.
|
|
@@ -20,7 +27,7 @@ function subjectOf(principal) {
|
|
|
20
27
|
*/
|
|
21
28
|
function toKitAuth(provider, perms, options = {}) {
|
|
22
29
|
const isMutating = options.isMutating ?? ((action) => DEFAULT_MUTATING.has(action));
|
|
23
|
-
const
|
|
30
|
+
const caller = async (request) => {
|
|
24
31
|
const found = await provider.getSubject(request);
|
|
25
32
|
if (found === null || found === void 0) return null;
|
|
26
33
|
if (typeof found.id !== "string" || found.id === "") throw new require_errors.NoSubjectError("the auth provider returned an identity with an empty id");
|
|
@@ -32,18 +39,26 @@ function toKitAuth(provider, perms, options = {}) {
|
|
|
32
39
|
if (found.name !== void 0) out.name = found.name;
|
|
33
40
|
if (found.email !== void 0) out.email = found.email;
|
|
34
41
|
if (found.attributes !== void 0) out.attributes = found.attributes;
|
|
35
|
-
return
|
|
42
|
+
return {
|
|
43
|
+
subject: out,
|
|
44
|
+
identity: found
|
|
45
|
+
};
|
|
36
46
|
};
|
|
47
|
+
const subject = async (request) => (await caller(request))?.subject ?? null;
|
|
37
48
|
const canManage = async (s, request) => provider.canManage ? await provider.canManage(s, request) === true : perms.catalog.isManagerRole(s.role);
|
|
38
49
|
const canRead = async (s, request) => options.canRead ? await options.canRead(s, request) === true : canManage(s, request);
|
|
39
50
|
return {
|
|
40
51
|
auth: { async authenticate(request) {
|
|
41
|
-
const
|
|
42
|
-
if (!
|
|
52
|
+
const found = await caller(request);
|
|
53
|
+
if (!found) return null;
|
|
54
|
+
const s = found.subject;
|
|
43
55
|
const principal = {
|
|
44
56
|
id: s.id,
|
|
45
57
|
roles: [s.role],
|
|
46
|
-
metadata: {
|
|
58
|
+
metadata: {
|
|
59
|
+
subject: s,
|
|
60
|
+
identity: found.identity
|
|
61
|
+
}
|
|
47
62
|
};
|
|
48
63
|
if (s.name !== void 0) principal.name = s.name;
|
|
49
64
|
if (s.email !== void 0) principal.email = s.email;
|
|
@@ -60,6 +75,12 @@ function toKitAuth(provider, perms, options = {}) {
|
|
|
60
75
|
};
|
|
61
76
|
}
|
|
62
77
|
//#endregion
|
|
78
|
+
Object.defineProperty(exports, "identityOf", {
|
|
79
|
+
enumerable: true,
|
|
80
|
+
get: function() {
|
|
81
|
+
return identityOf;
|
|
82
|
+
}
|
|
83
|
+
});
|
|
63
84
|
Object.defineProperty(exports, "subjectOf", {
|
|
64
85
|
enumerable: true,
|
|
65
86
|
get: function() {
|
|
@@ -73,4 +94,4 @@ Object.defineProperty(exports, "toKitAuth", {
|
|
|
73
94
|
}
|
|
74
95
|
});
|
|
75
96
|
|
|
76
|
-
//# sourceMappingURL=kit-
|
|
97
|
+
//# sourceMappingURL=kit-BrqnCcDw.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"kit-BrqnCcDw.cjs","names":["NoSubjectError"],"sources":["../src/auth-provider/kit.ts"],"sourcesContent":["/**\n * Adapt a {@link PermissionsAuthProvider} to the kit's `AuthProvider` + `AuthorizationProvider`, so\n * the panel, the router and the framework adapters use it unchanged (ADR 0006).\n *\n * @module\n */\n\nimport type { AuthProvider, Principal } from \"@xtandard/kit/auth\";\nimport type { AuthorizationProvider, AuthorizeInput } from \"@xtandard/kit/authorization\";\nimport { NoSubjectError } from \"../errors.ts\";\nimport type { PermissionsAuthProvider, PermissionsSubject, SubjectIdentity } from \"./contract.ts\";\n\n/** What {@link toKitAuth} needs from the engine (structural, so a test double works too). */\nexport interface KitAuthEngine {\n readonly catalog: { isManagerRole(role: unknown): boolean };\n /** Resolves the effective role: the stored role if any, else the provider's, parsed. */\n snapshot(subject: SubjectIdentity): Promise<{ subject: { id: string | null; role: string } }>;\n}\n\n/** Options for {@link toKitAuth}. */\nexport interface ToKitAuthOptions {\n /**\n * May this subject use the panel's read actions (catalog, subjects, grants, history, the check\n * playground)? Default: the same answer as `canManage` — the panel shows every subject's grants.\n */\n canRead?(subject: PermissionsSubject, request: Request): boolean | Promise<boolean>;\n /** Which actions write state. Default: `grants:write` and `roles:write` (ADR 0008). */\n isMutating?(action: string): boolean;\n}\n\n/** The kit providers built by {@link toKitAuth}. */\nexport interface KitAuth {\n auth: AuthProvider;\n authorization: AuthorizationProvider;\n /** Resolve the caller as a {@link PermissionsSubject} (effective role), or `null`. */\n subject(request: Request): Promise<PermissionsSubject | null>;\n /** The resolved manage decision for a subject (provider `canManage`, else `managerRoles`). */\n canManage(subject: PermissionsSubject, request: Request): Promise<boolean>;\n}\n\nconst DEFAULT_MUTATING: ReadonlySet<string> = new Set([\"grants:write\", \"roles:write\"]);\n\n/** The `PermissionsSubject` a kit principal built by {@link toKitAuth} carries, if any. */\nexport function subjectOf(principal: Principal | null | undefined): PermissionsSubject | null {\n const meta = principal?.metadata as { subject?: PermissionsSubject } | undefined;\n return meta?.subject ?? null;\n}\n\n/**\n * The identity the auth provider reported for a kit principal built by {@link toKitAuth} (its raw\n * role, before the engine resolved it), if any.\n */\nexport function identityOf(principal: Principal | null | undefined): SubjectIdentity | null {\n const meta = principal?.metadata as { identity?: SubjectIdentity } | undefined;\n return meta?.identity ?? null;\n}\n\n/**\n * Build the kit `auth` + `authorization` pair from a provider and the engine.\n *\n * - `auth.authenticate` → principal `{ id, name, email, roles: [effectiveRole], metadata: { subject } }`.\n * - `authorization.authorize` → no principal: deny; mutating actions: `canManage`; read actions:\n * `canRead` (default `canManage`).\n *\n * @example\n * ```ts\n * const { auth, authorization } = toKitAuth(betterAuthProvider(auth), perms);\n * createFetchHandler({ permissions: perms, auth, authorization });\n * ```\n */\nexport function toKitAuth(\n provider: PermissionsAuthProvider,\n perms: KitAuthEngine,\n options: ToKitAuthOptions = {},\n): KitAuth {\n const isMutating = options.isMutating ?? ((action: string) => DEFAULT_MUTATING.has(action));\n\n const caller = async (\n request: Request,\n ): Promise<{ subject: PermissionsSubject; identity: SubjectIdentity } | null> => {\n const found = await provider.getSubject(request);\n if (found === null || found === undefined) return null;\n if (typeof found.id !== \"string\" || found.id === \"\") {\n throw new NoSubjectError(\"the auth provider returned an identity with an empty id\");\n }\n const { subject: resolved } = await perms.snapshot(found);\n const out: PermissionsSubject = { id: found.id, role: resolved.role };\n if (found.name !== undefined) out.name = found.name;\n if (found.email !== undefined) out.email = found.email;\n if (found.attributes !== undefined) out.attributes = found.attributes;\n return { subject: out, identity: found };\n };\n\n const subject = async (request: Request): Promise<PermissionsSubject | null> =>\n (await caller(request))?.subject ?? null;\n\n const canManage = async (s: PermissionsSubject, request: Request): Promise<boolean> =>\n provider.canManage\n ? (await provider.canManage(s, request)) === true\n : perms.catalog.isManagerRole(s.role);\n\n const canRead = async (s: PermissionsSubject, request: Request): Promise<boolean> =>\n options.canRead ? (await options.canRead(s, request)) === true : canManage(s, request);\n\n const auth: AuthProvider = {\n async authenticate(request) {\n const found = await caller(request);\n if (!found) return null;\n const s = found.subject;\n const principal: Principal = {\n id: s.id,\n roles: [s.role],\n metadata: { subject: s, identity: found.identity },\n };\n if (s.name !== undefined) principal.name = s.name;\n if (s.email !== undefined) principal.email = s.email;\n return principal;\n },\n };\n\n const authorization: AuthorizationProvider = {\n async authorize({ principal, action, request }: AuthorizeInput) {\n if (!principal) return false;\n const s = subjectOf(principal) ?? (await subject(request));\n if (!s) return false;\n return isMutating(action) ? canManage(s, request) : canRead(s, request);\n },\n };\n\n return { auth, authorization, subject, canManage };\n}\n"],"mappings":";;AAwCA,MAAM,mBAAwC,IAAI,IAAI,CAAC,gBAAgB,aAAa,CAAC;;AAGrF,SAAgB,UAAU,WAAoE;CAE5F,QADa,WAAW,WACX,WAAW;AAC1B;;;;;AAMA,SAAgB,WAAW,WAAiE;CAE1F,QADa,WAAW,WACX,YAAY;AAC3B;;;;;;;;;;;;;;AAeA,SAAgB,UACd,UACA,OACA,UAA4B,CAAC,GACpB;CACT,MAAM,aAAa,QAAQ,gBAAgB,WAAmB,iBAAiB,IAAI,MAAM;CAEzF,MAAM,SAAS,OACb,YAC+E;EAC/E,MAAM,QAAQ,MAAM,SAAS,WAAW,OAAO;EAC/C,IAAI,UAAU,QAAQ,UAAU,KAAA,GAAW,OAAO;EAClD,IAAI,OAAO,MAAM,OAAO,YAAY,MAAM,OAAO,IAC/C,MAAM,IAAIA,eAAAA,eAAe,yDAAyD;EAEpF,MAAM,EAAE,SAAS,aAAa,MAAM,MAAM,SAAS,KAAK;EACxD,MAAM,MAA0B;GAAE,IAAI,MAAM;GAAI,MAAM,SAAS;EAAK;EACpE,IAAI,MAAM,SAAS,KAAA,GAAW,IAAI,OAAO,MAAM;EAC/C,IAAI,MAAM,UAAU,KAAA,GAAW,IAAI,QAAQ,MAAM;EACjD,IAAI,MAAM,eAAe,KAAA,GAAW,IAAI,aAAa,MAAM;EAC3D,OAAO;GAAE,SAAS;GAAK,UAAU;EAAM;CACzC;CAEA,MAAM,UAAU,OAAO,aACpB,MAAM,OAAO,OAAO,IAAI,WAAW;CAEtC,MAAM,YAAY,OAAO,GAAuB,YAC9C,SAAS,YACJ,MAAM,SAAS,UAAU,GAAG,OAAO,MAAO,OAC3C,MAAM,QAAQ,cAAc,EAAE,IAAI;CAExC,MAAM,UAAU,OAAO,GAAuB,YAC5C,QAAQ,UAAW,MAAM,QAAQ,QAAQ,GAAG,OAAO,MAAO,OAAO,UAAU,GAAG,OAAO;CA2BvF,OAAO;EAAE,MAAA,EAxBP,MAAM,aAAa,SAAS;GAC1B,MAAM,QAAQ,MAAM,OAAO,OAAO;GAClC,IAAI,CAAC,OAAO,OAAO;GACnB,MAAM,IAAI,MAAM;GAChB,MAAM,YAAuB;IAC3B,IAAI,EAAE;IACN,OAAO,CAAC,EAAE,IAAI;IACd,UAAU;KAAE,SAAS;KAAG,UAAU,MAAM;IAAS;GACnD;GACA,IAAI,EAAE,SAAS,KAAA,GAAW,UAAU,OAAO,EAAE;GAC7C,IAAI,EAAE,UAAU,KAAA,GAAW,UAAU,QAAQ,EAAE;GAC/C,OAAO;EACT,EAYU;EAAG,eAAA,EARb,MAAM,UAAU,EAAE,WAAW,QAAQ,WAA2B;GAC9D,IAAI,CAAC,WAAW,OAAO;GACvB,MAAM,IAAI,UAAU,SAAS,KAAM,MAAM,QAAQ,OAAO;GACxD,IAAI,CAAC,GAAG,OAAO;GACf,OAAO,WAAW,MAAM,IAAI,UAAU,GAAG,OAAO,IAAI,QAAQ,GAAG,OAAO;EACxE,EAGyB;EAAG;EAAS;CAAU;AACnD"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { f as PermissionsSubject, h as SubjectIdentity } from "./catalog-DA_d1SdC.cjs";
|
|
2
|
-
import { t as PermissionsAuthProvider } from "./contract-
|
|
2
|
+
import { t as PermissionsAuthProvider } from "./contract-BAisPHBw.cjs";
|
|
3
3
|
import { AuthProvider, Principal } from "@xtandard/kit/auth";
|
|
4
4
|
import { AuthorizationProvider } from "@xtandard/kit/authorization";
|
|
5
5
|
|
|
@@ -38,6 +38,11 @@ interface KitAuth {
|
|
|
38
38
|
}
|
|
39
39
|
/** The `PermissionsSubject` a kit principal built by {@link toKitAuth} carries, if any. */
|
|
40
40
|
declare function subjectOf(principal: Principal | null | undefined): PermissionsSubject | null;
|
|
41
|
+
/**
|
|
42
|
+
* The identity the auth provider reported for a kit principal built by {@link toKitAuth} (its raw
|
|
43
|
+
* role, before the engine resolved it), if any.
|
|
44
|
+
*/
|
|
45
|
+
declare function identityOf(principal: Principal | null | undefined): SubjectIdentity | null;
|
|
41
46
|
/**
|
|
42
47
|
* Build the kit `auth` + `authorization` pair from a provider and the engine.
|
|
43
48
|
*
|
|
@@ -53,5 +58,5 @@ declare function subjectOf(principal: Principal | null | undefined): Permissions
|
|
|
53
58
|
*/
|
|
54
59
|
declare function toKitAuth(provider: PermissionsAuthProvider, perms: KitAuthEngine, options?: ToKitAuthOptions): KitAuth;
|
|
55
60
|
//#endregion
|
|
56
|
-
export {
|
|
57
|
-
//# sourceMappingURL=kit-
|
|
61
|
+
export { subjectOf as a, identityOf as i, KitAuthEngine as n, toKitAuth as o, ToKitAuthOptions as r, KitAuth as t };
|
|
62
|
+
//# sourceMappingURL=kit-DLF7DZrp.d.cts.map
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { n as subjectOf, t as identityOf } from "./kit-BM3uosfV.mjs";
|
|
2
2
|
import { delegatedAuthorization } from "@xtandard/kit/authorization/delegated";
|
|
3
3
|
//#region src/authorization/permissions.ts
|
|
4
4
|
/**
|
|
@@ -7,12 +7,15 @@ import { delegatedAuthorization } from "@xtandard/kit/authorization/delegated";
|
|
|
7
7
|
*/
|
|
8
8
|
function principalIdentity(principal) {
|
|
9
9
|
const attached = subjectOf(principal);
|
|
10
|
+
const reported = identityOf(principal);
|
|
10
11
|
const out = {
|
|
11
12
|
id: principal.id,
|
|
12
13
|
role: principal.roles ?? null
|
|
13
14
|
};
|
|
14
15
|
if (attached && attached.id === principal.id) {
|
|
15
|
-
out.role = attached.role;
|
|
16
|
+
if (!reported || reported.id !== principal.id) out.role = attached.role;
|
|
17
|
+
else if (reported.role === void 0) delete out.role;
|
|
18
|
+
else out.role = reported.role;
|
|
16
19
|
if (attached.attributes !== void 0) out.attributes = attached.attributes;
|
|
17
20
|
}
|
|
18
21
|
const name = attached?.name ?? principal.name;
|
|
@@ -56,4 +59,4 @@ function permissionsAuthorization(perms, map, options = {}) {
|
|
|
56
59
|
//#endregion
|
|
57
60
|
export { principalIdentity as n, permissionsAuthorization as t };
|
|
58
61
|
|
|
59
|
-
//# sourceMappingURL=permissions-
|
|
62
|
+
//# sourceMappingURL=permissions-CeV6M3v7.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"permissions-CeV6M3v7.mjs","names":[],"sources":["../src/authorization/permissions.ts"],"sourcesContent":["/**\n * Authorize **another** xtandard panel (webhooks, flags, audit, entitlements…) with this catalog\n * (ADR 0011): {@link permissionsAuthorization} maps each of that panel's actions to a permission\n * path of your catalog and asks the engine, through the kit's `delegatedAuthorization`.\n *\n * The principal becomes the subject: the identity `toKitAuth` attached (`metadata.subject`) when\n * present, else `{ id, role: principal.roles, name, email }`. An unmapped action is denied.\n *\n * @module\n */\n\nimport type { Principal } from \"@xtandard/kit/auth\";\nimport type { AuthorizationProvider, AuthorizeInput } from \"@xtandard/kit/authorization\";\nimport { delegatedAuthorization } from \"@xtandard/kit/authorization/delegated\";\nimport { identityOf, subjectOf } from \"../auth-provider/kit.ts\";\nimport type { SubjectIdentity } from \"../catalog.ts\";\nimport type { AnyPermissions } from \"../engine.ts\";\n\n/** What one panel action maps to: a path, a path plus the data its rule reads, or deny. */\nexport type PermissionTarget = string | { path: string; data?: unknown } | null | undefined | false;\n\n/**\n * Panel action → permission path. A record (`{ \"endpoint:write\": \"webhooks.manage\" }`) or a\n * function of the full input (to pass the resource as the rule's data).\n */\nexport type PermissionsAuthorizationMap<Action extends string, Resource> =\n | Partial<Record<Action, PermissionTarget>>\n | ((input: AuthorizeInput<Action, Resource>) => PermissionTarget | Promise<PermissionTarget>);\n\n/** Options for {@link permissionsAuthorization}. */\nexport interface PermissionsAuthorizationOptions<Action extends string, Resource> {\n /** Override how a principal becomes a subject (default: see the module doc). */\n subject?(\n principal: Principal,\n input: AuthorizeInput<Action, Resource>,\n ): SubjectIdentity | null | Promise<SubjectIdentity | null>;\n /** The rules' `ctx`, for catalogs that need app facts. */\n ctx?(input: AuthorizeInput<Action, Resource>): unknown;\n}\n\n/**\n * The subject identity of a kit principal: the one `toKitAuth` attached, else the principal's own\n * id, roles, name and email.\n */\nexport function principalIdentity(principal: Principal): SubjectIdentity {\n const attached = subjectOf(principal);\n const reported = identityOf(principal);\n const out: SubjectIdentity = { id: principal.id, role: principal.roles ?? null };\n if (attached && attached.id === principal.id) {\n // The provider's own role (not the resolved one), so the engine reports its true roleSource.\n if (!reported || reported.id !== principal.id) out.role = attached.role;\n else if (reported.role === undefined)\n delete out.role; // the engine looks it up (cached)\n else out.role = reported.role;\n if (attached.attributes !== undefined) out.attributes = attached.attributes;\n }\n const name = attached?.name ?? principal.name;\n const email = attached?.email ?? principal.email;\n if (name !== undefined) out.name = name;\n if (email !== undefined) out.email = email;\n return out;\n}\n\n/**\n * Build a kit `AuthorizationProvider` that decides another panel's actions with this catalog.\n *\n * @example\n * ```ts\n * import { permissionsAuthorization } from \"@xtandard/permissions/authorization/permissions\";\n * import { createFetchHandler as webhooksPanel } from \"@xtandard/webhooks\";\n *\n * webhooksPanel({\n * auth, // the same kit auth, e.g. toKitAuth(betterAuthProvider(auth), perms).auth\n * authorization: permissionsAuthorization(perms, {\n * \"endpoint:read\": \"webhooks.read\",\n * \"endpoint:write\": \"webhooks.manage\",\n * }),\n * });\n * ```\n */\nexport function permissionsAuthorization<Action extends string = string, Resource = unknown>(\n perms: AnyPermissions,\n map: PermissionsAuthorizationMap<Action, Resource>,\n options: PermissionsAuthorizationOptions<Action, Resource> = {},\n): AuthorizationProvider<Action, Resource> {\n return delegatedAuthorization<Action, Resource>({\n async authorize(input) {\n if (!input.principal) return false;\n const target =\n typeof map === \"function\"\n ? await map(input)\n : Object.hasOwn(map, input.action)\n ? map[input.action]\n : undefined;\n if (!target) return false;\n const { path, data } =\n typeof target === \"string\" ? { path: target, data: undefined } : target;\n const subject = options.subject\n ? await options.subject(input.principal, input)\n : principalIdentity(input.principal);\n if (!subject) return false;\n const facts = options.ctx ? [{ ctx: options.ctx(input) }] : [];\n const ability = await perms.ability(subject, ...(facts as []));\n return (await ability.checkAsync(path, data)).allowed;\n },\n });\n}\n"],"mappings":";;;;;;;AA4CA,SAAgB,kBAAkB,WAAuC;CACvE,MAAM,WAAW,UAAU,SAAS;CACpC,MAAM,WAAW,WAAW,SAAS;CACrC,MAAM,MAAuB;EAAE,IAAI,UAAU;EAAI,MAAM,UAAU,SAAS;CAAK;CAC/E,IAAI,YAAY,SAAS,OAAO,UAAU,IAAI;EAE5C,IAAI,CAAC,YAAY,SAAS,OAAO,UAAU,IAAI,IAAI,OAAO,SAAS;OAC9D,IAAI,SAAS,SAAS,KAAA,GACzB,OAAO,IAAI;OACR,IAAI,OAAO,SAAS;EACzB,IAAI,SAAS,eAAe,KAAA,GAAW,IAAI,aAAa,SAAS;CACnE;CACA,MAAM,OAAO,UAAU,QAAQ,UAAU;CACzC,MAAM,QAAQ,UAAU,SAAS,UAAU;CAC3C,IAAI,SAAS,KAAA,GAAW,IAAI,OAAO;CACnC,IAAI,UAAU,KAAA,GAAW,IAAI,QAAQ;CACrC,OAAO;AACT;;;;;;;;;;;;;;;;;;AAmBA,SAAgB,yBACd,OACA,KACA,UAA6D,CAAC,GACrB;CACzC,OAAO,uBAAyC,EAC9C,MAAM,UAAU,OAAO;EACrB,IAAI,CAAC,MAAM,WAAW,OAAO;EAC7B,MAAM,SACJ,OAAO,QAAQ,aACX,MAAM,IAAI,KAAK,IACf,OAAO,OAAO,KAAK,MAAM,MAAM,IAC7B,IAAI,MAAM,UACV,KAAA;EACR,IAAI,CAAC,QAAQ,OAAO;EACpB,MAAM,EAAE,MAAM,SACZ,OAAO,WAAW,WAAW;GAAE,MAAM;GAAQ,MAAM,KAAA;EAAU,IAAI;EACnE,MAAM,UAAU,QAAQ,UACpB,MAAM,QAAQ,QAAQ,MAAM,WAAW,KAAK,IAC5C,kBAAkB,MAAM,SAAS;EACrC,IAAI,CAAC,SAAS,OAAO;EACrB,MAAM,QAAQ,QAAQ,MAAM,CAAC,EAAE,KAAK,QAAQ,IAAI,KAAK,EAAE,CAAC,IAAI,CAAC;EAE7D,QAAQ,OAAM,MADQ,MAAM,QAAQ,SAAS,GAAI,KAAY,GACvC,WAAW,MAAM,IAAI,GAAG;CAChD,EACF,CAAC;AACH"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const require_kit = require("./kit-
|
|
1
|
+
const require_kit = require("./kit-BrqnCcDw.cjs");
|
|
2
2
|
let _xtandard_kit_authorization_delegated = require("@xtandard/kit/authorization/delegated");
|
|
3
3
|
//#region src/authorization/permissions.ts
|
|
4
4
|
/**
|
|
@@ -7,12 +7,15 @@ let _xtandard_kit_authorization_delegated = require("@xtandard/kit/authorization
|
|
|
7
7
|
*/
|
|
8
8
|
function principalIdentity(principal) {
|
|
9
9
|
const attached = require_kit.subjectOf(principal);
|
|
10
|
+
const reported = require_kit.identityOf(principal);
|
|
10
11
|
const out = {
|
|
11
12
|
id: principal.id,
|
|
12
13
|
role: principal.roles ?? null
|
|
13
14
|
};
|
|
14
15
|
if (attached && attached.id === principal.id) {
|
|
15
|
-
out.role = attached.role;
|
|
16
|
+
if (!reported || reported.id !== principal.id) out.role = attached.role;
|
|
17
|
+
else if (reported.role === void 0) delete out.role;
|
|
18
|
+
else out.role = reported.role;
|
|
16
19
|
if (attached.attributes !== void 0) out.attributes = attached.attributes;
|
|
17
20
|
}
|
|
18
21
|
const name = attached?.name ?? principal.name;
|
|
@@ -67,4 +70,4 @@ Object.defineProperty(exports, "principalIdentity", {
|
|
|
67
70
|
}
|
|
68
71
|
});
|
|
69
72
|
|
|
70
|
-
//# sourceMappingURL=permissions-
|
|
73
|
+
//# sourceMappingURL=permissions-DDEkHNvN.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"permissions-DDEkHNvN.cjs","names":["subjectOf","identityOf"],"sources":["../src/authorization/permissions.ts"],"sourcesContent":["/**\n * Authorize **another** xtandard panel (webhooks, flags, audit, entitlements…) with this catalog\n * (ADR 0011): {@link permissionsAuthorization} maps each of that panel's actions to a permission\n * path of your catalog and asks the engine, through the kit's `delegatedAuthorization`.\n *\n * The principal becomes the subject: the identity `toKitAuth` attached (`metadata.subject`) when\n * present, else `{ id, role: principal.roles, name, email }`. An unmapped action is denied.\n *\n * @module\n */\n\nimport type { Principal } from \"@xtandard/kit/auth\";\nimport type { AuthorizationProvider, AuthorizeInput } from \"@xtandard/kit/authorization\";\nimport { delegatedAuthorization } from \"@xtandard/kit/authorization/delegated\";\nimport { identityOf, subjectOf } from \"../auth-provider/kit.ts\";\nimport type { SubjectIdentity } from \"../catalog.ts\";\nimport type { AnyPermissions } from \"../engine.ts\";\n\n/** What one panel action maps to: a path, a path plus the data its rule reads, or deny. */\nexport type PermissionTarget = string | { path: string; data?: unknown } | null | undefined | false;\n\n/**\n * Panel action → permission path. A record (`{ \"endpoint:write\": \"webhooks.manage\" }`) or a\n * function of the full input (to pass the resource as the rule's data).\n */\nexport type PermissionsAuthorizationMap<Action extends string, Resource> =\n | Partial<Record<Action, PermissionTarget>>\n | ((input: AuthorizeInput<Action, Resource>) => PermissionTarget | Promise<PermissionTarget>);\n\n/** Options for {@link permissionsAuthorization}. */\nexport interface PermissionsAuthorizationOptions<Action extends string, Resource> {\n /** Override how a principal becomes a subject (default: see the module doc). */\n subject?(\n principal: Principal,\n input: AuthorizeInput<Action, Resource>,\n ): SubjectIdentity | null | Promise<SubjectIdentity | null>;\n /** The rules' `ctx`, for catalogs that need app facts. */\n ctx?(input: AuthorizeInput<Action, Resource>): unknown;\n}\n\n/**\n * The subject identity of a kit principal: the one `toKitAuth` attached, else the principal's own\n * id, roles, name and email.\n */\nexport function principalIdentity(principal: Principal): SubjectIdentity {\n const attached = subjectOf(principal);\n const reported = identityOf(principal);\n const out: SubjectIdentity = { id: principal.id, role: principal.roles ?? null };\n if (attached && attached.id === principal.id) {\n // The provider's own role (not the resolved one), so the engine reports its true roleSource.\n if (!reported || reported.id !== principal.id) out.role = attached.role;\n else if (reported.role === undefined)\n delete out.role; // the engine looks it up (cached)\n else out.role = reported.role;\n if (attached.attributes !== undefined) out.attributes = attached.attributes;\n }\n const name = attached?.name ?? principal.name;\n const email = attached?.email ?? principal.email;\n if (name !== undefined) out.name = name;\n if (email !== undefined) out.email = email;\n return out;\n}\n\n/**\n * Build a kit `AuthorizationProvider` that decides another panel's actions with this catalog.\n *\n * @example\n * ```ts\n * import { permissionsAuthorization } from \"@xtandard/permissions/authorization/permissions\";\n * import { createFetchHandler as webhooksPanel } from \"@xtandard/webhooks\";\n *\n * webhooksPanel({\n * auth, // the same kit auth, e.g. toKitAuth(betterAuthProvider(auth), perms).auth\n * authorization: permissionsAuthorization(perms, {\n * \"endpoint:read\": \"webhooks.read\",\n * \"endpoint:write\": \"webhooks.manage\",\n * }),\n * });\n * ```\n */\nexport function permissionsAuthorization<Action extends string = string, Resource = unknown>(\n perms: AnyPermissions,\n map: PermissionsAuthorizationMap<Action, Resource>,\n options: PermissionsAuthorizationOptions<Action, Resource> = {},\n): AuthorizationProvider<Action, Resource> {\n return delegatedAuthorization<Action, Resource>({\n async authorize(input) {\n if (!input.principal) return false;\n const target =\n typeof map === \"function\"\n ? await map(input)\n : Object.hasOwn(map, input.action)\n ? map[input.action]\n : undefined;\n if (!target) return false;\n const { path, data } =\n typeof target === \"string\" ? { path: target, data: undefined } : target;\n const subject = options.subject\n ? await options.subject(input.principal, input)\n : principalIdentity(input.principal);\n if (!subject) return false;\n const facts = options.ctx ? [{ ctx: options.ctx(input) }] : [];\n const ability = await perms.ability(subject, ...(facts as []));\n return (await ability.checkAsync(path, data)).allowed;\n },\n });\n}\n"],"mappings":";;;;;;;AA4CA,SAAgB,kBAAkB,WAAuC;CACvE,MAAM,WAAWA,YAAAA,UAAU,SAAS;CACpC,MAAM,WAAWC,YAAAA,WAAW,SAAS;CACrC,MAAM,MAAuB;EAAE,IAAI,UAAU;EAAI,MAAM,UAAU,SAAS;CAAK;CAC/E,IAAI,YAAY,SAAS,OAAO,UAAU,IAAI;EAE5C,IAAI,CAAC,YAAY,SAAS,OAAO,UAAU,IAAI,IAAI,OAAO,SAAS;OAC9D,IAAI,SAAS,SAAS,KAAA,GACzB,OAAO,IAAI;OACR,IAAI,OAAO,SAAS;EACzB,IAAI,SAAS,eAAe,KAAA,GAAW,IAAI,aAAa,SAAS;CACnE;CACA,MAAM,OAAO,UAAU,QAAQ,UAAU;CACzC,MAAM,QAAQ,UAAU,SAAS,UAAU;CAC3C,IAAI,SAAS,KAAA,GAAW,IAAI,OAAO;CACnC,IAAI,UAAU,KAAA,GAAW,IAAI,QAAQ;CACrC,OAAO;AACT;;;;;;;;;;;;;;;;;;AAmBA,SAAgB,yBACd,OACA,KACA,UAA6D,CAAC,GACrB;CACzC,QAAA,GAAA,sCAAA,wBAAgD,EAC9C,MAAM,UAAU,OAAO;EACrB,IAAI,CAAC,MAAM,WAAW,OAAO;EAC7B,MAAM,SACJ,OAAO,QAAQ,aACX,MAAM,IAAI,KAAK,IACf,OAAO,OAAO,KAAK,MAAM,MAAM,IAC7B,IAAI,MAAM,UACV,KAAA;EACR,IAAI,CAAC,QAAQ,OAAO;EACpB,MAAM,EAAE,MAAM,SACZ,OAAO,WAAW,WAAW;GAAE,MAAM;GAAQ,MAAM,KAAA;EAAU,IAAI;EACnE,MAAM,UAAU,QAAQ,UACpB,MAAM,QAAQ,QAAQ,MAAM,WAAW,KAAK,IAC5C,kBAAkB,MAAM,SAAS;EACrC,IAAI,CAAC,SAAS,OAAO;EACrB,MAAM,QAAQ,QAAQ,MAAM,CAAC,EAAE,KAAK,QAAQ,IAAI,KAAK,EAAE,CAAC,IAAI,CAAC;EAE7D,QAAQ,OAAM,MADQ,MAAM,QAAQ,SAAS,GAAI,KAAY,GACvC,WAAW,MAAM,IAAI,GAAG;CAChD,EACF,CAAC;AACH"}
|