openadminjs 0.1.24 → 0.3.0-rc.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/README.md +117 -81
- package/dist/adapt-package-manager.d.ts +3 -0
- package/dist/adapt-package-manager.js +118 -0
- package/dist/create-project.d.ts +77 -2
- package/dist/create-project.js +685 -224
- package/dist/db-commands.d.ts +20 -0
- package/dist/db-commands.js +75 -0
- package/dist/detect-package-manager.d.ts +8 -0
- package/dist/detect-package-manager.js +37 -0
- package/dist/doctor.d.ts +26 -0
- package/dist/doctor.js +232 -0
- package/dist/index.d.ts +3 -1
- package/dist/index.js +476 -166
- package/dist/render-schema.d.ts +3 -0
- package/dist/render-schema.js +33 -0
- package/dist/resource-slug.d.ts +1 -0
- package/dist/resource-slug.js +7 -0
- package/dist/runtime-mode.d.ts +16 -0
- package/dist/runtime-mode.js +138 -0
- package/dist/secrets.d.ts +24 -0
- package/dist/secrets.js +93 -0
- package/dist/security.d.ts +13 -0
- package/dist/security.js +168 -0
- package/dist/upgrade.d.ts +10 -0
- package/dist/upgrade.js +133 -0
- package/dist/validate-name.d.ts +33 -0
- package/dist/validate-name.js +206 -0
- package/package.json +39 -11
- package/template/.dockerignore +15 -0
- package/template/.env.production.example +5 -0
- package/template/Dockerfile +12 -0
- package/template/README.md +23 -3
- package/template/apps/admin/app/api/auth/login/route.ts +9 -9
- package/template/apps/admin/app/api/auth/logout/route.ts +11 -3
- package/template/apps/admin/app/api/auth/oidc/callback/route.ts +19 -0
- package/template/apps/admin/app/api/auth/oidc/start/route.ts +11 -0
- package/template/apps/admin/app/api/auth/oidc/status/route.ts +6 -0
- package/template/apps/admin/app/api/auth/refresh/route.ts +18 -11
- package/template/apps/admin/app/api/backend/[...path]/route.ts +48 -0
- package/template/apps/admin/app/api-tokens/page.tsx +39 -39
- package/template/apps/admin/app/assistant/page.tsx +11 -0
- package/template/apps/admin/app/dashboard/page.tsx +45 -462
- package/template/apps/admin/app/globals.css +52 -16
- package/template/apps/admin/app/layout.tsx +1 -1
- package/template/apps/admin/app/login/page.tsx +13 -145
- package/template/apps/admin/app/plugins/page.tsx +16 -402
- package/template/apps/admin/app/profile/page.tsx +5 -2
- package/template/apps/admin/app/settings/page.tsx +14 -265
- package/template/apps/admin/components/ai-assistant-widget.tsx +3 -6
- package/template/apps/admin/components/app-shell.tsx +25 -373
- package/template/apps/admin/components/auth-guard.tsx +8 -28
- package/template/apps/admin/components/logo.tsx +1 -30
- package/template/apps/admin/components/resource-form.tsx +3 -3
- package/template/apps/admin/components/resource-table.tsx +12 -8
- package/template/apps/admin/lib/api.ts +45 -107
- package/template/apps/admin/lib/resource-table-prefs.ts +1 -57
- package/template/apps/admin/lib/server-api.ts +7 -0
- package/template/apps/admin/lib/tokens.ts +9 -81
- package/template/apps/admin/middleware.ts +9 -50
- package/template/apps/admin/next.config.mjs +4 -0
- package/template/apps/admin/package.json +2 -1
- package/template/apps/admin/public/brand/favicon.svg +4 -1
- package/template/apps/admin/tailwind.config.ts +9 -8
- package/template/apps/api/package.json +13 -6
- package/template/apps/api/src/access/catalog.ts +4 -0
- package/template/apps/api/src/admin/admin-ai.controller.ts +15 -125
- package/template/apps/api/src/admin/admin-analytics.service.ts +16 -148
- package/template/apps/api/src/admin/admin-logs.controller.ts +4 -1
- package/template/apps/api/src/admin/admin-plugins.controller.ts +8 -8
- package/template/apps/api/src/admin/admin-plugins.service.ts +15 -259
- package/template/apps/api/src/admin/admin-resource.service.ts +105 -254
- package/template/apps/api/src/admin/admin.controller.ts +17 -4
- package/template/apps/api/src/admin/admin.module.ts +2 -2
- package/template/apps/api/src/admin/resource-policy.ts +1 -0
- package/template/apps/api/src/admin/resource-query.ts +1 -0
- package/template/apps/api/src/admin/safe-ai.service.ts +43 -0
- package/template/apps/api/src/admin/validate-resource-payload.ts +2 -0
- package/template/apps/api/src/auth/auth.controller.ts +4 -2
- package/template/apps/api/src/auth/auth.service.ts +68 -83
- package/template/apps/api/src/auth/sso-oidc.controller.ts +12 -74
- package/template/apps/api/src/auth/sso-oidc.service.ts +51 -268
- package/template/apps/api/src/common/api-exception.filter.ts +1 -0
- package/template/apps/api/src/common/auth.guard.ts +66 -39
- package/template/apps/api/src/common/json-field-codec.ts +138 -0
- package/template/apps/api/src/common/prisma.service.ts +12 -7
- package/template/apps/api/src/common/request-logging.interceptor.ts +8 -3
- package/template/apps/api/src/graphql/graphql.module.ts +14 -1
- package/template/apps/api/src/health/health.controller.ts +6 -0
- package/template/apps/api/src/main.ts +11 -2
- package/template/apps/api/src/plugins/plugin-loader.ts +5 -3
- package/template/apps/api/src/resources/commerce.resource.ts +3 -34
- package/template/apps/api/src/resources/operations.resource.ts +1 -0
- package/template/apps/api/src/store/payment/payment.controller.ts +15 -92
- package/template/apps/api/src/store/payment/payment.module.ts +2 -3
- package/template/apps/api/src/store/payment/payment.service.ts +121 -227
- package/template/apps/api/webpack.config.cjs +2 -1
- package/template/apps/web/next.config.mjs +4 -0
- package/template/apps/web/public/brand/favicon.svg +4 -1
- package/template/compose.yaml +56 -0
- package/template/examples/crm-admin/README.md +31 -0
- package/template/examples/crm-admin/package.json +17 -0
- package/template/examples/crm-admin/prisma/models.fragment.prisma +50 -0
- package/template/examples/crm-admin/src/index.ts +7 -0
- package/template/examples/crm-admin/src/resources/activities.resource.ts +29 -0
- package/template/examples/crm-admin/src/resources/companies.resource.ts +21 -0
- package/template/examples/crm-admin/src/resources/contacts.resource.ts +24 -0
- package/template/examples/crm-admin/src/resources/deals.resource.ts +30 -0
- package/template/examples/crm-admin/tsconfig.json +7 -0
- package/template/package.json +8 -1
- package/template/packages/admin-client/package.json +18 -0
- package/template/packages/admin-client/src/contracts.ts +29 -0
- package/template/packages/admin-client/src/index.ts +2 -0
- package/template/packages/admin-client/src/preferences.ts +57 -0
- package/template/packages/admin-client/tsconfig.json +12 -0
- package/template/packages/auth/package.json +1 -1
- package/template/packages/brand/site/assets/brand/favicon.svg +4 -1
- package/template/packages/brand/src/generate-assets.mjs +17 -186
- package/template/packages/core/package.json +1 -1
- package/template/packages/mail/package.json +1 -1
- package/template/packages/nest/package.json +22 -0
- package/template/packages/nest/src/api-exception.filter.ts +23 -0
- package/template/packages/nest/src/index.ts +3 -0
- package/template/packages/nest/src/resource-policy.ts +64 -0
- package/template/packages/nest/src/resource-query.ts +103 -0
- package/template/packages/nest/tsconfig.json +12 -0
- package/template/packages/notifications/package.json +1 -1
- package/template/packages/payments/package.json +1 -1
- package/template/packages/permissions/package.json +1 -1
- package/template/packages/permissions/src/index.ts +2 -2
- package/template/packages/plugin-sdk/package.json +1 -1
- package/template/packages/plugin-sdk/src/index.ts +2 -2
- package/template/packages/prisma/package.json +21 -0
- package/template/packages/prisma/schema/ai.prisma +36 -0
- package/template/packages/prisma/schema/audit.prisma +13 -0
- package/template/packages/prisma/schema/commerce.prisma +79 -0
- package/template/packages/prisma/schema/content.prisma +27 -0
- package/template/packages/prisma/schema/core.prisma +106 -0
- package/template/packages/prisma/schema/header.prisma +8 -0
- package/template/packages/prisma/schema/jobs.prisma +9 -0
- package/template/packages/prisma/schema/media.prisma +9 -0
- package/template/packages/prisma/schema/modules.json +47 -0
- package/template/packages/prisma/schema/notifications.prisma +9 -0
- package/template/packages/prisma/schema/settings.prisma +8 -0
- package/template/packages/prisma/schema/tokens.prisma +10 -0
- package/template/packages/prisma/src/index.d.ts +12 -0
- package/template/packages/prisma/src/index.mjs +138 -0
- package/template/packages/queue/package.json +1 -1
- package/template/packages/resource/package.json +1 -1
- package/template/packages/resource/src/index.ts +1 -0
- package/template/packages/storage/package.json +1 -1
- package/template/packages/storage/src/index.ts +37 -0
- package/template/prisma/migrations/20260713120000_add_user_oidc_key_and_category_order/migration.sql +11 -0
- package/template/prisma/migrations/20261126130000_add_sessions/migration.sql +26 -0
- package/template/prisma/migrations/20261126140000_api_token_hash/migration.sql +3 -0
- package/template/prisma/migrations/20261126150000_payment_idempotency/migration.sql +6 -0
- package/template/prisma/schema.prisma +82 -54
- package/template/prisma/seed.ts +14 -38
- package/template/runtime-versions.json +15 -0
- package/template/scripts/db-init.mjs +33 -0
- package/template/scripts/install-crm.mjs +50 -0
- package/template/scripts/migrate-existing.mjs +65 -0
- package/template/scripts/schema.mjs +48 -0
- package/template/tsconfig.base.json +23 -5
- package/LICENSE +0 -21
- package/dist/create-project.test.d.ts +0 -1
- package/dist/create-project.test.js +0 -99
- package/dist/index.test.d.ts +0 -1
- package/dist/index.test.js +0 -7
- package/dist/resource-slug.test.d.ts +0 -1
- package/dist/resource-slug.test.js +0 -9
- package/template/apps/admin/.storybook/main.ts +0 -12
- package/template/apps/admin/.storybook/preview.ts +0 -10
- package/template/apps/admin/app/login/page.test.tsx +0 -18
- package/template/apps/admin/components/auth-guard.test.tsx +0 -55
- package/template/apps/admin/components/logo.stories.tsx +0 -24
- package/template/apps/admin/lib/api.test.ts +0 -118
- package/template/apps/admin/lib/auth-flow.e2e.test.ts +0 -79
- package/template/apps/admin/vitest.config.ts +0 -7
- package/template/apps/api/src/access/derive-permissions.test.ts +0 -26
- package/template/apps/api/src/admin/admin-resource.service.test.ts +0 -118
- package/template/apps/api/src/admin/validate-resource-payload.test.ts +0 -44
- package/template/apps/api/src/app.module.test.ts +0 -12
- package/template/apps/api/src/auth/auth.service.test.ts +0 -78
- package/template/apps/api/src/auth/sso-oidc.service.test.ts +0 -84
- package/template/apps/api/src/common/auth.guard.test.ts +0 -62
- package/template/apps/api/src/common/casl-ability.factory.test.ts +0 -33
- package/template/apps/api/src/common/request-logging.interceptor.test.ts +0 -39
- package/template/apps/api/src/graphql/admin-resources.resolver.test.ts +0 -45
- package/template/apps/api/src/graphql/ensure-gql-paramtypes.test.ts +0 -44
- package/template/apps/api/src/graphql/graphql-schema.test.ts +0 -26
- package/template/apps/api/src/plugins/manifest.schema.test.ts +0 -36
- package/template/apps/api/src/plugins/plugin-runtime.test.ts +0 -24
- package/template/apps/api/src/queue/queue.service.test.ts +0 -33
- package/template/apps/api/src/resources/resource-hooks.registry.test.ts +0 -72
- package/template/apps/api/vitest.config.ts +0 -21
- package/template/apps/web/lib/seo.test.ts +0 -41
- package/template/apps/web/vitest.config.ts +0 -19
- package/template/packages/auth/src/index.test.ts +0 -10
- package/template/packages/brand/src/index.test.ts +0 -9
- package/template/packages/core/src/index.test.ts +0 -27
- package/template/packages/core/vitest.config.ts +0 -9
- package/template/packages/mail/src/index.test.ts +0 -23
- package/template/packages/notifications/src/index.test.ts +0 -9
- package/template/packages/permissions/src/blueprints.test.ts +0 -62
- package/template/packages/permissions/src/index.test.ts +0 -34
- package/template/packages/plugin-sdk/src/sdk.test.ts +0 -13
- package/template/packages/plugin-sdk/vitest.config.ts +0 -5
- package/template/packages/queue/src/index.test.ts +0 -12
- package/template/packages/resource/src/index.test.ts +0 -196
- package/template/packages/storage/src/index.test.ts +0 -10
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
export declare class InvalidNameError extends Error {
|
|
2
|
+
constructor(message: string);
|
|
3
|
+
}
|
|
4
|
+
/**
|
|
5
|
+
* Documented valid input format:
|
|
6
|
+
* - must start with a letter
|
|
7
|
+
* - may contain letters, numbers, spaces, hyphens and underscores
|
|
8
|
+
* - separators are collapsed when converting to Pascal/camel/kebab case
|
|
9
|
+
*/
|
|
10
|
+
export declare const VALID_NAME_DESCRIPTION = "Names must start with a letter and contain only letters, numbers, spaces, hyphens or underscores.";
|
|
11
|
+
export declare function toPascalCase(input: string): string;
|
|
12
|
+
export declare function toCamelCase(input: string): string;
|
|
13
|
+
export declare function toKebabCase(input: string): string;
|
|
14
|
+
/**
|
|
15
|
+
* Validates a user-supplied resource/plugin/field name. Throws InvalidNameError
|
|
16
|
+
* on any unsafe or invalid input. Returns the trimmed raw value on success.
|
|
17
|
+
*
|
|
18
|
+
* Rejects: empty, `.`/`..`, path separators, absolute paths, null bytes,
|
|
19
|
+
* control characters, shell metacharacters, reserved OS names, reserved
|
|
20
|
+
* identifiers, and names that normalize to an empty string.
|
|
21
|
+
*/
|
|
22
|
+
export declare function validateName(raw: string | undefined, kind?: string): string;
|
|
23
|
+
/**
|
|
24
|
+
* Validates a plugin id. Plugin ids follow reverse-DNS style and may contain
|
|
25
|
+
* dots (e.g. `com.example.my-plugin`), but must not enable path traversal or
|
|
26
|
+
* shell injection. Returns the trimmed value on success.
|
|
27
|
+
*/
|
|
28
|
+
export declare function validatePluginId(raw: string | undefined): string;
|
|
29
|
+
/**
|
|
30
|
+
* Resolves `child` against `baseDir` and guarantees the result stays inside
|
|
31
|
+
* `baseDir`. Throws on any traversal attempt. Returns the resolved path.
|
|
32
|
+
*/
|
|
33
|
+
export declare function assertInsideDir(baseDir: string, child: string): string;
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
import { relative, resolve, isAbsolute } from "node:path";
|
|
2
|
+
/**
|
|
3
|
+
* Reserved Windows device names that must never be used as file/dir names.
|
|
4
|
+
* Rejected case-insensitively, with or without an extension.
|
|
5
|
+
*/
|
|
6
|
+
const RESERVED_NAMES = new Set([
|
|
7
|
+
"con",
|
|
8
|
+
"prn",
|
|
9
|
+
"aux",
|
|
10
|
+
"nul",
|
|
11
|
+
"com1",
|
|
12
|
+
"com2",
|
|
13
|
+
"com3",
|
|
14
|
+
"com4",
|
|
15
|
+
"com5",
|
|
16
|
+
"com6",
|
|
17
|
+
"com7",
|
|
18
|
+
"com8",
|
|
19
|
+
"com9",
|
|
20
|
+
"lpt1",
|
|
21
|
+
"lpt2",
|
|
22
|
+
"lpt3",
|
|
23
|
+
"lpt4",
|
|
24
|
+
"lpt5",
|
|
25
|
+
"lpt6",
|
|
26
|
+
"lpt7",
|
|
27
|
+
"lpt8",
|
|
28
|
+
"lpt9"
|
|
29
|
+
]);
|
|
30
|
+
/**
|
|
31
|
+
* TypeScript reserved words / keywords that would break generated identifiers
|
|
32
|
+
* if used verbatim as class or variable names.
|
|
33
|
+
*/
|
|
34
|
+
const RESERVED_IDENTIFIERS = new Set([
|
|
35
|
+
"class",
|
|
36
|
+
"function",
|
|
37
|
+
"const",
|
|
38
|
+
"let",
|
|
39
|
+
"var",
|
|
40
|
+
"return",
|
|
41
|
+
"if",
|
|
42
|
+
"else",
|
|
43
|
+
"for",
|
|
44
|
+
"while",
|
|
45
|
+
"do",
|
|
46
|
+
"switch",
|
|
47
|
+
"case",
|
|
48
|
+
"default",
|
|
49
|
+
"break",
|
|
50
|
+
"continue",
|
|
51
|
+
"new",
|
|
52
|
+
"delete",
|
|
53
|
+
"typeof",
|
|
54
|
+
"instanceof",
|
|
55
|
+
"void",
|
|
56
|
+
"this",
|
|
57
|
+
"super",
|
|
58
|
+
"extends",
|
|
59
|
+
"implements",
|
|
60
|
+
"interface",
|
|
61
|
+
"enum",
|
|
62
|
+
"export",
|
|
63
|
+
"import",
|
|
64
|
+
"package",
|
|
65
|
+
"private",
|
|
66
|
+
"protected",
|
|
67
|
+
"public",
|
|
68
|
+
"static",
|
|
69
|
+
"yield",
|
|
70
|
+
"await",
|
|
71
|
+
"async",
|
|
72
|
+
"null",
|
|
73
|
+
"true",
|
|
74
|
+
"false",
|
|
75
|
+
"undefined"
|
|
76
|
+
]);
|
|
77
|
+
export class InvalidNameError extends Error {
|
|
78
|
+
constructor(message) {
|
|
79
|
+
super(message);
|
|
80
|
+
this.name = "InvalidNameError";
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Documented valid input format:
|
|
85
|
+
* - must start with a letter
|
|
86
|
+
* - may contain letters, numbers, spaces, hyphens and underscores
|
|
87
|
+
* - separators are collapsed when converting to Pascal/camel/kebab case
|
|
88
|
+
*/
|
|
89
|
+
export const VALID_NAME_DESCRIPTION = "Names must start with a letter and contain only letters, numbers, spaces, hyphens or underscores.";
|
|
90
|
+
function splitWords(input) {
|
|
91
|
+
return input
|
|
92
|
+
.replace(/([a-z0-9])([A-Z])/g, "$1 $2")
|
|
93
|
+
.replace(/[^a-zA-Z0-9]+/g, " ")
|
|
94
|
+
.trim()
|
|
95
|
+
.split(/\s+/)
|
|
96
|
+
.filter(Boolean);
|
|
97
|
+
}
|
|
98
|
+
export function toPascalCase(input) {
|
|
99
|
+
return splitWords(input)
|
|
100
|
+
.map((w) => w.charAt(0).toUpperCase() + w.slice(1).toLowerCase())
|
|
101
|
+
.join("");
|
|
102
|
+
}
|
|
103
|
+
export function toCamelCase(input) {
|
|
104
|
+
const pascal = toPascalCase(input);
|
|
105
|
+
return pascal ? pascal.charAt(0).toLowerCase() + pascal.slice(1) : "";
|
|
106
|
+
}
|
|
107
|
+
export function toKebabCase(input) {
|
|
108
|
+
return splitWords(input)
|
|
109
|
+
.map((w) => w.toLowerCase())
|
|
110
|
+
.join("-");
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Validates a user-supplied resource/plugin/field name. Throws InvalidNameError
|
|
114
|
+
* on any unsafe or invalid input. Returns the trimmed raw value on success.
|
|
115
|
+
*
|
|
116
|
+
* Rejects: empty, `.`/`..`, path separators, absolute paths, null bytes,
|
|
117
|
+
* control characters, shell metacharacters, reserved OS names, reserved
|
|
118
|
+
* identifiers, and names that normalize to an empty string.
|
|
119
|
+
*/
|
|
120
|
+
export function validateName(raw, kind = "name") {
|
|
121
|
+
if (raw == null)
|
|
122
|
+
throw new InvalidNameError(`A ${kind} is required.`);
|
|
123
|
+
const value = String(raw).trim();
|
|
124
|
+
if (!value)
|
|
125
|
+
throw new InvalidNameError(`A ${kind} is required.`);
|
|
126
|
+
if (value === "." || value === "..") {
|
|
127
|
+
throw new InvalidNameError(`"${value}" is not a valid ${kind}.`);
|
|
128
|
+
}
|
|
129
|
+
if (value.includes("\0")) {
|
|
130
|
+
throw new InvalidNameError(`${kind} must not contain null bytes.`);
|
|
131
|
+
}
|
|
132
|
+
// eslint-disable-next-line no-control-regex
|
|
133
|
+
if (/[\u0000-\u001f\u007f]/.test(value)) {
|
|
134
|
+
throw new InvalidNameError(`${kind} must not contain control characters.`);
|
|
135
|
+
}
|
|
136
|
+
if (/[/\\]/.test(value)) {
|
|
137
|
+
throw new InvalidNameError(`${kind} must not contain path separators ("/" or "\\").`);
|
|
138
|
+
}
|
|
139
|
+
if (isAbsolute(value)) {
|
|
140
|
+
throw new InvalidNameError(`${kind} must not be an absolute path.`);
|
|
141
|
+
}
|
|
142
|
+
if (/[;&|`$(){}<>*?!"'\[\]#~]/.test(value)) {
|
|
143
|
+
throw new InvalidNameError(`${kind} must not contain shell metacharacters.`);
|
|
144
|
+
}
|
|
145
|
+
if (!/^[A-Za-z]/.test(value)) {
|
|
146
|
+
throw new InvalidNameError(`${kind} must start with a letter. ${VALID_NAME_DESCRIPTION}`);
|
|
147
|
+
}
|
|
148
|
+
if (!/^[A-Za-z0-9 _-]+$/.test(value)) {
|
|
149
|
+
throw new InvalidNameError(`${kind} contains invalid characters. ${VALID_NAME_DESCRIPTION}`);
|
|
150
|
+
}
|
|
151
|
+
const base = value.toLowerCase().split(".")[0] ?? "";
|
|
152
|
+
if (RESERVED_NAMES.has(base)) {
|
|
153
|
+
throw new InvalidNameError(`"${value}" is a reserved operating-system name.`);
|
|
154
|
+
}
|
|
155
|
+
if (RESERVED_IDENTIFIERS.has(value.toLowerCase())) {
|
|
156
|
+
throw new InvalidNameError(`"${value}" is a reserved keyword and cannot be used as a ${kind}.`);
|
|
157
|
+
}
|
|
158
|
+
if (!toPascalCase(value)) {
|
|
159
|
+
throw new InvalidNameError(`"${value}" cannot be converted into a valid identifier.`);
|
|
160
|
+
}
|
|
161
|
+
return value;
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Validates a plugin id. Plugin ids follow reverse-DNS style and may contain
|
|
165
|
+
* dots (e.g. `com.example.my-plugin`), but must not enable path traversal or
|
|
166
|
+
* shell injection. Returns the trimmed value on success.
|
|
167
|
+
*/
|
|
168
|
+
export function validatePluginId(raw) {
|
|
169
|
+
if (raw == null)
|
|
170
|
+
throw new InvalidNameError("A plugin id is required.");
|
|
171
|
+
const value = String(raw).trim();
|
|
172
|
+
if (!value)
|
|
173
|
+
throw new InvalidNameError("A plugin id is required.");
|
|
174
|
+
if (value.includes("\0"))
|
|
175
|
+
throw new InvalidNameError("plugin id must not contain null bytes.");
|
|
176
|
+
// eslint-disable-next-line no-control-regex
|
|
177
|
+
if (/[\u0000-\u001f\u007f]/.test(value))
|
|
178
|
+
throw new InvalidNameError("plugin id must not contain control characters.");
|
|
179
|
+
if (/[/\\]/.test(value))
|
|
180
|
+
throw new InvalidNameError('plugin id must not contain path separators ("/" or "\\").');
|
|
181
|
+
if (isAbsolute(value))
|
|
182
|
+
throw new InvalidNameError("plugin id must not be an absolute path.");
|
|
183
|
+
if (value.includes(".."))
|
|
184
|
+
throw new InvalidNameError("plugin id must not contain consecutive dots.");
|
|
185
|
+
if (/[;&|`$(){}<>*?!"'\[\]#~]/.test(value))
|
|
186
|
+
throw new InvalidNameError("plugin id must not contain shell metacharacters.");
|
|
187
|
+
if (!/^[A-Za-z][A-Za-z0-9]*(?:[.-][A-Za-z0-9]+)*$/.test(value)) {
|
|
188
|
+
throw new InvalidNameError("plugin id must start with a letter and use only letters, numbers, dots and hyphens (e.g. com.example.my-plugin).");
|
|
189
|
+
}
|
|
190
|
+
return value;
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Resolves `child` against `baseDir` and guarantees the result stays inside
|
|
194
|
+
* `baseDir`. Throws on any traversal attempt. Returns the resolved path.
|
|
195
|
+
*/
|
|
196
|
+
export function assertInsideDir(baseDir, child) {
|
|
197
|
+
const base = resolve(baseDir);
|
|
198
|
+
const target = resolve(base, child);
|
|
199
|
+
const rel = relative(base, target);
|
|
200
|
+
if (rel === "" || rel === ".")
|
|
201
|
+
return target;
|
|
202
|
+
if (rel.startsWith("..") || isAbsolute(rel)) {
|
|
203
|
+
throw new InvalidNameError(`Refusing to write outside the project directory: ${target}`);
|
|
204
|
+
}
|
|
205
|
+
return target;
|
|
206
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,28 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "openadminjs",
|
|
3
|
-
"version": "0.1
|
|
3
|
+
"version": "0.3.0-rc.1",
|
|
4
|
+
"description": "CLI scaffold generator for OpenAdminJS monorepos (NestJS, Prisma, Next.js). Not a runtime library — use npx openadminjs create.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"openadminjs",
|
|
8
|
+
"admin",
|
|
9
|
+
"nestjs",
|
|
10
|
+
"nextjs",
|
|
11
|
+
"prisma",
|
|
12
|
+
"cli",
|
|
13
|
+
"scaffold",
|
|
14
|
+
"generator",
|
|
15
|
+
"monorepo"
|
|
16
|
+
],
|
|
17
|
+
"homepage": "https://js-senpai.github.io/open-admin-js/",
|
|
18
|
+
"bugs": {
|
|
19
|
+
"url": "https://github.com/js-senpai/open-admin-js/issues"
|
|
20
|
+
},
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "git+https://github.com/js-senpai/open-admin-js.git",
|
|
24
|
+
"directory": "packages/cli"
|
|
25
|
+
},
|
|
4
26
|
"type": "module",
|
|
5
27
|
"bin": {
|
|
6
28
|
"openadminjs": "dist/index.js"
|
|
@@ -20,27 +42,33 @@
|
|
|
20
42
|
"publishConfig": {
|
|
21
43
|
"access": "public"
|
|
22
44
|
},
|
|
45
|
+
"engines": {
|
|
46
|
+
"node": ">=20"
|
|
47
|
+
},
|
|
23
48
|
"files": [
|
|
24
49
|
"dist",
|
|
50
|
+
"!dist/**/*.test.*",
|
|
51
|
+
"!dist/**/*.spec.*",
|
|
25
52
|
"template",
|
|
26
53
|
"README.md"
|
|
27
54
|
],
|
|
55
|
+
"scripts": {
|
|
56
|
+
"sync-template": "node ../../scripts/sync-template.mjs",
|
|
57
|
+
"prepack": "pnpm run build",
|
|
58
|
+
"build": "node -e \"require('fs').rmSync('dist',{recursive:true,force:true})\" && node ../../scripts/sync-template.mjs && tsc -p tsconfig.json && node ../../scripts/make-executable.mjs dist/index.js",
|
|
59
|
+
"lint": "tsc -p tsconfig.json --noEmit",
|
|
60
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
61
|
+
"test": "pnpm run sync-template && vitest run"
|
|
62
|
+
},
|
|
28
63
|
"dependencies": {
|
|
29
64
|
"@clack/prompts": "^0.8.2",
|
|
30
65
|
"cac": "^6.7.14",
|
|
31
66
|
"fs-extra": "^11.2.0",
|
|
32
|
-
"picocolors": "^1.1.1"
|
|
67
|
+
"picocolors": "^1.1.1",
|
|
68
|
+
"typescript": "^5.7.2"
|
|
33
69
|
},
|
|
34
70
|
"devDependencies": {
|
|
35
71
|
"@types/fs-extra": "^11.0.4",
|
|
36
|
-
"typescript": "^5.7.2",
|
|
37
72
|
"vitest": "^3.2.0"
|
|
38
|
-
},
|
|
39
|
-
"scripts": {
|
|
40
|
-
"sync-template": "node ../../scripts/sync-template.mjs",
|
|
41
|
-
"build": "node ../../scripts/sync-template.mjs && tsc -p tsconfig.json && node ../../scripts/make-executable.mjs dist/index.js",
|
|
42
|
-
"lint": "tsc -p tsconfig.json --noEmit",
|
|
43
|
-
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
44
|
-
"test": "pnpm run sync-template && vitest run"
|
|
45
73
|
}
|
|
46
|
-
}
|
|
74
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
FROM node:22-bookworm-slim AS build
|
|
2
|
+
RUN apt-get update && apt-get install -y --no-install-recommends openssl ca-certificates && rm -rf /var/lib/apt/lists/* && corepack enable && corepack prepare pnpm@9.15.0 --activate
|
|
3
|
+
WORKDIR /app
|
|
4
|
+
COPY . .
|
|
5
|
+
RUN pnpm install --frozen-lockfile
|
|
6
|
+
ENV DATABASE_URL=postgresql://build:build@127.0.0.1:5432/build
|
|
7
|
+
RUN pnpm --filter @openadminjs/brand build && pnpm --filter @openadminjs/api... build && pnpm --filter @openadminjs/admin build
|
|
8
|
+
RUN mkdir -p /app/storage /app/apps/api/logs && chown -R node:node /app
|
|
9
|
+
USER node
|
|
10
|
+
ENV NODE_ENV=production
|
|
11
|
+
EXPOSE 3000 4000
|
|
12
|
+
CMD ["pnpm","--filter","@openadminjs/api","start"]
|
package/template/README.md
CHANGED
|
@@ -1,17 +1,20 @@
|
|
|
1
1
|
# __APP_NAME__
|
|
2
2
|
|
|
3
|
-
Generated by [OpenAdminJS](https://
|
|
3
|
+
Generated by [OpenAdminJS](https://js-senpai.github.io/open-admin-js/).
|
|
4
4
|
|
|
5
5
|
## Stack
|
|
6
6
|
|
|
7
|
-
- **API** — Nest.js
|
|
7
|
+
- **API** — Nest.js 11 + Prisma 6 (port 4000)
|
|
8
8
|
- **Admin** — Next.js 15 back-office (port 3000)
|
|
9
9
|
- **Web** — Next.js 15 public site (port 3001)
|
|
10
|
+
- **Database** — PostgreSQL, MySQL, or SQLite (chosen during `openadminjs create`)
|
|
10
11
|
|
|
11
12
|
## Getting started
|
|
12
13
|
|
|
13
14
|
```bash
|
|
14
15
|
pnpm install
|
|
16
|
+
pnpm db:init # non-interactive provider-aware initialization
|
|
17
|
+
pnpm db:seed
|
|
15
18
|
pnpm dev
|
|
16
19
|
```
|
|
17
20
|
|
|
@@ -19,14 +22,31 @@ Admin panel → http://localhost:3000
|
|
|
19
22
|
API + Swagger → http://localhost:4000/api/docs
|
|
20
23
|
Public site → http://localhost:3001
|
|
21
24
|
|
|
25
|
+
Sign in with the superadmin email and password from the scaffold wizard (stored in `apps/api/.env`).
|
|
26
|
+
|
|
27
|
+
## Environment
|
|
28
|
+
|
|
29
|
+
Real secrets live in **`apps/api/.env`** only. A tracked **`.env.example`** at the repo root documents the required keys. Never commit `.env` files.
|
|
30
|
+
|
|
31
|
+
| Provider | Typical `DATABASE_URL` |
|
|
32
|
+
| --- | --- |
|
|
33
|
+
| PostgreSQL | `postgresql://USER:PASSWORD@localhost:5432/db?schema=public` |
|
|
34
|
+
| MySQL | `mysql://USER:PASSWORD@localhost:3306/db` |
|
|
35
|
+
| SQLite | `file:./dev.db` |
|
|
36
|
+
|
|
37
|
+
New SQLite projects use native Prisma JSON. Existing legacy projects with JSON text columns need a reviewed schema upgrade.
|
|
38
|
+
|
|
22
39
|
## Useful commands
|
|
23
40
|
|
|
24
41
|
| Command | Description |
|
|
25
42
|
|---|---|
|
|
26
43
|
| `pnpm dev` | Start API + admin in parallel |
|
|
27
|
-
| `pnpm db:
|
|
44
|
+
| `pnpm db:init` | Initialize DB non-interactively; deploy reviewed history or safe development schema push |
|
|
45
|
+
| `pnpm db:migrate` | Development schema changes (provider-specific) |
|
|
28
46
|
| `pnpm db:seed` | Seed database (creates superadmin) |
|
|
29
47
|
| `pnpm db:studio` | Open Prisma Studio |
|
|
30
48
|
| `pnpm build` | Build all workspaces |
|
|
31
49
|
| `pnpm test` | Run all tests |
|
|
32
50
|
| `pnpm lint` | Type-check all workspaces |
|
|
51
|
+
| `openadminjs doctor` | Check project health |
|
|
52
|
+
| `openadminjs security check` | Audit secrets and configuration |
|
|
@@ -1,27 +1,27 @@
|
|
|
1
1
|
import { NextRequest, NextResponse } from "next/server";
|
|
2
2
|
import { TOKEN_COOKIE_AT, TOKEN_COOKIE_RT } from "../../../../lib/tokens";
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
typeof process !== "undefined" && process.env.NEXT_PUBLIC_API_URL
|
|
6
|
-
? process.env.NEXT_PUBLIC_API_URL
|
|
7
|
-
: "http://localhost:4000";
|
|
4
|
+
import { API_URL, isSameOrigin } from "../../../../lib/server-api";
|
|
8
5
|
|
|
9
6
|
const COOKIE_OPTS = {
|
|
10
7
|
path: "/",
|
|
11
8
|
maxAge: 60 * 60 * 24 * 7,
|
|
12
9
|
sameSite: "lax" as const,
|
|
13
10
|
secure: process.env.NODE_ENV === "production",
|
|
14
|
-
httpOnly:
|
|
11
|
+
httpOnly: true
|
|
15
12
|
} as const;
|
|
16
13
|
|
|
17
14
|
export async function POST(req: NextRequest) {
|
|
15
|
+
if (!isSameOrigin(req)) return NextResponse.json({ message: "Invalid origin" }, { status: 403 });
|
|
18
16
|
const body = await req.json().catch(() => null);
|
|
19
17
|
if (!body) return NextResponse.json({ message: "Invalid request body" }, { status: 400 });
|
|
20
18
|
|
|
21
19
|
const upstream = await fetch(`${API_URL}/auth/login`, {
|
|
22
20
|
method: "POST",
|
|
21
|
+
redirect: "error",
|
|
22
|
+
cache: "no-store",
|
|
23
23
|
headers: { "content-type": "application/json" },
|
|
24
|
-
body: JSON.stringify(body)
|
|
24
|
+
body: JSON.stringify({ email: body.email, password: body.password, realm: "admin" })
|
|
25
25
|
}).catch(() => null);
|
|
26
26
|
|
|
27
27
|
if (!upstream) {
|
|
@@ -40,12 +40,12 @@ export async function POST(req: NextRequest) {
|
|
|
40
40
|
if (!upstream.ok || !data.accessToken || !data.refreshToken) {
|
|
41
41
|
return NextResponse.json(
|
|
42
42
|
{ message: data.message ?? "Invalid credentials" },
|
|
43
|
-
{ status: upstream.status }
|
|
43
|
+
{ status: upstream.ok ? 502 : upstream.status }
|
|
44
44
|
);
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
-
const res = NextResponse.json({ ok: true });
|
|
48
|
-
res.cookies.set(TOKEN_COOKIE_AT, data.accessToken, COOKIE_OPTS);
|
|
47
|
+
const res = NextResponse.json({ ok: true }, { headers: { "cache-control": "no-store" } });
|
|
48
|
+
res.cookies.set(TOKEN_COOKIE_AT, data.accessToken, { ...COOKIE_OPTS, maxAge: 900 });
|
|
49
49
|
res.cookies.set(TOKEN_COOKIE_RT, data.refreshToken, COOKIE_OPTS);
|
|
50
50
|
return res;
|
|
51
51
|
}
|
|
@@ -1,9 +1,17 @@
|
|
|
1
|
-
import { NextResponse } from "next/server";
|
|
1
|
+
import { NextRequest, NextResponse } from "next/server";
|
|
2
2
|
import { TOKEN_COOKIE_AT, TOKEN_COOKIE_RT } from "../../../../lib/tokens";
|
|
3
|
+
import { API_URL, isSameOrigin } from "../../../../lib/server-api";
|
|
3
4
|
|
|
4
|
-
const CLEAR_OPTS = { path: "/", maxAge: 0, sameSite: "lax" as const, httpOnly:
|
|
5
|
+
const CLEAR_OPTS = { path: "/", maxAge: 0, sameSite: "lax" as const, httpOnly: true, secure: process.env.NODE_ENV === "production" } as const;
|
|
5
6
|
|
|
6
|
-
export async function POST() {
|
|
7
|
+
export async function POST(req: NextRequest) {
|
|
8
|
+
if (!isSameOrigin(req)) return NextResponse.json({ message: "Invalid origin" }, { status: 403 });
|
|
9
|
+
const at = req.cookies.get(TOKEN_COOKIE_AT)?.value;
|
|
10
|
+
if (!at) return NextResponse.json({ message: "Session required" }, { status: 401 });
|
|
11
|
+
const upstream = await fetch(`${API_URL}/auth/logout`, {
|
|
12
|
+
method: "POST", headers: { authorization: `Bearer ${at}` }, cache: "no-store", redirect: "error"
|
|
13
|
+
}).catch(() => null);
|
|
14
|
+
if (!upstream?.ok) return NextResponse.json({ message: "Logout failed" }, { status: upstream?.status ?? 502 });
|
|
7
15
|
const res = NextResponse.json({ ok: true });
|
|
8
16
|
res.cookies.set(TOKEN_COOKIE_AT, "", CLEAR_OPTS);
|
|
9
17
|
res.cookies.set(TOKEN_COOKIE_RT, "", CLEAR_OPTS);
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import {NextRequest,NextResponse} from "next/server";
|
|
2
|
+
import {API_URL} from "../../../../../lib/server-api";
|
|
3
|
+
import {TOKEN_COOKIE_AT,TOKEN_COOKIE_RT} from "../../../../../lib/tokens";
|
|
4
|
+
export async function GET(req:NextRequest){
|
|
5
|
+
const origin=(process.env.ADMIN_ORIGIN||req.nextUrl.origin).split(",")[0]!.trim();
|
|
6
|
+
const cookieOpts={httpOnly:true,secure:process.env.NODE_ENV==="production",sameSite:"lax" as const,path:"/"};
|
|
7
|
+
const transaction=req.cookies.get("oajs_oidc")?.value;
|
|
8
|
+
const q=req.nextUrl.searchParams;
|
|
9
|
+
let data:{accessToken?:string;refreshToken?:string}={};
|
|
10
|
+
if(transaction&&q.getAll("code").length===1&&q.getAll("state").length===1&&!q.has("error")){
|
|
11
|
+
const r=await fetch(API_URL+"/auth/sso/oidc/exchange",{method:"POST",headers:{"content-type":"application/json"},body:JSON.stringify({transaction,code:q.get("code"),state:q.get("state"),iss:q.get("iss")||undefined}),cache:"no-store",redirect:"error"}).catch(()=>null);
|
|
12
|
+
if(r?.ok)data=await r.json().catch(()=>({}));
|
|
13
|
+
}
|
|
14
|
+
const ok=typeof data.accessToken==="string"&&typeof data.refreshToken==="string";
|
|
15
|
+
const res=NextResponse.redirect(new URL(ok?"/dashboard":"/login?sso_error=1",origin));
|
|
16
|
+
res.cookies.set("oajs_oidc","",{...cookieOpts,path:"/api/auth/oidc",maxAge:0});
|
|
17
|
+
if(ok){res.cookies.set(TOKEN_COOKIE_AT,data.accessToken!,{...cookieOpts,maxAge:900});res.cookies.set(TOKEN_COOKIE_RT,data.refreshToken!,{...cookieOpts,maxAge:604800});}
|
|
18
|
+
res.headers.set("cache-control","no-store");res.headers.set("referrer-policy","no-referrer");return res;
|
|
19
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import {NextRequest,NextResponse} from "next/server";
|
|
2
|
+
import {API_URL} from "../../../../../lib/server-api";
|
|
3
|
+
export async function GET(req:NextRequest){
|
|
4
|
+
const origin=(process.env.ADMIN_ORIGIN||req.nextUrl.origin).split(",")[0]!.trim();
|
|
5
|
+
const failed=()=>NextResponse.redirect(new URL("/login?sso_error=1",origin));
|
|
6
|
+
const r=await fetch(API_URL+"/auth/sso/oidc/start",{method:"POST",cache:"no-store",redirect:"error"}).catch(()=>null);
|
|
7
|
+
if(!r?.ok)return failed();
|
|
8
|
+
const data=await r.json().catch(()=>({}));if(typeof data.authorizeUrl!=="string"||typeof data.transaction!=="string")return failed();
|
|
9
|
+
const url=new URL(data.authorizeUrl);if(url.protocol!=="https:")return failed();
|
|
10
|
+
const res=NextResponse.redirect(url);res.cookies.set("oajs_oidc",data.transaction,{path:"/api/auth/oidc",httpOnly:true,secure:process.env.NODE_ENV==="production",sameSite:"lax",maxAge:600});res.headers.set("cache-control","no-store");return res;
|
|
11
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import {NextResponse} from "next/server";
|
|
2
|
+
import {API_URL} from "../../../../../lib/server-api";
|
|
3
|
+
export async function GET(){
|
|
4
|
+
const r=await fetch(API_URL+"/auth/sso/oidc/status",{cache:"no-store",redirect:"error"}).catch(()=>null);
|
|
5
|
+
const data=r?.ok?await r.json().catch(()=>({})):{};return NextResponse.json({enabled:data.enabled===true},{headers:{"cache-control":"no-store"}});
|
|
6
|
+
}
|
|
@@ -1,30 +1,31 @@
|
|
|
1
1
|
import { NextRequest, NextResponse } from "next/server";
|
|
2
2
|
import { TOKEN_COOKIE_AT, TOKEN_COOKIE_RT } from "../../../../lib/tokens";
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
typeof process !== "undefined" && process.env.NEXT_PUBLIC_API_URL
|
|
6
|
-
? process.env.NEXT_PUBLIC_API_URL
|
|
7
|
-
: "http://localhost:4000";
|
|
4
|
+
import { API_URL, isSameOrigin } from "../../../../lib/server-api";
|
|
8
5
|
|
|
6
|
+
const noStore = { "cache-control": "no-store" };
|
|
9
7
|
const COOKIE_OPTS = {
|
|
10
8
|
path: "/",
|
|
11
9
|
maxAge: 60 * 60 * 24 * 7,
|
|
12
10
|
sameSite: "lax" as const,
|
|
13
11
|
secure: process.env.NODE_ENV === "production",
|
|
14
|
-
httpOnly:
|
|
12
|
+
httpOnly: true
|
|
15
13
|
} as const;
|
|
16
14
|
|
|
17
15
|
export async function POST(req: NextRequest) {
|
|
16
|
+
if (!isSameOrigin(req)) return NextResponse.json({ message: "Invalid origin" }, { status: 403, headers: noStore });
|
|
18
17
|
const rt = req.cookies.get(TOKEN_COOKIE_RT)?.value;
|
|
19
|
-
if (!rt) return NextResponse.json({ message: "No refresh token" }, { status: 401 });
|
|
18
|
+
if (!rt) return NextResponse.json({ message: "No refresh token" }, { status: 401, headers: noStore });
|
|
20
19
|
|
|
21
20
|
const upstream = await fetch(`${API_URL}/auth/refresh`, {
|
|
22
21
|
method: "POST",
|
|
22
|
+
redirect: "error",
|
|
23
|
+
cache: "no-store",
|
|
23
24
|
headers: { "content-type": "application/json" },
|
|
24
25
|
body: JSON.stringify({ refreshToken: rt, realm: "admin" })
|
|
25
26
|
}).catch(() => null);
|
|
26
27
|
|
|
27
|
-
if (!upstream) return NextResponse.json({ message: "API unavailable" }, { status: 502 });
|
|
28
|
+
if (!upstream) return NextResponse.json({ message: "API unavailable" }, { status: 502, headers: noStore });
|
|
28
29
|
|
|
29
30
|
const data = (await upstream.json().catch(() => ({}))) as {
|
|
30
31
|
accessToken?: string;
|
|
@@ -32,15 +33,21 @@ export async function POST(req: NextRequest) {
|
|
|
32
33
|
message?: string;
|
|
33
34
|
};
|
|
34
35
|
|
|
35
|
-
if (
|
|
36
|
-
const res = NextResponse.json({ message:
|
|
36
|
+
if (upstream.status === 401 || upstream.status === 403) {
|
|
37
|
+
const res = NextResponse.json({ message: "Session expired" }, { status: 401, headers: noStore });
|
|
37
38
|
res.cookies.set(TOKEN_COOKIE_AT, "", { ...COOKIE_OPTS, maxAge: 0 });
|
|
38
39
|
res.cookies.set(TOKEN_COOKIE_RT, "", { ...COOKIE_OPTS, maxAge: 0 });
|
|
39
40
|
return res;
|
|
40
41
|
}
|
|
41
42
|
|
|
42
|
-
|
|
43
|
-
|
|
43
|
+
if (!upstream.ok || typeof data?.accessToken !== "string" || !data.accessToken || typeof data.refreshToken !== "string" || !data.refreshToken) {
|
|
44
|
+
const headers = new Headers(noStore);
|
|
45
|
+
const retryAfter = upstream.headers.get("retry-after");
|
|
46
|
+
if (retryAfter) headers.set("retry-after", retryAfter);
|
|
47
|
+
return NextResponse.json({ message: "Session refresh temporarily unavailable" }, { status: upstream.status === 429 ? 429 : 502, headers });
|
|
48
|
+
}
|
|
49
|
+
const res = NextResponse.json({ ok: true }, { headers: noStore });
|
|
50
|
+
res.cookies.set(TOKEN_COOKIE_AT, data.accessToken, { ...COOKIE_OPTS, maxAge: 900 });
|
|
44
51
|
res.cookies.set(TOKEN_COOKIE_RT, data.refreshToken, COOKIE_OPTS);
|
|
45
52
|
return res;
|
|
46
53
|
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { NextRequest, NextResponse } from "next/server";
|
|
2
|
+
import { API_URL, isSameOrigin } from "../../../../lib/server-api";
|
|
3
|
+
import { TOKEN_COOKIE_AT } from "../../../../lib/tokens";
|
|
4
|
+
|
|
5
|
+
type RouteContext = { params: Promise<{ path: string[] }> };
|
|
6
|
+
async function proxy(req: NextRequest, context: RouteContext) {
|
|
7
|
+
const { path } = await context.params;
|
|
8
|
+
if (!path.length || path.some(part => !/^[A-Za-z0-9_.@-]+$/.test(part) || part === "." || part === "..")) return NextResponse.json({ message: "Invalid path" }, { status: 400 });
|
|
9
|
+
const route = "/" + path.map(encodeURIComponent).join("/");
|
|
10
|
+
if (!(route.startsWith("/admin/") || route.startsWith("/jobs/") || route === "/auth/me" || route === "/auth/me/realms")) return NextResponse.json({ message: "Route not allowed" }, { status: 404 });
|
|
11
|
+
if (req.method !== "GET" && req.method !== "HEAD" && !isSameOrigin(req)) return NextResponse.json({ message: "Invalid origin" }, { status: 403 });
|
|
12
|
+
const token = req.cookies.get(TOKEN_COOKIE_AT)?.value;
|
|
13
|
+
if (!token) return NextResponse.json({ message: "Session required" }, { status: 401 });
|
|
14
|
+
const headers = new Headers({ authorization: `Bearer ${token}` });
|
|
15
|
+
const contentType = req.headers.get("content-type");
|
|
16
|
+
if (contentType) headers.set("content-type", contentType);
|
|
17
|
+
const init: RequestInit = { method: req.method, headers, cache: "no-store", redirect: "error" };
|
|
18
|
+
if (req.method !== "GET" && req.method !== "HEAD") {
|
|
19
|
+
const max = 15 * 1024 * 1024;
|
|
20
|
+
if (Number(req.headers.get("content-length") || 0) > max) return NextResponse.json({message:"Body too large"},{status:413});
|
|
21
|
+
const reader = req.body?.getReader();
|
|
22
|
+
if(reader) {
|
|
23
|
+
const chunks: Uint8Array[] = []; let total = 0;
|
|
24
|
+
for(;;) {
|
|
25
|
+
const {done,value}=await reader.read(); if(done) break;
|
|
26
|
+
total += value.byteLength;
|
|
27
|
+
if(total > max) {await reader.cancel();return NextResponse.json({message:"Body too large"},{status:413});}
|
|
28
|
+
chunks.push(value);
|
|
29
|
+
}
|
|
30
|
+
const data=new Uint8Array(total);let offset=0;
|
|
31
|
+
for(const chunk of chunks){data.set(chunk,offset);offset+=chunk.byteLength;}
|
|
32
|
+
init.body=data.buffer;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
const response = await fetch(API_URL + route + req.nextUrl.search, init).catch(() => null);
|
|
36
|
+
if (!response) return NextResponse.json({ message: "API unavailable" }, { status: 502 });
|
|
37
|
+
const outgoing = new Headers({ "cache-control": "no-store", "x-content-type-options": "nosniff" });
|
|
38
|
+
for (const name of ["content-type", "content-disposition", "retry-after", "x-request-id"]) {
|
|
39
|
+
const value = response.headers.get(name);
|
|
40
|
+
if (value) outgoing.set(name, value);
|
|
41
|
+
}
|
|
42
|
+
return new NextResponse(response.body, { status: response.status, headers: outgoing });
|
|
43
|
+
}
|
|
44
|
+
export const GET = proxy;
|
|
45
|
+
export const POST = proxy;
|
|
46
|
+
export const PATCH = proxy;
|
|
47
|
+
export const PUT = proxy;
|
|
48
|
+
export const DELETE = proxy;
|