@xtandard/permissions 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +362 -2
- package/bin/xtandard-permissions.mjs +3 -0
- package/dist/better-auth-Bp9YVyQa.d.cts +56 -0
- package/dist/better-auth-DHyW5-yt.cjs +91 -0
- package/dist/better-auth-DHyW5-yt.cjs.map +1 -0
- package/dist/better-auth-NO40ZqSM.mjs +74 -0
- package/dist/better-auth-NO40ZqSM.mjs.map +1 -0
- package/dist/better-auth-NSXgt6ZU.d.mts +56 -0
- package/dist/catalog-DA_d1SdC.d.cts +153 -0
- package/dist/catalog-DJcJ3ycq.d.mts +153 -0
- package/dist/catalog.cjs +198 -0
- package/dist/catalog.cjs.map +1 -0
- package/dist/catalog.d.cts +3 -0
- package/dist/catalog.d.mts +3 -0
- package/dist/catalog.mjs +141 -0
- package/dist/catalog.mjs.map +1 -0
- package/dist/chunk-DakpK96I.cjs +43 -0
- package/dist/cli.cjs +548 -0
- package/dist/cli.cjs.map +1 -0
- package/dist/cli.d.cts +28 -0
- package/dist/cli.d.mts +28 -0
- package/dist/cli.mjs +544 -0
- package/dist/cli.mjs.map +1 -0
- package/dist/contract-BMiOmg2f.d.cts +50 -0
- package/dist/contract-Bpy8yPrS.cjs +70 -0
- package/dist/contract-Bpy8yPrS.cjs.map +1 -0
- package/dist/contract-CNgMXr2u.d.cts +46 -0
- package/dist/contract-CNgMXr2u.d.mts +46 -0
- package/dist/contract-CPmJVoRx.cjs +70 -0
- package/dist/contract-CPmJVoRx.cjs.map +1 -0
- package/dist/contract-D32H1t0u.d.mts +50 -0
- package/dist/contract-DTdTXZtJ.mjs +47 -0
- package/dist/contract-DTdTXZtJ.mjs.map +1 -0
- package/dist/contract-DUzgB-He.d.mts +375 -0
- package/dist/contract-Dk6d2ylD.d.cts +375 -0
- package/dist/contract-RMkmERhT.d.cts +2 -0
- package/dist/contract-RMkmERhT.d.mts +2 -0
- package/dist/contract-Yc5AZuvv.mjs +47 -0
- package/dist/contract-Yc5AZuvv.mjs.map +1 -0
- package/dist/create-fetch-handler-BHDU4chu.d.mts +77 -0
- package/dist/create-fetch-handler-BtX64_rS.d.cts +77 -0
- package/dist/create-fetch-handler-C-WyaRCR.cjs +653 -0
- package/dist/create-fetch-handler-C-WyaRCR.cjs.map +1 -0
- package/dist/create-fetch-handler-ojjpk1Iw.mjs +636 -0
- package/dist/create-fetch-handler-ojjpk1Iw.mjs.map +1 -0
- package/dist/elysia-shared-BM6cPoCU.mjs +78 -0
- package/dist/elysia-shared-BM6cPoCU.mjs.map +1 -0
- package/dist/elysia-shared-BUTFTeal.cjs +101 -0
- package/dist/elysia-shared-BUTFTeal.cjs.map +1 -0
- package/dist/elysia-shared-DtKViycb.d.cts +58 -0
- package/dist/elysia-shared-g8zsb_pU.d.mts +58 -0
- package/dist/engine-Bufv0TbD.mjs +574 -0
- package/dist/engine-Bufv0TbD.mjs.map +1 -0
- package/dist/engine-CGGVDCqi.d.cts +296 -0
- package/dist/engine-DpFIcbgh.d.mts +296 -0
- package/dist/engine-bMoz3iW-.cjs +587 -0
- package/dist/engine-bMoz3iW-.cjs.map +1 -0
- package/dist/entry-auth-basic.cjs +40 -0
- package/dist/entry-auth-basic.cjs.map +1 -0
- package/dist/entry-auth-basic.d.cts +24 -0
- package/dist/entry-auth-basic.d.mts +24 -0
- package/dist/entry-auth-basic.mjs +26 -0
- package/dist/entry-auth-basic.mjs.map +1 -0
- package/dist/entry-auth-better-auth.cjs +12 -0
- package/dist/entry-auth-better-auth.d.cts +5 -0
- package/dist/entry-auth-better-auth.d.mts +5 -0
- package/dist/entry-auth-better-auth.mjs +4 -0
- package/dist/entry-auth-delegated.cjs +8 -0
- package/dist/entry-auth-delegated.d.cts +2 -0
- package/dist/entry-auth-delegated.d.mts +2 -0
- package/dist/entry-auth-delegated.mjs +2 -0
- package/dist/entry-auth-jwt.cjs +116 -0
- package/dist/entry-auth-jwt.cjs.map +1 -0
- package/dist/entry-auth-jwt.d.cts +65 -0
- package/dist/entry-auth-jwt.d.mts +65 -0
- package/dist/entry-auth-jwt.mjs +108 -0
- package/dist/entry-auth-jwt.mjs.map +1 -0
- package/dist/entry-auth-none.cjs +14 -0
- package/dist/entry-auth-none.d.cts +2 -0
- package/dist/entry-auth-none.d.mts +2 -0
- package/dist/entry-auth-none.mjs +2 -0
- package/dist/entry-auth-session.cjs +82 -0
- package/dist/entry-auth-session.cjs.map +1 -0
- package/dist/entry-auth-session.d.cts +20 -0
- package/dist/entry-auth-session.d.mts +20 -0
- package/dist/entry-auth-session.mjs +75 -0
- package/dist/entry-auth-session.mjs.map +1 -0
- package/dist/entry-authorization-delegated.cjs +31 -0
- package/dist/entry-authorization-delegated.cjs.map +1 -0
- package/dist/entry-authorization-delegated.d.cts +27 -0
- package/dist/entry-authorization-delegated.d.mts +27 -0
- package/dist/entry-authorization-delegated.mjs +30 -0
- package/dist/entry-authorization-delegated.mjs.map +1 -0
- package/dist/entry-authorization-none.cjs +29 -0
- package/dist/entry-authorization-none.cjs.map +1 -0
- package/dist/entry-authorization-none.d.cts +17 -0
- package/dist/entry-authorization-none.d.mts +17 -0
- package/dist/entry-authorization-none.mjs +28 -0
- package/dist/entry-authorization-none.mjs.map +1 -0
- package/dist/entry-authorization-permissions.cjs +4 -0
- package/dist/entry-authorization-permissions.d.cts +49 -0
- package/dist/entry-authorization-permissions.d.mts +49 -0
- package/dist/entry-authorization-permissions.mjs +2 -0
- package/dist/entry-authorization-roles.cjs +9 -0
- package/dist/entry-authorization-roles.d.cts +37 -0
- package/dist/entry-authorization-roles.d.mts +37 -0
- package/dist/entry-authorization-roles.mjs +3 -0
- package/dist/entry-better-auth-client.cjs +31 -0
- package/dist/entry-better-auth-client.cjs.map +1 -0
- package/dist/entry-better-auth-client.d.cts +63 -0
- package/dist/entry-better-auth-client.d.mts +63 -0
- package/dist/entry-better-auth-client.mjs +29 -0
- package/dist/entry-better-auth-client.mjs.map +1 -0
- package/dist/entry-better-auth.cjs +289 -0
- package/dist/entry-better-auth.cjs.map +1 -0
- package/dist/entry-better-auth.d.cts +3 -0
- package/dist/entry-better-auth.d.mts +3 -0
- package/dist/entry-better-auth.mjs +284 -0
- package/dist/entry-better-auth.mjs.map +1 -0
- package/dist/entry-bridges-audit.cjs +106 -0
- package/dist/entry-bridges-audit.cjs.map +1 -0
- package/dist/entry-bridges-audit.d.cts +58 -0
- package/dist/entry-bridges-audit.d.mts +58 -0
- package/dist/entry-bridges-audit.mjs +105 -0
- package/dist/entry-bridges-audit.mjs.map +1 -0
- package/dist/entry-bridges-webhooks.cjs +90 -0
- package/dist/entry-bridges-webhooks.cjs.map +1 -0
- package/dist/entry-bridges-webhooks.d.cts +46 -0
- package/dist/entry-bridges-webhooks.d.mts +46 -0
- package/dist/entry-bridges-webhooks.mjs +88 -0
- package/dist/entry-bridges-webhooks.mjs.map +1 -0
- package/dist/entry-bun.cjs +33 -0
- package/dist/entry-bun.cjs.map +1 -0
- package/dist/entry-bun.d.cts +31 -0
- package/dist/entry-bun.d.mts +31 -0
- package/dist/entry-bun.mjs +30 -0
- package/dist/entry-bun.mjs.map +1 -0
- package/dist/entry-drizzle-mysql.cjs +34 -0
- package/dist/entry-drizzle-mysql.cjs.map +1 -0
- package/dist/entry-drizzle-mysql.d.cts +20 -0
- package/dist/entry-drizzle-mysql.d.mts +20 -0
- package/dist/entry-drizzle-mysql.mjs +33 -0
- package/dist/entry-drizzle-mysql.mjs.map +1 -0
- package/dist/entry-drizzle-pg.cjs +44 -0
- package/dist/entry-drizzle-pg.cjs.map +1 -0
- package/dist/entry-drizzle-pg.d.cts +20 -0
- package/dist/entry-drizzle-pg.d.mts +20 -0
- package/dist/entry-drizzle-pg.mjs +43 -0
- package/dist/entry-drizzle-pg.mjs.map +1 -0
- package/dist/entry-drizzle-sqlite.cjs +34 -0
- package/dist/entry-drizzle-sqlite.cjs.map +1 -0
- package/dist/entry-drizzle-sqlite.d.cts +15 -0
- package/dist/entry-drizzle-sqlite.d.mts +15 -0
- package/dist/entry-drizzle-sqlite.mjs +33 -0
- package/dist/entry-drizzle-sqlite.mjs.map +1 -0
- package/dist/entry-elysia.cjs +117 -0
- package/dist/entry-elysia.cjs.map +1 -0
- package/dist/entry-elysia.d.cts +446 -0
- package/dist/entry-elysia.d.mts +446 -0
- package/dist/entry-elysia.mjs +113 -0
- package/dist/entry-elysia.mjs.map +1 -0
- package/dist/entry-elysia2.cjs +117 -0
- package/dist/entry-elysia2.cjs.map +1 -0
- package/dist/entry-elysia2.d.cts +571 -0
- package/dist/entry-elysia2.d.mts +571 -0
- package/dist/entry-elysia2.mjs +113 -0
- package/dist/entry-elysia2.mjs.map +1 -0
- package/dist/entry-express.cjs +101 -0
- package/dist/entry-express.cjs.map +1 -0
- package/dist/entry-express.d.cts +36 -0
- package/dist/entry-express.d.mts +36 -0
- package/dist/entry-express.mjs +97 -0
- package/dist/entry-express.mjs.map +1 -0
- package/dist/entry-hono.cjs +63 -0
- package/dist/entry-hono.cjs.map +1 -0
- package/dist/entry-hono.d.cts +40 -0
- package/dist/entry-hono.d.mts +40 -0
- package/dist/entry-hono.mjs +60 -0
- package/dist/entry-hono.mjs.map +1 -0
- package/dist/entry-hooks-log.cjs +39 -0
- package/dist/entry-hooks-log.cjs.map +1 -0
- package/dist/entry-hooks-log.d.cts +19 -0
- package/dist/entry-hooks-log.d.mts +19 -0
- package/dist/entry-hooks-log.mjs +36 -0
- package/dist/entry-hooks-log.mjs.map +1 -0
- package/dist/entry-next.cjs +67 -0
- package/dist/entry-next.cjs.map +1 -0
- package/dist/entry-next.d.cts +60 -0
- package/dist/entry-next.d.mts +60 -0
- package/dist/entry-next.mjs +57 -0
- package/dist/entry-next.mjs.map +1 -0
- package/dist/entry-react.cjs +401 -0
- package/dist/entry-react.cjs.map +1 -0
- package/dist/entry-react.d.cts +206 -0
- package/dist/entry-react.d.mts +206 -0
- package/dist/entry-react.mjs +388 -0
- package/dist/entry-react.mjs.map +1 -0
- package/dist/entry-storage-cloudflare-kv.cjs +8 -0
- package/dist/entry-storage-cloudflare-kv.d.cts +2 -0
- package/dist/entry-storage-cloudflare-kv.d.mts +2 -0
- package/dist/entry-storage-cloudflare-kv.mjs +2 -0
- package/dist/entry-storage-drizzle.cjs +71 -0
- package/dist/entry-storage-drizzle.cjs.map +1 -0
- package/dist/entry-storage-drizzle.d.cts +27 -0
- package/dist/entry-storage-drizzle.d.mts +27 -0
- package/dist/entry-storage-drizzle.mjs +68 -0
- package/dist/entry-storage-drizzle.mjs.map +1 -0
- package/dist/entry-storage-file.cjs +54 -0
- package/dist/entry-storage-file.cjs.map +1 -0
- package/dist/entry-storage-file.d.cts +24 -0
- package/dist/entry-storage-file.d.mts +24 -0
- package/dist/entry-storage-file.mjs +46 -0
- package/dist/entry-storage-file.mjs.map +1 -0
- package/dist/entry-storage-libsql.cjs +64 -0
- package/dist/entry-storage-libsql.cjs.map +1 -0
- package/dist/entry-storage-libsql.d.cts +28 -0
- package/dist/entry-storage-libsql.d.mts +28 -0
- package/dist/entry-storage-libsql.mjs +61 -0
- package/dist/entry-storage-libsql.mjs.map +1 -0
- package/dist/entry-storage-memory.cjs +3 -0
- package/dist/entry-storage-memory.d.cts +25 -0
- package/dist/entry-storage-memory.d.mts +25 -0
- package/dist/entry-storage-memory.mjs +2 -0
- package/dist/entry-storage-mongodb.cjs +61 -0
- package/dist/entry-storage-mongodb.cjs.map +1 -0
- package/dist/entry-storage-mongodb.d.cts +25 -0
- package/dist/entry-storage-mongodb.d.mts +25 -0
- package/dist/entry-storage-mongodb.mjs +58 -0
- package/dist/entry-storage-mongodb.mjs.map +1 -0
- package/dist/entry-storage-postgres.cjs +62 -0
- package/dist/entry-storage-postgres.cjs.map +1 -0
- package/dist/entry-storage-postgres.d.cts +29 -0
- package/dist/entry-storage-postgres.d.mts +29 -0
- package/dist/entry-storage-postgres.mjs +58 -0
- package/dist/entry-storage-postgres.mjs.map +1 -0
- package/dist/entry-storage-redis.cjs +76 -0
- package/dist/entry-storage-redis.cjs.map +1 -0
- package/dist/entry-storage-redis.d.cts +29 -0
- package/dist/entry-storage-redis.d.mts +29 -0
- package/dist/entry-storage-redis.mjs +72 -0
- package/dist/entry-storage-redis.mjs.map +1 -0
- package/dist/entry-storage-sqlite.cjs +46 -0
- package/dist/entry-storage-sqlite.cjs.map +1 -0
- package/dist/entry-storage-sqlite.d.cts +22 -0
- package/dist/entry-storage-sqlite.d.mts +22 -0
- package/dist/entry-storage-sqlite.mjs +43 -0
- package/dist/entry-storage-sqlite.mjs.map +1 -0
- package/dist/entry-storage-unstorage.cjs +8 -0
- package/dist/entry-storage-unstorage.d.cts +2 -0
- package/dist/entry-storage-unstorage.d.mts +2 -0
- package/dist/entry-storage-unstorage.mjs +2 -0
- package/dist/error-codes-9MRMSSMQ.cjs +49 -0
- package/dist/error-codes-9MRMSSMQ.cjs.map +1 -0
- package/dist/error-codes-CCKuDDu7.mjs +44 -0
- package/dist/error-codes-CCKuDDu7.mjs.map +1 -0
- package/dist/errors-bL3XQLfE.mjs +54 -0
- package/dist/errors-bL3XQLfE.mjs.map +1 -0
- package/dist/errors-vVYic8gz.cjs +89 -0
- package/dist/errors-vVYic8gz.cjs.map +1 -0
- package/dist/events-AE4Ytqmz.d.cts +6 -0
- package/dist/events-AE4Ytqmz.d.mts +6 -0
- package/dist/events-DYZShrYB.mjs +15 -0
- package/dist/events-DYZShrYB.mjs.map +1 -0
- package/dist/events-FJ2K0wly.cjs +26 -0
- package/dist/events-FJ2K0wly.cjs.map +1 -0
- package/dist/guard-BOx5cnMP.mjs +141 -0
- package/dist/guard-BOx5cnMP.mjs.map +1 -0
- package/dist/guard-C3o7V14m.d.cts +62 -0
- package/dist/guard-DCbTnqPz.d.mts +62 -0
- package/dist/guard-nwdOYe90.cjs +188 -0
- package/dist/guard-nwdOYe90.cjs.map +1 -0
- package/dist/index-BVVgDSdq.d.cts +1 -0
- package/dist/index-BVVgDSdq.d.mts +1 -0
- package/dist/index.cjs +240 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +195 -0
- package/dist/index.d.mts +195 -0
- package/dist/index.mjs +60 -0
- package/dist/index.mjs.map +1 -0
- package/dist/kit-C7tepDB2.cjs +76 -0
- package/dist/kit-C7tepDB2.cjs.map +1 -0
- package/dist/kit-DMVBLBI9.d.mts +57 -0
- package/dist/kit-fzXHYb1l.mjs +65 -0
- package/dist/kit-fzXHYb1l.mjs.map +1 -0
- package/dist/kit-qAROuhOI.d.cts +57 -0
- package/dist/memory-BbBbbY8-.mjs +32 -0
- package/dist/memory-BbBbbY8-.mjs.map +1 -0
- package/dist/memory-knijqKw_.cjs +39 -0
- package/dist/memory-knijqKw_.cjs.map +1 -0
- package/dist/permissions-C9PjftY6.mjs +59 -0
- package/dist/permissions-C9PjftY6.mjs.map +1 -0
- package/dist/permissions-Cxri_P_k.cjs +70 -0
- package/dist/permissions-Cxri_P_k.cjs.map +1 -0
- package/dist/policy-BQmlo2lu.mjs +3 -0
- package/dist/policy-CGA6QD0a.d.mts +4 -0
- package/dist/policy-DFq_SYR3.d.cts +4 -0
- package/dist/react-embed.js +18710 -0
- package/dist/react.css +1 -0
- package/dist/relationships-BLYEJ9tc.cjs +907 -0
- package/dist/relationships-BLYEJ9tc.cjs.map +1 -0
- package/dist/relationships-BfLonEMI.mjs +756 -0
- package/dist/relationships-BfLonEMI.mjs.map +1 -0
- package/dist/roles-DBk4i8b5.mjs +69 -0
- package/dist/roles-DBk4i8b5.mjs.map +1 -0
- package/dist/roles-a8B5uZ35.cjs +104 -0
- package/dist/roles-a8B5uZ35.cjs.map +1 -0
- package/dist/rolldown-runtime-D7D4PA-g.mjs +13 -0
- package/dist/routes-DtRb3EO0.mjs +1001 -0
- package/dist/routes-DtRb3EO0.mjs.map +1 -0
- package/dist/routes-irNuuZqo.cjs +1049 -0
- package/dist/routes-irNuuZqo.cjs.map +1 -0
- package/dist/server-C2F4H6bR.d.cts +886 -0
- package/dist/server-CJ7AIJpO.d.mts +886 -0
- package/dist/table-DA98mVVT.d.cts +2 -0
- package/dist/table-DA98mVVT.d.mts +2 -0
- package/dist/testing.cjs +59 -0
- package/dist/testing.cjs.map +1 -0
- package/dist/testing.d.cts +37 -0
- package/dist/testing.d.mts +37 -0
- package/dist/testing.mjs +57 -0
- package/dist/testing.mjs.map +1 -0
- package/dist/types-elysia2/adapters/elysia-shared.d.ts +71 -0
- package/dist/types-elysia2/adapters/elysia2.d.ts +432 -0
- package/dist/types-elysia2/adapters/guard.d.ts +140 -0
- package/dist/types-elysia2/auth/contract.d.ts +9 -0
- package/dist/types-elysia2/auth-provider/contract.d.ts +57 -0
- package/dist/types-elysia2/auth-provider/kit.d.ts +57 -0
- package/dist/types-elysia2/authorization/contract.d.ts +53 -0
- package/dist/types-elysia2/authorization/permissions.d.ts +54 -0
- package/dist/types-elysia2/catalog.d.ts +197 -0
- package/dist/types-elysia2/engine.d.ts +192 -0
- package/dist/types-elysia2/entry-elysia2.d.ts +9 -0
- package/dist/types-elysia2/errors.d.ts +47 -0
- package/dist/types-elysia2/hooks/contract.d.ts +100 -0
- package/dist/types-elysia2/keys.d.ts +78 -0
- package/dist/types-elysia2/policy.d.ts +13 -0
- package/dist/types-elysia2/relationships.d.ts +162 -0
- package/dist/types-elysia2/schema.d.ts +299 -0
- package/dist/types-elysia2/server/cors.d.ts +8 -0
- package/dist/types-elysia2/server/create-fetch-handler.d.ts +87 -0
- package/dist/types-elysia2/server/openapi.d.ts +29 -0
- package/dist/types-elysia2/server/routes.d.ts +132 -0
- package/dist/types-elysia2/server/validation.d.ts +205 -0
- package/dist/types-elysia2/storage/contract.d.ts +18 -0
- package/dist/types-react/catalog.d.ts +197 -0
- package/dist/types-react/engine.d.ts +192 -0
- package/dist/types-react/entry-react-embed.d.ts +20 -0
- package/dist/types-react/errors.d.ts +47 -0
- package/dist/types-react/hooks/contract.d.ts +100 -0
- package/dist/types-react/keys.d.ts +78 -0
- package/dist/types-react/policy.d.ts +13 -0
- package/dist/types-react/react-embed.d.ts +37 -0
- package/dist/types-react/relationships.d.ts +162 -0
- package/dist/types-react/schema.d.ts +299 -0
- package/dist/types-react/storage/contract.d.ts +18 -0
- package/dist/types-react/ui/App.d.ts +23 -0
- package/dist/types-react/ui/api.d.ts +73 -0
- package/dist/types-react/ui/components/perm-bits.d.ts +64 -0
- package/dist/types-react/ui/lib/queries.d.ts +46 -0
- package/dist/types-react/ui/theme.d.ts +10 -0
- package/dist/types-react/ui/types.d.ts +109 -0
- package/dist/types-react/ui/views/CatalogView.d.ts +3 -0
- package/dist/types-react/ui/views/HistoryView.d.ts +6 -0
- package/dist/types-react/ui/views/PlaygroundView.d.ts +14 -0
- package/dist/types-react/ui/views/RelationshipsView.d.ts +6 -0
- package/dist/types-react/ui/views/SubjectDetailView.d.ts +7 -0
- package/dist/types-react/ui/views/SubjectsView.d.ts +6 -0
- package/dist/ui/apple-touch-icon.png +0 -0
- package/dist/ui/assets/index-DCPZRo-Z.css +1 -0
- package/dist/ui/assets/index-uXGOtyuj.js +104 -0
- package/dist/ui/favicon.ico +0 -0
- package/dist/ui/favicon.svg +15 -0
- package/dist/ui/index.html +20 -0
- package/package.json +587 -4
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Adapt a {@link PermissionsAuthProvider} to the kit's `AuthProvider` + `AuthorizationProvider`, so
|
|
3
|
+
* the panel, the router and the framework adapters use it unchanged (ADR 0006).
|
|
4
|
+
*
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
import type { AuthProvider, Principal } from "@xtandard/kit/auth";
|
|
8
|
+
import type { AuthorizationProvider } from "@xtandard/kit/authorization";
|
|
9
|
+
import type { PermissionsAuthProvider, PermissionsSubject, SubjectIdentity } from "./contract.ts";
|
|
10
|
+
/** What {@link toKitAuth} needs from the engine (structural, so a test double works too). */
|
|
11
|
+
export interface KitAuthEngine {
|
|
12
|
+
readonly catalog: {
|
|
13
|
+
isManagerRole(role: unknown): boolean;
|
|
14
|
+
};
|
|
15
|
+
/** Resolves the effective role: the stored role if any, else the provider's, parsed. */
|
|
16
|
+
snapshot(subject: SubjectIdentity): Promise<{
|
|
17
|
+
subject: {
|
|
18
|
+
id: string | null;
|
|
19
|
+
role: string;
|
|
20
|
+
};
|
|
21
|
+
}>;
|
|
22
|
+
}
|
|
23
|
+
/** Options for {@link toKitAuth}. */
|
|
24
|
+
export interface ToKitAuthOptions {
|
|
25
|
+
/**
|
|
26
|
+
* May this subject use the panel's read actions (catalog, subjects, grants, history, the check
|
|
27
|
+
* playground)? Default: the same answer as `canManage` — the panel shows every subject's grants.
|
|
28
|
+
*/
|
|
29
|
+
canRead?(subject: PermissionsSubject, request: Request): boolean | Promise<boolean>;
|
|
30
|
+
/** Which actions write state. Default: `grants:write` and `roles:write` (ADR 0008). */
|
|
31
|
+
isMutating?(action: string): boolean;
|
|
32
|
+
}
|
|
33
|
+
/** The kit providers built by {@link toKitAuth}. */
|
|
34
|
+
export interface KitAuth {
|
|
35
|
+
auth: AuthProvider;
|
|
36
|
+
authorization: AuthorizationProvider;
|
|
37
|
+
/** Resolve the caller as a {@link PermissionsSubject} (effective role), or `null`. */
|
|
38
|
+
subject(request: Request): Promise<PermissionsSubject | null>;
|
|
39
|
+
/** The resolved manage decision for a subject (provider `canManage`, else `managerRoles`). */
|
|
40
|
+
canManage(subject: PermissionsSubject, request: Request): Promise<boolean>;
|
|
41
|
+
}
|
|
42
|
+
/** The `PermissionsSubject` a kit principal built by {@link toKitAuth} carries, if any. */
|
|
43
|
+
export declare function subjectOf(principal: Principal | null | undefined): PermissionsSubject | null;
|
|
44
|
+
/**
|
|
45
|
+
* Build the kit `auth` + `authorization` pair from a provider and the engine.
|
|
46
|
+
*
|
|
47
|
+
* - `auth.authenticate` → principal `{ id, name, email, roles: [effectiveRole], metadata: { subject } }`.
|
|
48
|
+
* - `authorization.authorize` → no principal: deny; mutating actions: `canManage`; read actions:
|
|
49
|
+
* `canRead` (default `canManage`).
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* ```ts
|
|
53
|
+
* const { auth, authorization } = toKitAuth(betterAuthProvider(auth), perms);
|
|
54
|
+
* createFetchHandler({ permissions: perms, auth, authorization });
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
export declare function toKitAuth(provider: PermissionsAuthProvider, perms: KitAuthEngine, options?: ToKitAuthOptions): KitAuth;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Authorization contract for the panel's HTTP API (ADR 0008). Answers: "Can this principal perform
|
|
3
|
+
* this action on this resource?"
|
|
4
|
+
*
|
|
5
|
+
* Every admin route consults the {@link AuthorizationProvider} with one {@link PermissionsAction}
|
|
6
|
+
* and the {@link PermissionsResource} it targets. The runtime routes (`GET /api/me`,
|
|
7
|
+
* `POST /api/me/check`) need only an authenticated principal and never reach the provider.
|
|
8
|
+
* Ships `none` / `roles` / `delegated` implementations (the kit's, with this product's actions).
|
|
9
|
+
*
|
|
10
|
+
* @module
|
|
11
|
+
*/
|
|
12
|
+
import { type AuthorizationProvider as KitAuthorizationProvider, type AuthorizeInput as KitAuthorizeInput } from "@xtandard/kit/authorization";
|
|
13
|
+
declare const ACTIONS: import("@xtandard/kit/authorization").ActionsDefinitionResult<"grants:write" | "roles:write" | "catalog:read" | "subjects:read" | "grants:read" | "history:read" | "check:run" | "relationships:read" | "relationships:write">;
|
|
14
|
+
/** The full set of authorizable actions. */
|
|
15
|
+
export type PermissionsAction = (typeof ACTIONS.ALL_ACTIONS)[number];
|
|
16
|
+
/**
|
|
17
|
+
* The resource an action targets. `subject` is absent on collection routes (list subjects, every
|
|
18
|
+
* grant, global history).
|
|
19
|
+
*/
|
|
20
|
+
export type PermissionsResource = {
|
|
21
|
+
type: "catalog";
|
|
22
|
+
} | {
|
|
23
|
+
type: "subject";
|
|
24
|
+
subject?: string;
|
|
25
|
+
} | {
|
|
26
|
+
type: "grant";
|
|
27
|
+
subject?: string;
|
|
28
|
+
permission?: string;
|
|
29
|
+
} | {
|
|
30
|
+
type: "history";
|
|
31
|
+
subject?: string;
|
|
32
|
+
} | {
|
|
33
|
+
type: "check";
|
|
34
|
+
subject?: string | null;
|
|
35
|
+
role?: string;
|
|
36
|
+
path?: string;
|
|
37
|
+
} | {
|
|
38
|
+
type: "relationship";
|
|
39
|
+
object?: string;
|
|
40
|
+
relation?: string;
|
|
41
|
+
subject?: string;
|
|
42
|
+
};
|
|
43
|
+
/** Input passed to {@link AuthorizationProvider.authorize}. */
|
|
44
|
+
export type AuthorizeInput = KitAuthorizeInput<PermissionsAction, PermissionsResource>;
|
|
45
|
+
/** Decides whether an action is permitted. */
|
|
46
|
+
export type AuthorizationProvider = KitAuthorizationProvider<PermissionsAction, PermissionsResource>;
|
|
47
|
+
/** Every action, in a stable order (role presets, docs, the UI). */
|
|
48
|
+
export declare const ALL_ACTIONS: readonly PermissionsAction[];
|
|
49
|
+
/** Actions that write state: blocked (423) when the panel is readonly. */
|
|
50
|
+
export declare const MUTATING_ACTIONS: ReadonlySet<PermissionsAction>;
|
|
51
|
+
/** True if the action writes state. */
|
|
52
|
+
export declare const isMutatingAction: (action: PermissionsAction) => boolean;
|
|
53
|
+
export {};
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Authorize **another** xtandard panel (webhooks, flags, audit, entitlements…) with this catalog
|
|
3
|
+
* (ADR 0011): {@link permissionsAuthorization} maps each of that panel's actions to a permission
|
|
4
|
+
* path of your catalog and asks the engine, through the kit's `delegatedAuthorization`.
|
|
5
|
+
*
|
|
6
|
+
* The principal becomes the subject: the identity `toKitAuth` attached (`metadata.subject`) when
|
|
7
|
+
* present, else `{ id, role: principal.roles, name, email }`. An unmapped action is denied.
|
|
8
|
+
*
|
|
9
|
+
* @module
|
|
10
|
+
*/
|
|
11
|
+
import type { Principal } from "@xtandard/kit/auth";
|
|
12
|
+
import type { AuthorizationProvider, AuthorizeInput } from "@xtandard/kit/authorization";
|
|
13
|
+
import type { SubjectIdentity } from "../catalog.ts";
|
|
14
|
+
import type { AnyPermissions } from "../engine.ts";
|
|
15
|
+
/** What one panel action maps to: a path, a path plus the data its rule reads, or deny. */
|
|
16
|
+
export type PermissionTarget = string | {
|
|
17
|
+
path: string;
|
|
18
|
+
data?: unknown;
|
|
19
|
+
} | null | undefined | false;
|
|
20
|
+
/**
|
|
21
|
+
* Panel action → permission path. A record (`{ "endpoint:write": "webhooks.manage" }`) or a
|
|
22
|
+
* function of the full input (to pass the resource as the rule's data).
|
|
23
|
+
*/
|
|
24
|
+
export type PermissionsAuthorizationMap<Action extends string, Resource> = Partial<Record<Action, PermissionTarget>> | ((input: AuthorizeInput<Action, Resource>) => PermissionTarget | Promise<PermissionTarget>);
|
|
25
|
+
/** Options for {@link permissionsAuthorization}. */
|
|
26
|
+
export interface PermissionsAuthorizationOptions<Action extends string, Resource> {
|
|
27
|
+
/** Override how a principal becomes a subject (default: see the module doc). */
|
|
28
|
+
subject?(principal: Principal, input: AuthorizeInput<Action, Resource>): SubjectIdentity | null | Promise<SubjectIdentity | null>;
|
|
29
|
+
/** The rules' `ctx`, for catalogs that need app facts. */
|
|
30
|
+
ctx?(input: AuthorizeInput<Action, Resource>): unknown;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* The subject identity of a kit principal: the one `toKitAuth` attached, else the principal's own
|
|
34
|
+
* id, roles, name and email.
|
|
35
|
+
*/
|
|
36
|
+
export declare function principalIdentity(principal: Principal): SubjectIdentity;
|
|
37
|
+
/**
|
|
38
|
+
* Build a kit `AuthorizationProvider` that decides another panel's actions with this catalog.
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* ```ts
|
|
42
|
+
* import { permissionsAuthorization } from "@xtandard/permissions/authorization/permissions";
|
|
43
|
+
* import { createFetchHandler as webhooksPanel } from "@xtandard/webhooks";
|
|
44
|
+
*
|
|
45
|
+
* webhooksPanel({
|
|
46
|
+
* auth, // the same kit auth, e.g. toKitAuth(betterAuthProvider(auth), perms).auth
|
|
47
|
+
* authorization: permissionsAuthorization(perms, {
|
|
48
|
+
* "endpoint:read": "webhooks.read",
|
|
49
|
+
* "endpoint:write": "webhooks.manage",
|
|
50
|
+
* }),
|
|
51
|
+
* });
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
export declare function permissionsAuthorization<Action extends string = string, Resource = unknown>(perms: AnyPermissions, map: PermissionsAuthorizationMap<Action, Resource>, options?: PermissionsAuthorizationOptions<Action, Resource>): AuthorizationProvider<Action, Resource>;
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The catalog: roles, assignable permissions and per-role rules, declared once
|
|
3
|
+
* in code and shared by the server, the browser and the admin panel (ADR 0002).
|
|
4
|
+
*
|
|
5
|
+
* `definePermissions()` wraps `defineAuthorization` from
|
|
6
|
+
* `@xtandard/lib/policy/permix` (which owns role parsing, the assignable list,
|
|
7
|
+
* the sync-only rule helpers, `createAbility` and `explain`) and adds what the
|
|
8
|
+
* product needs on top: `managerRoles`, labels for the panel, the
|
|
9
|
+
* `directGrants` switch and `effectiveRole()`. The rule user is fixed to
|
|
10
|
+
* {@link PermissionsSubject}, whose `role` the engine sets to the subject's
|
|
11
|
+
* effective role.
|
|
12
|
+
*
|
|
13
|
+
* Depends only on `@xtandard/lib/policy/permix` (and its `permix` peer), so
|
|
14
|
+
* browsers and edge runtimes can import it.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* import { definePermissions } from "@xtandard/permissions/catalog";
|
|
19
|
+
*
|
|
20
|
+
* type Definition = { post: ["read", { name: "publish"; type: Post }, "delete"] };
|
|
21
|
+
*
|
|
22
|
+
* export const permissions = definePermissions<Definition>()({
|
|
23
|
+
* roles: ["guest", "member", "editor", "admin"], // least → most privileged
|
|
24
|
+
* guestRole: "guest",
|
|
25
|
+
* defaultRole: "member",
|
|
26
|
+
* managerRoles: ["admin"],
|
|
27
|
+
* assignable: ["post:moderate", "billing:refund"],
|
|
28
|
+
* labels: { roles: { admin: "Administrator" }, permissions: { "post:moderate": "Moderate posts" } },
|
|
29
|
+
* rules: {
|
|
30
|
+
* guest: () => ({ post: { read: true, publish: false, delete: false } }),
|
|
31
|
+
* member: ({ user, permissions }, { or, fieldEquals, hasPermission }) => ({
|
|
32
|
+
* post: {
|
|
33
|
+
* read: true,
|
|
34
|
+
* publish: or<Post>(fieldEquals(user!.id, { field: "authorId" }), () =>
|
|
35
|
+
* hasPermission(permissions, "post:moderate"),
|
|
36
|
+
* ),
|
|
37
|
+
* delete: false,
|
|
38
|
+
* },
|
|
39
|
+
* }),
|
|
40
|
+
* editor: () => ({ post: { read: true, publish: true, delete: false } }),
|
|
41
|
+
* admin: () => ({ post: { read: true, publish: true, delete: true } }),
|
|
42
|
+
* },
|
|
43
|
+
* });
|
|
44
|
+
* ```
|
|
45
|
+
*
|
|
46
|
+
* @module catalog
|
|
47
|
+
*/
|
|
48
|
+
import { type Authorization, type AuthorizationConfig, type Definition, type RelationSchema } from "./policy.ts";
|
|
49
|
+
/** A catalog without relations (ReBAC off): no object type, so no relation query type-checks. */
|
|
50
|
+
export type NoRelations = Record<never, never>;
|
|
51
|
+
export { computed, defineRelations, direct, exclusion, from, intersection, RelationDepthError, RelationSchemaError, union, } from "./policy.ts";
|
|
52
|
+
export type { ObjectRef, RelationExpr, RelationSchema, RelationTuple, SubjectRef, } from "./policy.ts";
|
|
53
|
+
/** The user the rules see: the engine passes the subject with its effective `role`. */
|
|
54
|
+
export interface PermissionsSubject {
|
|
55
|
+
/** The subject id (a user id in 0.1). */
|
|
56
|
+
id: string;
|
|
57
|
+
/** The effective role (stored assignment, else the auth provider's, else `defaultRole`). */
|
|
58
|
+
role: string;
|
|
59
|
+
name?: string;
|
|
60
|
+
email?: string;
|
|
61
|
+
/** Extra facts from the auth provider (claims, profile fields). */
|
|
62
|
+
attributes?: Record<string, unknown>;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Who a check is for, as an auth provider reports it. `role` may be one role, several (the most
|
|
66
|
+
* privileged known one wins, see {@link PermissionsCatalog.effectiveRole}) or missing
|
|
67
|
+
* (`defaultRole`); a stored role assignment overrides it.
|
|
68
|
+
*/
|
|
69
|
+
export interface SubjectIdentity {
|
|
70
|
+
id: string;
|
|
71
|
+
role?: string | readonly string[] | null;
|
|
72
|
+
name?: string;
|
|
73
|
+
email?: string;
|
|
74
|
+
attributes?: Record<string, unknown>;
|
|
75
|
+
}
|
|
76
|
+
/** A human label: a string, or a label with a description. */
|
|
77
|
+
export type LabelInput = string | {
|
|
78
|
+
label: string;
|
|
79
|
+
description?: string;
|
|
80
|
+
};
|
|
81
|
+
/** A normalized label. */
|
|
82
|
+
export interface Label {
|
|
83
|
+
label: string;
|
|
84
|
+
description?: string;
|
|
85
|
+
}
|
|
86
|
+
/** What `definePermissions` adds on top of the lib's `AuthorizationConfig`. */
|
|
87
|
+
export interface CatalogMeta<R extends string, P extends string, S extends RelationSchema = NoRelations> {
|
|
88
|
+
/**
|
|
89
|
+
* The relation schema (ReBAC, ADR 0013), built with `defineRelations` and the `direct` /
|
|
90
|
+
* `computed` / `from` / `union` / `intersection` / `exclusion` builders. Validated again here.
|
|
91
|
+
* Without it, relationship writes and checks throw `RelationSchemaError`.
|
|
92
|
+
*/
|
|
93
|
+
relations?: S;
|
|
94
|
+
/**
|
|
95
|
+
* Roles that may grant / revoke / assign roles by default (an auth provider can override it).
|
|
96
|
+
* Default: the most privileged role (the last of `roles`).
|
|
97
|
+
*/
|
|
98
|
+
managerRoles?: readonly R[];
|
|
99
|
+
/** Labels and descriptions for the admin panel. Missing entries default to the key. */
|
|
100
|
+
labels?: {
|
|
101
|
+
roles?: Partial<Record<R, LabelInput>>;
|
|
102
|
+
permissions?: Partial<Record<P, LabelInput>>;
|
|
103
|
+
};
|
|
104
|
+
/**
|
|
105
|
+
* When `true`, a non-expired grant whose name equals the checked path allows it outright
|
|
106
|
+
* (`reason: "grant"`), for catalogs whose assignable names are paths. Default `false`: grants
|
|
107
|
+
* only matter through the rules (ADR 0005).
|
|
108
|
+
*/
|
|
109
|
+
directGrants?: boolean;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* The configuration of {@link definePermissions}: the lib's `AuthorizationConfig` (roles ordered
|
|
113
|
+
* **least → most privileged**, `guestRole`, `defaultRole`, `assignable`, `rules`) without
|
|
114
|
+
* `getRole` (the engine sets the role), plus {@link CatalogMeta}.
|
|
115
|
+
*/
|
|
116
|
+
export type PermissionsConfig<D extends Definition, R extends string, P extends string, X, S extends RelationSchema = NoRelations> = Omit<AuthorizationConfig<D, R, P, PermissionsSubject, X>, "getRole"> & CatalogMeta<NoInfer<R>, NoInfer<P>, S>;
|
|
117
|
+
/** A catalog: the wrapped lib `Authorization` plus the product metadata. */
|
|
118
|
+
export interface PermissionsCatalog<D extends Definition = Definition, R extends string = string, P extends string = string, X = undefined, S extends RelationSchema = RelationSchema> {
|
|
119
|
+
/** The lib authorization (`createAbility`, `createRules`, `parseRole`, …). */
|
|
120
|
+
readonly authorization: Authorization<D, R, P, PermissionsSubject, X>;
|
|
121
|
+
/** Every role, least → most privileged. */
|
|
122
|
+
readonly roles: readonly R[];
|
|
123
|
+
/** The role of a request without a subject. */
|
|
124
|
+
readonly guestRole: R;
|
|
125
|
+
/** The role of a signed-in subject whose role is missing or unknown. */
|
|
126
|
+
readonly defaultRole: R;
|
|
127
|
+
/** The assignable permissions. */
|
|
128
|
+
readonly assignable: readonly P[];
|
|
129
|
+
/** Roles that may manage permissions by default. */
|
|
130
|
+
readonly managerRoles: readonly R[];
|
|
131
|
+
/** Labels for every role and permission (defaulting to the key). */
|
|
132
|
+
readonly labels: {
|
|
133
|
+
readonly roles: Readonly<Record<R, Label>>;
|
|
134
|
+
readonly permissions: Readonly<Record<P, Label>>;
|
|
135
|
+
};
|
|
136
|
+
/** See {@link CatalogMeta.directGrants}. */
|
|
137
|
+
readonly directGrants: boolean;
|
|
138
|
+
/** The relation schema (ReBAC), `null` when the catalog declares none. */
|
|
139
|
+
readonly relations: S | null;
|
|
140
|
+
/** True when `value` is one of `roles`. */
|
|
141
|
+
isRole(value: unknown): value is R;
|
|
142
|
+
/** True when `value` is one of `assignable`. */
|
|
143
|
+
isAssignable(value: unknown): value is P;
|
|
144
|
+
/** True when `role` is one of `managerRoles`. */
|
|
145
|
+
isManagerRole(role: unknown): boolean;
|
|
146
|
+
/** A signed-in subject's role: `value` when it is a known non-guest role, else `defaultRole`. */
|
|
147
|
+
parseRole(value: unknown): R;
|
|
148
|
+
/**
|
|
149
|
+
* The effective role of a signed-in subject from what an auth provider reports: one role, several
|
|
150
|
+
* (an array, or a comma-separated string as better-auth's admin plugin stores them) → the most
|
|
151
|
+
* privileged known one; none known (or missing) → `defaultRole`. Never the guest role.
|
|
152
|
+
*/
|
|
153
|
+
effectiveRole(raw: string | readonly string[] | null | undefined): R;
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* The permission type of a catalog: the `assignable` union, or `string` for a roles-only catalog
|
|
157
|
+
* (`assignable: []` infers `never`, which no generic `AnyPermissionsCatalog` slot accepts because
|
|
158
|
+
* rules are contravariant in it). Every permission is still rejected at runtime there.
|
|
159
|
+
*/
|
|
160
|
+
export type PermissionUnion<P extends string> = [P] extends [never] ? string : P;
|
|
161
|
+
/** Any catalog (for generic constraints). */
|
|
162
|
+
export type AnyPermissionsCatalog = PermissionsCatalog<any, any, any, any, any>;
|
|
163
|
+
/** The permix definition of a catalog. */
|
|
164
|
+
export type DefinitionOf<C> = C extends PermissionsCatalog<infer D, any, any, any, any> ? D : never;
|
|
165
|
+
/** The role union of a catalog. */
|
|
166
|
+
export type RoleOf<C> = C extends {
|
|
167
|
+
readonly roles: readonly (infer R)[];
|
|
168
|
+
} ? R : never;
|
|
169
|
+
/** The assignable-permission union of a catalog. */
|
|
170
|
+
export type PermissionOf<C> = C extends {
|
|
171
|
+
readonly assignable: readonly (infer P)[];
|
|
172
|
+
} ? P : never;
|
|
173
|
+
/** The rule context (`ctx`) type of a catalog. */
|
|
174
|
+
export type CtxOf<C> = C extends PermissionsCatalog<any, any, any, infer X, any> ? X : never;
|
|
175
|
+
/** The relation schema of a catalog. */
|
|
176
|
+
export type RelationsOf<C> = C extends PermissionsCatalog<any, any, any, any, infer S> ? S : never;
|
|
177
|
+
/**
|
|
178
|
+
* Define the catalog. Curried like the lib's `defineAuthorization`: the permix definition and the
|
|
179
|
+
* rule context type go in the first call, the roles and permissions are inferred by the second:
|
|
180
|
+
* `definePermissions<Definition, Ctx>()({ ... })`.
|
|
181
|
+
*
|
|
182
|
+
* Rules are synchronous (a rule that returns a Promise denies and throws `AsyncRuleError`); load
|
|
183
|
+
* what they need first and pass it as `ctx` or as the checked data. A rule wrapped in the lib's
|
|
184
|
+
* `asyncRule()` is evaluated only by the engine's async `check()` / `can()`, never by the
|
|
185
|
+
* synchronous `ability()` (ADR 0005).
|
|
186
|
+
*
|
|
187
|
+
* @throws {Error} When `guestRole`, `defaultRole` or a `managerRoles` entry is not in `roles`, a
|
|
188
|
+
* role has no rules, or the guest role is a manager role.
|
|
189
|
+
*/
|
|
190
|
+
export declare function definePermissions<D extends Definition, X = undefined>(): <const R extends string, const P extends string, const S extends RelationSchema = NoRelations>(config: PermissionsConfig<D, R, P, X, S>) => PermissionsCatalog<D, R, PermissionUnion<P>, X, S>;
|
|
191
|
+
/**
|
|
192
|
+
* Wrap an existing lib `Authorization` (from `defineAuthorization`) as a catalog. Its config is
|
|
193
|
+
* re-defined with `getRole: (subject) => subject.role`, so its rules receive a
|
|
194
|
+
* {@link PermissionsSubject} (`{ id, role, name?, email?, attributes? }`) as `user`; rules that read
|
|
195
|
+
* other user fields must move them into `attributes` or `ctx`.
|
|
196
|
+
*/
|
|
197
|
+
export declare function fromAuthorization<D extends Definition, R extends string, P extends string, X, const S extends RelationSchema = NoRelations>(authorization: Authorization<D, R, P, any, X>, meta?: CatalogMeta<NoInfer<R>, NoInfer<P>, S>): PermissionsCatalog<D, R, P, X, S>;
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The engine: `createPermissions({ catalog, storage, hooks, cache, now, readonly })`.
|
|
3
|
+
*
|
|
4
|
+
* Writes (grant, revoke, role assignment, subject records) go to the storage
|
|
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), then evaluate
|
|
7
|
+
* the catalog's rules **synchronously** with `@xtandard/lib/policy/permix`
|
|
8
|
+
* (ADR 0005).
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* import { createPermissions } from "@xtandard/permissions";
|
|
13
|
+
* import { createMemoryStorage } from "@xtandard/permissions/storage/memory";
|
|
14
|
+
* import { permissions } from "./permissions.ts"; // definePermissions(...)
|
|
15
|
+
*
|
|
16
|
+
* const perms = createPermissions({ catalog: permissions, storage: createMemoryStorage() });
|
|
17
|
+
* await perms.grant({ subject: "user_1", permission: "post:moderate", actor: "admin_1" });
|
|
18
|
+
* await perms.can({ id: "user_1", role: "member" }, "post.publish", post); // true
|
|
19
|
+
* ```
|
|
20
|
+
*
|
|
21
|
+
* @module
|
|
22
|
+
*/
|
|
23
|
+
import type { PermissionsCatalog, PermissionsSubject, SubjectIdentity } from "./catalog.ts";
|
|
24
|
+
export type { SubjectIdentity } from "./catalog.ts";
|
|
25
|
+
import { type HookErrorReporter, type PermissionsHooksInput } from "./hooks/contract.ts";
|
|
26
|
+
import type { Ability, DataAtPath, Definition, RelationSchema, RulesPaths } from "./policy.ts";
|
|
27
|
+
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 PermissionsStorage } from "./storage/contract.ts";
|
|
30
|
+
/** Options for {@link createPermissions}. */
|
|
31
|
+
export interface PermissionsOptions<D extends Definition = Definition, R extends string = string, P extends string = string, X = undefined, S extends RelationSchema = RelationSchema> {
|
|
32
|
+
/** The catalog from `definePermissions()`. */
|
|
33
|
+
catalog: PermissionsCatalog<D, R, P, X, S>;
|
|
34
|
+
/** Where grants, role assignments and history live (any `@xtandard/permissions/storage/*`). */
|
|
35
|
+
storage: PermissionsStorage;
|
|
36
|
+
/** `before` (veto) / `after` (side effects) hooks around grant, revoke and role writes. */
|
|
37
|
+
hooks?: PermissionsHooksInput;
|
|
38
|
+
/** Receives errors thrown by `after` hooks. Default: a prefixed `console.warn`. */
|
|
39
|
+
onHookError?: HookErrorReporter;
|
|
40
|
+
/**
|
|
41
|
+
* Per-subject cache of the stored role and grants used by checks. Entries expire after `ttlMs`
|
|
42
|
+
* (default 5 000), are dropped on this engine's writes and, when the storage is watchable, on
|
|
43
|
+
* every change it reports. `false` disables it (every check reads the storage).
|
|
44
|
+
*/
|
|
45
|
+
cache?: false | {
|
|
46
|
+
ttlMs?: number;
|
|
47
|
+
max?: number;
|
|
48
|
+
};
|
|
49
|
+
/** Clock (unix ms). Default `Date.now`. Decides grant expiry and every stored timestamp. */
|
|
50
|
+
now?: () => number;
|
|
51
|
+
/** When `true`, every write throws {@link ReadonlyError}. */
|
|
52
|
+
readonly?: boolean;
|
|
53
|
+
/** Where operational warnings go. Default: a prefixed `console.warn`. */
|
|
54
|
+
logger?: {
|
|
55
|
+
warn(message: string, error?: unknown): void;
|
|
56
|
+
};
|
|
57
|
+
/** ReBAC tuning (the catalog's `relations`): `maxDepth` of checks and listing walks. */
|
|
58
|
+
relations?: RelationsOptions;
|
|
59
|
+
}
|
|
60
|
+
/** A subject id, an identity, or `null` / `undefined` for a guest (no session). */
|
|
61
|
+
export type SubjectInput = string | SubjectIdentity | null | undefined;
|
|
62
|
+
/** The app context argument of checks: required when the catalog's context type is. */
|
|
63
|
+
export type FactsArgs<X> = undefined extends X ? [facts?: {
|
|
64
|
+
ctx?: X;
|
|
65
|
+
}] : [facts: {
|
|
66
|
+
ctx: X;
|
|
67
|
+
}];
|
|
68
|
+
/** The data a typed check at `Path` takes (`undefined` when the action takes none). */
|
|
69
|
+
export type PathData<D extends Definition, Path extends string> = DataAtPath<D, Path> extends readonly [] ? undefined : DataAtPath<D, Path>[0];
|
|
70
|
+
/** The lib `Ability` of one subject, plus the engine's decision with reasons. */
|
|
71
|
+
export interface PermissionsAbility<D extends Definition = Definition, R extends string = string, P extends string = string> extends Ability<D, R, P, PermissionsSubject> {
|
|
72
|
+
/** The subject id, `null` for a guest. */
|
|
73
|
+
subject: string | null;
|
|
74
|
+
/** The non-expired grants the ability was built from (assignable or not). */
|
|
75
|
+
grants: readonly Grant<P>[];
|
|
76
|
+
/**
|
|
77
|
+
* Decide `path` with reasons (see {@link CheckResult}). Takes an untyped path (e.g. from an HTTP
|
|
78
|
+
* request); an unknown path is denied with `reason: "unknown-path"`. Throws `AsyncRuleError` when
|
|
79
|
+
* the rule returns a Promise (the check denies).
|
|
80
|
+
*/
|
|
81
|
+
check(path: string, data?: unknown): CheckResult<R, P>;
|
|
82
|
+
/**
|
|
83
|
+
* Like {@link check}, and also evaluates rules wrapped in the lib's `asyncRule()` (which the
|
|
84
|
+
* synchronous `check` refuses with `AsyncRuleError`). The engine's `check()` / `can()` use it.
|
|
85
|
+
*/
|
|
86
|
+
checkAsync(path: string, data?: unknown): Promise<CheckResult<R, P>>;
|
|
87
|
+
}
|
|
88
|
+
/** What `revoke()` returns. */
|
|
89
|
+
export interface RevokeResult<P extends string = string> {
|
|
90
|
+
/** `false` when there was nothing to revoke. */
|
|
91
|
+
removed: boolean;
|
|
92
|
+
/** The revoked grant, when there was one. */
|
|
93
|
+
grant: Grant<P> | null;
|
|
94
|
+
}
|
|
95
|
+
/** What `setRole()` returns. */
|
|
96
|
+
export interface SetRoleResult {
|
|
97
|
+
/** The subject record after the write (`null` when none exists and the role was cleared). */
|
|
98
|
+
record: SubjectRecord | null;
|
|
99
|
+
/** `false` when the stored role already was the requested one. */
|
|
100
|
+
changed: boolean;
|
|
101
|
+
}
|
|
102
|
+
/** A subject as stored: its record and grants. */
|
|
103
|
+
export interface SubjectView<R extends string = string, P extends string = string> {
|
|
104
|
+
id: string;
|
|
105
|
+
/** The stored record, `null` when the subject has none. */
|
|
106
|
+
record: SubjectRecord | null;
|
|
107
|
+
/** The stored role assignment when it is a known, assignable role; else `null`. */
|
|
108
|
+
role: R | null;
|
|
109
|
+
/** The subject's grants (non-expired unless `includeExpired`). */
|
|
110
|
+
grants: Grant<P>[];
|
|
111
|
+
}
|
|
112
|
+
/** One row of `listSubjects()`. */
|
|
113
|
+
export interface SubjectSummary {
|
|
114
|
+
id: string;
|
|
115
|
+
/** The stored role assignment, if any. */
|
|
116
|
+
role: string | null;
|
|
117
|
+
name?: string;
|
|
118
|
+
email?: string;
|
|
119
|
+
/** Stored grants (expired ones included). */
|
|
120
|
+
grantCount: number;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* The engine. The relationship methods (`writeRelationship`, `checkRelation`, `listObjects`, …;
|
|
124
|
+
* see {@link RelationshipsApi}) need the catalog's `relations`.
|
|
125
|
+
*/
|
|
126
|
+
export interface Permissions<D extends Definition = Definition, R extends string = string, P extends string = string, X = undefined, S extends RelationSchema = RelationSchema> extends RelationshipsApi<S> {
|
|
127
|
+
readonly catalog: PermissionsCatalog<D, R, P, X, S>;
|
|
128
|
+
readonly storage: PermissionsStorage;
|
|
129
|
+
readonly readonly: boolean;
|
|
130
|
+
/**
|
|
131
|
+
* Grant a permission. Idempotent: re-granting returns the existing grant with `created: false`
|
|
132
|
+
* (no history entry, no hooks). An **expired** existing grant is replaced (`created: true`).
|
|
133
|
+
*
|
|
134
|
+
* @throws {NoSubjectError} empty subject. {@link UnknownPermissionError} not assignable.
|
|
135
|
+
* `ValidationError` malformed input or `expiresAt` not in the future. {@link ReadonlyError}.
|
|
136
|
+
*/
|
|
137
|
+
grant(input: GrantInput<P>): Promise<{
|
|
138
|
+
grant: Grant<P>;
|
|
139
|
+
created: boolean;
|
|
140
|
+
}>;
|
|
141
|
+
/** Revoke a permission. Idempotent (`removed: false` when absent). Validates like `grant`. */
|
|
142
|
+
revoke(input: RevokeInput<P>): Promise<RevokeResult<P>>;
|
|
143
|
+
/**
|
|
144
|
+
* Grants by subject, by permission (a key scan with a suffix filter), both, or all. Expired
|
|
145
|
+
* grants are left out unless `includeExpired`. Sorted by subject, then permission.
|
|
146
|
+
*/
|
|
147
|
+
list(query?: ListQuery<P>): Promise<Grant<P>[]>;
|
|
148
|
+
/**
|
|
149
|
+
* Store a role assignment (overrides the auth provider's role); `role: null` clears it.
|
|
150
|
+
*
|
|
151
|
+
* @throws {UnknownRoleError} not one of `roles`, or the guest role.
|
|
152
|
+
*/
|
|
153
|
+
setRole(input: SetRoleInput<R>): Promise<SetRoleResult>;
|
|
154
|
+
/** A subject's stored record and grants (an empty view when nothing is stored). */
|
|
155
|
+
getSubject(id: string, options?: {
|
|
156
|
+
includeExpired?: boolean;
|
|
157
|
+
}): Promise<SubjectView<R, P>>;
|
|
158
|
+
/** Every subject with a record or a grant, sorted by id. */
|
|
159
|
+
listSubjects(): Promise<SubjectSummary[]>;
|
|
160
|
+
/** Create or update a subject's display fields (`null` clears one). Keeps the stored role. */
|
|
161
|
+
upsertSubject(input: UpsertSubjectInput): Promise<SubjectRecord>;
|
|
162
|
+
/** The grant / revoke / role history, newest first, paginated by `cursor`. */
|
|
163
|
+
history(query?: HistoryQuery): Promise<HistoryPage>;
|
|
164
|
+
/**
|
|
165
|
+
* A synchronous ability for many checks against one loaded snapshot of the subject. `null` /
|
|
166
|
+
* `undefined` is a guest (the guest role, no grants).
|
|
167
|
+
*/
|
|
168
|
+
ability(subject: SubjectInput, ...facts: FactsArgs<X>): Promise<PermissionsAbility<D, R, P>>;
|
|
169
|
+
/** Decide one typed path with reasons. */
|
|
170
|
+
check<Path extends RulesPaths<D>>(subject: SubjectInput, path: Path, data?: PathData<D, Path>, ...facts: FactsArgs<X>): Promise<CheckResult<R, P>>;
|
|
171
|
+
/** `(await check(...)).allowed`. */
|
|
172
|
+
can<Path extends RulesPaths<D>>(subject: SubjectInput, path: Path, data?: PathData<D, Path>, ...facts: FactsArgs<X>): Promise<boolean>;
|
|
173
|
+
/** The effective role and active assignable permissions, for client hydration (ADR 0009). */
|
|
174
|
+
snapshot(subject: SubjectInput): Promise<PermissionsSnapshot<R, P>>;
|
|
175
|
+
/** Drop one subject's cache entry, or all of them. */
|
|
176
|
+
invalidate(subject?: string): void;
|
|
177
|
+
/** Stop watching the storage. */
|
|
178
|
+
close(): Promise<void>;
|
|
179
|
+
}
|
|
180
|
+
/** Any engine, generics erased (for integrations: auth providers, servers, bridges). */
|
|
181
|
+
export type AnyPermissions = Permissions<any, any, any, any, any>;
|
|
182
|
+
/**
|
|
183
|
+
* Create the engine.
|
|
184
|
+
*
|
|
185
|
+
* @example
|
|
186
|
+
* ```ts
|
|
187
|
+
* const perms = createPermissions({ catalog, storage, cache: { ttlMs: 5_000 } });
|
|
188
|
+
* const decision = await perms.check({ id: "u1", role: "member" }, "post.publish", post);
|
|
189
|
+
* // { allowed, path, subject, role, reason: "role-rule" | "denied-by-rule" | …, explanation }
|
|
190
|
+
* ```
|
|
191
|
+
*/
|
|
192
|
+
export declare function createPermissions<D extends Definition, R extends string, P extends string, X, S extends RelationSchema = RelationSchema>(options: PermissionsOptions<D, R, P, X, S>): Permissions<D, R, P, X, S>;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Elysia 2.x adapter: `permissionsPanel()` for `app.mount()`, the typed `permissionsElysia()`
|
|
3
|
+
* plugin (Eden treaty) and the `requirePermission()` `beforeHandle` guard.
|
|
4
|
+
*
|
|
5
|
+
* Import from `@xtandard/permissions/elysia2`.
|
|
6
|
+
*
|
|
7
|
+
* @module elysia2
|
|
8
|
+
*/
|
|
9
|
+
export * from "./adapters/elysia2.ts";
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Typed errors. Every class sets a distinct `name`: the kit maps errors to HTTP
|
|
3
|
+
* responses **by name** (`isNamed`), so a copy from another bundle still maps.
|
|
4
|
+
*
|
|
5
|
+
* | name | when |
|
|
6
|
+
* | ------------------------ | --------------------------------------------------------------- |
|
|
7
|
+
* | `NoSubjectError` | a write (or a check) without a subject id — never `""` |
|
|
8
|
+
* | `UnknownPermissionError` | a permission not in the catalog's `assignable` list |
|
|
9
|
+
* | `UnknownRoleError` | a role not in the catalog's `roles` |
|
|
10
|
+
* | `ForbiddenError` | the caller may not manage permissions |
|
|
11
|
+
* | `NotFoundError` | a referenced record does not exist |
|
|
12
|
+
* | `ReadonlyError` | a mutation on a `readonly` engine |
|
|
13
|
+
* | `ValidationError` | malformed input (the kit's, with `errors` / `issues`) |
|
|
14
|
+
* | `AsyncRuleError` | a rule returned a Promise (the lib's; the check denied) |
|
|
15
|
+
*
|
|
16
|
+
* @module
|
|
17
|
+
*/
|
|
18
|
+
export { ValidationError, type ValidationIssue } from "@xtandard/kit/util";
|
|
19
|
+
export { AsyncRuleError } from "./policy.ts";
|
|
20
|
+
/** A write (or a check) needs a subject id and got none (no session, or an empty id). */
|
|
21
|
+
export declare class NoSubjectError extends Error {
|
|
22
|
+
constructor(message?: string);
|
|
23
|
+
}
|
|
24
|
+
/** The permission is not in the catalog's `assignable` list. */
|
|
25
|
+
export declare class UnknownPermissionError extends Error {
|
|
26
|
+
/** The rejected permission. */
|
|
27
|
+
readonly permission: string;
|
|
28
|
+
constructor(permission: unknown);
|
|
29
|
+
}
|
|
30
|
+
/** The role is not one of the catalog's `roles` (or may not be assigned). */
|
|
31
|
+
export declare class UnknownRoleError extends Error {
|
|
32
|
+
/** The rejected role. */
|
|
33
|
+
readonly role: string;
|
|
34
|
+
constructor(role: unknown, message?: string);
|
|
35
|
+
}
|
|
36
|
+
/** The caller may not perform the operation (e.g. grant without a manager role). */
|
|
37
|
+
export declare class ForbiddenError extends Error {
|
|
38
|
+
constructor(message?: string);
|
|
39
|
+
}
|
|
40
|
+
/** A referenced record does not exist. */
|
|
41
|
+
export declare class NotFoundError extends Error {
|
|
42
|
+
constructor(message: string);
|
|
43
|
+
}
|
|
44
|
+
/** Thrown by mutating operations when the engine is readonly. */
|
|
45
|
+
export declare class ReadonlyError extends Error {
|
|
46
|
+
constructor(operation: string);
|
|
47
|
+
}
|