@hammadj/better-auth 1.5.0-beta.10
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 +33 -0
- package/dist/_virtual/rolldown_runtime.mjs +36 -0
- package/dist/adapters/drizzle-adapter/index.d.mts +1 -0
- package/dist/adapters/drizzle-adapter/index.mjs +3 -0
- package/dist/adapters/index.d.mts +23 -0
- package/dist/adapters/index.mjs +13 -0
- package/dist/adapters/index.mjs.map +1 -0
- package/dist/adapters/kysely-adapter/index.d.mts +1 -0
- package/dist/adapters/kysely-adapter/index.mjs +3 -0
- package/dist/adapters/memory-adapter/index.d.mts +1 -0
- package/dist/adapters/memory-adapter/index.mjs +3 -0
- package/dist/adapters/mongodb-adapter/index.d.mts +1 -0
- package/dist/adapters/mongodb-adapter/index.mjs +3 -0
- package/dist/adapters/prisma-adapter/index.d.mts +1 -0
- package/dist/adapters/prisma-adapter/index.mjs +3 -0
- package/dist/api/index.d.mts +40 -0
- package/dist/api/index.mjs +205 -0
- package/dist/api/index.mjs.map +1 -0
- package/dist/api/middlewares/index.d.mts +1 -0
- package/dist/api/middlewares/index.mjs +3 -0
- package/dist/api/middlewares/origin-check.d.mts +17 -0
- package/dist/api/middlewares/origin-check.mjs +140 -0
- package/dist/api/middlewares/origin-check.mjs.map +1 -0
- package/dist/api/rate-limiter/index.mjs +177 -0
- package/dist/api/rate-limiter/index.mjs.map +1 -0
- package/dist/api/routes/account.d.mts +10 -0
- package/dist/api/routes/account.mjs +493 -0
- package/dist/api/routes/account.mjs.map +1 -0
- package/dist/api/routes/callback.d.mts +5 -0
- package/dist/api/routes/callback.mjs +178 -0
- package/dist/api/routes/callback.mjs.map +1 -0
- package/dist/api/routes/email-verification.d.mts +29 -0
- package/dist/api/routes/email-verification.mjs +301 -0
- package/dist/api/routes/email-verification.mjs.map +1 -0
- package/dist/api/routes/error.d.mts +5 -0
- package/dist/api/routes/error.mjs +386 -0
- package/dist/api/routes/error.mjs.map +1 -0
- package/dist/api/routes/index.d.mts +11 -0
- package/dist/api/routes/index.mjs +13 -0
- package/dist/api/routes/ok.d.mts +5 -0
- package/dist/api/routes/ok.mjs +30 -0
- package/dist/api/routes/ok.mjs.map +1 -0
- package/dist/api/routes/password.d.mts +8 -0
- package/dist/api/routes/password.mjs +198 -0
- package/dist/api/routes/password.mjs.map +1 -0
- package/dist/api/routes/session.d.mts +52 -0
- package/dist/api/routes/session.mjs +478 -0
- package/dist/api/routes/session.mjs.map +1 -0
- package/dist/api/routes/sign-in.d.mts +8 -0
- package/dist/api/routes/sign-in.mjs +262 -0
- package/dist/api/routes/sign-in.mjs.map +1 -0
- package/dist/api/routes/sign-out.d.mts +5 -0
- package/dist/api/routes/sign-out.mjs +33 -0
- package/dist/api/routes/sign-out.mjs.map +1 -0
- package/dist/api/routes/sign-up.d.mts +7 -0
- package/dist/api/routes/sign-up.mjs +227 -0
- package/dist/api/routes/sign-up.mjs.map +1 -0
- package/dist/api/routes/update-user.d.mts +12 -0
- package/dist/api/routes/update-user.mjs +493 -0
- package/dist/api/routes/update-user.mjs.map +1 -0
- package/dist/api/state/oauth.d.mts +5 -0
- package/dist/api/state/oauth.mjs +8 -0
- package/dist/api/state/oauth.mjs.map +1 -0
- package/dist/api/state/should-session-refresh.d.mts +13 -0
- package/dist/api/state/should-session-refresh.mjs +16 -0
- package/dist/api/state/should-session-refresh.mjs.map +1 -0
- package/dist/api/to-auth-endpoints.mjs +197 -0
- package/dist/api/to-auth-endpoints.mjs.map +1 -0
- package/dist/auth/base.mjs +44 -0
- package/dist/auth/base.mjs.map +1 -0
- package/dist/auth/full.d.mts +30 -0
- package/dist/auth/full.mjs +32 -0
- package/dist/auth/full.mjs.map +1 -0
- package/dist/auth/minimal.d.mts +12 -0
- package/dist/auth/minimal.mjs +14 -0
- package/dist/auth/minimal.mjs.map +1 -0
- package/dist/auth/trusted-origins.mjs +31 -0
- package/dist/auth/trusted-origins.mjs.map +1 -0
- package/dist/client/broadcast-channel.d.mts +20 -0
- package/dist/client/broadcast-channel.mjs +46 -0
- package/dist/client/broadcast-channel.mjs.map +1 -0
- package/dist/client/config.mjs +90 -0
- package/dist/client/config.mjs.map +1 -0
- package/dist/client/fetch-plugins.mjs +18 -0
- package/dist/client/fetch-plugins.mjs.map +1 -0
- package/dist/client/focus-manager.d.mts +11 -0
- package/dist/client/focus-manager.mjs +32 -0
- package/dist/client/focus-manager.mjs.map +1 -0
- package/dist/client/index.d.mts +30 -0
- package/dist/client/index.mjs +21 -0
- package/dist/client/index.mjs.map +1 -0
- package/dist/client/lynx/index.d.mts +62 -0
- package/dist/client/lynx/index.mjs +24 -0
- package/dist/client/lynx/index.mjs.map +1 -0
- package/dist/client/lynx/lynx-store.d.mts +47 -0
- package/dist/client/lynx/lynx-store.mjs +47 -0
- package/dist/client/lynx/lynx-store.mjs.map +1 -0
- package/dist/client/online-manager.d.mts +12 -0
- package/dist/client/online-manager.mjs +35 -0
- package/dist/client/online-manager.mjs.map +1 -0
- package/dist/client/parser.mjs +73 -0
- package/dist/client/parser.mjs.map +1 -0
- package/dist/client/path-to-object.d.mts +57 -0
- package/dist/client/plugins/index.d.mts +58 -0
- package/dist/client/plugins/index.mjs +33 -0
- package/dist/client/plugins/infer-plugin.d.mts +9 -0
- package/dist/client/plugins/infer-plugin.mjs +11 -0
- package/dist/client/plugins/infer-plugin.mjs.map +1 -0
- package/dist/client/proxy.mjs +79 -0
- package/dist/client/proxy.mjs.map +1 -0
- package/dist/client/query.d.mts +23 -0
- package/dist/client/query.mjs +98 -0
- package/dist/client/query.mjs.map +1 -0
- package/dist/client/react/index.d.mts +63 -0
- package/dist/client/react/index.mjs +24 -0
- package/dist/client/react/index.mjs.map +1 -0
- package/dist/client/react/react-store.d.mts +47 -0
- package/dist/client/react/react-store.mjs +47 -0
- package/dist/client/react/react-store.mjs.map +1 -0
- package/dist/client/session-atom.mjs +29 -0
- package/dist/client/session-atom.mjs.map +1 -0
- package/dist/client/session-refresh.d.mts +28 -0
- package/dist/client/session-refresh.mjs +140 -0
- package/dist/client/session-refresh.mjs.map +1 -0
- package/dist/client/solid/index.d.mts +57 -0
- package/dist/client/solid/index.mjs +22 -0
- package/dist/client/solid/index.mjs.map +1 -0
- package/dist/client/solid/solid-store.mjs +24 -0
- package/dist/client/solid/solid-store.mjs.map +1 -0
- package/dist/client/svelte/index.d.mts +63 -0
- package/dist/client/svelte/index.mjs +20 -0
- package/dist/client/svelte/index.mjs.map +1 -0
- package/dist/client/types.d.mts +58 -0
- package/dist/client/vanilla.d.mts +62 -0
- package/dist/client/vanilla.mjs +20 -0
- package/dist/client/vanilla.mjs.map +1 -0
- package/dist/client/vue/index.d.mts +86 -0
- package/dist/client/vue/index.mjs +38 -0
- package/dist/client/vue/index.mjs.map +1 -0
- package/dist/client/vue/vue-store.mjs +26 -0
- package/dist/client/vue/vue-store.mjs.map +1 -0
- package/dist/context/create-context.mjs +211 -0
- package/dist/context/create-context.mjs.map +1 -0
- package/dist/context/helpers.mjs +62 -0
- package/dist/context/helpers.mjs.map +1 -0
- package/dist/context/init-minimal.mjs +20 -0
- package/dist/context/init-minimal.mjs.map +1 -0
- package/dist/context/init.mjs +22 -0
- package/dist/context/init.mjs.map +1 -0
- package/dist/cookies/cookie-utils.d.mts +29 -0
- package/dist/cookies/cookie-utils.mjs +105 -0
- package/dist/cookies/cookie-utils.mjs.map +1 -0
- package/dist/cookies/index.d.mts +67 -0
- package/dist/cookies/index.mjs +264 -0
- package/dist/cookies/index.mjs.map +1 -0
- package/dist/cookies/session-store.d.mts +36 -0
- package/dist/cookies/session-store.mjs +200 -0
- package/dist/cookies/session-store.mjs.map +1 -0
- package/dist/crypto/buffer.d.mts +8 -0
- package/dist/crypto/buffer.mjs +18 -0
- package/dist/crypto/buffer.mjs.map +1 -0
- package/dist/crypto/index.d.mts +27 -0
- package/dist/crypto/index.mjs +38 -0
- package/dist/crypto/index.mjs.map +1 -0
- package/dist/crypto/jwt.d.mts +8 -0
- package/dist/crypto/jwt.mjs +95 -0
- package/dist/crypto/jwt.mjs.map +1 -0
- package/dist/crypto/password.d.mts +12 -0
- package/dist/crypto/password.mjs +36 -0
- package/dist/crypto/password.mjs.map +1 -0
- package/dist/crypto/random.d.mts +5 -0
- package/dist/crypto/random.mjs +8 -0
- package/dist/crypto/random.mjs.map +1 -0
- package/dist/db/adapter-base.d.mts +8 -0
- package/dist/db/adapter-base.mjs +28 -0
- package/dist/db/adapter-base.mjs.map +1 -0
- package/dist/db/adapter-kysely.d.mts +8 -0
- package/dist/db/adapter-kysely.mjs +21 -0
- package/dist/db/adapter-kysely.mjs.map +1 -0
- package/dist/db/field-converter.d.mts +8 -0
- package/dist/db/field-converter.mjs +21 -0
- package/dist/db/field-converter.mjs.map +1 -0
- package/dist/db/field.d.mts +55 -0
- package/dist/db/field.mjs +11 -0
- package/dist/db/field.mjs.map +1 -0
- package/dist/db/get-migration.d.mts +23 -0
- package/dist/db/get-migration.mjs +339 -0
- package/dist/db/get-migration.mjs.map +1 -0
- package/dist/db/get-schema.d.mts +11 -0
- package/dist/db/get-schema.mjs +39 -0
- package/dist/db/get-schema.mjs.map +1 -0
- package/dist/db/index.d.mts +9 -0
- package/dist/db/index.mjs +36 -0
- package/dist/db/index.mjs.map +1 -0
- package/dist/db/internal-adapter.d.mts +14 -0
- package/dist/db/internal-adapter.mjs +616 -0
- package/dist/db/internal-adapter.mjs.map +1 -0
- package/dist/db/schema.d.mts +26 -0
- package/dist/db/schema.mjs +118 -0
- package/dist/db/schema.mjs.map +1 -0
- package/dist/db/to-zod.d.mts +36 -0
- package/dist/db/to-zod.mjs +26 -0
- package/dist/db/to-zod.mjs.map +1 -0
- package/dist/db/verification-token-storage.mjs +28 -0
- package/dist/db/verification-token-storage.mjs.map +1 -0
- package/dist/db/with-hooks.d.mts +33 -0
- package/dist/db/with-hooks.mjs +159 -0
- package/dist/db/with-hooks.mjs.map +1 -0
- package/dist/index.d.mts +52 -0
- package/dist/index.mjs +26 -0
- package/dist/integrations/next-js.d.mts +14 -0
- package/dist/integrations/next-js.mjs +78 -0
- package/dist/integrations/next-js.mjs.map +1 -0
- package/dist/integrations/node.d.mts +13 -0
- package/dist/integrations/node.mjs +16 -0
- package/dist/integrations/node.mjs.map +1 -0
- package/dist/integrations/solid-start.d.mts +23 -0
- package/dist/integrations/solid-start.mjs +17 -0
- package/dist/integrations/solid-start.mjs.map +1 -0
- package/dist/integrations/svelte-kit.d.mts +29 -0
- package/dist/integrations/svelte-kit.mjs +57 -0
- package/dist/integrations/svelte-kit.mjs.map +1 -0
- package/dist/integrations/tanstack-start-solid.d.mts +22 -0
- package/dist/integrations/tanstack-start-solid.mjs +61 -0
- package/dist/integrations/tanstack-start-solid.mjs.map +1 -0
- package/dist/integrations/tanstack-start.d.mts +22 -0
- package/dist/integrations/tanstack-start.mjs +61 -0
- package/dist/integrations/tanstack-start.mjs.map +1 -0
- package/dist/oauth2/index.d.mts +5 -0
- package/dist/oauth2/index.mjs +7 -0
- package/dist/oauth2/link-account.d.mts +31 -0
- package/dist/oauth2/link-account.mjs +144 -0
- package/dist/oauth2/link-account.mjs.map +1 -0
- package/dist/oauth2/state.d.mts +26 -0
- package/dist/oauth2/state.mjs +51 -0
- package/dist/oauth2/state.mjs.map +1 -0
- package/dist/oauth2/utils.d.mts +8 -0
- package/dist/oauth2/utils.mjs +31 -0
- package/dist/oauth2/utils.mjs.map +1 -0
- package/dist/plugins/access/access.d.mts +30 -0
- package/dist/plugins/access/access.mjs +46 -0
- package/dist/plugins/access/access.mjs.map +1 -0
- package/dist/plugins/access/index.d.mts +3 -0
- package/dist/plugins/access/index.mjs +3 -0
- package/dist/plugins/access/types.d.mts +17 -0
- package/dist/plugins/additional-fields/client.d.mts +14 -0
- package/dist/plugins/additional-fields/client.mjs +11 -0
- package/dist/plugins/additional-fields/client.mjs.map +1 -0
- package/dist/plugins/admin/access/index.d.mts +2 -0
- package/dist/plugins/admin/access/index.mjs +3 -0
- package/dist/plugins/admin/access/statement.d.mts +118 -0
- package/dist/plugins/admin/access/statement.mjs +53 -0
- package/dist/plugins/admin/access/statement.mjs.map +1 -0
- package/dist/plugins/admin/admin.d.mts +14 -0
- package/dist/plugins/admin/admin.mjs +95 -0
- package/dist/plugins/admin/admin.mjs.map +1 -0
- package/dist/plugins/admin/client.d.mts +14 -0
- package/dist/plugins/admin/client.mjs +36 -0
- package/dist/plugins/admin/client.mjs.map +1 -0
- package/dist/plugins/admin/error-codes.d.mts +5 -0
- package/dist/plugins/admin/error-codes.mjs +30 -0
- package/dist/plugins/admin/error-codes.mjs.map +1 -0
- package/dist/plugins/admin/has-permission.mjs +16 -0
- package/dist/plugins/admin/has-permission.mjs.map +1 -0
- package/dist/plugins/admin/index.d.mts +3 -0
- package/dist/plugins/admin/index.mjs +3 -0
- package/dist/plugins/admin/routes.mjs +855 -0
- package/dist/plugins/admin/routes.mjs.map +1 -0
- package/dist/plugins/admin/schema.d.mts +6 -0
- package/dist/plugins/admin/schema.mjs +34 -0
- package/dist/plugins/admin/schema.mjs.map +1 -0
- package/dist/plugins/admin/types.d.mts +89 -0
- package/dist/plugins/anonymous/client.d.mts +9 -0
- package/dist/plugins/anonymous/client.mjs +22 -0
- package/dist/plugins/anonymous/client.mjs.map +1 -0
- package/dist/plugins/anonymous/error-codes.d.mts +5 -0
- package/dist/plugins/anonymous/error-codes.mjs +16 -0
- package/dist/plugins/anonymous/error-codes.mjs.map +1 -0
- package/dist/plugins/anonymous/index.d.mts +14 -0
- package/dist/plugins/anonymous/index.mjs +163 -0
- package/dist/plugins/anonymous/index.mjs.map +1 -0
- package/dist/plugins/anonymous/schema.d.mts +5 -0
- package/dist/plugins/anonymous/schema.mjs +11 -0
- package/dist/plugins/anonymous/schema.mjs.map +1 -0
- package/dist/plugins/anonymous/types.d.mts +68 -0
- package/dist/plugins/api-key/adapter.mjs +468 -0
- package/dist/plugins/api-key/adapter.mjs.map +1 -0
- package/dist/plugins/api-key/client.d.mts +9 -0
- package/dist/plugins/api-key/client.mjs +19 -0
- package/dist/plugins/api-key/client.mjs.map +1 -0
- package/dist/plugins/api-key/error-codes.d.mts +5 -0
- package/dist/plugins/api-key/error-codes.mjs +34 -0
- package/dist/plugins/api-key/error-codes.mjs.map +1 -0
- package/dist/plugins/api-key/index.d.mts +17 -0
- package/dist/plugins/api-key/index.mjs +134 -0
- package/dist/plugins/api-key/index.mjs.map +1 -0
- package/dist/plugins/api-key/rate-limit.mjs +74 -0
- package/dist/plugins/api-key/rate-limit.mjs.map +1 -0
- package/dist/plugins/api-key/routes/create-api-key.mjs +252 -0
- package/dist/plugins/api-key/routes/create-api-key.mjs.map +1 -0
- package/dist/plugins/api-key/routes/delete-all-expired-api-keys.mjs +24 -0
- package/dist/plugins/api-key/routes/delete-all-expired-api-keys.mjs.map +1 -0
- package/dist/plugins/api-key/routes/delete-api-key.mjs +74 -0
- package/dist/plugins/api-key/routes/delete-api-key.mjs.map +1 -0
- package/dist/plugins/api-key/routes/get-api-key.mjs +158 -0
- package/dist/plugins/api-key/routes/get-api-key.mjs.map +1 -0
- package/dist/plugins/api-key/routes/index.mjs +71 -0
- package/dist/plugins/api-key/routes/index.mjs.map +1 -0
- package/dist/plugins/api-key/routes/list-api-keys.mjs +194 -0
- package/dist/plugins/api-key/routes/list-api-keys.mjs.map +1 -0
- package/dist/plugins/api-key/routes/update-api-key.mjs +248 -0
- package/dist/plugins/api-key/routes/update-api-key.mjs.map +1 -0
- package/dist/plugins/api-key/routes/verify-api-key.mjs +223 -0
- package/dist/plugins/api-key/routes/verify-api-key.mjs.map +1 -0
- package/dist/plugins/api-key/schema.d.mts +11 -0
- package/dist/plugins/api-key/schema.mjs +130 -0
- package/dist/plugins/api-key/schema.mjs.map +1 -0
- package/dist/plugins/api-key/types.d.mts +346 -0
- package/dist/plugins/bearer/index.d.mts +25 -0
- package/dist/plugins/bearer/index.mjs +66 -0
- package/dist/plugins/bearer/index.mjs.map +1 -0
- package/dist/plugins/captcha/constants.d.mts +10 -0
- package/dist/plugins/captcha/constants.mjs +22 -0
- package/dist/plugins/captcha/constants.mjs.map +1 -0
- package/dist/plugins/captcha/error-codes.mjs +16 -0
- package/dist/plugins/captcha/error-codes.mjs.map +1 -0
- package/dist/plugins/captcha/index.d.mts +14 -0
- package/dist/plugins/captcha/index.mjs +60 -0
- package/dist/plugins/captcha/index.mjs.map +1 -0
- package/dist/plugins/captcha/types.d.mts +28 -0
- package/dist/plugins/captcha/utils.mjs +11 -0
- package/dist/plugins/captcha/utils.mjs.map +1 -0
- package/dist/plugins/captcha/verify-handlers/captchafox.mjs +27 -0
- package/dist/plugins/captcha/verify-handlers/captchafox.mjs.map +1 -0
- package/dist/plugins/captcha/verify-handlers/cloudflare-turnstile.mjs +25 -0
- package/dist/plugins/captcha/verify-handlers/cloudflare-turnstile.mjs.map +1 -0
- package/dist/plugins/captcha/verify-handlers/google-recaptcha.mjs +29 -0
- package/dist/plugins/captcha/verify-handlers/google-recaptcha.mjs.map +1 -0
- package/dist/plugins/captcha/verify-handlers/h-captcha.mjs +27 -0
- package/dist/plugins/captcha/verify-handlers/h-captcha.mjs.map +1 -0
- package/dist/plugins/captcha/verify-handlers/index.mjs +6 -0
- package/dist/plugins/custom-session/client.d.mts +10 -0
- package/dist/plugins/custom-session/client.mjs +11 -0
- package/dist/plugins/custom-session/client.mjs.map +1 -0
- package/dist/plugins/custom-session/index.d.mts +26 -0
- package/dist/plugins/custom-session/index.mjs +70 -0
- package/dist/plugins/custom-session/index.mjs.map +1 -0
- package/dist/plugins/device-authorization/client.d.mts +5 -0
- package/dist/plugins/device-authorization/client.mjs +18 -0
- package/dist/plugins/device-authorization/client.mjs.map +1 -0
- package/dist/plugins/device-authorization/error-codes.mjs +21 -0
- package/dist/plugins/device-authorization/error-codes.mjs.map +1 -0
- package/dist/plugins/device-authorization/index.d.mts +28 -0
- package/dist/plugins/device-authorization/index.mjs +50 -0
- package/dist/plugins/device-authorization/index.mjs.map +1 -0
- package/dist/plugins/device-authorization/routes.mjs +510 -0
- package/dist/plugins/device-authorization/routes.mjs.map +1 -0
- package/dist/plugins/device-authorization/schema.mjs +57 -0
- package/dist/plugins/device-authorization/schema.mjs.map +1 -0
- package/dist/plugins/email-otp/client.d.mts +7 -0
- package/dist/plugins/email-otp/client.mjs +18 -0
- package/dist/plugins/email-otp/client.mjs.map +1 -0
- package/dist/plugins/email-otp/error-codes.d.mts +5 -0
- package/dist/plugins/email-otp/error-codes.mjs +12 -0
- package/dist/plugins/email-otp/error-codes.mjs.map +1 -0
- package/dist/plugins/email-otp/index.d.mts +14 -0
- package/dist/plugins/email-otp/index.mjs +108 -0
- package/dist/plugins/email-otp/index.mjs.map +1 -0
- package/dist/plugins/email-otp/otp-token.mjs +29 -0
- package/dist/plugins/email-otp/otp-token.mjs.map +1 -0
- package/dist/plugins/email-otp/routes.mjs +564 -0
- package/dist/plugins/email-otp/routes.mjs.map +1 -0
- package/dist/plugins/email-otp/types.d.mts +74 -0
- package/dist/plugins/email-otp/utils.mjs +17 -0
- package/dist/plugins/email-otp/utils.mjs.map +1 -0
- package/dist/plugins/generic-oauth/client.d.mts +19 -0
- package/dist/plugins/generic-oauth/client.mjs +14 -0
- package/dist/plugins/generic-oauth/client.mjs.map +1 -0
- package/dist/plugins/generic-oauth/error-codes.d.mts +5 -0
- package/dist/plugins/generic-oauth/error-codes.mjs +15 -0
- package/dist/plugins/generic-oauth/error-codes.mjs.map +1 -0
- package/dist/plugins/generic-oauth/index.d.mts +34 -0
- package/dist/plugins/generic-oauth/index.mjs +137 -0
- package/dist/plugins/generic-oauth/index.mjs.map +1 -0
- package/dist/plugins/generic-oauth/providers/auth0.d.mts +37 -0
- package/dist/plugins/generic-oauth/providers/auth0.mjs +62 -0
- package/dist/plugins/generic-oauth/providers/auth0.mjs.map +1 -0
- package/dist/plugins/generic-oauth/providers/gumroad.d.mts +32 -0
- package/dist/plugins/generic-oauth/providers/gumroad.mjs +60 -0
- package/dist/plugins/generic-oauth/providers/gumroad.mjs.map +1 -0
- package/dist/plugins/generic-oauth/providers/hubspot.d.mts +37 -0
- package/dist/plugins/generic-oauth/providers/hubspot.mjs +60 -0
- package/dist/plugins/generic-oauth/providers/hubspot.mjs.map +1 -0
- package/dist/plugins/generic-oauth/providers/index.d.mts +9 -0
- package/dist/plugins/generic-oauth/providers/index.mjs +11 -0
- package/dist/plugins/generic-oauth/providers/keycloak.d.mts +37 -0
- package/dist/plugins/generic-oauth/providers/keycloak.mjs +62 -0
- package/dist/plugins/generic-oauth/providers/keycloak.mjs.map +1 -0
- package/dist/plugins/generic-oauth/providers/line.d.mts +55 -0
- package/dist/plugins/generic-oauth/providers/line.mjs +91 -0
- package/dist/plugins/generic-oauth/providers/line.mjs.map +1 -0
- package/dist/plugins/generic-oauth/providers/microsoft-entra-id.d.mts +37 -0
- package/dist/plugins/generic-oauth/providers/microsoft-entra-id.mjs +66 -0
- package/dist/plugins/generic-oauth/providers/microsoft-entra-id.mjs.map +1 -0
- package/dist/plugins/generic-oauth/providers/okta.d.mts +37 -0
- package/dist/plugins/generic-oauth/providers/okta.mjs +62 -0
- package/dist/plugins/generic-oauth/providers/okta.mjs.map +1 -0
- package/dist/plugins/generic-oauth/providers/patreon.d.mts +30 -0
- package/dist/plugins/generic-oauth/providers/patreon.mjs +59 -0
- package/dist/plugins/generic-oauth/providers/patreon.mjs.map +1 -0
- package/dist/plugins/generic-oauth/providers/slack.d.mts +30 -0
- package/dist/plugins/generic-oauth/providers/slack.mjs +61 -0
- package/dist/plugins/generic-oauth/providers/slack.mjs.map +1 -0
- package/dist/plugins/generic-oauth/routes.mjs +394 -0
- package/dist/plugins/generic-oauth/routes.mjs.map +1 -0
- package/dist/plugins/generic-oauth/types.d.mts +145 -0
- package/dist/plugins/haveibeenpwned/index.d.mts +21 -0
- package/dist/plugins/haveibeenpwned/index.mjs +56 -0
- package/dist/plugins/haveibeenpwned/index.mjs.map +1 -0
- package/dist/plugins/index.d.mts +68 -0
- package/dist/plugins/index.mjs +51 -0
- package/dist/plugins/jwt/adapter.mjs +27 -0
- package/dist/plugins/jwt/adapter.mjs.map +1 -0
- package/dist/plugins/jwt/client.d.mts +18 -0
- package/dist/plugins/jwt/client.mjs +19 -0
- package/dist/plugins/jwt/client.mjs.map +1 -0
- package/dist/plugins/jwt/index.d.mts +17 -0
- package/dist/plugins/jwt/index.mjs +202 -0
- package/dist/plugins/jwt/index.mjs.map +1 -0
- package/dist/plugins/jwt/schema.d.mts +5 -0
- package/dist/plugins/jwt/schema.mjs +23 -0
- package/dist/plugins/jwt/schema.mjs.map +1 -0
- package/dist/plugins/jwt/sign.d.mts +57 -0
- package/dist/plugins/jwt/sign.mjs +66 -0
- package/dist/plugins/jwt/sign.mjs.map +1 -0
- package/dist/plugins/jwt/types.d.mts +194 -0
- package/dist/plugins/jwt/utils.d.mts +42 -0
- package/dist/plugins/jwt/utils.mjs +64 -0
- package/dist/plugins/jwt/utils.mjs.map +1 -0
- package/dist/plugins/jwt/verify.d.mts +12 -0
- package/dist/plugins/jwt/verify.mjs +46 -0
- package/dist/plugins/jwt/verify.mjs.map +1 -0
- package/dist/plugins/last-login-method/client.d.mts +18 -0
- package/dist/plugins/last-login-method/client.mjs +32 -0
- package/dist/plugins/last-login-method/client.mjs.map +1 -0
- package/dist/plugins/last-login-method/index.d.mts +52 -0
- package/dist/plugins/last-login-method/index.mjs +77 -0
- package/dist/plugins/last-login-method/index.mjs.map +1 -0
- package/dist/plugins/magic-link/client.d.mts +5 -0
- package/dist/plugins/magic-link/client.mjs +11 -0
- package/dist/plugins/magic-link/client.mjs.map +1 -0
- package/dist/plugins/magic-link/index.d.mts +61 -0
- package/dist/plugins/magic-link/index.mjs +167 -0
- package/dist/plugins/magic-link/index.mjs.map +1 -0
- package/dist/plugins/magic-link/utils.mjs +12 -0
- package/dist/plugins/magic-link/utils.mjs.map +1 -0
- package/dist/plugins/mcp/authorize.mjs +133 -0
- package/dist/plugins/mcp/authorize.mjs.map +1 -0
- package/dist/plugins/mcp/index.d.mts +46 -0
- package/dist/plugins/mcp/index.mjs +717 -0
- package/dist/plugins/mcp/index.mjs.map +1 -0
- package/dist/plugins/multi-session/client.d.mts +8 -0
- package/dist/plugins/multi-session/client.mjs +20 -0
- package/dist/plugins/multi-session/client.mjs.map +1 -0
- package/dist/plugins/multi-session/error-codes.d.mts +5 -0
- package/dist/plugins/multi-session/error-codes.mjs +8 -0
- package/dist/plugins/multi-session/error-codes.mjs.map +1 -0
- package/dist/plugins/multi-session/index.d.mts +22 -0
- package/dist/plugins/multi-session/index.mjs +172 -0
- package/dist/plugins/multi-session/index.mjs.map +1 -0
- package/dist/plugins/oauth-proxy/index.d.mts +39 -0
- package/dist/plugins/oauth-proxy/index.mjs +305 -0
- package/dist/plugins/oauth-proxy/index.mjs.map +1 -0
- package/dist/plugins/oauth-proxy/utils.mjs +44 -0
- package/dist/plugins/oauth-proxy/utils.mjs.map +1 -0
- package/dist/plugins/oidc-provider/authorize.mjs +194 -0
- package/dist/plugins/oidc-provider/authorize.mjs.map +1 -0
- package/dist/plugins/oidc-provider/client.d.mts +8 -0
- package/dist/plugins/oidc-provider/client.mjs +11 -0
- package/dist/plugins/oidc-provider/client.mjs.map +1 -0
- package/dist/plugins/oidc-provider/error.mjs +17 -0
- package/dist/plugins/oidc-provider/error.mjs.map +1 -0
- package/dist/plugins/oidc-provider/index.d.mts +32 -0
- package/dist/plugins/oidc-provider/index.mjs +1093 -0
- package/dist/plugins/oidc-provider/index.mjs.map +1 -0
- package/dist/plugins/oidc-provider/schema.d.mts +26 -0
- package/dist/plugins/oidc-provider/schema.mjs +132 -0
- package/dist/plugins/oidc-provider/schema.mjs.map +1 -0
- package/dist/plugins/oidc-provider/types.d.mts +517 -0
- package/dist/plugins/oidc-provider/utils/prompt.mjs +19 -0
- package/dist/plugins/oidc-provider/utils/prompt.mjs.map +1 -0
- package/dist/plugins/oidc-provider/utils.mjs +15 -0
- package/dist/plugins/oidc-provider/utils.mjs.map +1 -0
- package/dist/plugins/one-tap/client.d.mts +159 -0
- package/dist/plugins/one-tap/client.mjs +214 -0
- package/dist/plugins/one-tap/client.mjs.map +1 -0
- package/dist/plugins/one-tap/index.d.mts +27 -0
- package/dist/plugins/one-tap/index.mjs +96 -0
- package/dist/plugins/one-tap/index.mjs.map +1 -0
- package/dist/plugins/one-time-token/client.d.mts +7 -0
- package/dist/plugins/one-time-token/client.mjs +11 -0
- package/dist/plugins/one-time-token/client.mjs.map +1 -0
- package/dist/plugins/one-time-token/index.d.mts +53 -0
- package/dist/plugins/one-time-token/index.mjs +82 -0
- package/dist/plugins/one-time-token/index.mjs.map +1 -0
- package/dist/plugins/one-time-token/utils.mjs +12 -0
- package/dist/plugins/one-time-token/utils.mjs.map +1 -0
- package/dist/plugins/open-api/generator.d.mts +115 -0
- package/dist/plugins/open-api/generator.mjs +315 -0
- package/dist/plugins/open-api/generator.mjs.map +1 -0
- package/dist/plugins/open-api/index.d.mts +45 -0
- package/dist/plugins/open-api/index.mjs +67 -0
- package/dist/plugins/open-api/index.mjs.map +1 -0
- package/dist/plugins/open-api/logo.mjs +15 -0
- package/dist/plugins/open-api/logo.mjs.map +1 -0
- package/dist/plugins/organization/access/index.d.mts +2 -0
- package/dist/plugins/organization/access/index.mjs +3 -0
- package/dist/plugins/organization/access/statement.d.mts +249 -0
- package/dist/plugins/organization/access/statement.mjs +81 -0
- package/dist/plugins/organization/access/statement.mjs.map +1 -0
- package/dist/plugins/organization/adapter.d.mts +205 -0
- package/dist/plugins/organization/adapter.mjs +624 -0
- package/dist/plugins/organization/adapter.mjs.map +1 -0
- package/dist/plugins/organization/call.mjs +19 -0
- package/dist/plugins/organization/call.mjs.map +1 -0
- package/dist/plugins/organization/client.d.mts +151 -0
- package/dist/plugins/organization/client.mjs +107 -0
- package/dist/plugins/organization/client.mjs.map +1 -0
- package/dist/plugins/organization/error-codes.d.mts +5 -0
- package/dist/plugins/organization/error-codes.mjs +65 -0
- package/dist/plugins/organization/error-codes.mjs.map +1 -0
- package/dist/plugins/organization/has-permission.mjs +35 -0
- package/dist/plugins/organization/has-permission.mjs.map +1 -0
- package/dist/plugins/organization/index.d.mts +5 -0
- package/dist/plugins/organization/index.mjs +4 -0
- package/dist/plugins/organization/organization.d.mts +252 -0
- package/dist/plugins/organization/organization.mjs +428 -0
- package/dist/plugins/organization/organization.mjs.map +1 -0
- package/dist/plugins/organization/permission.d.mts +26 -0
- package/dist/plugins/organization/permission.mjs +16 -0
- package/dist/plugins/organization/permission.mjs.map +1 -0
- package/dist/plugins/organization/routes/crud-access-control.d.mts +11 -0
- package/dist/plugins/organization/routes/crud-access-control.mjs +656 -0
- package/dist/plugins/organization/routes/crud-access-control.mjs.map +1 -0
- package/dist/plugins/organization/routes/crud-invites.d.mts +16 -0
- package/dist/plugins/organization/routes/crud-invites.mjs +555 -0
- package/dist/plugins/organization/routes/crud-invites.mjs.map +1 -0
- package/dist/plugins/organization/routes/crud-members.d.mts +13 -0
- package/dist/plugins/organization/routes/crud-members.mjs +473 -0
- package/dist/plugins/organization/routes/crud-members.mjs.map +1 -0
- package/dist/plugins/organization/routes/crud-org.d.mts +13 -0
- package/dist/plugins/organization/routes/crud-org.mjs +447 -0
- package/dist/plugins/organization/routes/crud-org.mjs.map +1 -0
- package/dist/plugins/organization/routes/crud-team.d.mts +15 -0
- package/dist/plugins/organization/routes/crud-team.mjs +676 -0
- package/dist/plugins/organization/routes/crud-team.mjs.map +1 -0
- package/dist/plugins/organization/schema.d.mts +376 -0
- package/dist/plugins/organization/schema.mjs +68 -0
- package/dist/plugins/organization/schema.mjs.map +1 -0
- package/dist/plugins/organization/types.d.mts +733 -0
- package/dist/plugins/phone-number/client.d.mts +8 -0
- package/dist/plugins/phone-number/client.mjs +20 -0
- package/dist/plugins/phone-number/client.mjs.map +1 -0
- package/dist/plugins/phone-number/error-codes.d.mts +5 -0
- package/dist/plugins/phone-number/error-codes.mjs +21 -0
- package/dist/plugins/phone-number/error-codes.mjs.map +1 -0
- package/dist/plugins/phone-number/index.d.mts +14 -0
- package/dist/plugins/phone-number/index.mjs +49 -0
- package/dist/plugins/phone-number/index.mjs.map +1 -0
- package/dist/plugins/phone-number/routes.mjs +459 -0
- package/dist/plugins/phone-number/routes.mjs.map +1 -0
- package/dist/plugins/phone-number/schema.d.mts +5 -0
- package/dist/plugins/phone-number/schema.mjs +20 -0
- package/dist/plugins/phone-number/schema.mjs.map +1 -0
- package/dist/plugins/phone-number/types.d.mts +118 -0
- package/dist/plugins/siwe/client.d.mts +5 -0
- package/dist/plugins/siwe/client.mjs +11 -0
- package/dist/plugins/siwe/client.mjs.map +1 -0
- package/dist/plugins/siwe/error-codes.mjs +13 -0
- package/dist/plugins/siwe/error-codes.mjs.map +1 -0
- package/dist/plugins/siwe/index.d.mts +26 -0
- package/dist/plugins/siwe/index.mjs +261 -0
- package/dist/plugins/siwe/index.mjs.map +1 -0
- package/dist/plugins/siwe/schema.d.mts +5 -0
- package/dist/plugins/siwe/schema.mjs +32 -0
- package/dist/plugins/siwe/schema.mjs.map +1 -0
- package/dist/plugins/siwe/types.d.mts +44 -0
- package/dist/plugins/two-factor/backup-codes/index.d.mts +91 -0
- package/dist/plugins/two-factor/backup-codes/index.mjs +277 -0
- package/dist/plugins/two-factor/backup-codes/index.mjs.map +1 -0
- package/dist/plugins/two-factor/client.d.mts +17 -0
- package/dist/plugins/two-factor/client.mjs +37 -0
- package/dist/plugins/two-factor/client.mjs.map +1 -0
- package/dist/plugins/two-factor/constant.mjs +8 -0
- package/dist/plugins/two-factor/constant.mjs.map +1 -0
- package/dist/plugins/two-factor/error-code.d.mts +5 -0
- package/dist/plugins/two-factor/error-code.mjs +18 -0
- package/dist/plugins/two-factor/error-code.mjs.map +1 -0
- package/dist/plugins/two-factor/index.d.mts +19 -0
- package/dist/plugins/two-factor/index.mjs +207 -0
- package/dist/plugins/two-factor/index.mjs.map +1 -0
- package/dist/plugins/two-factor/otp/index.d.mts +96 -0
- package/dist/plugins/two-factor/otp/index.mjs +199 -0
- package/dist/plugins/two-factor/otp/index.mjs.map +1 -0
- package/dist/plugins/two-factor/schema.d.mts +5 -0
- package/dist/plugins/two-factor/schema.mjs +36 -0
- package/dist/plugins/two-factor/schema.mjs.map +1 -0
- package/dist/plugins/two-factor/totp/index.d.mts +81 -0
- package/dist/plugins/two-factor/totp/index.mjs +157 -0
- package/dist/plugins/two-factor/totp/index.mjs.map +1 -0
- package/dist/plugins/two-factor/types.d.mts +65 -0
- package/dist/plugins/two-factor/utils.mjs +12 -0
- package/dist/plugins/two-factor/utils.mjs.map +1 -0
- package/dist/plugins/two-factor/verify-two-factor.mjs +76 -0
- package/dist/plugins/two-factor/verify-two-factor.mjs.map +1 -0
- package/dist/plugins/username/client.d.mts +7 -0
- package/dist/plugins/username/client.mjs +18 -0
- package/dist/plugins/username/client.mjs.map +1 -0
- package/dist/plugins/username/error-codes.d.mts +5 -0
- package/dist/plugins/username/error-codes.mjs +17 -0
- package/dist/plugins/username/error-codes.mjs.map +1 -0
- package/dist/plugins/username/index.d.mts +74 -0
- package/dist/plugins/username/index.mjs +237 -0
- package/dist/plugins/username/index.mjs.map +1 -0
- package/dist/plugins/username/schema.d.mts +9 -0
- package/dist/plugins/username/schema.mjs +26 -0
- package/dist/plugins/username/schema.mjs.map +1 -0
- package/dist/social-providers/index.d.mts +1 -0
- package/dist/social-providers/index.mjs +3 -0
- package/dist/state.d.mts +42 -0
- package/dist/state.mjs +107 -0
- package/dist/state.mjs.map +1 -0
- package/dist/test-utils/headers.d.mts +9 -0
- package/dist/test-utils/headers.mjs +24 -0
- package/dist/test-utils/headers.mjs.map +1 -0
- package/dist/test-utils/index.d.mts +3 -0
- package/dist/test-utils/index.mjs +4 -0
- package/dist/test-utils/test-instance.d.mts +181 -0
- package/dist/test-utils/test-instance.mjs +210 -0
- package/dist/test-utils/test-instance.mjs.map +1 -0
- package/dist/types/adapter.d.mts +24 -0
- package/dist/types/api.d.mts +62 -0
- package/dist/types/auth.d.mts +30 -0
- package/dist/types/helper.d.mts +21 -0
- package/dist/types/index.d.mts +11 -0
- package/dist/types/index.mjs +1 -0
- package/dist/types/models.d.mts +17 -0
- package/dist/types/plugins.d.mts +16 -0
- package/dist/utils/boolean.mjs +8 -0
- package/dist/utils/boolean.mjs.map +1 -0
- package/dist/utils/constants.mjs +6 -0
- package/dist/utils/constants.mjs.map +1 -0
- package/dist/utils/date.mjs +8 -0
- package/dist/utils/date.mjs.map +1 -0
- package/dist/utils/get-request-ip.d.mts +7 -0
- package/dist/utils/get-request-ip.mjs +23 -0
- package/dist/utils/get-request-ip.mjs.map +1 -0
- package/dist/utils/hashing.mjs +21 -0
- package/dist/utils/hashing.mjs.map +1 -0
- package/dist/utils/hide-metadata.d.mts +7 -0
- package/dist/utils/hide-metadata.mjs +6 -0
- package/dist/utils/hide-metadata.mjs.map +1 -0
- package/dist/utils/index.d.mts +3 -0
- package/dist/utils/index.mjs +5 -0
- package/dist/utils/is-api-error.d.mts +7 -0
- package/dist/utils/is-api-error.mjs +11 -0
- package/dist/utils/is-api-error.mjs.map +1 -0
- package/dist/utils/is-atom.mjs +8 -0
- package/dist/utils/is-atom.mjs.map +1 -0
- package/dist/utils/is-promise.mjs +8 -0
- package/dist/utils/is-promise.mjs.map +1 -0
- package/dist/utils/middleware-response.mjs +6 -0
- package/dist/utils/middleware-response.mjs.map +1 -0
- package/dist/utils/password.mjs +26 -0
- package/dist/utils/password.mjs.map +1 -0
- package/dist/utils/plugin-helper.mjs +17 -0
- package/dist/utils/plugin-helper.mjs.map +1 -0
- package/dist/utils/shim.mjs +24 -0
- package/dist/utils/shim.mjs.map +1 -0
- package/dist/utils/time.d.mts +49 -0
- package/dist/utils/time.mjs +100 -0
- package/dist/utils/time.mjs.map +1 -0
- package/dist/utils/url.mjs +92 -0
- package/dist/utils/url.mjs.map +1 -0
- package/dist/utils/wildcard.mjs +108 -0
- package/dist/utils/wildcard.mjs.map +1 -0
- package/package.json +601 -0
|
@@ -0,0 +1,656 @@
|
|
|
1
|
+
import { toZodSchema } from "../../../db/to-zod.mjs";
|
|
2
|
+
import "../../../db/index.mjs";
|
|
3
|
+
import { ORGANIZATION_ERROR_CODES } from "../error-codes.mjs";
|
|
4
|
+
import { orgSessionMiddleware } from "../call.mjs";
|
|
5
|
+
import { hasPermission } from "../has-permission.mjs";
|
|
6
|
+
import { APIError } from "@better-auth/core/error";
|
|
7
|
+
import { createAuthEndpoint } from "@better-auth/core/api";
|
|
8
|
+
import * as z from "zod";
|
|
9
|
+
|
|
10
|
+
//#region src/plugins/organization/routes/crud-access-control.ts
|
|
11
|
+
const normalizeRoleName = (role) => role.toLowerCase();
|
|
12
|
+
const DEFAULT_MAXIMUM_ROLES_PER_ORGANIZATION = Number.POSITIVE_INFINITY;
|
|
13
|
+
const getAdditionalFields = (options, shouldBePartial = false) => {
|
|
14
|
+
const additionalFields = options?.schema?.organizationRole?.additionalFields || {};
|
|
15
|
+
if (shouldBePartial) for (const key in additionalFields) additionalFields[key].required = false;
|
|
16
|
+
return {
|
|
17
|
+
additionalFieldsSchema: toZodSchema({
|
|
18
|
+
fields: additionalFields,
|
|
19
|
+
isClientSide: true
|
|
20
|
+
}),
|
|
21
|
+
$AdditionalFields: {},
|
|
22
|
+
$ReturnAdditionalFields: {}
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
const baseCreateOrgRoleSchema = z.object({
|
|
26
|
+
organizationId: z.string().optional().meta({ description: "The id of the organization to create the role in. If not provided, the user's active organization will be used." }),
|
|
27
|
+
role: z.string().meta({ description: "The name of the role to create" }),
|
|
28
|
+
permission: z.record(z.string(), z.array(z.string())).meta({ description: "The permission to assign to the role" })
|
|
29
|
+
});
|
|
30
|
+
const createOrgRole = (options) => {
|
|
31
|
+
const { additionalFieldsSchema, $AdditionalFields, $ReturnAdditionalFields } = getAdditionalFields(options, false);
|
|
32
|
+
return createAuthEndpoint("/organization/create-role", {
|
|
33
|
+
method: "POST",
|
|
34
|
+
body: baseCreateOrgRoleSchema.safeExtend({ additionalFields: z.object({ ...additionalFieldsSchema.shape }).optional() }),
|
|
35
|
+
metadata: { $Infer: { body: {} } },
|
|
36
|
+
requireHeaders: true,
|
|
37
|
+
use: [orgSessionMiddleware]
|
|
38
|
+
}, async (ctx) => {
|
|
39
|
+
const { session, user } = ctx.context.session;
|
|
40
|
+
let roleName = ctx.body.role;
|
|
41
|
+
const permission = ctx.body.permission;
|
|
42
|
+
const additionalFields = ctx.body.additionalFields;
|
|
43
|
+
const ac = options.ac;
|
|
44
|
+
if (!ac) {
|
|
45
|
+
ctx.context.logger.error(`[Dynamic Access Control] The organization plugin is missing a pre-defined ac instance.`, `\nPlease refer to the documentation here: https://better-auth.com/docs/plugins/organization#dynamic-access-control`);
|
|
46
|
+
throw APIError.from("NOT_IMPLEMENTED", ORGANIZATION_ERROR_CODES.MISSING_AC_INSTANCE);
|
|
47
|
+
}
|
|
48
|
+
const organizationId = ctx.body.organizationId ?? session.activeOrganizationId;
|
|
49
|
+
if (!organizationId) {
|
|
50
|
+
ctx.context.logger.error(`[Dynamic Access Control] The session is missing an active organization id to create a role. Either set an active org id, or pass an organizationId in the request body.`);
|
|
51
|
+
throw APIError.from("BAD_REQUEST", ORGANIZATION_ERROR_CODES.YOU_MUST_BE_IN_AN_ORGANIZATION_TO_CREATE_A_ROLE);
|
|
52
|
+
}
|
|
53
|
+
roleName = normalizeRoleName(roleName);
|
|
54
|
+
await checkIfRoleNameIsTakenByPreDefinedRole({
|
|
55
|
+
role: roleName,
|
|
56
|
+
organizationId,
|
|
57
|
+
options,
|
|
58
|
+
ctx
|
|
59
|
+
});
|
|
60
|
+
const member = await ctx.context.adapter.findOne({
|
|
61
|
+
model: "member",
|
|
62
|
+
where: [{
|
|
63
|
+
field: "organizationId",
|
|
64
|
+
value: organizationId,
|
|
65
|
+
operator: "eq",
|
|
66
|
+
connector: "AND"
|
|
67
|
+
}, {
|
|
68
|
+
field: "userId",
|
|
69
|
+
value: user.id,
|
|
70
|
+
operator: "eq",
|
|
71
|
+
connector: "AND"
|
|
72
|
+
}]
|
|
73
|
+
});
|
|
74
|
+
if (!member) {
|
|
75
|
+
ctx.context.logger.error(`[Dynamic Access Control] The user is not a member of the organization to create a role.`, {
|
|
76
|
+
userId: user.id,
|
|
77
|
+
organizationId
|
|
78
|
+
});
|
|
79
|
+
throw APIError.from("FORBIDDEN", ORGANIZATION_ERROR_CODES.YOU_ARE_NOT_A_MEMBER_OF_THIS_ORGANIZATION);
|
|
80
|
+
}
|
|
81
|
+
if (!await hasPermission({
|
|
82
|
+
options,
|
|
83
|
+
organizationId,
|
|
84
|
+
permissions: { ac: ["create"] },
|
|
85
|
+
role: member.role
|
|
86
|
+
}, ctx)) {
|
|
87
|
+
ctx.context.logger.error(`[Dynamic Access Control] The user is not permitted to create a role. If this is unexpected, please make sure the role associated to that member has the "ac" resource with the "create" permission.`, {
|
|
88
|
+
userId: user.id,
|
|
89
|
+
organizationId,
|
|
90
|
+
role: member.role
|
|
91
|
+
});
|
|
92
|
+
throw APIError.from("FORBIDDEN", ORGANIZATION_ERROR_CODES.YOU_ARE_NOT_ALLOWED_TO_CREATE_A_ROLE);
|
|
93
|
+
}
|
|
94
|
+
const maximumRolesPerOrganization = typeof options.dynamicAccessControl?.maximumRolesPerOrganization === "function" ? await options.dynamicAccessControl.maximumRolesPerOrganization(organizationId) : options.dynamicAccessControl?.maximumRolesPerOrganization ?? DEFAULT_MAXIMUM_ROLES_PER_ORGANIZATION;
|
|
95
|
+
const rolesInDB = await ctx.context.adapter.count({
|
|
96
|
+
model: "organizationRole",
|
|
97
|
+
where: [{
|
|
98
|
+
field: "organizationId",
|
|
99
|
+
value: organizationId,
|
|
100
|
+
operator: "eq",
|
|
101
|
+
connector: "AND"
|
|
102
|
+
}]
|
|
103
|
+
});
|
|
104
|
+
if (rolesInDB >= maximumRolesPerOrganization) {
|
|
105
|
+
ctx.context.logger.error(`[Dynamic Access Control] Failed to create a new role, the organization has too many roles. Maximum allowed roles is ${maximumRolesPerOrganization}.`, {
|
|
106
|
+
organizationId,
|
|
107
|
+
maximumRolesPerOrganization,
|
|
108
|
+
rolesInDB
|
|
109
|
+
});
|
|
110
|
+
throw APIError.from("BAD_REQUEST", ORGANIZATION_ERROR_CODES.TOO_MANY_ROLES);
|
|
111
|
+
}
|
|
112
|
+
await checkForInvalidResources({
|
|
113
|
+
ac,
|
|
114
|
+
ctx,
|
|
115
|
+
permission
|
|
116
|
+
});
|
|
117
|
+
await checkIfMemberHasPermission({
|
|
118
|
+
ctx,
|
|
119
|
+
member,
|
|
120
|
+
options,
|
|
121
|
+
organizationId,
|
|
122
|
+
permissionRequired: permission,
|
|
123
|
+
user,
|
|
124
|
+
action: "create"
|
|
125
|
+
});
|
|
126
|
+
await checkIfRoleNameIsTakenByRoleInDB({
|
|
127
|
+
ctx,
|
|
128
|
+
organizationId,
|
|
129
|
+
role: roleName
|
|
130
|
+
});
|
|
131
|
+
const newRole = ac.newRole(permission);
|
|
132
|
+
const data = {
|
|
133
|
+
...await ctx.context.adapter.create({
|
|
134
|
+
model: "organizationRole",
|
|
135
|
+
data: {
|
|
136
|
+
createdAt: /* @__PURE__ */ new Date(),
|
|
137
|
+
organizationId,
|
|
138
|
+
permission: JSON.stringify(permission),
|
|
139
|
+
role: roleName,
|
|
140
|
+
...additionalFields
|
|
141
|
+
}
|
|
142
|
+
}),
|
|
143
|
+
permission
|
|
144
|
+
};
|
|
145
|
+
return ctx.json({
|
|
146
|
+
success: true,
|
|
147
|
+
roleData: data,
|
|
148
|
+
statements: newRole.statements
|
|
149
|
+
});
|
|
150
|
+
});
|
|
151
|
+
};
|
|
152
|
+
const deleteOrgRoleBodySchema = z.object({ organizationId: z.string().optional().meta({ description: "The id of the organization to create the role in. If not provided, the user's active organization will be used." }) }).and(z.union([z.object({ roleName: z.string().nonempty().meta({ description: "The name of the role to delete" }) }), z.object({ roleId: z.string().nonempty().meta({ description: "The id of the role to delete" }) })]));
|
|
153
|
+
const deleteOrgRole = (options) => {
|
|
154
|
+
return createAuthEndpoint("/organization/delete-role", {
|
|
155
|
+
method: "POST",
|
|
156
|
+
body: deleteOrgRoleBodySchema,
|
|
157
|
+
requireHeaders: true,
|
|
158
|
+
use: [orgSessionMiddleware],
|
|
159
|
+
metadata: { $Infer: { body: {} } }
|
|
160
|
+
}, async (ctx) => {
|
|
161
|
+
const { session, user } = ctx.context.session;
|
|
162
|
+
const organizationId = ctx.body.organizationId ?? session.activeOrganizationId;
|
|
163
|
+
if (!organizationId) {
|
|
164
|
+
ctx.context.logger.error(`[Dynamic Access Control] The session is missing an active organization id to delete a role. Either set an active org id, or pass an organizationId in the request body.`);
|
|
165
|
+
throw APIError.from("BAD_REQUEST", ORGANIZATION_ERROR_CODES.NO_ACTIVE_ORGANIZATION);
|
|
166
|
+
}
|
|
167
|
+
const member = await ctx.context.adapter.findOne({
|
|
168
|
+
model: "member",
|
|
169
|
+
where: [{
|
|
170
|
+
field: "organizationId",
|
|
171
|
+
value: organizationId,
|
|
172
|
+
operator: "eq",
|
|
173
|
+
connector: "AND"
|
|
174
|
+
}, {
|
|
175
|
+
field: "userId",
|
|
176
|
+
value: user.id,
|
|
177
|
+
operator: "eq",
|
|
178
|
+
connector: "AND"
|
|
179
|
+
}]
|
|
180
|
+
});
|
|
181
|
+
if (!member) {
|
|
182
|
+
ctx.context.logger.error(`[Dynamic Access Control] The user is not a member of the organization to delete a role.`, {
|
|
183
|
+
userId: user.id,
|
|
184
|
+
organizationId
|
|
185
|
+
});
|
|
186
|
+
throw APIError.from("FORBIDDEN", ORGANIZATION_ERROR_CODES.YOU_ARE_NOT_A_MEMBER_OF_THIS_ORGANIZATION);
|
|
187
|
+
}
|
|
188
|
+
if (!await hasPermission({
|
|
189
|
+
options,
|
|
190
|
+
organizationId,
|
|
191
|
+
permissions: { ac: ["delete"] },
|
|
192
|
+
role: member.role
|
|
193
|
+
}, ctx)) {
|
|
194
|
+
ctx.context.logger.error(`[Dynamic Access Control] The user is not permitted to delete a role. If this is unexpected, please make sure the role associated to that member has the "ac" resource with the "delete" permission.`, {
|
|
195
|
+
userId: user.id,
|
|
196
|
+
organizationId,
|
|
197
|
+
role: member.role
|
|
198
|
+
});
|
|
199
|
+
throw APIError.from("FORBIDDEN", ORGANIZATION_ERROR_CODES.YOU_ARE_NOT_ALLOWED_TO_DELETE_A_ROLE);
|
|
200
|
+
}
|
|
201
|
+
if (ctx.body.roleName) {
|
|
202
|
+
const roleName = ctx.body.roleName;
|
|
203
|
+
const defaultRoles = options.roles ? Object.keys(options.roles) : [
|
|
204
|
+
"owner",
|
|
205
|
+
"admin",
|
|
206
|
+
"member"
|
|
207
|
+
];
|
|
208
|
+
if (defaultRoles.includes(roleName)) {
|
|
209
|
+
ctx.context.logger.error(`[Dynamic Access Control] Cannot delete a pre-defined role.`, {
|
|
210
|
+
roleName,
|
|
211
|
+
organizationId,
|
|
212
|
+
defaultRoles
|
|
213
|
+
});
|
|
214
|
+
throw APIError.from("BAD_REQUEST", ORGANIZATION_ERROR_CODES.CANNOT_DELETE_A_PRE_DEFINED_ROLE);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
let condition;
|
|
218
|
+
if (ctx.body.roleName) condition = {
|
|
219
|
+
field: "role",
|
|
220
|
+
value: ctx.body.roleName,
|
|
221
|
+
operator: "eq",
|
|
222
|
+
connector: "AND"
|
|
223
|
+
};
|
|
224
|
+
else if (ctx.body.roleId) condition = {
|
|
225
|
+
field: "id",
|
|
226
|
+
value: ctx.body.roleId,
|
|
227
|
+
operator: "eq",
|
|
228
|
+
connector: "AND"
|
|
229
|
+
};
|
|
230
|
+
else {
|
|
231
|
+
ctx.context.logger.error(`[Dynamic Access Control] The role name/id is not provided in the request body.`);
|
|
232
|
+
throw APIError.from("BAD_REQUEST", ORGANIZATION_ERROR_CODES.ROLE_NOT_FOUND);
|
|
233
|
+
}
|
|
234
|
+
const existingRoleInDB = await ctx.context.adapter.findOne({
|
|
235
|
+
model: "organizationRole",
|
|
236
|
+
where: [{
|
|
237
|
+
field: "organizationId",
|
|
238
|
+
value: organizationId,
|
|
239
|
+
operator: "eq",
|
|
240
|
+
connector: "AND"
|
|
241
|
+
}, condition]
|
|
242
|
+
});
|
|
243
|
+
if (!existingRoleInDB) {
|
|
244
|
+
ctx.context.logger.error(`[Dynamic Access Control] The role name/id does not exist in the database.`, {
|
|
245
|
+
..."roleName" in ctx.body ? { roleName: ctx.body.roleName } : { roleId: ctx.body.roleId },
|
|
246
|
+
organizationId
|
|
247
|
+
});
|
|
248
|
+
throw APIError.from("BAD_REQUEST", ORGANIZATION_ERROR_CODES.ROLE_NOT_FOUND);
|
|
249
|
+
}
|
|
250
|
+
existingRoleInDB.permission = JSON.parse(existingRoleInDB.permission);
|
|
251
|
+
await ctx.context.adapter.delete({
|
|
252
|
+
model: "organizationRole",
|
|
253
|
+
where: [{
|
|
254
|
+
field: "organizationId",
|
|
255
|
+
value: organizationId,
|
|
256
|
+
operator: "eq",
|
|
257
|
+
connector: "AND"
|
|
258
|
+
}, condition]
|
|
259
|
+
});
|
|
260
|
+
return ctx.json({ success: true });
|
|
261
|
+
});
|
|
262
|
+
};
|
|
263
|
+
const listOrgRolesQuerySchema = z.object({ organizationId: z.string().optional().meta({ description: "The id of the organization to list roles for. If not provided, the user's active organization will be used." }) }).optional();
|
|
264
|
+
const listOrgRoles = (options) => {
|
|
265
|
+
const { $ReturnAdditionalFields } = getAdditionalFields(options, false);
|
|
266
|
+
return createAuthEndpoint("/organization/list-roles", {
|
|
267
|
+
method: "GET",
|
|
268
|
+
requireHeaders: true,
|
|
269
|
+
use: [orgSessionMiddleware],
|
|
270
|
+
query: listOrgRolesQuerySchema
|
|
271
|
+
}, async (ctx) => {
|
|
272
|
+
const { session, user } = ctx.context.session;
|
|
273
|
+
const organizationId = ctx.query?.organizationId ?? session.activeOrganizationId;
|
|
274
|
+
if (!organizationId) {
|
|
275
|
+
ctx.context.logger.error(`[Dynamic Access Control] The session is missing an active organization id to list roles. Either set an active org id, or pass an organizationId in the request query.`);
|
|
276
|
+
throw APIError.from("BAD_REQUEST", ORGANIZATION_ERROR_CODES.NO_ACTIVE_ORGANIZATION);
|
|
277
|
+
}
|
|
278
|
+
const member = await ctx.context.adapter.findOne({
|
|
279
|
+
model: "member",
|
|
280
|
+
where: [{
|
|
281
|
+
field: "organizationId",
|
|
282
|
+
value: organizationId,
|
|
283
|
+
operator: "eq",
|
|
284
|
+
connector: "AND"
|
|
285
|
+
}, {
|
|
286
|
+
field: "userId",
|
|
287
|
+
value: user.id,
|
|
288
|
+
operator: "eq",
|
|
289
|
+
connector: "AND"
|
|
290
|
+
}]
|
|
291
|
+
});
|
|
292
|
+
if (!member) {
|
|
293
|
+
ctx.context.logger.error(`[Dynamic Access Control] The user is not a member of the organization to list roles.`, {
|
|
294
|
+
userId: user.id,
|
|
295
|
+
organizationId
|
|
296
|
+
});
|
|
297
|
+
throw APIError.from("FORBIDDEN", ORGANIZATION_ERROR_CODES.YOU_ARE_NOT_A_MEMBER_OF_THIS_ORGANIZATION);
|
|
298
|
+
}
|
|
299
|
+
if (!await hasPermission({
|
|
300
|
+
options,
|
|
301
|
+
organizationId,
|
|
302
|
+
permissions: { ac: ["read"] },
|
|
303
|
+
role: member.role
|
|
304
|
+
}, ctx)) {
|
|
305
|
+
ctx.context.logger.error(`[Dynamic Access Control] The user is not permitted to list roles.`, {
|
|
306
|
+
userId: user.id,
|
|
307
|
+
organizationId,
|
|
308
|
+
role: member.role
|
|
309
|
+
});
|
|
310
|
+
throw APIError.from("FORBIDDEN", ORGANIZATION_ERROR_CODES.YOU_ARE_NOT_ALLOWED_TO_LIST_A_ROLE);
|
|
311
|
+
}
|
|
312
|
+
let roles = await ctx.context.adapter.findMany({
|
|
313
|
+
model: "organizationRole",
|
|
314
|
+
where: [{
|
|
315
|
+
field: "organizationId",
|
|
316
|
+
value: organizationId,
|
|
317
|
+
operator: "eq",
|
|
318
|
+
connector: "AND"
|
|
319
|
+
}]
|
|
320
|
+
});
|
|
321
|
+
roles = roles.map((x) => ({
|
|
322
|
+
...x,
|
|
323
|
+
permission: JSON.parse(x.permission)
|
|
324
|
+
}));
|
|
325
|
+
return ctx.json(roles);
|
|
326
|
+
});
|
|
327
|
+
};
|
|
328
|
+
const getOrgRoleQuerySchema = z.object({ organizationId: z.string().optional().meta({ description: "The id of the organization to read a role for. If not provided, the user's active organization will be used." }) }).and(z.union([z.object({ roleName: z.string().nonempty().meta({ description: "The name of the role to read" }) }), z.object({ roleId: z.string().nonempty().meta({ description: "The id of the role to read" }) })])).optional();
|
|
329
|
+
const getOrgRole = (options) => {
|
|
330
|
+
const { $ReturnAdditionalFields } = getAdditionalFields(options, false);
|
|
331
|
+
return createAuthEndpoint("/organization/get-role", {
|
|
332
|
+
method: "GET",
|
|
333
|
+
requireHeaders: true,
|
|
334
|
+
use: [orgSessionMiddleware],
|
|
335
|
+
query: getOrgRoleQuerySchema,
|
|
336
|
+
metadata: { $Infer: { query: {} } }
|
|
337
|
+
}, async (ctx) => {
|
|
338
|
+
const { session, user } = ctx.context.session;
|
|
339
|
+
const organizationId = ctx.query?.organizationId ?? session.activeOrganizationId;
|
|
340
|
+
if (!organizationId) {
|
|
341
|
+
ctx.context.logger.error(`[Dynamic Access Control] The session is missing an active organization id to read a role. Either set an active org id, or pass an organizationId in the request query.`);
|
|
342
|
+
throw APIError.from("BAD_REQUEST", ORGANIZATION_ERROR_CODES.NO_ACTIVE_ORGANIZATION);
|
|
343
|
+
}
|
|
344
|
+
const member = await ctx.context.adapter.findOne({
|
|
345
|
+
model: "member",
|
|
346
|
+
where: [{
|
|
347
|
+
field: "organizationId",
|
|
348
|
+
value: organizationId,
|
|
349
|
+
operator: "eq",
|
|
350
|
+
connector: "AND"
|
|
351
|
+
}, {
|
|
352
|
+
field: "userId",
|
|
353
|
+
value: user.id,
|
|
354
|
+
operator: "eq",
|
|
355
|
+
connector: "AND"
|
|
356
|
+
}]
|
|
357
|
+
});
|
|
358
|
+
if (!member) {
|
|
359
|
+
ctx.context.logger.error(`[Dynamic Access Control] The user is not a member of the organization to read a role.`, {
|
|
360
|
+
userId: user.id,
|
|
361
|
+
organizationId
|
|
362
|
+
});
|
|
363
|
+
throw APIError.from("FORBIDDEN", ORGANIZATION_ERROR_CODES.YOU_ARE_NOT_A_MEMBER_OF_THIS_ORGANIZATION);
|
|
364
|
+
}
|
|
365
|
+
if (!await hasPermission({
|
|
366
|
+
options,
|
|
367
|
+
organizationId,
|
|
368
|
+
permissions: { ac: ["read"] },
|
|
369
|
+
role: member.role
|
|
370
|
+
}, ctx)) {
|
|
371
|
+
ctx.context.logger.error(`[Dynamic Access Control] The user is not permitted to read a role.`, {
|
|
372
|
+
userId: user.id,
|
|
373
|
+
organizationId,
|
|
374
|
+
role: member.role
|
|
375
|
+
});
|
|
376
|
+
throw APIError.from("FORBIDDEN", ORGANIZATION_ERROR_CODES.YOU_ARE_NOT_ALLOWED_TO_READ_A_ROLE);
|
|
377
|
+
}
|
|
378
|
+
let condition;
|
|
379
|
+
if (ctx.query.roleName) condition = {
|
|
380
|
+
field: "role",
|
|
381
|
+
value: ctx.query.roleName,
|
|
382
|
+
operator: "eq",
|
|
383
|
+
connector: "AND"
|
|
384
|
+
};
|
|
385
|
+
else if (ctx.query.roleId) condition = {
|
|
386
|
+
field: "id",
|
|
387
|
+
value: ctx.query.roleId,
|
|
388
|
+
operator: "eq",
|
|
389
|
+
connector: "AND"
|
|
390
|
+
};
|
|
391
|
+
else {
|
|
392
|
+
ctx.context.logger.error(`[Dynamic Access Control] The role name/id is not provided in the request query.`);
|
|
393
|
+
throw APIError.from("BAD_REQUEST", ORGANIZATION_ERROR_CODES.ROLE_NOT_FOUND);
|
|
394
|
+
}
|
|
395
|
+
const role = await ctx.context.adapter.findOne({
|
|
396
|
+
model: "organizationRole",
|
|
397
|
+
where: [{
|
|
398
|
+
field: "organizationId",
|
|
399
|
+
value: organizationId,
|
|
400
|
+
operator: "eq",
|
|
401
|
+
connector: "AND"
|
|
402
|
+
}, condition]
|
|
403
|
+
});
|
|
404
|
+
if (!role) {
|
|
405
|
+
ctx.context.logger.error(`[Dynamic Access Control] The role name/id does not exist in the database.`, {
|
|
406
|
+
..."roleName" in ctx.query ? { roleName: ctx.query.roleName } : { roleId: ctx.query.roleId },
|
|
407
|
+
organizationId
|
|
408
|
+
});
|
|
409
|
+
throw APIError.from("BAD_REQUEST", ORGANIZATION_ERROR_CODES.ROLE_NOT_FOUND);
|
|
410
|
+
}
|
|
411
|
+
role.permission = JSON.parse(role.permission);
|
|
412
|
+
return ctx.json(role);
|
|
413
|
+
});
|
|
414
|
+
};
|
|
415
|
+
const roleNameOrIdSchema = z.union([z.object({ roleName: z.string().nonempty().meta({ description: "The name of the role to update" }) }), z.object({ roleId: z.string().nonempty().meta({ description: "The id of the role to update" }) })]);
|
|
416
|
+
const updateOrgRole = (options) => {
|
|
417
|
+
const { additionalFieldsSchema, $AdditionalFields, $ReturnAdditionalFields } = getAdditionalFields(options, true);
|
|
418
|
+
return createAuthEndpoint("/organization/update-role", {
|
|
419
|
+
method: "POST",
|
|
420
|
+
body: z.object({
|
|
421
|
+
organizationId: z.string().optional().meta({ description: "The id of the organization to update the role in. If not provided, the user's active organization will be used." }),
|
|
422
|
+
data: z.object({
|
|
423
|
+
permission: z.record(z.string(), z.array(z.string())).optional().meta({ description: "The permission to update the role with" }),
|
|
424
|
+
roleName: z.string().optional().meta({ description: "The name of the role to update" }),
|
|
425
|
+
...additionalFieldsSchema.shape
|
|
426
|
+
})
|
|
427
|
+
}).and(roleNameOrIdSchema),
|
|
428
|
+
metadata: { $Infer: { body: {} } },
|
|
429
|
+
requireHeaders: true,
|
|
430
|
+
use: [orgSessionMiddleware]
|
|
431
|
+
}, async (ctx) => {
|
|
432
|
+
const { session, user } = ctx.context.session;
|
|
433
|
+
const ac = options.ac;
|
|
434
|
+
if (!ac) {
|
|
435
|
+
ctx.context.logger.error(`[Dynamic Access Control] The organization plugin is missing a pre-defined ac instance.`, `\nPlease refer to the documentation here: https://better-auth.com/docs/plugins/organization#dynamic-access-control`);
|
|
436
|
+
throw APIError.from("NOT_IMPLEMENTED", ORGANIZATION_ERROR_CODES.MISSING_AC_INSTANCE);
|
|
437
|
+
}
|
|
438
|
+
const organizationId = ctx.body.organizationId ?? session.activeOrganizationId;
|
|
439
|
+
if (!organizationId) {
|
|
440
|
+
ctx.context.logger.error(`[Dynamic Access Control] The session is missing an active organization id to update a role. Either set an active org id, or pass an organizationId in the request body.`);
|
|
441
|
+
throw APIError.from("BAD_REQUEST", ORGANIZATION_ERROR_CODES.NO_ACTIVE_ORGANIZATION);
|
|
442
|
+
}
|
|
443
|
+
const member = await ctx.context.adapter.findOne({
|
|
444
|
+
model: "member",
|
|
445
|
+
where: [{
|
|
446
|
+
field: "organizationId",
|
|
447
|
+
value: organizationId,
|
|
448
|
+
operator: "eq",
|
|
449
|
+
connector: "AND"
|
|
450
|
+
}, {
|
|
451
|
+
field: "userId",
|
|
452
|
+
value: user.id,
|
|
453
|
+
operator: "eq",
|
|
454
|
+
connector: "AND"
|
|
455
|
+
}]
|
|
456
|
+
});
|
|
457
|
+
if (!member) {
|
|
458
|
+
ctx.context.logger.error(`[Dynamic Access Control] The user is not a member of the organization to update a role.`, {
|
|
459
|
+
userId: user.id,
|
|
460
|
+
organizationId
|
|
461
|
+
});
|
|
462
|
+
throw APIError.from("FORBIDDEN", ORGANIZATION_ERROR_CODES.YOU_ARE_NOT_A_MEMBER_OF_THIS_ORGANIZATION);
|
|
463
|
+
}
|
|
464
|
+
if (!await hasPermission({
|
|
465
|
+
options,
|
|
466
|
+
organizationId,
|
|
467
|
+
role: member.role,
|
|
468
|
+
permissions: { ac: ["update"] }
|
|
469
|
+
}, ctx)) {
|
|
470
|
+
ctx.context.logger.error(`[Dynamic Access Control] The user is not permitted to update a role.`);
|
|
471
|
+
throw APIError.from("FORBIDDEN", ORGANIZATION_ERROR_CODES.YOU_ARE_NOT_ALLOWED_TO_UPDATE_A_ROLE);
|
|
472
|
+
}
|
|
473
|
+
let condition;
|
|
474
|
+
if (ctx.body.roleName) condition = {
|
|
475
|
+
field: "role",
|
|
476
|
+
value: ctx.body.roleName,
|
|
477
|
+
operator: "eq",
|
|
478
|
+
connector: "AND"
|
|
479
|
+
};
|
|
480
|
+
else if (ctx.body.roleId) condition = {
|
|
481
|
+
field: "id",
|
|
482
|
+
value: ctx.body.roleId,
|
|
483
|
+
operator: "eq",
|
|
484
|
+
connector: "AND"
|
|
485
|
+
};
|
|
486
|
+
else {
|
|
487
|
+
ctx.context.logger.error(`[Dynamic Access Control] The role name/id is not provided in the request body.`);
|
|
488
|
+
throw APIError.from("BAD_REQUEST", ORGANIZATION_ERROR_CODES.ROLE_NOT_FOUND);
|
|
489
|
+
}
|
|
490
|
+
const role = await ctx.context.adapter.findOne({
|
|
491
|
+
model: "organizationRole",
|
|
492
|
+
where: [{
|
|
493
|
+
field: "organizationId",
|
|
494
|
+
value: organizationId,
|
|
495
|
+
operator: "eq",
|
|
496
|
+
connector: "AND"
|
|
497
|
+
}, condition]
|
|
498
|
+
});
|
|
499
|
+
if (!role) {
|
|
500
|
+
ctx.context.logger.error(`[Dynamic Access Control] The role name/id does not exist in the database.`, {
|
|
501
|
+
..."roleName" in ctx.body ? { roleName: ctx.body.roleName } : { roleId: ctx.body.roleId },
|
|
502
|
+
organizationId
|
|
503
|
+
});
|
|
504
|
+
throw APIError.from("BAD_REQUEST", ORGANIZATION_ERROR_CODES.ROLE_NOT_FOUND);
|
|
505
|
+
}
|
|
506
|
+
role.permission = role.permission ? JSON.parse(role.permission) : void 0;
|
|
507
|
+
const { permission: _, roleName: __, ...additionalFields } = ctx.body.data;
|
|
508
|
+
const updateData = { ...additionalFields };
|
|
509
|
+
if (ctx.body.data.permission) {
|
|
510
|
+
const newPermission = ctx.body.data.permission;
|
|
511
|
+
await checkForInvalidResources({
|
|
512
|
+
ac,
|
|
513
|
+
ctx,
|
|
514
|
+
permission: newPermission
|
|
515
|
+
});
|
|
516
|
+
await checkIfMemberHasPermission({
|
|
517
|
+
ctx,
|
|
518
|
+
member,
|
|
519
|
+
options,
|
|
520
|
+
organizationId,
|
|
521
|
+
permissionRequired: newPermission,
|
|
522
|
+
user,
|
|
523
|
+
action: "update"
|
|
524
|
+
});
|
|
525
|
+
updateData.permission = newPermission;
|
|
526
|
+
}
|
|
527
|
+
if (ctx.body.data.roleName) {
|
|
528
|
+
let newRoleName = ctx.body.data.roleName;
|
|
529
|
+
newRoleName = normalizeRoleName(newRoleName);
|
|
530
|
+
await checkIfRoleNameIsTakenByPreDefinedRole({
|
|
531
|
+
role: newRoleName,
|
|
532
|
+
organizationId,
|
|
533
|
+
options,
|
|
534
|
+
ctx
|
|
535
|
+
});
|
|
536
|
+
await checkIfRoleNameIsTakenByRoleInDB({
|
|
537
|
+
role: newRoleName,
|
|
538
|
+
organizationId,
|
|
539
|
+
ctx
|
|
540
|
+
});
|
|
541
|
+
updateData.role = newRoleName;
|
|
542
|
+
}
|
|
543
|
+
const update = {
|
|
544
|
+
...updateData,
|
|
545
|
+
...updateData.permission ? { permission: JSON.stringify(updateData.permission) } : {}
|
|
546
|
+
};
|
|
547
|
+
await ctx.context.adapter.update({
|
|
548
|
+
model: "organizationRole",
|
|
549
|
+
where: [{
|
|
550
|
+
field: "organizationId",
|
|
551
|
+
value: organizationId,
|
|
552
|
+
operator: "eq",
|
|
553
|
+
connector: "AND"
|
|
554
|
+
}, condition],
|
|
555
|
+
update
|
|
556
|
+
});
|
|
557
|
+
return ctx.json({
|
|
558
|
+
success: true,
|
|
559
|
+
roleData: {
|
|
560
|
+
...role,
|
|
561
|
+
...update,
|
|
562
|
+
permission: updateData.permission || role.permission || null
|
|
563
|
+
}
|
|
564
|
+
});
|
|
565
|
+
});
|
|
566
|
+
};
|
|
567
|
+
async function checkForInvalidResources({ ac, ctx, permission }) {
|
|
568
|
+
const validResources = Object.keys(ac.statements);
|
|
569
|
+
const providedResources = Object.keys(permission);
|
|
570
|
+
if (providedResources.some((r) => !validResources.includes(r))) {
|
|
571
|
+
ctx.context.logger.error(`[Dynamic Access Control] The provided permission includes an invalid resource.`, {
|
|
572
|
+
providedResources,
|
|
573
|
+
validResources
|
|
574
|
+
});
|
|
575
|
+
throw APIError.from("BAD_REQUEST", ORGANIZATION_ERROR_CODES.INVALID_RESOURCE);
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
async function checkIfMemberHasPermission({ ctx, permissionRequired: permission, options, organizationId, member, user, action }) {
|
|
579
|
+
const hasNecessaryPermissions = [];
|
|
580
|
+
const permissionEntries = Object.entries(permission);
|
|
581
|
+
for await (const [resource, permissions] of permissionEntries) for await (const perm of permissions) hasNecessaryPermissions.push({
|
|
582
|
+
resource: { [resource]: [perm] },
|
|
583
|
+
hasPermission: await hasPermission({
|
|
584
|
+
options,
|
|
585
|
+
organizationId,
|
|
586
|
+
permissions: { [resource]: [perm] },
|
|
587
|
+
useMemoryCache: true,
|
|
588
|
+
role: member.role
|
|
589
|
+
}, ctx)
|
|
590
|
+
});
|
|
591
|
+
const missingPermissions = hasNecessaryPermissions.filter((x) => x.hasPermission === false).map((x) => {
|
|
592
|
+
const key = Object.keys(x.resource)[0];
|
|
593
|
+
return `${key}:${x.resource[key][0]}`;
|
|
594
|
+
});
|
|
595
|
+
if (missingPermissions.length > 0) {
|
|
596
|
+
ctx.context.logger.error(`[Dynamic Access Control] The user is missing permissions necessary to ${action} a role with those set of permissions.\n`, {
|
|
597
|
+
userId: user.id,
|
|
598
|
+
organizationId,
|
|
599
|
+
role: member.role,
|
|
600
|
+
missingPermissions
|
|
601
|
+
});
|
|
602
|
+
let error;
|
|
603
|
+
if (action === "create") error = ORGANIZATION_ERROR_CODES.YOU_ARE_NOT_ALLOWED_TO_CREATE_A_ROLE;
|
|
604
|
+
else if (action === "update") error = ORGANIZATION_ERROR_CODES.YOU_ARE_NOT_ALLOWED_TO_UPDATE_A_ROLE;
|
|
605
|
+
else if (action === "delete") error = ORGANIZATION_ERROR_CODES.YOU_ARE_NOT_ALLOWED_TO_DELETE_A_ROLE;
|
|
606
|
+
else if (action === "read") error = ORGANIZATION_ERROR_CODES.YOU_ARE_NOT_ALLOWED_TO_READ_A_ROLE;
|
|
607
|
+
else if (action === "list") error = ORGANIZATION_ERROR_CODES.YOU_ARE_NOT_ALLOWED_TO_LIST_A_ROLE;
|
|
608
|
+
else error = ORGANIZATION_ERROR_CODES.YOU_ARE_NOT_ALLOWED_TO_GET_A_ROLE;
|
|
609
|
+
throw APIError.fromStatus("FORBIDDEN", {
|
|
610
|
+
message: error.message,
|
|
611
|
+
code: error.code,
|
|
612
|
+
missingPermissions
|
|
613
|
+
});
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
async function checkIfRoleNameIsTakenByPreDefinedRole({ options, organizationId, role, ctx }) {
|
|
617
|
+
const defaultRoles = options.roles ? Object.keys(options.roles) : [
|
|
618
|
+
"owner",
|
|
619
|
+
"admin",
|
|
620
|
+
"member"
|
|
621
|
+
];
|
|
622
|
+
if (defaultRoles.includes(role)) {
|
|
623
|
+
ctx.context.logger.error(`[Dynamic Access Control] The role name "${role}" is already taken by a pre-defined role.`, {
|
|
624
|
+
role,
|
|
625
|
+
organizationId,
|
|
626
|
+
defaultRoles
|
|
627
|
+
});
|
|
628
|
+
throw APIError.from("BAD_REQUEST", ORGANIZATION_ERROR_CODES.ROLE_NAME_IS_ALREADY_TAKEN);
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
async function checkIfRoleNameIsTakenByRoleInDB({ organizationId, role, ctx }) {
|
|
632
|
+
if (await ctx.context.adapter.findOne({
|
|
633
|
+
model: "organizationRole",
|
|
634
|
+
where: [{
|
|
635
|
+
field: "organizationId",
|
|
636
|
+
value: organizationId,
|
|
637
|
+
operator: "eq",
|
|
638
|
+
connector: "AND"
|
|
639
|
+
}, {
|
|
640
|
+
field: "role",
|
|
641
|
+
value: role,
|
|
642
|
+
operator: "eq",
|
|
643
|
+
connector: "AND"
|
|
644
|
+
}]
|
|
645
|
+
})) {
|
|
646
|
+
ctx.context.logger.error(`[Dynamic Access Control] The role name "${role}" is already taken by a role in the database.`, {
|
|
647
|
+
role,
|
|
648
|
+
organizationId
|
|
649
|
+
});
|
|
650
|
+
throw APIError.from("BAD_REQUEST", ORGANIZATION_ERROR_CODES.ROLE_NAME_IS_ALREADY_TAKEN);
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
//#endregion
|
|
655
|
+
export { createOrgRole, deleteOrgRole, getOrgRole, listOrgRoles, updateOrgRole };
|
|
656
|
+
//# sourceMappingURL=crud-access-control.mjs.map
|