openadminjs 0.1.4 → 0.1.5
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 +16 -113
- package/dist/create-project.d.ts +34 -0
- package/dist/create-project.js +245 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +138 -0
- package/dist/index.test.d.ts +1 -0
- package/dist/index.test.js +7 -0
- package/dist/resource-slug.d.ts +2 -0
- package/dist/resource-slug.js +4 -0
- package/dist/resource-slug.test.d.ts +1 -0
- package/dist/resource-slug.test.js +9 -0
- package/package.json +35 -24
- package/.env.example +0 -9
- package/.github/FUNDING.yml +0 -3
- package/.github/ISSUE_TEMPLATE/bug_report.yml +0 -10
- package/.github/ISSUE_TEMPLATE/feature_request.yml +0 -10
- package/.github/PULL_REQUEST_TEMPLATE.md +0 -9
- package/.github/dependabot.yml +0 -10
- package/.github/workflows/ci.yml +0 -26
- package/CHANGELOG.md +0 -5
- package/CODE_OF_CONDUCT.md +0 -3
- package/CONTRIBUTING.md +0 -11
- package/LICENSE +0 -21
- package/ROADMAP.md +0 -7
- package/SECURITY.md +0 -11
- package/apps/admin/app/api/auth/login/route.ts +0 -51
- package/apps/admin/app/api/auth/logout/route.ts +0 -11
- package/apps/admin/app/api/auth/refresh/route.ts +0 -46
- package/apps/admin/app/api-tokens/page.tsx +0 -253
- package/apps/admin/app/audit-logs/page.tsx +0 -5
- package/apps/admin/app/dashboard/page.tsx +0 -392
- package/apps/admin/app/files/page.tsx +0 -5
- package/apps/admin/app/globals.css +0 -206
- package/apps/admin/app/jobs/page.tsx +0 -19
- package/apps/admin/app/layout.tsx +0 -57
- package/apps/admin/app/login/page.test.tsx +0 -18
- package/apps/admin/app/login/page.tsx +0 -161
- package/apps/admin/app/login/safe-next-param.ts +0 -4
- package/apps/admin/app/notifications/page.tsx +0 -146
- package/apps/admin/app/page.tsx +0 -5
- package/apps/admin/app/plugins/page.tsx +0 -407
- package/apps/admin/app/profile/page.tsx +0 -116
- package/apps/admin/app/resources/[resource]/[id]/edit/page.tsx +0 -45
- package/apps/admin/app/resources/[resource]/[id]/page.tsx +0 -449
- package/apps/admin/app/resources/[resource]/create/page.tsx +0 -34
- package/apps/admin/app/resources/[resource]/page.tsx +0 -100
- package/apps/admin/app/settings/page.tsx +0 -233
- package/apps/admin/components/ai-assistant-widget.tsx +0 -684
- package/apps/admin/components/app-shell.tsx +0 -305
- package/apps/admin/components/auth-guard.test.tsx +0 -55
- package/apps/admin/components/auth-guard.tsx +0 -39
- package/apps/admin/components/jobs-panel.tsx +0 -103
- package/apps/admin/components/logo.tsx +0 -30
- package/apps/admin/components/resource-form.tsx +0 -349
- package/apps/admin/components/resource-table.tsx +0 -277
- package/apps/admin/lib/ai-events.ts +0 -23
- package/apps/admin/lib/api.test.ts +0 -118
- package/apps/admin/lib/api.ts +0 -135
- package/apps/admin/lib/auth-flow.e2e.test.ts +0 -79
- package/apps/admin/lib/brand.ts +0 -14
- package/apps/admin/lib/locale.ts +0 -49
- package/apps/admin/lib/tokens.ts +0 -93
- package/apps/admin/middleware.ts +0 -58
- package/apps/admin/next-env.d.ts +0 -6
- package/apps/admin/next.config.mjs +0 -7
- package/apps/admin/package.json +0 -44
- package/apps/admin/postcss.config.mjs +0 -6
- package/apps/admin/public/brand/apple-touch-icon.png +0 -0
- package/apps/admin/public/brand/favicon.svg +0 -1
- package/apps/admin/public/brand/og-image.png +0 -0
- package/apps/admin/public/brand/openadminjs-icon-new.png +0 -0
- package/apps/admin/public/brand/openadminjs-logo-monochrome.png +0 -0
- package/apps/admin/public/brand/openadminjs-logo-new.png +0 -0
- package/apps/admin/tailwind.config.ts +0 -37
- package/apps/admin/tsconfig.json +0 -23
- package/apps/admin/vitest.config.ts +0 -7
- package/apps/api/package.json +0 -56
- package/apps/api/plugins.manifest.example.json +0 -34
- package/apps/api/plugins.manifest.json +0 -4
- package/apps/api/src/access/catalog.ts +0 -112
- package/apps/api/src/access/derive-permissions.test.ts +0 -26
- package/apps/api/src/access/derive-permissions.ts +0 -36
- package/apps/api/src/access/public-catalog.ts +0 -28
- package/apps/api/src/admin/admin-ai.controller.ts +0 -125
- package/apps/api/src/admin/admin-ai.events.ts +0 -23
- package/apps/api/src/admin/admin-ai.service.ts +0 -1271
- package/apps/api/src/admin/admin-analytics.service.ts +0 -164
- package/apps/api/src/admin/admin-plugins.controller.ts +0 -32
- package/apps/api/src/admin/admin-plugins.service.ts +0 -267
- package/apps/api/src/admin/admin-resource.service.test.ts +0 -118
- package/apps/api/src/admin/admin-resource.service.ts +0 -375
- package/apps/api/src/admin/admin.controller.ts +0 -72
- package/apps/api/src/admin/admin.module.ts +0 -30
- package/apps/api/src/admin/dto/plugins.dto.ts +0 -55
- package/apps/api/src/admin/validate-resource-payload.test.ts +0 -44
- package/apps/api/src/admin/validate-resource-payload.ts +0 -103
- package/apps/api/src/app.module.ts +0 -45
- package/apps/api/src/auth/auth.controller.ts +0 -70
- package/apps/api/src/auth/auth.dto.ts +0 -49
- package/apps/api/src/auth/auth.module.ts +0 -30
- package/apps/api/src/auth/auth.service.test.ts +0 -75
- package/apps/api/src/auth/auth.service.ts +0 -231
- package/apps/api/src/common/auth-realm.decorator.ts +0 -6
- package/apps/api/src/common/auth.guard.test.ts +0 -61
- package/apps/api/src/common/auth.guard.ts +0 -78
- package/apps/api/src/common/casl-ability.factory.test.ts +0 -33
- package/apps/api/src/common/casl-ability.factory.ts +0 -41
- package/apps/api/src/common/current-user.decorator.ts +0 -6
- package/apps/api/src/common/permission.decorator.ts +0 -7
- package/apps/api/src/common/permissions.guard.ts +0 -31
- package/apps/api/src/common/prisma.service.ts +0 -13
- package/apps/api/src/health/health.controller.ts +0 -25
- package/apps/api/src/mail/mail.module.ts +0 -9
- package/apps/api/src/mail/mail.service.ts +0 -115
- package/apps/api/src/main.ts +0 -37
- package/apps/api/src/plugins/bootstrap.ts +0 -4
- package/apps/api/src/plugins/bundled/admin-widget.plugin.ts +0 -25
- package/apps/api/src/plugins/bundled/hello.plugin.ts +0 -19
- package/apps/api/src/plugins/bundled/image-optimizer.plugin.ts +0 -22
- package/apps/api/src/plugins/bundled/registry.ts +0 -13
- package/apps/api/src/plugins/bundled/seo.plugin.ts +0 -25
- package/apps/api/src/plugins/manifest-path.ts +0 -35
- package/apps/api/src/plugins/manifest.schema.test.ts +0 -36
- package/apps/api/src/plugins/manifest.schema.ts +0 -58
- package/apps/api/src/plugins/plugin-api.interceptor.ts +0 -47
- package/apps/api/src/plugins/plugin-loader.ts +0 -134
- package/apps/api/src/plugins/plugin-runtime.test.ts +0 -24
- package/apps/api/src/plugins/plugin-runtime.ts +0 -85
- package/apps/api/src/queue/queue.controller.ts +0 -28
- package/apps/api/src/queue/queue.module.ts +0 -23
- package/apps/api/src/queue/queue.processor.ts +0 -47
- package/apps/api/src/queue/queue.service.ts +0 -50
- package/apps/api/src/resources/categories.resource.ts +0 -21
- package/apps/api/src/resources/commerce.resource.ts +0 -157
- package/apps/api/src/resources/operations.resource.ts +0 -111
- package/apps/api/src/resources/posts.resource.ts +0 -88
- package/apps/api/src/resources/registry.ts +0 -27
- package/apps/api/src/resources/resource-hooks.install.ts +0 -18
- package/apps/api/src/resources/resource-hooks.registry.test.ts +0 -72
- package/apps/api/src/resources/resource-hooks.registry.ts +0 -66
- package/apps/api/src/resources/resource-hooks.runner.ts +0 -93
- package/apps/api/src/resources/system.resource.ts +0 -51
- package/apps/api/src/resources/users.resource.ts +0 -19
- package/apps/api/src/store/payment/payment.controller.ts +0 -98
- package/apps/api/src/store/payment/payment.module.ts +0 -14
- package/apps/api/src/store/payment/payment.provider.ts +0 -38
- package/apps/api/src/store/payment/payment.service.ts +0 -235
- package/apps/api/src/store/store.controller.ts +0 -26
- package/apps/api/src/store/store.module.ts +0 -12
- package/apps/api/src/test-setup.ts +0 -1
- package/apps/api/tsconfig.json +0 -11
- package/apps/api/vitest.config.ts +0 -21
- package/apps/web/README.md +0 -12
- package/apps/web/app/globals.css +0 -12
- package/apps/web/app/layout.tsx +0 -18
- package/apps/web/app/page.tsx +0 -27
- package/apps/web/app/posts/[slug]/page.tsx +0 -38
- package/apps/web/app/posts/page.tsx +0 -20
- package/apps/web/app/robots.txt/route.ts +0 -9
- package/apps/web/app/sitemap.xml/route.ts +0 -20
- package/apps/web/components/public-fields.tsx +0 -22
- package/apps/web/lib/openadmin-client.ts +0 -82
- package/apps/web/lib/seo.test.ts +0 -41
- package/apps/web/lib/seo.ts +0 -53
- package/apps/web/next-env.d.ts +0 -6
- package/apps/web/next.config.mjs +0 -5
- package/apps/web/package.json +0 -30
- package/apps/web/postcss.config.mjs +0 -6
- package/apps/web/public/brand/apple-touch-icon.png +0 -0
- package/apps/web/public/brand/favicon.svg +0 -1
- package/apps/web/public/brand/og-image.png +0 -0
- package/apps/web/public/brand/openadminjs-icon-new.png +0 -0
- package/apps/web/public/brand/openadminjs-logo-monochrome.png +0 -0
- package/apps/web/public/brand/openadminjs-logo-new.png +0 -0
- package/apps/web/tailwind.config.ts +0 -21
- package/apps/web/test/next-stub.ts +0 -7
- package/apps/web/tsconfig.json +0 -23
- package/apps/web/vitest.config.ts +0 -19
- package/docs/content/security.md +0 -7
- package/eslint.config.mjs +0 -11
- package/examples/README.md +0 -80
- package/examples/basic-admin/README.md +0 -24
- package/examples/basic-admin/package.json +0 -17
- package/examples/basic-admin/prisma/models.fragment.prisma +0 -12
- package/examples/basic-admin/src/index.test.ts +0 -8
- package/examples/basic-admin/src/index.ts +0 -5
- package/examples/basic-admin/src/resources/lead.resource.ts +0 -30
- package/examples/basic-admin/tsconfig.json +0 -7
- package/examples/basic-admin/vitest.config.ts +0 -16
- package/examples/blog-admin/README.md +0 -18
- package/examples/blog-admin/package.json +0 -17
- package/examples/blog-admin/prisma/models.fragment.prisma +0 -30
- package/examples/blog-admin/src/index.test.ts +0 -8
- package/examples/blog-admin/src/index.ts +0 -5
- package/examples/blog-admin/src/resources/authors.resource.ts +0 -21
- package/examples/blog-admin/src/resources/tags.resource.ts +0 -20
- package/examples/blog-admin/tsconfig.json +0 -7
- package/examples/blog-admin/vitest.config.ts +0 -16
- package/examples/crm-admin/README.md +0 -17
- package/examples/crm-admin/package.json +0 -17
- package/examples/crm-admin/prisma/models.fragment.prisma +0 -50
- package/examples/crm-admin/src/index.test.ts +0 -9
- package/examples/crm-admin/src/index.ts +0 -7
- package/examples/crm-admin/src/resources/activities.resource.ts +0 -29
- package/examples/crm-admin/src/resources/companies.resource.ts +0 -21
- package/examples/crm-admin/src/resources/contacts.resource.ts +0 -24
- package/examples/crm-admin/src/resources/deals.resource.ts +0 -30
- package/examples/crm-admin/tsconfig.json +0 -7
- package/examples/crm-admin/vitest.config.ts +0 -16
- package/examples/ecommerce-admin/README.md +0 -17
- package/examples/ecommerce-admin/package.json +0 -17
- package/examples/ecommerce-admin/prisma/models.fragment.prisma +0 -43
- package/examples/ecommerce-admin/src/index.test.ts +0 -8
- package/examples/ecommerce-admin/src/index.ts +0 -7
- package/examples/ecommerce-admin/src/resources/customers.resource.ts +0 -21
- package/examples/ecommerce-admin/src/resources/order-items.resource.ts +0 -22
- package/examples/ecommerce-admin/src/resources/orders.resource.ts +0 -27
- package/examples/ecommerce-admin/src/resources/products.resource.ts +0 -23
- package/examples/ecommerce-admin/tsconfig.json +0 -7
- package/examples/ecommerce-admin/vitest.config.ts +0 -16
- package/examples/marketplace-admin/README.md +0 -17
- package/examples/marketplace-admin/package.json +0 -17
- package/examples/marketplace-admin/prisma/models.fragment.prisma +0 -32
- package/examples/marketplace-admin/src/index.test.ts +0 -8
- package/examples/marketplace-admin/src/index.ts +0 -6
- package/examples/marketplace-admin/src/resources/listings.resource.ts +0 -29
- package/examples/marketplace-admin/src/resources/payouts.resource.ts +0 -28
- package/examples/marketplace-admin/src/resources/sellers.resource.ts +0 -28
- package/examples/marketplace-admin/tsconfig.json +0 -7
- package/examples/marketplace-admin/vitest.config.ts +0 -16
- package/examples/support-admin/README.md +0 -18
- package/examples/support-admin/package.json +0 -17
- package/examples/support-admin/prisma/models.fragment.prisma +0 -20
- package/examples/support-admin/src/index.test.ts +0 -8
- package/examples/support-admin/src/index.ts +0 -5
- package/examples/support-admin/src/resources/ticket-messages.resource.ts +0 -22
- package/examples/support-admin/src/resources/tickets.resource.ts +0 -35
- package/examples/support-admin/tsconfig.json +0 -7
- package/examples/support-admin/vitest.config.ts +0 -16
- package/packages/auth/package.json +0 -17
- package/packages/auth/src/index.test.ts +0 -10
- package/packages/auth/src/index.ts +0 -17
- package/packages/auth/tsconfig.json +0 -8
- package/packages/brand/apps/admin/public/brand/apple-touch-icon.png +0 -0
- package/packages/brand/apps/admin/public/brand/favicon.svg +0 -1
- package/packages/brand/apps/admin/public/brand/og-image.png +0 -0
- package/packages/brand/apps/admin/public/brand/openadminjs-icon-new.png +0 -0
- package/packages/brand/apps/admin/public/brand/openadminjs-logo-monochrome.png +0 -0
- package/packages/brand/apps/admin/public/brand/openadminjs-logo-new.png +0 -0
- package/packages/brand/apps/web/public/brand/apple-touch-icon.png +0 -0
- package/packages/brand/apps/web/public/brand/favicon.svg +0 -1
- package/packages/brand/apps/web/public/brand/og-image.png +0 -0
- package/packages/brand/apps/web/public/brand/openadminjs-icon-new.png +0 -0
- package/packages/brand/apps/web/public/brand/openadminjs-logo-monochrome.png +0 -0
- package/packages/brand/apps/web/public/brand/openadminjs-logo-new.png +0 -0
- package/packages/brand/package.json +0 -19
- package/packages/brand/site/assets/brand/favicon.svg +0 -1
- package/packages/brand/site/assets/brand/og-image.svg +0 -1
- package/packages/brand/site/assets/brand/openadminjs-icon.svg +0 -1
- package/packages/brand/site/assets/brand/openadminjs-logo-dark.svg +0 -1
- package/packages/brand/site/assets/brand/openadminjs-logo-horizontal.svg +0 -1
- package/packages/brand/site/assets/brand/openadminjs-logo-monochrome.svg +0 -1
- package/packages/brand/site/assets/brand/openadminjs-logo.svg +0 -1
- package/packages/brand/src/generate-assets.mjs +0 -182
- package/packages/brand/src/index.test.ts +0 -9
- package/packages/brand/src/index.ts +0 -7
- package/packages/brand/tsconfig.json +0 -7
- package/packages/cli/package.json +0 -44
- package/packages/cli/src/create-project.ts +0 -316
- package/packages/cli/src/index.test.ts +0 -8
- package/packages/cli/src/index.ts +0 -149
- package/packages/cli/src/resource-slug.test.ts +0 -10
- package/packages/cli/src/resource-slug.ts +0 -4
- package/packages/cli/template/.env.example +0 -6
- package/packages/cli/template/README.md +0 -13
- package/packages/cli/template/apps/web/README.md +0 -12
- package/packages/cli/template/apps/web/app/globals.css +0 -12
- package/packages/cli/template/apps/web/app/layout.tsx +0 -18
- package/packages/cli/template/apps/web/app/page.tsx +0 -27
- package/packages/cli/template/apps/web/app/posts/[slug]/page.tsx +0 -38
- package/packages/cli/template/apps/web/app/posts/page.tsx +0 -20
- package/packages/cli/template/apps/web/app/robots.txt/route.ts +0 -9
- package/packages/cli/template/apps/web/app/sitemap.xml/route.ts +0 -20
- package/packages/cli/template/apps/web/components/public-fields.tsx +0 -22
- package/packages/cli/template/apps/web/lib/openadmin-client.ts +0 -71
- package/packages/cli/template/apps/web/lib/seo.ts +0 -42
- package/packages/cli/template/apps/web/next-env.d.ts +0 -2
- package/packages/cli/template/apps/web/next.config.mjs +0 -5
- package/packages/cli/template/apps/web/package.json +0 -29
- package/packages/cli/template/apps/web/postcss.config.mjs +0 -6
- package/packages/cli/template/apps/web/public/brand/apple-touch-icon.png +0 -0
- package/packages/cli/template/apps/web/public/brand/favicon.svg +0 -1
- package/packages/cli/template/apps/web/public/brand/og-image.png +0 -0
- package/packages/cli/template/apps/web/public/brand/openadminjs-icon-new.png +0 -0
- package/packages/cli/template/apps/web/public/brand/openadminjs-logo-monochrome.png +0 -0
- package/packages/cli/template/apps/web/public/brand/openadminjs-logo-new.png +0 -0
- package/packages/cli/template/apps/web/tailwind.config.ts +0 -21
- package/packages/cli/template/apps/web/tsconfig.json +0 -23
- package/packages/cli/template/package.json +0 -19
- package/packages/cli/template/pnpm-workspace.yaml +0 -3
- package/packages/cli/tsconfig.json +0 -10
- package/packages/core/package.json +0 -20
- package/packages/core/src/index.js +0 -1
- package/packages/core/src/index.test.ts +0 -27
- package/packages/core/src/index.ts +0 -61
- package/packages/core/tsconfig.json +0 -8
- package/packages/create-openadminjs/package.json +0 -25
- package/packages/create-openadminjs/src/index.ts +0 -122
- package/packages/create-openadminjs/src/package-name.test.ts +0 -13
- package/packages/create-openadminjs/src/package-name.ts +0 -8
- package/packages/create-openadminjs/src/render-files.test.ts +0 -26
- package/packages/create-openadminjs/src/render-files.ts +0 -21
- package/packages/create-openadminjs/template/.env.example +0 -7
- package/packages/create-openadminjs/template/README.md +0 -13
- package/packages/create-openadminjs/template/apps/web/README.md +0 -12
- package/packages/create-openadminjs/template/apps/web/app/globals.css +0 -12
- package/packages/create-openadminjs/template/apps/web/app/layout.tsx +0 -18
- package/packages/create-openadminjs/template/apps/web/app/page.tsx +0 -27
- package/packages/create-openadminjs/template/apps/web/app/posts/[slug]/page.tsx +0 -38
- package/packages/create-openadminjs/template/apps/web/app/posts/page.tsx +0 -20
- package/packages/create-openadminjs/template/apps/web/app/robots.txt/route.ts +0 -9
- package/packages/create-openadminjs/template/apps/web/app/sitemap.xml/route.ts +0 -20
- package/packages/create-openadminjs/template/apps/web/components/public-fields.tsx +0 -22
- package/packages/create-openadminjs/template/apps/web/lib/openadmin-client.ts +0 -71
- package/packages/create-openadminjs/template/apps/web/lib/seo.ts +0 -42
- package/packages/create-openadminjs/template/apps/web/next-env.d.ts +0 -2
- package/packages/create-openadminjs/template/apps/web/next.config.mjs +0 -5
- package/packages/create-openadminjs/template/apps/web/package.json +0 -29
- package/packages/create-openadminjs/template/apps/web/postcss.config.mjs +0 -6
- package/packages/create-openadminjs/template/apps/web/public/brand/apple-touch-icon.png +0 -0
- package/packages/create-openadminjs/template/apps/web/public/brand/favicon.svg +0 -1
- package/packages/create-openadminjs/template/apps/web/public/brand/og-image.png +0 -0
- package/packages/create-openadminjs/template/apps/web/public/brand/openadminjs-icon-new.png +0 -0
- package/packages/create-openadminjs/template/apps/web/public/brand/openadminjs-logo-monochrome.png +0 -0
- package/packages/create-openadminjs/template/apps/web/public/brand/openadminjs-logo-new.png +0 -0
- package/packages/create-openadminjs/template/apps/web/tailwind.config.ts +0 -21
- package/packages/create-openadminjs/template/apps/web/tsconfig.json +0 -23
- package/packages/create-openadminjs/template/package.json +0 -19
- package/packages/create-openadminjs/template/pnpm-workspace.yaml +0 -3
- package/packages/create-openadminjs/tsconfig.json +0 -10
- package/packages/mail/package.json +0 -18
- package/packages/mail/src/index.test.ts +0 -23
- package/packages/mail/src/index.ts +0 -246
- package/packages/mail/tsconfig.json +0 -5
- package/packages/notifications/package.json +0 -16
- package/packages/notifications/src/index.test.ts +0 -9
- package/packages/notifications/src/index.ts +0 -5
- package/packages/notifications/tsconfig.json +0 -5
- package/packages/payments/package.json +0 -15
- package/packages/payments/src/index.ts +0 -256
- package/packages/payments/tsconfig.json +0 -9
- package/packages/permissions/package.json +0 -17
- package/packages/permissions/src/blueprints.js +0 -57
- package/packages/permissions/src/blueprints.test.ts +0 -62
- package/packages/permissions/src/blueprints.ts +0 -89
- package/packages/permissions/src/index.js +0 -11
- package/packages/permissions/src/index.test.ts +0 -34
- package/packages/permissions/src/index.ts +0 -22
- package/packages/permissions/tsconfig.json +0 -8
- package/packages/plugin-sdk/package.json +0 -26
- package/packages/plugin-sdk/src/hooks.ts +0 -135
- package/packages/plugin-sdk/src/index.ts +0 -42
- package/packages/plugin-sdk/src/sdk.test.ts +0 -13
- package/packages/plugin-sdk/tsconfig.json +0 -8
- package/packages/plugin-sdk/vitest.config.ts +0 -5
- package/packages/queue/package.json +0 -16
- package/packages/queue/src/index.test.ts +0 -12
- package/packages/queue/src/index.ts +0 -6
- package/packages/queue/tsconfig.json +0 -5
- package/packages/resource/package.json +0 -17
- package/packages/resource/src/index.js +0 -177
- package/packages/resource/src/index.test.ts +0 -151
- package/packages/resource/src/index.ts +0 -344
- package/packages/resource/tsconfig.json +0 -8
- package/packages/storage/package.json +0 -16
- package/packages/storage/src/index.test.ts +0 -10
- package/packages/storage/src/index.ts +0 -23
- package/packages/storage/tsconfig.json +0 -5
- package/pnpm-workspace.yaml +0 -4
- package/prisma/migrations/20260505151751_baseline_init_all_tables/migration.sql +0 -371
- package/prisma/migrations/migration_lock.toml +0 -3
- package/prisma/schema.prisma +0 -291
- package/prisma/seed.ts +0 -210
- package/scripts/check-static-site.mjs +0 -17
- package/scripts/e2e-smoke.mjs +0 -17
- package/scripts/make-executable.mjs +0 -4
- package/site/.nojekyll +0 -0
- package/site/assets/brand/apple-touch-icon.png +0 -0
- package/site/assets/brand/favicon.svg +0 -1
- package/site/assets/brand/og-image.png +0 -0
- package/site/assets/brand/openadminjs-icon-new.png +0 -0
- package/site/assets/brand/openadminjs-logo-monochrome.png +0 -0
- package/site/assets/brand/openadminjs-logo-new.png +0 -0
- package/site/assets/css/styles.css +0 -1521
- package/site/assets/js/main.js +0 -193
- package/site/assets/partners/arcbyte.svg +0 -11
- package/site/assets/partners/gridstack.svg +0 -8
- package/site/assets/partners/nebula.svg +0 -12
- package/site/assets/partners/orbitlab.svg +0 -7
- package/site/assets/partners/pulseforge.svg +0 -11
- package/site/assets/partners/vertex.svg +0 -6
- package/site/docs.html +0 -1459
- package/site/index.html +0 -357
- package/tsconfig.base.json +0 -23
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import "./resources/resource-hooks.install";
|
|
2
|
-
import "./plugins/bootstrap";
|
|
3
|
-
import { existsSync } from "node:fs";
|
|
4
|
-
import { join } from "node:path";
|
|
5
|
-
import { Module } from "@nestjs/common";
|
|
6
|
-
import { APP_INTERCEPTOR } from "@nestjs/core";
|
|
7
|
-
import { ConfigModule } from "@nestjs/config";
|
|
8
|
-
import { ThrottlerModule } from "@nestjs/throttler";
|
|
9
|
-
import { PrismaService } from "./common/prisma.service";
|
|
10
|
-
import { AuthModule } from "./auth/auth.module";
|
|
11
|
-
import { AdminModule } from "./admin/admin.module";
|
|
12
|
-
import { HealthController } from "./health/health.controller";
|
|
13
|
-
import { QueueModule } from "./queue/queue.module";
|
|
14
|
-
import { StoreModule } from "./store/store.module";
|
|
15
|
-
import { MailModule } from "./mail/mail.module";
|
|
16
|
-
import { PluginApiInterceptor } from "./plugins/plugin-api.interceptor";
|
|
17
|
-
|
|
18
|
-
const envFilePath = [join(process.cwd(), ".env"), join(process.cwd(), "apps", "api", ".env")].filter((p) =>
|
|
19
|
-
existsSync(p)
|
|
20
|
-
);
|
|
21
|
-
|
|
22
|
-
@Module({
|
|
23
|
-
imports: [
|
|
24
|
-
ConfigModule.forRoot({
|
|
25
|
-
isGlobal: true,
|
|
26
|
-
envFilePath: envFilePath.length > 0 ? envFilePath : [".env"]
|
|
27
|
-
}),
|
|
28
|
-
ThrottlerModule.forRoot([{ ttl: 60000, limit: 100 }]),
|
|
29
|
-
AuthModule,
|
|
30
|
-
AdminModule,
|
|
31
|
-
MailModule,
|
|
32
|
-
QueueModule,
|
|
33
|
-
StoreModule
|
|
34
|
-
],
|
|
35
|
-
controllers: [HealthController],
|
|
36
|
-
providers: [
|
|
37
|
-
PrismaService,
|
|
38
|
-
{
|
|
39
|
-
provide: APP_INTERCEPTOR,
|
|
40
|
-
useClass: PluginApiInterceptor
|
|
41
|
-
}
|
|
42
|
-
],
|
|
43
|
-
exports: [PrismaService]
|
|
44
|
-
})
|
|
45
|
-
export class AppModule {}
|
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
import { Body, Controller, Get, Inject, Post, Req, UseGuards } from "@nestjs/common";
|
|
2
|
-
import { Throttle } from "@nestjs/throttler";
|
|
3
|
-
import { DEFAULT_AUTH_REALM } from "@openadminjs/permissions";
|
|
4
|
-
import { AuthGuard } from "../common/auth.guard";
|
|
5
|
-
import type { RequestUser } from "../common/auth.guard";
|
|
6
|
-
import { RequireAuthRealm } from "../common/auth-realm.decorator";
|
|
7
|
-
import { CurrentUser } from "../common/current-user.decorator";
|
|
8
|
-
import { AuthService } from "./auth.service";
|
|
9
|
-
import { ForgotPasswordDto, LoginDto, RefreshDto, ResetPasswordDto, VerifyEmailDto } from "./auth.dto";
|
|
10
|
-
|
|
11
|
-
@Controller("auth")
|
|
12
|
-
export class AuthController {
|
|
13
|
-
constructor(@Inject(AuthService) private readonly auth: AuthService) {}
|
|
14
|
-
|
|
15
|
-
@Throttle({ default: { limit: 5, ttl: 60000 } })
|
|
16
|
-
@Post("login")
|
|
17
|
-
login(@Body() dto: LoginDto, @Req() req: Request & { ip?: string; headers: Record<string, string> }) {
|
|
18
|
-
const realm = dto.realm ?? DEFAULT_AUTH_REALM;
|
|
19
|
-
return this.auth.login(
|
|
20
|
-
dto.email,
|
|
21
|
-
dto.password,
|
|
22
|
-
{
|
|
23
|
-
ipAddress: req.ip,
|
|
24
|
-
userAgent: req.headers["user-agent"]
|
|
25
|
-
},
|
|
26
|
-
realm
|
|
27
|
-
);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
@Post("refresh")
|
|
31
|
-
refresh(@Body() dto: RefreshDto) {
|
|
32
|
-
const realm = dto.realm ?? DEFAULT_AUTH_REALM;
|
|
33
|
-
return this.auth.refresh(dto.refreshToken, realm);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
@UseGuards(AuthGuard)
|
|
37
|
-
@Post("logout")
|
|
38
|
-
logout(@CurrentUser() user: { id: string }, @Req() req: Request & { ip?: string; headers: Record<string, string> }) {
|
|
39
|
-
return this.auth.logout(user.id, { ipAddress: req.ip, userAgent: req.headers["user-agent"] });
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
@UseGuards(AuthGuard)
|
|
43
|
-
@Get("me")
|
|
44
|
-
me(@CurrentUser() user: RequestUser) {
|
|
45
|
-
return this.auth.me(user.id, user.realm);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/** Cross-realm role summary (admin JWT only — avoids leaking admin role names to storefront tokens). */
|
|
49
|
-
@UseGuards(AuthGuard)
|
|
50
|
-
@RequireAuthRealm("admin")
|
|
51
|
-
@Get("me/realms")
|
|
52
|
-
membership(@CurrentUser() user: RequestUser) {
|
|
53
|
-
return this.auth.membershipSummary(user.id);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
@Post("password/forgot")
|
|
57
|
-
forgotPassword(@Body() dto: ForgotPasswordDto) {
|
|
58
|
-
return this.auth.forgotPassword(dto.email);
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
@Post("password/reset")
|
|
62
|
-
resetPassword(@Body() dto: ResetPasswordDto) {
|
|
63
|
-
return this.auth.resetPassword(dto.token, dto.password);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
@Post("email/verify")
|
|
67
|
-
verifyEmail(@Body() dto: VerifyEmailDto) {
|
|
68
|
-
return this.auth.verifyEmail(dto.token);
|
|
69
|
-
}
|
|
70
|
-
}
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import { IsEmail, IsOptional, IsString, Matches, MinLength } from "class-validator";
|
|
2
|
-
|
|
3
|
-
/** Safe realm segment for `Role.realm` / JWT (extend pattern in DB if you need more). */
|
|
4
|
-
const REALM_SLUG = /^[a-z][a-z0-9_-]{0,63}$/i;
|
|
5
|
-
|
|
6
|
-
export class LoginDto {
|
|
7
|
-
@IsEmail()
|
|
8
|
-
email!: string;
|
|
9
|
-
|
|
10
|
-
@IsString()
|
|
11
|
-
@MinLength(8)
|
|
12
|
-
password!: string;
|
|
13
|
-
|
|
14
|
-
/** When omitted, defaults to `admin` (back-office). Use `public` for storefront tokens, or any custom realm you seed. */
|
|
15
|
-
@IsOptional()
|
|
16
|
-
@IsString()
|
|
17
|
-
@Matches(REALM_SLUG, { message: "realm must be a short slug (e.g. admin, public, partner_api)" })
|
|
18
|
-
realm?: string;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export class RefreshDto {
|
|
22
|
-
@IsString()
|
|
23
|
-
refreshToken!: string;
|
|
24
|
-
|
|
25
|
-
/** Must match the realm used when the refresh token was issued (same default as login). */
|
|
26
|
-
@IsOptional()
|
|
27
|
-
@IsString()
|
|
28
|
-
@Matches(REALM_SLUG, { message: "realm must be a short slug (e.g. admin, public, partner_api)" })
|
|
29
|
-
realm?: string;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export class ForgotPasswordDto {
|
|
33
|
-
@IsEmail()
|
|
34
|
-
email!: string;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export class ResetPasswordDto {
|
|
38
|
-
@IsString()
|
|
39
|
-
token!: string;
|
|
40
|
-
|
|
41
|
-
@IsString()
|
|
42
|
-
@MinLength(8)
|
|
43
|
-
password!: string;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export class VerifyEmailDto {
|
|
47
|
-
@IsString()
|
|
48
|
-
token!: string;
|
|
49
|
-
}
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import { Module } from "@nestjs/common";
|
|
2
|
-
import { ConfigModule, ConfigService } from "@nestjs/config";
|
|
3
|
-
import { JwtModule } from "@nestjs/jwt";
|
|
4
|
-
import { AuthGuard } from "../common/auth.guard";
|
|
5
|
-
import { PrismaService } from "../common/prisma.service";
|
|
6
|
-
import { MailService } from "../mail/mail.service";
|
|
7
|
-
import { AuthController } from "./auth.controller";
|
|
8
|
-
import { AuthService } from "./auth.service";
|
|
9
|
-
|
|
10
|
-
@Module({
|
|
11
|
-
imports: [
|
|
12
|
-
JwtModule.registerAsync({
|
|
13
|
-
imports: [ConfigModule],
|
|
14
|
-
inject: [ConfigService],
|
|
15
|
-
useFactory: (config: ConfigService) => {
|
|
16
|
-
const secret = config.get<string>("JWT_SECRET")?.trim();
|
|
17
|
-
if (!secret) {
|
|
18
|
-
throw new Error(
|
|
19
|
-
"JWT_SECRET is not set. Add it to apps/api/.env (see README). Example: JWT_SECRET=$(openssl rand -hex 32)"
|
|
20
|
-
);
|
|
21
|
-
}
|
|
22
|
-
return { secret, signOptions: { expiresIn: "15m" } };
|
|
23
|
-
}
|
|
24
|
-
})
|
|
25
|
-
],
|
|
26
|
-
controllers: [AuthController],
|
|
27
|
-
providers: [AuthService, PrismaService, AuthGuard, MailService],
|
|
28
|
-
exports: [AuthService, AuthGuard]
|
|
29
|
-
})
|
|
30
|
-
export class AuthModule {}
|
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
import { UnauthorizedException } from "@nestjs/common";
|
|
2
|
-
import { describe, expect, it, vi } from "vitest";
|
|
3
|
-
|
|
4
|
-
vi.mock("bcryptjs", () => ({
|
|
5
|
-
default: {
|
|
6
|
-
compare: vi.fn(),
|
|
7
|
-
hash: vi.fn().mockResolvedValue("hashed")
|
|
8
|
-
}
|
|
9
|
-
}));
|
|
10
|
-
|
|
11
|
-
import bcrypt from "bcryptjs";
|
|
12
|
-
import { AuthService } from "./auth.service";
|
|
13
|
-
|
|
14
|
-
const bcryptCompareMock = bcrypt.compare as unknown as ReturnType<typeof vi.fn>;
|
|
15
|
-
|
|
16
|
-
function createPrismaMock() {
|
|
17
|
-
return {
|
|
18
|
-
user: {
|
|
19
|
-
findUnique: vi.fn(),
|
|
20
|
-
findMany: vi.fn(),
|
|
21
|
-
update: vi.fn()
|
|
22
|
-
},
|
|
23
|
-
auditLog: {
|
|
24
|
-
create: vi.fn()
|
|
25
|
-
}
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
describe("AuthService", () => {
|
|
30
|
-
it("login succeeds for valid credentials and admin realm", async () => {
|
|
31
|
-
const prisma = createPrismaMock();
|
|
32
|
-
const jwt = { signAsync: vi.fn().mockResolvedValue("jwt") };
|
|
33
|
-
prisma.user.findUnique.mockResolvedValue({
|
|
34
|
-
id: "u1",
|
|
35
|
-
email: "openadminjs@proton.me",
|
|
36
|
-
name: "Admin",
|
|
37
|
-
passwordHash: "hash",
|
|
38
|
-
roles: [{ role: { name: "admin", realm: "admin", permissions: [{ permission: { name: "*" } }] } }]
|
|
39
|
-
});
|
|
40
|
-
bcryptCompareMock.mockResolvedValue(true);
|
|
41
|
-
|
|
42
|
-
const service = new AuthService(prisma as never, jwt as never);
|
|
43
|
-
const result = await service.login("openadminjs@proton.me", "password", {}, "admin");
|
|
44
|
-
|
|
45
|
-
expect(result.accessToken).toBe("jwt");
|
|
46
|
-
expect(result.user.email).toBe("openadminjs@proton.me");
|
|
47
|
-
expect(prisma.auditLog.create).toHaveBeenCalled();
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
it("login fails for wrong realm", async () => {
|
|
51
|
-
const prisma = createPrismaMock();
|
|
52
|
-
const jwt = { signAsync: vi.fn().mockResolvedValue("jwt") };
|
|
53
|
-
prisma.user.findUnique.mockResolvedValue({
|
|
54
|
-
id: "u1",
|
|
55
|
-
email: "openadminjs@proton.me",
|
|
56
|
-
name: "Admin",
|
|
57
|
-
passwordHash: "hash",
|
|
58
|
-
roles: [{ role: { name: "admin", realm: "public", permissions: [] } }]
|
|
59
|
-
});
|
|
60
|
-
bcryptCompareMock.mockResolvedValue(true);
|
|
61
|
-
|
|
62
|
-
const service = new AuthService(prisma as never, jwt as never);
|
|
63
|
-
await expect(service.login("openadminjs@proton.me", "password", {}, "admin")).rejects.toBeInstanceOf(UnauthorizedException);
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
it("refresh fails when token does not match any user", async () => {
|
|
67
|
-
const prisma = createPrismaMock();
|
|
68
|
-
const jwt = { signAsync: vi.fn().mockResolvedValue("jwt") };
|
|
69
|
-
prisma.user.findMany.mockResolvedValue([{ id: "u1", refreshTokenHash: "hash-1" }]);
|
|
70
|
-
bcryptCompareMock.mockResolvedValue(false);
|
|
71
|
-
|
|
72
|
-
const service = new AuthService(prisma as never, jwt as never);
|
|
73
|
-
await expect(service.refresh("bad-token", "admin")).rejects.toBeInstanceOf(UnauthorizedException);
|
|
74
|
-
});
|
|
75
|
-
});
|
|
@@ -1,231 +0,0 @@
|
|
|
1
|
-
import { BadRequestException, Inject, Injectable, Optional, UnauthorizedException } from "@nestjs/common";
|
|
2
|
-
import { JwtService } from "@nestjs/jwt";
|
|
3
|
-
import { DEFAULT_AUTH_REALM } from "@openadminjs/permissions";
|
|
4
|
-
import bcrypt from "bcryptjs";
|
|
5
|
-
import { createHash, randomBytes } from "node:crypto";
|
|
6
|
-
import { PrismaService } from "../common/prisma.service";
|
|
7
|
-
import { MailService } from "../mail/mail.service";
|
|
8
|
-
|
|
9
|
-
@Injectable()
|
|
10
|
-
export class AuthService {
|
|
11
|
-
constructor(
|
|
12
|
-
@Inject(PrismaService) private readonly prisma: PrismaService,
|
|
13
|
-
@Inject(JwtService) private readonly jwt: JwtService,
|
|
14
|
-
@Optional() private readonly mail?: MailService
|
|
15
|
-
) {}
|
|
16
|
-
|
|
17
|
-
async login(
|
|
18
|
-
email: string,
|
|
19
|
-
password: string,
|
|
20
|
-
meta: { ipAddress?: string; userAgent?: string },
|
|
21
|
-
realm: string = DEFAULT_AUTH_REALM
|
|
22
|
-
) {
|
|
23
|
-
const user = await this.prisma.user.findUnique({
|
|
24
|
-
where: { email },
|
|
25
|
-
include: { roles: { include: { role: { include: { permissions: { include: { permission: true } } } } } } }
|
|
26
|
-
});
|
|
27
|
-
if (!user || !(await bcrypt.compare(password, user.passwordHash))) {
|
|
28
|
-
throw new UnauthorizedException({ message: "Invalid email or password", code: "AUTH_INVALID" });
|
|
29
|
-
}
|
|
30
|
-
const assignments = user.roles.filter((ur) => ur.role.realm === realm);
|
|
31
|
-
if (assignments.length === 0) {
|
|
32
|
-
throw new UnauthorizedException({
|
|
33
|
-
message: "Invalid email or password",
|
|
34
|
-
code: "AUTH_REALM_DENIED"
|
|
35
|
-
});
|
|
36
|
-
}
|
|
37
|
-
const session = await this.issueSession(user.id, realm);
|
|
38
|
-
await this.prisma.auditLog.create({
|
|
39
|
-
data: {
|
|
40
|
-
userId: user.id,
|
|
41
|
-
action: "login",
|
|
42
|
-
ipAddress: meta.ipAddress,
|
|
43
|
-
userAgent: meta.userAgent,
|
|
44
|
-
after: { realm }
|
|
45
|
-
}
|
|
46
|
-
});
|
|
47
|
-
return {
|
|
48
|
-
...session,
|
|
49
|
-
realm,
|
|
50
|
-
user: {
|
|
51
|
-
id: user.id,
|
|
52
|
-
email: user.email,
|
|
53
|
-
name: user.name,
|
|
54
|
-
roles: assignments.map((item) => item.role.name),
|
|
55
|
-
permissions: assignments.flatMap((item) => item.role.permissions.map((permission) => permission.permission.name))
|
|
56
|
-
}
|
|
57
|
-
};
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
async issueSession(userId: string, realm: string = DEFAULT_AUTH_REALM) {
|
|
61
|
-
const refreshToken = randomBytes(48).toString("base64url");
|
|
62
|
-
await this.prisma.user.update({
|
|
63
|
-
where: { id: userId },
|
|
64
|
-
data: { refreshTokenHash: await bcrypt.hash(refreshToken, 12) }
|
|
65
|
-
});
|
|
66
|
-
return {
|
|
67
|
-
accessToken: await this.jwt.signAsync({ sub: userId, realm }),
|
|
68
|
-
refreshToken,
|
|
69
|
-
expiresIn: 900
|
|
70
|
-
};
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
async refresh(refreshToken: string, realm: string = DEFAULT_AUTH_REALM) {
|
|
74
|
-
const users = await this.prisma.user.findMany({ where: { refreshTokenHash: { not: null } } });
|
|
75
|
-
const user = await users.reduce<Promise<(typeof users)[number] | undefined>>(async (match, candidate) => {
|
|
76
|
-
const found = await match;
|
|
77
|
-
if (found) return found;
|
|
78
|
-
return candidate.refreshTokenHash && (await bcrypt.compare(refreshToken, candidate.refreshTokenHash)) ? candidate : undefined;
|
|
79
|
-
}, Promise.resolve(undefined));
|
|
80
|
-
if (!user) throw new UnauthorizedException({ message: "Invalid refresh token", code: "REFRESH_INVALID" });
|
|
81
|
-
return this.issueSession(user.id, realm);
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
async logout(userId: string, meta: { ipAddress?: string; userAgent?: string }) {
|
|
85
|
-
await this.prisma.user.update({ where: { id: userId }, data: { refreshTokenHash: null } });
|
|
86
|
-
await this.prisma.auditLog.create({
|
|
87
|
-
data: { userId, action: "logout", ipAddress: meta.ipAddress, userAgent: meta.userAgent }
|
|
88
|
-
});
|
|
89
|
-
return { ok: true };
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
async forgotPassword(email: string) {
|
|
93
|
-
const user = await this.prisma.user.findUnique({ where: { email } });
|
|
94
|
-
if (!user) return { ok: true, message: "If the email exists, reset instructions will be sent." };
|
|
95
|
-
|
|
96
|
-
const rawToken = randomBytes(32).toString("base64url");
|
|
97
|
-
const tokenHash = this.hashToken(rawToken);
|
|
98
|
-
const expiresAt = new Date(Date.now() + 1000 * 60 * 30);
|
|
99
|
-
|
|
100
|
-
await this.prisma.passwordResetToken.create({
|
|
101
|
-
data: {
|
|
102
|
-
userId: user.id,
|
|
103
|
-
tokenHash,
|
|
104
|
-
expiresAt
|
|
105
|
-
}
|
|
106
|
-
});
|
|
107
|
-
|
|
108
|
-
await this.prisma.auditLog.create({
|
|
109
|
-
data: { userId: user.id, action: "password.reset.requested", resource: "auth", after: { expiresAt } }
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
if (this.mail) {
|
|
113
|
-
await this.mail.sendPasswordReset(user.email, user.name ?? user.email, rawToken).catch(() => {
|
|
114
|
-
// Don't fail the request if mail sending fails
|
|
115
|
-
});
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
if (process.env.NODE_ENV !== "production") return { ok: true, message: "Reset token generated", token: rawToken };
|
|
119
|
-
return { ok: true, message: "If the email exists, reset instructions will be sent." };
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
async resetPassword(token: string, password: string) {
|
|
123
|
-
const tokenHash = this.hashToken(token);
|
|
124
|
-
const now = new Date();
|
|
125
|
-
const resetToken = await this.prisma.passwordResetToken.findFirst({
|
|
126
|
-
where: {
|
|
127
|
-
tokenHash,
|
|
128
|
-
usedAt: null,
|
|
129
|
-
expiresAt: { gt: now }
|
|
130
|
-
},
|
|
131
|
-
include: { user: true }
|
|
132
|
-
});
|
|
133
|
-
|
|
134
|
-
if (!resetToken) throw new BadRequestException({ message: "Invalid or expired reset token", code: "RESET_TOKEN_INVALID" });
|
|
135
|
-
|
|
136
|
-
const nextPasswordHash = await bcrypt.hash(password, 12);
|
|
137
|
-
await this.prisma.$transaction([
|
|
138
|
-
this.prisma.user.update({
|
|
139
|
-
where: { id: resetToken.userId },
|
|
140
|
-
data: { passwordHash: nextPasswordHash, refreshTokenHash: null }
|
|
141
|
-
}),
|
|
142
|
-
this.prisma.passwordResetToken.update({
|
|
143
|
-
where: { id: resetToken.id },
|
|
144
|
-
data: { usedAt: now }
|
|
145
|
-
}),
|
|
146
|
-
this.prisma.auditLog.create({
|
|
147
|
-
data: { userId: resetToken.userId, action: "password.reset.completed", resource: "auth" }
|
|
148
|
-
})
|
|
149
|
-
]);
|
|
150
|
-
|
|
151
|
-
return { ok: true };
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
async verifyEmail(token: string) {
|
|
155
|
-
const tokenHash = this.hashToken(token);
|
|
156
|
-
const now = new Date();
|
|
157
|
-
const verification = await this.prisma.emailVerificationToken.findFirst({
|
|
158
|
-
where: {
|
|
159
|
-
tokenHash,
|
|
160
|
-
usedAt: null,
|
|
161
|
-
expiresAt: { gt: now }
|
|
162
|
-
}
|
|
163
|
-
});
|
|
164
|
-
if (!verification) throw new BadRequestException({ message: "Invalid or expired verification token", code: "VERIFY_TOKEN_INVALID" });
|
|
165
|
-
|
|
166
|
-
await this.prisma.$transaction([
|
|
167
|
-
this.prisma.emailVerificationToken.update({
|
|
168
|
-
where: { id: verification.id },
|
|
169
|
-
data: { usedAt: now }
|
|
170
|
-
}),
|
|
171
|
-
this.prisma.user.update({
|
|
172
|
-
where: { id: verification.userId },
|
|
173
|
-
data: { status: "active" }
|
|
174
|
-
}),
|
|
175
|
-
this.prisma.auditLog.create({
|
|
176
|
-
data: { userId: verification.userId, action: "email.verified", resource: "auth" }
|
|
177
|
-
})
|
|
178
|
-
]);
|
|
179
|
-
|
|
180
|
-
return { ok: true };
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
async me(userId: string, realm: string = DEFAULT_AUTH_REALM) {
|
|
184
|
-
const user = await this.prisma.user.findUnique({
|
|
185
|
-
where: { id: userId },
|
|
186
|
-
include: { roles: { include: { role: { include: { permissions: { include: { permission: true } } } } } } }
|
|
187
|
-
});
|
|
188
|
-
if (!user) throw new UnauthorizedException({ message: "Invalid session", code: "AUTH_INVALID" });
|
|
189
|
-
const assignments = user.roles.filter((ur) => ur.role.realm === realm);
|
|
190
|
-
return {
|
|
191
|
-
id: user.id,
|
|
192
|
-
email: user.email,
|
|
193
|
-
name: user.name,
|
|
194
|
-
avatarUrl: user.avatarUrl,
|
|
195
|
-
status: user.status,
|
|
196
|
-
realm,
|
|
197
|
-
roles: assignments.map((item) => item.role.name),
|
|
198
|
-
permissions: assignments.flatMap((item) => item.role.permissions.map((permission) => permission.permission.name))
|
|
199
|
-
};
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
/** All role bindings across realms (account / picker UI). */
|
|
203
|
-
async membershipSummary(userId: string) {
|
|
204
|
-
const user = await this.prisma.user.findUnique({
|
|
205
|
-
where: { id: userId },
|
|
206
|
-
include: { roles: { include: { role: { include: { permissions: { include: { permission: true } } } } } } }
|
|
207
|
-
});
|
|
208
|
-
if (!user) throw new UnauthorizedException({ message: "Invalid session", code: "AUTH_INVALID" });
|
|
209
|
-
const byRealm = new Map<string, { roles: string[]; permissions: string[] }>();
|
|
210
|
-
for (const ur of user.roles) {
|
|
211
|
-
const r = ur.role.realm;
|
|
212
|
-
const bucket = byRealm.get(r) ?? { roles: [], permissions: [] };
|
|
213
|
-
bucket.roles.push(ur.role.name);
|
|
214
|
-
bucket.permissions.push(...ur.role.permissions.map((p) => p.permission.name));
|
|
215
|
-
byRealm.set(r, bucket);
|
|
216
|
-
}
|
|
217
|
-
return {
|
|
218
|
-
id: user.id,
|
|
219
|
-
email: user.email,
|
|
220
|
-
realms: [...byRealm.entries()].map(([realm, data]) => ({
|
|
221
|
-
realm,
|
|
222
|
-
roles: [...new Set(data.roles)].sort(),
|
|
223
|
-
permissions: [...new Set(data.permissions)].sort()
|
|
224
|
-
}))
|
|
225
|
-
};
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
private hashToken(value: string) {
|
|
229
|
-
return createHash("sha256").update(value).digest("hex");
|
|
230
|
-
}
|
|
231
|
-
}
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import { SetMetadata } from "@nestjs/common";
|
|
2
|
-
|
|
3
|
-
/** When set on a route/controller, JWT must have been issued for this `realm` (see `Role.realm`). */
|
|
4
|
-
export const AUTH_REALM_KEY = "openadminjs:authRealm";
|
|
5
|
-
|
|
6
|
-
export const RequireAuthRealm = (realm: string) => SetMetadata(AUTH_REALM_KEY, realm);
|
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
import { ForbiddenException, UnauthorizedException } from "@nestjs/common";
|
|
2
|
-
import { describe, expect, it, vi } from "vitest";
|
|
3
|
-
import { AuthGuard } from "./auth.guard";
|
|
4
|
-
|
|
5
|
-
function createContext(authHeader?: string, requiredRealm?: string) {
|
|
6
|
-
const request: { headers: Record<string, string>; user?: unknown } = { headers: {} };
|
|
7
|
-
if (authHeader) request.headers.authorization = authHeader;
|
|
8
|
-
return {
|
|
9
|
-
request,
|
|
10
|
-
ctx: {
|
|
11
|
-
switchToHttp: () => ({ getRequest: () => request }),
|
|
12
|
-
getHandler: () => "handler",
|
|
13
|
-
getClass: () => "class"
|
|
14
|
-
},
|
|
15
|
-
reflector: {
|
|
16
|
-
getAllAndOverride: vi.fn().mockReturnValue(requiredRealm)
|
|
17
|
-
}
|
|
18
|
-
};
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
const mockConfig = { get: vi.fn().mockReturnValue("test-secret") };
|
|
22
|
-
|
|
23
|
-
describe("AuthGuard", () => {
|
|
24
|
-
it("rejects missing token", async () => {
|
|
25
|
-
const { ctx, reflector } = createContext(undefined);
|
|
26
|
-
const guard = new AuthGuard({ verifyAsync: vi.fn() } as never, { user: { findUnique: vi.fn() } } as never, reflector as never, mockConfig as never);
|
|
27
|
-
await expect(guard.canActivate(ctx as never)).rejects.toBeInstanceOf(UnauthorizedException);
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
it("rejects realm mismatch", async () => {
|
|
31
|
-
const { ctx, reflector } = createContext("Bearer token", "public");
|
|
32
|
-
const guard = new AuthGuard(
|
|
33
|
-
{ verifyAsync: vi.fn().mockResolvedValue({ sub: "u1", realm: "admin" }) } as never,
|
|
34
|
-
{ user: { findUnique: vi.fn() } } as never,
|
|
35
|
-
reflector as never,
|
|
36
|
-
mockConfig as never
|
|
37
|
-
);
|
|
38
|
-
await expect(guard.canActivate(ctx as never)).rejects.toBeInstanceOf(ForbiddenException);
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
it("attaches request.user for valid token", async () => {
|
|
42
|
-
const { ctx, reflector, request } = createContext("Bearer token", "admin");
|
|
43
|
-
const guard = new AuthGuard(
|
|
44
|
-
{ verifyAsync: vi.fn().mockResolvedValue({ sub: "u1", realm: "admin" }) } as never,
|
|
45
|
-
{
|
|
46
|
-
user: {
|
|
47
|
-
findUnique: vi.fn().mockResolvedValue({
|
|
48
|
-
id: "u1",
|
|
49
|
-
email: "openadminjs@proton.me",
|
|
50
|
-
name: "Admin",
|
|
51
|
-
roles: [{ role: { name: "admin", realm: "admin", permissions: [{ permission: { name: "*" } }] } }]
|
|
52
|
-
})
|
|
53
|
-
}
|
|
54
|
-
} as never,
|
|
55
|
-
reflector as never,
|
|
56
|
-
mockConfig as never
|
|
57
|
-
);
|
|
58
|
-
await expect(guard.canActivate(ctx as never)).resolves.toBe(true);
|
|
59
|
-
expect((request.user as { id: string }).id).toBe("u1");
|
|
60
|
-
});
|
|
61
|
-
});
|
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
import { CanActivate, ExecutionContext, ForbiddenException, Injectable, UnauthorizedException } from "@nestjs/common";
|
|
2
|
-
import { ConfigService } from "@nestjs/config";
|
|
3
|
-
import { Reflector } from "@nestjs/core";
|
|
4
|
-
import { JwtService } from "@nestjs/jwt";
|
|
5
|
-
import { DEFAULT_AUTH_REALM } from "@openadminjs/permissions";
|
|
6
|
-
import { PrismaService } from "./prisma.service";
|
|
7
|
-
import { AUTH_REALM_KEY } from "./auth-realm.decorator";
|
|
8
|
-
|
|
9
|
-
export type RequestUser = {
|
|
10
|
-
id: string;
|
|
11
|
-
email?: string;
|
|
12
|
-
name?: string | null;
|
|
13
|
-
realm: string;
|
|
14
|
-
roles: string[];
|
|
15
|
-
permissions: string[];
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
@Injectable()
|
|
19
|
-
export class AuthGuard implements CanActivate {
|
|
20
|
-
constructor(
|
|
21
|
-
private readonly jwt: JwtService,
|
|
22
|
-
private readonly prisma: PrismaService,
|
|
23
|
-
private readonly reflector: Reflector,
|
|
24
|
-
private readonly config: ConfigService
|
|
25
|
-
) {}
|
|
26
|
-
|
|
27
|
-
async canActivate(context: ExecutionContext): Promise<boolean> {
|
|
28
|
-
const request = context.switchToHttp().getRequest();
|
|
29
|
-
const header = request.headers.authorization;
|
|
30
|
-
const token = header?.startsWith("Bearer ") ? header.slice(7) : undefined;
|
|
31
|
-
if (!token) throw new UnauthorizedException({ message: "Missing access token", code: "AUTH_REQUIRED" });
|
|
32
|
-
try {
|
|
33
|
-
const jwtSecret = this.config.get<string>("JWT_SECRET")?.trim() ?? process.env.JWT_SECRET?.trim();
|
|
34
|
-
const payload = await this.jwt.verifyAsync<{ sub: string; realm?: string }>(token, { secret: jwtSecret });
|
|
35
|
-
const realm = payload.realm ?? DEFAULT_AUTH_REALM;
|
|
36
|
-
|
|
37
|
-
const requiredRealm = this.reflector.getAllAndOverride<string | undefined>(AUTH_REALM_KEY, [
|
|
38
|
-
context.getHandler(),
|
|
39
|
-
context.getClass()
|
|
40
|
-
]);
|
|
41
|
-
if (requiredRealm !== undefined && requiredRealm !== realm) {
|
|
42
|
-
throw new ForbiddenException({
|
|
43
|
-
message: "Token was issued for a different application area",
|
|
44
|
-
code: "AUTH_REALM_MISMATCH",
|
|
45
|
-
details: { expected: requiredRealm, tokenRealm: realm }
|
|
46
|
-
});
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
const user = await this.prisma.user.findUnique({
|
|
50
|
-
where: { id: payload.sub },
|
|
51
|
-
include: { roles: { include: { role: { include: { permissions: { include: { permission: true } } } } } } }
|
|
52
|
-
});
|
|
53
|
-
if (!user) throw new UnauthorizedException({ message: "Invalid session", code: "AUTH_INVALID" });
|
|
54
|
-
|
|
55
|
-
const assignments = user.roles.filter((ur) => ur.role.realm === realm);
|
|
56
|
-
if (assignments.length === 0) {
|
|
57
|
-
throw new ForbiddenException({
|
|
58
|
-
message: "No roles for this application area",
|
|
59
|
-
code: "AUTH_REALM_EMPTY",
|
|
60
|
-
details: { realm }
|
|
61
|
-
});
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
request.user = {
|
|
65
|
-
id: user.id,
|
|
66
|
-
email: user.email,
|
|
67
|
-
name: user.name,
|
|
68
|
-
realm,
|
|
69
|
-
roles: assignments.map((item) => item.role.name),
|
|
70
|
-
permissions: assignments.flatMap((item) => item.role.permissions.map((p) => p.permission.name))
|
|
71
|
-
} satisfies RequestUser;
|
|
72
|
-
return true;
|
|
73
|
-
} catch (e) {
|
|
74
|
-
if (e instanceof ForbiddenException) throw e;
|
|
75
|
-
throw new UnauthorizedException({ message: "Invalid access token", code: "AUTH_INVALID" });
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
}
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from "vitest";
|
|
2
|
-
import { CaslAbilityFactory } from "./casl-ability.factory";
|
|
3
|
-
|
|
4
|
-
describe("CaslAbilityFactory", () => {
|
|
5
|
-
const factory = new CaslAbilityFactory();
|
|
6
|
-
|
|
7
|
-
it("maps dotted permissions to CASL rules", () => {
|
|
8
|
-
const ability = factory.createForUser({ permissions: ["posts.read", "users.update"] });
|
|
9
|
-
expect(ability.can("read", "posts")).toBe(true);
|
|
10
|
-
expect(ability.can("update", "users")).toBe(true);
|
|
11
|
-
expect(ability.can("delete", "posts")).toBe(false);
|
|
12
|
-
});
|
|
13
|
-
|
|
14
|
-
it("uses the last dot for namespaced slugs", () => {
|
|
15
|
-
const ability = factory.createForUser({ permissions: ["public.orders.read_own"] });
|
|
16
|
-
expect(ability.can("manage", "public.orders")).toBe(true);
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
it("grants manage all for star permission", () => {
|
|
20
|
-
const ability = factory.createForUser({ permissions: ["*"] });
|
|
21
|
-
expect(ability.can("manage", "all")).toBe(true);
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
it("maps unknown actions to manage", () => {
|
|
25
|
-
const ability = factory.createForUser({ permissions: ["reports.export"] });
|
|
26
|
-
expect(ability.can("manage", "reports")).toBe(true);
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
it("ignores malformed permission strings", () => {
|
|
30
|
-
const ability = factory.createForUser({ permissions: ["noparts", ".", "ok.read"] });
|
|
31
|
-
expect(ability.can("read", "ok")).toBe(true);
|
|
32
|
-
});
|
|
33
|
-
});
|