@sirketio/auth 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/_virtual/_rolldown/runtime.mjs +36 -0
- package/dist/adapter/index.d.mts +4 -0
- package/dist/adapter/index.mjs +7 -0
- package/dist/api/index.d.mts +3872 -0
- package/dist/api/index.mjs +206 -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 +18 -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 +204 -0
- package/dist/api/rate-limiter/index.mjs.map +1 -0
- package/dist/api/routes/account.d.mts +410 -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 +31 -0
- package/dist/api/routes/callback.mjs +179 -0
- package/dist/api/routes/callback.mjs.map +1 -0
- package/dist/api/routes/email-verification.d.mts +161 -0
- package/dist/api/routes/email-verification.mjs +299 -0
- package/dist/api/routes/email-verification.mjs.map +1 -0
- package/dist/api/routes/error.d.mts +28 -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 +36 -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 +182 -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 +415 -0
- package/dist/api/routes/session.mjs +483 -0
- package/dist/api/routes/session.mjs.map +1 -0
- package/dist/api/routes/sign-in.d.mts +171 -0
- package/dist/api/routes/sign-in.mjs +263 -0
- package/dist/api/routes/sign-in.mjs.map +1 -0
- package/dist/api/routes/sign-out.d.mts +36 -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 +160 -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 +445 -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 +18 -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 +45 -0
- package/dist/auth/base.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 +33 -0
- package/dist/client/index.mjs +21 -0
- package/dist/client/index.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 +65 -0
- package/dist/client/plugins/index.d.mts +53 -0
- package/dist/client/plugins/index.mjs +30 -0
- package/dist/client/plugins/infer-plugin.d.mts +16 -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 +128 -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/types.d.mts +41 -0
- package/dist/client/vanilla.d.mts +127 -0
- package/dist/client/vanilla.mjs +20 -0
- package/dist/client/vanilla.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 +83 -0
- package/dist/context/helpers.mjs.map +1 -0
- package/dist/context/init.mjs +20 -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 +121 -0
- package/dist/cookies/index.mjs +261 -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 +19 -0
- package/dist/db/adapter-base.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 +42 -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 +18 -0
- package/dist/db/index.mjs +34 -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 +49 -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 +53 -0
- package/dist/index.mjs +27 -0
- package/dist/integrations/next-js.d.mts +29 -0
- package/dist/integrations/next-js.mjs +85 -0
- package/dist/integrations/next-js.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 +48 -0
- package/dist/oauth2/link-account.mjs +143 -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 +96 -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 +911 -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 +76 -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 +29 -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 +841 -0
- package/dist/plugins/admin/routes.mjs.map +1 -0
- package/dist/plugins/admin/schema.d.mts +40 -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/api-key/adapter.mjs +468 -0
- package/dist/plugins/api-key/adapter.mjs.map +1 -0
- package/dist/plugins/api-key/client.d.mts +46 -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 +33 -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 +1251 -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 +224 -0
- package/dist/plugins/api-key/routes/verify-api-key.mjs.map +1 -0
- package/dist/plugins/api-key/schema.d.mts +199 -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 +45 -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 +21 -0
- package/dist/plugins/captcha/index.mjs +62 -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 +28 -0
- package/dist/plugins/captcha/verify-handlers/captchafox.mjs.map +1 -0
- package/dist/plugins/captcha/verify-handlers/cloudflare-turnstile.mjs +26 -0
- package/dist/plugins/captcha/verify-handlers/cloudflare-turnstile.mjs.map +1 -0
- package/dist/plugins/captcha/verify-handlers/google-recaptcha.mjs +30 -0
- package/dist/plugins/captcha/verify-handlers/google-recaptcha.mjs.map +1 -0
- package/dist/plugins/captcha/verify-handlers/h-captcha.mjs +28 -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 +17 -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 +72 -0
- package/dist/plugins/custom-session/index.mjs +78 -0
- package/dist/plugins/custom-session/index.mjs.map +1 -0
- package/dist/plugins/device-authorization/client.d.mts +17 -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 +424 -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 +21 -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 +11 -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 +428 -0
- package/dist/plugins/email-otp/index.mjs +130 -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 +631 -0
- package/dist/plugins/email-otp/routes.mjs.map +1 -0
- package/dist/plugins/email-otp/types.d.mts +86 -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 +33 -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 +16 -0
- package/dist/plugins/generic-oauth/error-codes.mjs +17 -0
- package/dist/plugins/generic-oauth/error-codes.mjs.map +1 -0
- package/dist/plugins/generic-oauth/index.d.mts +201 -0
- package/dist/plugins/generic-oauth/index.mjs +145 -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 +411 -0
- package/dist/plugins/generic-oauth/routes.mjs.map +1 -0
- package/dist/plugins/generic-oauth/types.d.mts +159 -0
- package/dist/plugins/haveibeenpwned/index.d.mts +46 -0
- package/dist/plugins/haveibeenpwned/index.mjs +57 -0
- package/dist/plugins/haveibeenpwned/index.mjs.map +1 -0
- package/dist/plugins/index.d.mts +65 -0
- package/dist/plugins/index.mjs +48 -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 +40 -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 +224 -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 +26 -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 +38 -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 +118 -0
- package/dist/plugins/last-login-method/index.mjs +76 -0
- package/dist/plugins/last-login-method/index.mjs.map +1 -0
- package/dist/plugins/magic-link/client.d.mts +10 -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 +193 -0
- package/dist/plugins/magic-link/index.mjs +177 -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 +458 -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 +19 -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 +9 -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 +235 -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 +97 -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 +51 -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 +12 -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 +702 -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 +160 -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 +174 -0
- package/dist/plugins/one-tap/client.mjs +188 -0
- package/dist/plugins/one-tap/client.mjs.map +1 -0
- package/dist/plugins/one-tap/index.d.mts +83 -0
- package/dist/plugins/one-tap/index.mjs +95 -0
- package/dist/plugins/one-tap/index.mjs.map +1 -0
- package/dist/plugins/one-time-token/client.d.mts +10 -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 +133 -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 +97 -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 +792 -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 +372 -0
- package/dist/plugins/organization/client.mjs +95 -0
- package/dist/plugins/organization/client.mjs.map +1 -0
- package/dist/plugins/organization/error-codes.d.mts +65 -0
- package/dist/plugins/organization/error-codes.mjs +66 -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 +394 -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 +17 -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 +394 -0
- package/dist/plugins/organization/routes/crud-access-control.mjs +678 -0
- package/dist/plugins/organization/routes/crud-access-control.mjs.map +1 -0
- package/dist/plugins/organization/routes/crud-invites.d.mts +1031 -0
- package/dist/plugins/organization/routes/crud-invites.mjs +551 -0
- package/dist/plugins/organization/routes/crud-invites.mjs.map +1 -0
- package/dist/plugins/organization/routes/crud-members.d.mts +940 -0
- package/dist/plugins/organization/routes/crud-members.mjs +466 -0
- package/dist/plugins/organization/routes/crud-members.mjs.map +1 -0
- package/dist/plugins/organization/routes/crud-org.d.mts +708 -0
- package/dist/plugins/organization/routes/crud-org.mjs +423 -0
- package/dist/plugins/organization/routes/crud-org.mjs.map +1 -0
- package/dist/plugins/organization/routes/crud-team.d.mts +1071 -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 +677 -0
- package/dist/plugins/phone-number/client.d.mts +31 -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 +20 -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 +318 -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 +472 -0
- package/dist/plugins/phone-number/routes.mjs.map +1 -0
- package/dist/plugins/phone-number/schema.d.mts +23 -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/two-factor/backup-codes/index.d.mts +279 -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 +55 -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 +17 -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 +670 -0
- package/dist/plugins/two-factor/index.mjs +228 -0
- package/dist/plugins/two-factor/index.mjs.map +1 -0
- package/dist/plugins/two-factor/otp/index.d.mts +216 -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 +41 -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 +210 -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 +73 -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 +85 -0
- package/dist/plugins/two-factor/verify-two-factor.mjs.map +1 -0
- package/dist/plugins/username/client.d.mts +26 -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 +16 -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 +251 -0
- package/dist/plugins/username/index.mjs +234 -0
- package/dist/plugins/username/index.mjs.map +1 -0
- package/dist/plugins/username/schema.d.mts +33 -0
- package/dist/plugins/username/schema.mjs +26 -0
- package/dist/plugins/username/schema.mjs.map +1 -0
- package/dist/providers/index.d.mts +1 -0
- package/dist/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/types/adapter.d.mts +2 -0
- package/dist/types/api.d.mts +29 -0
- package/dist/types/auth.d.mts +29 -0
- package/dist/types/helper.d.mts +10 -0
- package/dist/types/index.d.mts +11 -0
- package/dist/types/index.mjs +1 -0
- package/dist/types/models.d.mts +11 -0
- package/dist/types/plugins.d.mts +20 -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/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 +4 -0
- package/dist/utils/index.mjs +6 -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 +9 -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.d.mts +8 -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 +428 -0
|
@@ -0,0 +1,678 @@
|
|
|
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
|
+
const roleToDelete = existingRoleInDB.role;
|
|
252
|
+
if ((await ctx.context.adapter.findMany({
|
|
253
|
+
model: "member",
|
|
254
|
+
where: [{
|
|
255
|
+
field: "organizationId",
|
|
256
|
+
value: organizationId,
|
|
257
|
+
operator: "eq",
|
|
258
|
+
connector: "AND"
|
|
259
|
+
}, {
|
|
260
|
+
field: "role",
|
|
261
|
+
value: roleToDelete,
|
|
262
|
+
operator: "contains"
|
|
263
|
+
}]
|
|
264
|
+
})).find((member) => {
|
|
265
|
+
return member.role.split(",").map((r) => r.trim()).includes(roleToDelete);
|
|
266
|
+
})) {
|
|
267
|
+
ctx.context.logger.error(`[Dynamic Access Control] Cannot delete a role that is assigned to members.`, {
|
|
268
|
+
role: existingRoleInDB.role,
|
|
269
|
+
organizationId
|
|
270
|
+
});
|
|
271
|
+
throw APIError.from("BAD_REQUEST", ORGANIZATION_ERROR_CODES.ROLE_IS_ASSIGNED_TO_MEMBERS);
|
|
272
|
+
}
|
|
273
|
+
await ctx.context.adapter.delete({
|
|
274
|
+
model: "organizationRole",
|
|
275
|
+
where: [{
|
|
276
|
+
field: "organizationId",
|
|
277
|
+
value: organizationId,
|
|
278
|
+
operator: "eq",
|
|
279
|
+
connector: "AND"
|
|
280
|
+
}, condition]
|
|
281
|
+
});
|
|
282
|
+
return ctx.json({ success: true });
|
|
283
|
+
});
|
|
284
|
+
};
|
|
285
|
+
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();
|
|
286
|
+
const listOrgRoles = (options) => {
|
|
287
|
+
const { $ReturnAdditionalFields } = getAdditionalFields(options, false);
|
|
288
|
+
return createAuthEndpoint("/organization/list-roles", {
|
|
289
|
+
method: "GET",
|
|
290
|
+
requireHeaders: true,
|
|
291
|
+
use: [orgSessionMiddleware],
|
|
292
|
+
query: listOrgRolesQuerySchema
|
|
293
|
+
}, async (ctx) => {
|
|
294
|
+
const { session, user } = ctx.context.session;
|
|
295
|
+
const organizationId = ctx.query?.organizationId ?? session.activeOrganizationId;
|
|
296
|
+
if (!organizationId) {
|
|
297
|
+
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.`);
|
|
298
|
+
throw APIError.from("BAD_REQUEST", ORGANIZATION_ERROR_CODES.NO_ACTIVE_ORGANIZATION);
|
|
299
|
+
}
|
|
300
|
+
const member = await ctx.context.adapter.findOne({
|
|
301
|
+
model: "member",
|
|
302
|
+
where: [{
|
|
303
|
+
field: "organizationId",
|
|
304
|
+
value: organizationId,
|
|
305
|
+
operator: "eq",
|
|
306
|
+
connector: "AND"
|
|
307
|
+
}, {
|
|
308
|
+
field: "userId",
|
|
309
|
+
value: user.id,
|
|
310
|
+
operator: "eq",
|
|
311
|
+
connector: "AND"
|
|
312
|
+
}]
|
|
313
|
+
});
|
|
314
|
+
if (!member) {
|
|
315
|
+
ctx.context.logger.error(`[Dynamic Access Control] The user is not a member of the organization to list roles.`, {
|
|
316
|
+
userId: user.id,
|
|
317
|
+
organizationId
|
|
318
|
+
});
|
|
319
|
+
throw APIError.from("FORBIDDEN", ORGANIZATION_ERROR_CODES.YOU_ARE_NOT_A_MEMBER_OF_THIS_ORGANIZATION);
|
|
320
|
+
}
|
|
321
|
+
if (!await hasPermission({
|
|
322
|
+
options,
|
|
323
|
+
organizationId,
|
|
324
|
+
permissions: { ac: ["read"] },
|
|
325
|
+
role: member.role
|
|
326
|
+
}, ctx)) {
|
|
327
|
+
ctx.context.logger.error(`[Dynamic Access Control] The user is not permitted to list roles.`, {
|
|
328
|
+
userId: user.id,
|
|
329
|
+
organizationId,
|
|
330
|
+
role: member.role
|
|
331
|
+
});
|
|
332
|
+
throw APIError.from("FORBIDDEN", ORGANIZATION_ERROR_CODES.YOU_ARE_NOT_ALLOWED_TO_LIST_A_ROLE);
|
|
333
|
+
}
|
|
334
|
+
let roles = await ctx.context.adapter.findMany({
|
|
335
|
+
model: "organizationRole",
|
|
336
|
+
where: [{
|
|
337
|
+
field: "organizationId",
|
|
338
|
+
value: organizationId,
|
|
339
|
+
operator: "eq",
|
|
340
|
+
connector: "AND"
|
|
341
|
+
}]
|
|
342
|
+
});
|
|
343
|
+
roles = roles.map((x) => ({
|
|
344
|
+
...x,
|
|
345
|
+
permission: JSON.parse(x.permission)
|
|
346
|
+
}));
|
|
347
|
+
return ctx.json(roles);
|
|
348
|
+
});
|
|
349
|
+
};
|
|
350
|
+
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();
|
|
351
|
+
const getOrgRole = (options) => {
|
|
352
|
+
const { $ReturnAdditionalFields } = getAdditionalFields(options, false);
|
|
353
|
+
return createAuthEndpoint("/organization/get-role", {
|
|
354
|
+
method: "GET",
|
|
355
|
+
requireHeaders: true,
|
|
356
|
+
use: [orgSessionMiddleware],
|
|
357
|
+
query: getOrgRoleQuerySchema,
|
|
358
|
+
metadata: { $Infer: { query: {} } }
|
|
359
|
+
}, async (ctx) => {
|
|
360
|
+
const { session, user } = ctx.context.session;
|
|
361
|
+
const organizationId = ctx.query?.organizationId ?? session.activeOrganizationId;
|
|
362
|
+
if (!organizationId) {
|
|
363
|
+
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.`);
|
|
364
|
+
throw APIError.from("BAD_REQUEST", ORGANIZATION_ERROR_CODES.NO_ACTIVE_ORGANIZATION);
|
|
365
|
+
}
|
|
366
|
+
const member = await ctx.context.adapter.findOne({
|
|
367
|
+
model: "member",
|
|
368
|
+
where: [{
|
|
369
|
+
field: "organizationId",
|
|
370
|
+
value: organizationId,
|
|
371
|
+
operator: "eq",
|
|
372
|
+
connector: "AND"
|
|
373
|
+
}, {
|
|
374
|
+
field: "userId",
|
|
375
|
+
value: user.id,
|
|
376
|
+
operator: "eq",
|
|
377
|
+
connector: "AND"
|
|
378
|
+
}]
|
|
379
|
+
});
|
|
380
|
+
if (!member) {
|
|
381
|
+
ctx.context.logger.error(`[Dynamic Access Control] The user is not a member of the organization to read a role.`, {
|
|
382
|
+
userId: user.id,
|
|
383
|
+
organizationId
|
|
384
|
+
});
|
|
385
|
+
throw APIError.from("FORBIDDEN", ORGANIZATION_ERROR_CODES.YOU_ARE_NOT_A_MEMBER_OF_THIS_ORGANIZATION);
|
|
386
|
+
}
|
|
387
|
+
if (!await hasPermission({
|
|
388
|
+
options,
|
|
389
|
+
organizationId,
|
|
390
|
+
permissions: { ac: ["read"] },
|
|
391
|
+
role: member.role
|
|
392
|
+
}, ctx)) {
|
|
393
|
+
ctx.context.logger.error(`[Dynamic Access Control] The user is not permitted to read a role.`, {
|
|
394
|
+
userId: user.id,
|
|
395
|
+
organizationId,
|
|
396
|
+
role: member.role
|
|
397
|
+
});
|
|
398
|
+
throw APIError.from("FORBIDDEN", ORGANIZATION_ERROR_CODES.YOU_ARE_NOT_ALLOWED_TO_READ_A_ROLE);
|
|
399
|
+
}
|
|
400
|
+
let condition;
|
|
401
|
+
if (ctx.query.roleName) condition = {
|
|
402
|
+
field: "role",
|
|
403
|
+
value: ctx.query.roleName,
|
|
404
|
+
operator: "eq",
|
|
405
|
+
connector: "AND"
|
|
406
|
+
};
|
|
407
|
+
else if (ctx.query.roleId) condition = {
|
|
408
|
+
field: "id",
|
|
409
|
+
value: ctx.query.roleId,
|
|
410
|
+
operator: "eq",
|
|
411
|
+
connector: "AND"
|
|
412
|
+
};
|
|
413
|
+
else {
|
|
414
|
+
ctx.context.logger.error(`[Dynamic Access Control] The role name/id is not provided in the request query.`);
|
|
415
|
+
throw APIError.from("BAD_REQUEST", ORGANIZATION_ERROR_CODES.ROLE_NOT_FOUND);
|
|
416
|
+
}
|
|
417
|
+
const role = await ctx.context.adapter.findOne({
|
|
418
|
+
model: "organizationRole",
|
|
419
|
+
where: [{
|
|
420
|
+
field: "organizationId",
|
|
421
|
+
value: organizationId,
|
|
422
|
+
operator: "eq",
|
|
423
|
+
connector: "AND"
|
|
424
|
+
}, condition]
|
|
425
|
+
});
|
|
426
|
+
if (!role) {
|
|
427
|
+
ctx.context.logger.error(`[Dynamic Access Control] The role name/id does not exist in the database.`, {
|
|
428
|
+
..."roleName" in ctx.query ? { roleName: ctx.query.roleName } : { roleId: ctx.query.roleId },
|
|
429
|
+
organizationId
|
|
430
|
+
});
|
|
431
|
+
throw APIError.from("BAD_REQUEST", ORGANIZATION_ERROR_CODES.ROLE_NOT_FOUND);
|
|
432
|
+
}
|
|
433
|
+
role.permission = JSON.parse(role.permission);
|
|
434
|
+
return ctx.json(role);
|
|
435
|
+
});
|
|
436
|
+
};
|
|
437
|
+
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" }) })]);
|
|
438
|
+
const updateOrgRole = (options) => {
|
|
439
|
+
const { additionalFieldsSchema, $AdditionalFields, $ReturnAdditionalFields } = getAdditionalFields(options, true);
|
|
440
|
+
return createAuthEndpoint("/organization/update-role", {
|
|
441
|
+
method: "POST",
|
|
442
|
+
body: z.object({
|
|
443
|
+
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." }),
|
|
444
|
+
data: z.object({
|
|
445
|
+
permission: z.record(z.string(), z.array(z.string())).optional().meta({ description: "The permission to update the role with" }),
|
|
446
|
+
roleName: z.string().optional().meta({ description: "The name of the role to update" }),
|
|
447
|
+
...additionalFieldsSchema.shape
|
|
448
|
+
})
|
|
449
|
+
}).and(roleNameOrIdSchema),
|
|
450
|
+
metadata: { $Infer: { body: {} } },
|
|
451
|
+
requireHeaders: true,
|
|
452
|
+
use: [orgSessionMiddleware]
|
|
453
|
+
}, async (ctx) => {
|
|
454
|
+
const { session, user } = ctx.context.session;
|
|
455
|
+
const ac = options.ac;
|
|
456
|
+
if (!ac) {
|
|
457
|
+
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`);
|
|
458
|
+
throw APIError.from("NOT_IMPLEMENTED", ORGANIZATION_ERROR_CODES.MISSING_AC_INSTANCE);
|
|
459
|
+
}
|
|
460
|
+
const organizationId = ctx.body.organizationId ?? session.activeOrganizationId;
|
|
461
|
+
if (!organizationId) {
|
|
462
|
+
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.`);
|
|
463
|
+
throw APIError.from("BAD_REQUEST", ORGANIZATION_ERROR_CODES.NO_ACTIVE_ORGANIZATION);
|
|
464
|
+
}
|
|
465
|
+
const member = await ctx.context.adapter.findOne({
|
|
466
|
+
model: "member",
|
|
467
|
+
where: [{
|
|
468
|
+
field: "organizationId",
|
|
469
|
+
value: organizationId,
|
|
470
|
+
operator: "eq",
|
|
471
|
+
connector: "AND"
|
|
472
|
+
}, {
|
|
473
|
+
field: "userId",
|
|
474
|
+
value: user.id,
|
|
475
|
+
operator: "eq",
|
|
476
|
+
connector: "AND"
|
|
477
|
+
}]
|
|
478
|
+
});
|
|
479
|
+
if (!member) {
|
|
480
|
+
ctx.context.logger.error(`[Dynamic Access Control] The user is not a member of the organization to update a role.`, {
|
|
481
|
+
userId: user.id,
|
|
482
|
+
organizationId
|
|
483
|
+
});
|
|
484
|
+
throw APIError.from("FORBIDDEN", ORGANIZATION_ERROR_CODES.YOU_ARE_NOT_A_MEMBER_OF_THIS_ORGANIZATION);
|
|
485
|
+
}
|
|
486
|
+
if (!await hasPermission({
|
|
487
|
+
options,
|
|
488
|
+
organizationId,
|
|
489
|
+
role: member.role,
|
|
490
|
+
permissions: { ac: ["update"] }
|
|
491
|
+
}, ctx)) {
|
|
492
|
+
ctx.context.logger.error(`[Dynamic Access Control] The user is not permitted to update a role.`);
|
|
493
|
+
throw APIError.from("FORBIDDEN", ORGANIZATION_ERROR_CODES.YOU_ARE_NOT_ALLOWED_TO_UPDATE_A_ROLE);
|
|
494
|
+
}
|
|
495
|
+
let condition;
|
|
496
|
+
if (ctx.body.roleName) condition = {
|
|
497
|
+
field: "role",
|
|
498
|
+
value: ctx.body.roleName,
|
|
499
|
+
operator: "eq",
|
|
500
|
+
connector: "AND"
|
|
501
|
+
};
|
|
502
|
+
else if (ctx.body.roleId) condition = {
|
|
503
|
+
field: "id",
|
|
504
|
+
value: ctx.body.roleId,
|
|
505
|
+
operator: "eq",
|
|
506
|
+
connector: "AND"
|
|
507
|
+
};
|
|
508
|
+
else {
|
|
509
|
+
ctx.context.logger.error(`[Dynamic Access Control] The role name/id is not provided in the request body.`);
|
|
510
|
+
throw APIError.from("BAD_REQUEST", ORGANIZATION_ERROR_CODES.ROLE_NOT_FOUND);
|
|
511
|
+
}
|
|
512
|
+
const role = await ctx.context.adapter.findOne({
|
|
513
|
+
model: "organizationRole",
|
|
514
|
+
where: [{
|
|
515
|
+
field: "organizationId",
|
|
516
|
+
value: organizationId,
|
|
517
|
+
operator: "eq",
|
|
518
|
+
connector: "AND"
|
|
519
|
+
}, condition]
|
|
520
|
+
});
|
|
521
|
+
if (!role) {
|
|
522
|
+
ctx.context.logger.error(`[Dynamic Access Control] The role name/id does not exist in the database.`, {
|
|
523
|
+
..."roleName" in ctx.body ? { roleName: ctx.body.roleName } : { roleId: ctx.body.roleId },
|
|
524
|
+
organizationId
|
|
525
|
+
});
|
|
526
|
+
throw APIError.from("BAD_REQUEST", ORGANIZATION_ERROR_CODES.ROLE_NOT_FOUND);
|
|
527
|
+
}
|
|
528
|
+
role.permission = role.permission ? JSON.parse(role.permission) : void 0;
|
|
529
|
+
const { permission: _, roleName: __, ...additionalFields } = ctx.body.data;
|
|
530
|
+
const updateData = { ...additionalFields };
|
|
531
|
+
if (ctx.body.data.permission) {
|
|
532
|
+
const newPermission = ctx.body.data.permission;
|
|
533
|
+
await checkForInvalidResources({
|
|
534
|
+
ac,
|
|
535
|
+
ctx,
|
|
536
|
+
permission: newPermission
|
|
537
|
+
});
|
|
538
|
+
await checkIfMemberHasPermission({
|
|
539
|
+
ctx,
|
|
540
|
+
member,
|
|
541
|
+
options,
|
|
542
|
+
organizationId,
|
|
543
|
+
permissionRequired: newPermission,
|
|
544
|
+
user,
|
|
545
|
+
action: "update"
|
|
546
|
+
});
|
|
547
|
+
updateData.permission = newPermission;
|
|
548
|
+
}
|
|
549
|
+
if (ctx.body.data.roleName) {
|
|
550
|
+
let newRoleName = ctx.body.data.roleName;
|
|
551
|
+
newRoleName = normalizeRoleName(newRoleName);
|
|
552
|
+
await checkIfRoleNameIsTakenByPreDefinedRole({
|
|
553
|
+
role: newRoleName,
|
|
554
|
+
organizationId,
|
|
555
|
+
options,
|
|
556
|
+
ctx
|
|
557
|
+
});
|
|
558
|
+
await checkIfRoleNameIsTakenByRoleInDB({
|
|
559
|
+
role: newRoleName,
|
|
560
|
+
organizationId,
|
|
561
|
+
ctx
|
|
562
|
+
});
|
|
563
|
+
updateData.role = newRoleName;
|
|
564
|
+
}
|
|
565
|
+
const update = {
|
|
566
|
+
...updateData,
|
|
567
|
+
...updateData.permission ? { permission: JSON.stringify(updateData.permission) } : {}
|
|
568
|
+
};
|
|
569
|
+
await ctx.context.adapter.update({
|
|
570
|
+
model: "organizationRole",
|
|
571
|
+
where: [{
|
|
572
|
+
field: "organizationId",
|
|
573
|
+
value: organizationId,
|
|
574
|
+
operator: "eq",
|
|
575
|
+
connector: "AND"
|
|
576
|
+
}, condition],
|
|
577
|
+
update
|
|
578
|
+
});
|
|
579
|
+
return ctx.json({
|
|
580
|
+
success: true,
|
|
581
|
+
roleData: {
|
|
582
|
+
...role,
|
|
583
|
+
...update,
|
|
584
|
+
permission: updateData.permission || role.permission || null
|
|
585
|
+
}
|
|
586
|
+
});
|
|
587
|
+
});
|
|
588
|
+
};
|
|
589
|
+
async function checkForInvalidResources({ ac, ctx, permission }) {
|
|
590
|
+
const validResources = Object.keys(ac.statements);
|
|
591
|
+
const providedResources = Object.keys(permission);
|
|
592
|
+
if (providedResources.some((r) => !validResources.includes(r))) {
|
|
593
|
+
ctx.context.logger.error(`[Dynamic Access Control] The provided permission includes an invalid resource.`, {
|
|
594
|
+
providedResources,
|
|
595
|
+
validResources
|
|
596
|
+
});
|
|
597
|
+
throw APIError.from("BAD_REQUEST", ORGANIZATION_ERROR_CODES.INVALID_RESOURCE);
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
async function checkIfMemberHasPermission({ ctx, permissionRequired: permission, options, organizationId, member, user, action }) {
|
|
601
|
+
const hasNecessaryPermissions = [];
|
|
602
|
+
const permissionEntries = Object.entries(permission);
|
|
603
|
+
for await (const [resource, permissions] of permissionEntries) for await (const perm of permissions) hasNecessaryPermissions.push({
|
|
604
|
+
resource: { [resource]: [perm] },
|
|
605
|
+
hasPermission: await hasPermission({
|
|
606
|
+
options,
|
|
607
|
+
organizationId,
|
|
608
|
+
permissions: { [resource]: [perm] },
|
|
609
|
+
useMemoryCache: true,
|
|
610
|
+
role: member.role
|
|
611
|
+
}, ctx)
|
|
612
|
+
});
|
|
613
|
+
const missingPermissions = hasNecessaryPermissions.filter((x) => x.hasPermission === false).map((x) => {
|
|
614
|
+
const key = Object.keys(x.resource)[0];
|
|
615
|
+
return `${key}:${x.resource[key][0]}`;
|
|
616
|
+
});
|
|
617
|
+
if (missingPermissions.length > 0) {
|
|
618
|
+
ctx.context.logger.error(`[Dynamic Access Control] The user is missing permissions necessary to ${action} a role with those set of permissions.\n`, {
|
|
619
|
+
userId: user.id,
|
|
620
|
+
organizationId,
|
|
621
|
+
role: member.role,
|
|
622
|
+
missingPermissions
|
|
623
|
+
});
|
|
624
|
+
let error;
|
|
625
|
+
if (action === "create") error = ORGANIZATION_ERROR_CODES.YOU_ARE_NOT_ALLOWED_TO_CREATE_A_ROLE;
|
|
626
|
+
else if (action === "update") error = ORGANIZATION_ERROR_CODES.YOU_ARE_NOT_ALLOWED_TO_UPDATE_A_ROLE;
|
|
627
|
+
else if (action === "delete") error = ORGANIZATION_ERROR_CODES.YOU_ARE_NOT_ALLOWED_TO_DELETE_A_ROLE;
|
|
628
|
+
else if (action === "read") error = ORGANIZATION_ERROR_CODES.YOU_ARE_NOT_ALLOWED_TO_READ_A_ROLE;
|
|
629
|
+
else if (action === "list") error = ORGANIZATION_ERROR_CODES.YOU_ARE_NOT_ALLOWED_TO_LIST_A_ROLE;
|
|
630
|
+
else error = ORGANIZATION_ERROR_CODES.YOU_ARE_NOT_ALLOWED_TO_GET_A_ROLE;
|
|
631
|
+
throw APIError.fromStatus("FORBIDDEN", {
|
|
632
|
+
message: error.message,
|
|
633
|
+
code: error.code,
|
|
634
|
+
missingPermissions
|
|
635
|
+
});
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
async function checkIfRoleNameIsTakenByPreDefinedRole({ options, organizationId, role, ctx }) {
|
|
639
|
+
const defaultRoles = options.roles ? Object.keys(options.roles) : [
|
|
640
|
+
"owner",
|
|
641
|
+
"admin",
|
|
642
|
+
"member"
|
|
643
|
+
];
|
|
644
|
+
if (defaultRoles.includes(role)) {
|
|
645
|
+
ctx.context.logger.error(`[Dynamic Access Control] The role name "${role}" is already taken by a pre-defined role.`, {
|
|
646
|
+
role,
|
|
647
|
+
organizationId,
|
|
648
|
+
defaultRoles
|
|
649
|
+
});
|
|
650
|
+
throw APIError.from("BAD_REQUEST", ORGANIZATION_ERROR_CODES.ROLE_NAME_IS_ALREADY_TAKEN);
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
async function checkIfRoleNameIsTakenByRoleInDB({ organizationId, role, ctx }) {
|
|
654
|
+
if (await ctx.context.adapter.findOne({
|
|
655
|
+
model: "organizationRole",
|
|
656
|
+
where: [{
|
|
657
|
+
field: "organizationId",
|
|
658
|
+
value: organizationId,
|
|
659
|
+
operator: "eq",
|
|
660
|
+
connector: "AND"
|
|
661
|
+
}, {
|
|
662
|
+
field: "role",
|
|
663
|
+
value: role,
|
|
664
|
+
operator: "eq",
|
|
665
|
+
connector: "AND"
|
|
666
|
+
}]
|
|
667
|
+
})) {
|
|
668
|
+
ctx.context.logger.error(`[Dynamic Access Control] The role name "${role}" is already taken by a role in the database.`, {
|
|
669
|
+
role,
|
|
670
|
+
organizationId
|
|
671
|
+
});
|
|
672
|
+
throw APIError.from("BAD_REQUEST", ORGANIZATION_ERROR_CODES.ROLE_NAME_IS_ALREADY_TAKEN);
|
|
673
|
+
}
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
//#endregion
|
|
677
|
+
export { createOrgRole, deleteOrgRole, getOrgRole, listOrgRoles, updateOrgRole };
|
|
678
|
+
//# sourceMappingURL=crud-access-control.mjs.map
|