@hintergrund/worker 0.1.9 → 0.1.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/api/cms/handlers/admin-users.handler.js +9 -9
- package/dist/api/cms/handlers/agent-conversations.handler.js +4 -4
- package/dist/api/cms/handlers/auth.handler.d.ts.map +1 -1
- package/dist/api/cms/handlers/auth.handler.js +7 -5
- package/dist/api/cms/handlers/github-secrets.handler.d.ts +8 -0
- package/dist/api/cms/handlers/github-secrets.handler.d.ts.map +1 -0
- package/dist/api/cms/handlers/github-secrets.handler.js +51 -0
- package/dist/api/cms/handlers/setup.handler.d.ts +6 -0
- package/dist/api/cms/handlers/setup.handler.d.ts.map +1 -0
- package/dist/api/cms/handlers/setup.handler.js +85 -0
- package/dist/api/cms/routes/index.d.ts.map +1 -1
- package/dist/api/cms/routes/index.js +6 -0
- package/dist/api/emails/handlers/webhook.handler.js +1 -1
- package/dist/api/shop/handlers/products.handler.js +1 -1
- package/dist/api/shop/handlers/public.handler.js +11 -11
- package/dist/hintergrund-assets.d.ts +1 -1
- package/dist/hintergrund-assets.d.ts.map +1 -1
- package/dist/hintergrund-assets.js +1 -1
- package/dist/index.d.ts +17 -20
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +17 -8
- package/dist/lib/auth/jwt.d.ts +0 -4
- package/dist/lib/auth/jwt.d.ts.map +1 -1
- package/dist/lib/auth/jwt.js +0 -7
- package/dist/lib/config.d.ts +1 -1
- package/dist/lib/config.d.ts.map +1 -1
- package/dist/lib/config.js +19 -19
- package/dist/lib/email.d.ts +1 -1
- package/dist/lib/email.d.ts.map +1 -1
- package/dist/lib/email.js +13 -10
- package/dist/lib/github-app.js +1 -1
- package/dist/lib/github.d.ts +10 -0
- package/dist/lib/github.d.ts.map +1 -1
- package/dist/lib/github.js +56 -0
- package/dist/lib/project-db.d.ts +0 -2
- package/dist/lib/project-db.d.ts.map +1 -1
- package/dist/lib/project-db.js +3 -7
- package/dist/lib/template.d.ts.map +1 -1
- package/dist/lib/template.js +9 -0
- package/dist/middleware/auth.d.ts.map +1 -1
- package/dist/middleware/auth.js +5 -4
- package/package.json +35 -33
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { hashPw } from '../../../lib/auth/crypto';
|
|
2
|
-
import { createId,
|
|
2
|
+
import { createId, ensureSchema, nowSeconds } from '../../../lib/project-db';
|
|
3
3
|
const LOGIN_METHODS = ['password', 'magic-link'];
|
|
4
4
|
function userToResponse(user) {
|
|
5
5
|
return {
|
|
@@ -14,12 +14,12 @@ function userToResponse(user) {
|
|
|
14
14
|
};
|
|
15
15
|
}
|
|
16
16
|
async function hasAdminUsers(c) {
|
|
17
|
-
const db = await
|
|
17
|
+
const db = await ensureSchema(c.env);
|
|
18
18
|
const row = await db.prepare('SELECT COUNT(*) AS count FROM admin_users').first();
|
|
19
19
|
return (row?.count ?? 0) > 0;
|
|
20
20
|
}
|
|
21
21
|
function verifyBootstrapToken(c, provided) {
|
|
22
|
-
const configured = c.env.
|
|
22
|
+
const configured = c.env.BOOTSTRAP_TOKEN?.trim();
|
|
23
23
|
if (!configured) {
|
|
24
24
|
return c.json({ error: 'Admin bootstrap is not configured' }, 403);
|
|
25
25
|
}
|
|
@@ -29,7 +29,7 @@ function verifyBootstrapToken(c, provided) {
|
|
|
29
29
|
return null;
|
|
30
30
|
}
|
|
31
31
|
export async function listAdminUsers(c) {
|
|
32
|
-
const db = await
|
|
32
|
+
const db = await ensureSchema(c.env);
|
|
33
33
|
const { results } = await db
|
|
34
34
|
.prepare('SELECT * FROM admin_users ORDER BY created_at ASC, id ASC')
|
|
35
35
|
.all();
|
|
@@ -62,10 +62,10 @@ async function createAdminUserFromBody(c, body, status) {
|
|
|
62
62
|
if (loginMethod === 'password' && !body.passwordHash && !body.password) {
|
|
63
63
|
return c.json({ error: 'password is required for password users' }, 400);
|
|
64
64
|
}
|
|
65
|
-
const db = await
|
|
65
|
+
const db = await ensureSchema(c.env);
|
|
66
66
|
const now = nowSeconds();
|
|
67
67
|
const id = createId('user');
|
|
68
|
-
const passwordHash = body.password ? await hashPw(String(body.password), c.env.
|
|
68
|
+
const passwordHash = body.password ? await hashPw(String(body.password), c.env.SECRET ?? '') : body.passwordHash ?? null;
|
|
69
69
|
await db
|
|
70
70
|
.prepare(`INSERT INTO admin_users (id, email, name, password_hash, login_method, disabled_at, created_at, updated_at)
|
|
71
71
|
VALUES (?, ?, ?, ?, ?, NULL, ?, ?)`)
|
|
@@ -79,7 +79,7 @@ export async function patchAdminUser(c) {
|
|
|
79
79
|
if (!userId)
|
|
80
80
|
return c.json({ error: 'Missing user id' }, 400);
|
|
81
81
|
const body = await c.req.json();
|
|
82
|
-
const db = await
|
|
82
|
+
const db = await ensureSchema(c.env);
|
|
83
83
|
const existing = await db.prepare('SELECT * FROM admin_users WHERE id = ?').bind(userId).first();
|
|
84
84
|
if (!existing) {
|
|
85
85
|
return c.json({ error: 'User not found' }, 404);
|
|
@@ -91,7 +91,7 @@ export async function patchAdminUser(c) {
|
|
|
91
91
|
: body.loginMethod;
|
|
92
92
|
const disabledAt = body.disabled === undefined ? existing.disabled_at : (body.disabled ? nowSeconds() : null);
|
|
93
93
|
const passwordHash = body.password
|
|
94
|
-
? await hashPw(String(body.password), c.env.
|
|
94
|
+
? await hashPw(String(body.password), c.env.SECRET ?? '')
|
|
95
95
|
: (body.passwordHash === undefined ? existing.password_hash : body.passwordHash);
|
|
96
96
|
const now = nowSeconds();
|
|
97
97
|
await db
|
|
@@ -109,7 +109,7 @@ export async function deleteAdminUser(c) {
|
|
|
109
109
|
if (currentUser?.userId === userId) {
|
|
110
110
|
return c.json({ error: 'You cannot delete your own admin user' }, 400);
|
|
111
111
|
}
|
|
112
|
-
const db = await
|
|
112
|
+
const db = await ensureSchema(c.env);
|
|
113
113
|
const firstUser = await db
|
|
114
114
|
.prepare('SELECT id FROM admin_users ORDER BY created_at ASC, id ASC LIMIT 1')
|
|
115
115
|
.first();
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { createId,
|
|
1
|
+
import { createId, ensureSchema, nowSeconds } from '../../../lib/project-db';
|
|
2
2
|
function getUserId(c) {
|
|
3
3
|
return c.get('user')?.userId ?? null;
|
|
4
4
|
}
|
|
@@ -25,7 +25,7 @@ export async function listConversations(c) {
|
|
|
25
25
|
const mode = parseMode(c.req.query('mode'));
|
|
26
26
|
if (!mode)
|
|
27
27
|
return c.json({ error: 'mode must be builder or cms' }, 400);
|
|
28
|
-
const db = await
|
|
28
|
+
const db = await ensureSchema(c.env);
|
|
29
29
|
const { results } = await db
|
|
30
30
|
.prepare('SELECT * FROM agent_conversations WHERE user_id = ? AND mode = ? ORDER BY updated_at DESC')
|
|
31
31
|
.bind(userId, mode)
|
|
@@ -46,7 +46,7 @@ export async function upsertConversation(c) {
|
|
|
46
46
|
const messagesJson = JSON.stringify(body.messages ?? []);
|
|
47
47
|
const modelMetadataJson = JSON.stringify(body.modelMetadata ?? {});
|
|
48
48
|
const gitDiff = body.gitDiff ? String(body.gitDiff) : null;
|
|
49
|
-
const db = await
|
|
49
|
+
const db = await ensureSchema(c.env);
|
|
50
50
|
const existing = await db
|
|
51
51
|
.prepare('SELECT id FROM agent_conversations WHERE id = ? AND user_id = ?')
|
|
52
52
|
.bind(id, userId)
|
|
@@ -78,7 +78,7 @@ export async function deleteConversation(c) {
|
|
|
78
78
|
if (!userId)
|
|
79
79
|
return c.json({ error: 'Project admin user required' }, 401);
|
|
80
80
|
const id = c.req.param('id');
|
|
81
|
-
const db = await
|
|
81
|
+
const db = await ensureSchema(c.env);
|
|
82
82
|
await db
|
|
83
83
|
.prepare('DELETE FROM agent_conversations WHERE id = ? AND user_id = ?')
|
|
84
84
|
.bind(id, userId)
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"auth.handler.d.ts","sourceRoot":"","sources":["../../../../src/api/cms/handlers/auth.handler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AAC/B,OAAO,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;
|
|
1
|
+
{"version":3,"file":"auth.handler.d.ts","sourceRoot":"","sources":["../../../../src/api/cms/handlers/auth.handler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AAC/B,OAAO,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AAiBzC,wBAAsB,KAAK,CAAC,CAAC,EAAE,OAAO,CAAC,OAAO,CAAC,GAAG,OAAO,CAAC,QAAQ,CAAC,CAsClE;AAiFD;;GAEG;AACH,wBAAsB,eAAe,CAAC,CAAC,EAAE,OAAO,CAAC,OAAO,CAAC,GAAG,OAAO,CAAC,QAAQ,CAAC,CAqD5E;AAYD,wBAAsB,MAAM,CAAC,CAAC,EAAE,OAAO,CAAC,OAAO,CAAC,GAAG,OAAO,CAAC,QAAQ,CAAC,CAWnE"}
|
|
@@ -2,7 +2,8 @@ import { readRequestBody, verifyPw, generateRandomToken } from '../../../lib/aut
|
|
|
2
2
|
import { signWithPayload } from '../../../lib/auth/jwt';
|
|
3
3
|
import { loadConfig } from '../../../lib/config';
|
|
4
4
|
import { sendMagicLinkEmail } from '../../../lib/email';
|
|
5
|
-
import {
|
|
5
|
+
import { readEmailSettings } from '../../emails/handlers/settings.handler';
|
|
6
|
+
import { createId, ensureSchema, getAdminUserByEmail, issueSessionForUser, nowSeconds, sha256Base64Url } from '../../../lib/project-db';
|
|
6
7
|
export async function login(c) {
|
|
7
8
|
try {
|
|
8
9
|
const appConfig = loadConfig(c.env);
|
|
@@ -55,7 +56,7 @@ async function handleProjectMagicLinkLogin(c, user, appConfig) {
|
|
|
55
56
|
}
|
|
56
57
|
const token = generateRandomToken();
|
|
57
58
|
const tokenHash = await sha256Base64Url(`${token}.${appConfig.global.hgSecret}`);
|
|
58
|
-
const db = await
|
|
59
|
+
const db = await ensureSchema(c.env);
|
|
59
60
|
const now = nowSeconds();
|
|
60
61
|
await db
|
|
61
62
|
.prepare(`INSERT INTO admin_magic_links (id, user_id, token_hash, expires_at, used_at, created_at)
|
|
@@ -64,11 +65,12 @@ async function handleProjectMagicLinkLogin(c, user, appConfig) {
|
|
|
64
65
|
.run();
|
|
65
66
|
const baseUrl = c.req.url.replace(/\/api\/admin\/login.*$/, '');
|
|
66
67
|
const magicLinkUrl = `${baseUrl}/api/admin/verify-magic-link?token=${encodeURIComponent(token)}`;
|
|
68
|
+
const { settings } = await readEmailSettings(c.env);
|
|
67
69
|
await sendMagicLinkEmail({
|
|
68
70
|
to: user.email,
|
|
69
71
|
apiKey: emailsConfig.emailApiKey,
|
|
70
|
-
fromEmail:
|
|
71
|
-
fromName:
|
|
72
|
+
fromEmail: settings.fromEmail ?? '',
|
|
73
|
+
fromName: settings.fromName,
|
|
72
74
|
magicLinkUrl,
|
|
73
75
|
});
|
|
74
76
|
return new Response(JSON.stringify({ success: true, emailSent: true, message: 'Check your email for the login link.' }), { status: 200 });
|
|
@@ -85,7 +87,7 @@ export async function verifyMagicLink(c) {
|
|
|
85
87
|
if (!token) {
|
|
86
88
|
return new Response('Missing token', { status: 400 });
|
|
87
89
|
}
|
|
88
|
-
const db = await
|
|
90
|
+
const db = await ensureSchema(c.env);
|
|
89
91
|
const tokenHash = await sha256Base64Url(`${token}.${appConfig.global.hgSecret}`);
|
|
90
92
|
const now = nowSeconds();
|
|
91
93
|
const link = await db
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Context } from 'hono';
|
|
2
|
+
import { type GitHubSecret } from '../../../lib/github';
|
|
3
|
+
import type { HonoEnv } from '../../../index';
|
|
4
|
+
export declare function listGitHubSecrets(c: Context<HonoEnv>): Promise<Response>;
|
|
5
|
+
export declare function upsertGitHubSecret(c: Context<HonoEnv>): Promise<Response>;
|
|
6
|
+
export declare function deleteGitHubSecret(c: Context<HonoEnv>): Promise<Response>;
|
|
7
|
+
export type { GitHubSecret };
|
|
8
|
+
//# sourceMappingURL=github-secrets.handler.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"github-secrets.handler.d.ts","sourceRoot":"","sources":["../../../../src/api/cms/handlers/github-secrets.handler.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AAEpC,OAAO,EAAsB,KAAK,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAC5E,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AAqB9C,wBAAsB,iBAAiB,CAAC,CAAC,EAAE,OAAO,CAAC,OAAO,CAAC,GAAG,OAAO,CAAC,QAAQ,CAAC,CAI9E;AAED,wBAAsB,kBAAkB,CAAC,CAAC,EAAE,OAAO,CAAC,OAAO,CAAC,GAAG,OAAO,CAAC,QAAQ,CAAC,CAe/E;AAED,wBAAsB,kBAAkB,CAAC,CAAC,EAAE,OAAO,CAAC,OAAO,CAAC,GAAG,OAAO,CAAC,QAAQ,CAAC,CAS/E;AAED,YAAY,EAAE,YAAY,EAAE,CAAC"}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { getGitHubConfig } from '../../../lib/config';
|
|
2
|
+
import { createGitHubClient } from '../../../lib/github';
|
|
3
|
+
const SECRET_NAME = /^[A-Z_][A-Z0-9_]*$/;
|
|
4
|
+
const MAX_SECRET_BYTES = 64 * 1024;
|
|
5
|
+
function secretName(value) {
|
|
6
|
+
const name = value.trim();
|
|
7
|
+
return SECRET_NAME.test(name) ? name : null;
|
|
8
|
+
}
|
|
9
|
+
async function dispatchDeploy(c) {
|
|
10
|
+
const client = createGitHubClient(getGitHubConfig(c.env));
|
|
11
|
+
const { owner, repo, branch } = client.getRepoInfo();
|
|
12
|
+
await (await client.getOctokit()).rest.actions.createWorkflowDispatch({
|
|
13
|
+
owner,
|
|
14
|
+
repo,
|
|
15
|
+
workflow_id: 'deploy.yml',
|
|
16
|
+
ref: branch,
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
export async function listGitHubSecrets(c) {
|
|
20
|
+
const client = createGitHubClient(getGitHubConfig(c.env));
|
|
21
|
+
const secrets = await client.listActionsSecrets();
|
|
22
|
+
return c.json({ secrets });
|
|
23
|
+
}
|
|
24
|
+
export async function upsertGitHubSecret(c) {
|
|
25
|
+
const name = secretName(c.req.param('name') ?? '');
|
|
26
|
+
if (!name)
|
|
27
|
+
return c.json({ error: 'Secret names must use uppercase letters, numbers, and underscores.' }, 400);
|
|
28
|
+
const body = await c.req.json();
|
|
29
|
+
if (typeof body.value !== 'string')
|
|
30
|
+
return c.json({ error: 'Secret value is required.' }, 400);
|
|
31
|
+
if (new TextEncoder().encode(body.value).byteLength > MAX_SECRET_BYTES) {
|
|
32
|
+
return c.json({ error: 'Secret value exceeds GitHub’s 64 KB limit.' }, 400);
|
|
33
|
+
}
|
|
34
|
+
const client = createGitHubClient(getGitHubConfig(c.env));
|
|
35
|
+
await client.setActionsSecret(name, body.value);
|
|
36
|
+
const deploy = body.deploy !== false;
|
|
37
|
+
if (deploy)
|
|
38
|
+
await dispatchDeploy(c);
|
|
39
|
+
return c.json({ success: true, deployed: deploy });
|
|
40
|
+
}
|
|
41
|
+
export async function deleteGitHubSecret(c) {
|
|
42
|
+
const name = secretName(c.req.param('name') ?? '');
|
|
43
|
+
if (!name)
|
|
44
|
+
return c.json({ error: 'Secret names must use uppercase letters, numbers, and underscores.' }, 400);
|
|
45
|
+
const client = createGitHubClient(getGitHubConfig(c.env));
|
|
46
|
+
await client.deleteActionsSecret(name);
|
|
47
|
+
const deploy = c.req.query('deploy') !== 'false';
|
|
48
|
+
if (deploy)
|
|
49
|
+
await dispatchDeploy(c);
|
|
50
|
+
return c.json({ success: true, deployed: deploy });
|
|
51
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { Context } from 'hono';
|
|
2
|
+
import type { HonoEnv } from '../../../index';
|
|
3
|
+
/** One-time, project-owned bootstrap. It is unavailable as soon as an admin exists. */
|
|
4
|
+
export declare function setupProject(c: Context<HonoEnv>): Promise<Response>;
|
|
5
|
+
export declare function projectNeedsSetup(c: Context<HonoEnv>): Promise<boolean>;
|
|
6
|
+
//# sourceMappingURL=setup.handler.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"setup.handler.d.ts","sourceRoot":"","sources":["../../../../src/api/cms/handlers/setup.handler.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AASpC,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AAc9C,uFAAuF;AACvF,wBAAsB,YAAY,CAAC,CAAC,EAAE,OAAO,CAAC,OAAO,CAAC,GAAG,OAAO,CAAC,QAAQ,CAAC,CA0DzE;AAED,wBAAsB,iBAAiB,CAAC,CAAC,EAAE,OAAO,CAAC,OAAO,CAAC,GAAG,OAAO,CAAC,OAAO,CAAC,CAE7E"}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { hashPw } from '../../../lib/auth/crypto';
|
|
2
|
+
import { signWithPayload } from '../../../lib/auth/jwt';
|
|
3
|
+
import { getGitHubConfig } from '../../../lib/config';
|
|
4
|
+
import { createGitHubClient } from '../../../lib/github';
|
|
5
|
+
import { readJsonConfig, writeJsonConfig } from '../../../lib/github-config';
|
|
6
|
+
import { DEFAULT_EMAIL_SETTINGS, EMAIL_SETTINGS_PATH } from '../../emails/handlers/settings.handler';
|
|
7
|
+
import Stripe from 'stripe';
|
|
8
|
+
import { createId, ensureSchema, issueSessionForUser, nowSeconds } from '../../../lib/project-db';
|
|
9
|
+
async function hasAdminUsers(c) {
|
|
10
|
+
const db = await ensureSchema(c.env);
|
|
11
|
+
const row = await db.prepare('SELECT COUNT(*) AS count FROM admin_users').first();
|
|
12
|
+
return (row?.count ?? 0) > 0;
|
|
13
|
+
}
|
|
14
|
+
async function deploy(c) {
|
|
15
|
+
const client = createGitHubClient(getGitHubConfig(c.env));
|
|
16
|
+
const { owner, repo, branch } = client.getRepoInfo();
|
|
17
|
+
await (await client.getOctokit()).rest.actions.createWorkflowDispatch({ owner, repo, workflow_id: 'deploy.yml', ref: branch });
|
|
18
|
+
}
|
|
19
|
+
/** One-time, project-owned bootstrap. It is unavailable as soon as an admin exists. */
|
|
20
|
+
export async function setupProject(c) {
|
|
21
|
+
if (await hasAdminUsers(c))
|
|
22
|
+
return c.json({ error: 'Project setup has already been completed.' }, 409);
|
|
23
|
+
const body = await c.req.json();
|
|
24
|
+
const email = typeof body.email === 'string' ? body.email.trim().toLowerCase() : '';
|
|
25
|
+
const password = typeof body.password === 'string' ? body.password : '';
|
|
26
|
+
const name = typeof body.name === 'string' ? body.name.trim() : null;
|
|
27
|
+
if (!email || !password)
|
|
28
|
+
return c.json({ error: 'Email and password are required.' }, 400);
|
|
29
|
+
if (password.length < 12)
|
|
30
|
+
return c.json({ error: 'Use a password of at least 12 characters.' }, 400);
|
|
31
|
+
const db = await ensureSchema(c.env);
|
|
32
|
+
const now = nowSeconds();
|
|
33
|
+
const user = {
|
|
34
|
+
id: createId('user'), email, name, login_method: 'password', password_hash: await hashPw(password, c.env.SECRET ?? ''),
|
|
35
|
+
disabled_at: null, created_at: now, updated_at: now,
|
|
36
|
+
};
|
|
37
|
+
await db.prepare(`INSERT INTO admin_users (id, email, name, password_hash, login_method, disabled_at, created_at, updated_at) VALUES (?, ?, ?, ?, ?, NULL, ?, ?)`)
|
|
38
|
+
.bind(user.id, user.email, user.name, user.password_hash, user.login_method, now, now).run();
|
|
39
|
+
try {
|
|
40
|
+
const client = createGitHubClient(getGitHubConfig(c.env));
|
|
41
|
+
const stripeSecretKey = typeof body.stripeSecretKey === 'string' ? body.stripeSecretKey : '';
|
|
42
|
+
let stripeWebhookSecret = typeof body.stripeWebhookSecret === 'string' ? body.stripeWebhookSecret : '';
|
|
43
|
+
if (stripeSecretKey && !stripeWebhookSecret) {
|
|
44
|
+
try {
|
|
45
|
+
const webhook = await new Stripe(stripeSecretKey, { apiVersion: '2025-02-24.acacia' }).webhookEndpoints.create({
|
|
46
|
+
url: `${new URL(c.req.url).origin}/api/email/webhook`,
|
|
47
|
+
enabled_events: ['checkout.session.completed', 'charge.succeeded', 'payment_intent.succeeded'],
|
|
48
|
+
});
|
|
49
|
+
stripeWebhookSecret = webhook.secret ?? '';
|
|
50
|
+
}
|
|
51
|
+
catch (error) {
|
|
52
|
+
console.error('Stripe webhook setup failed:', error);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
const secrets = {};
|
|
56
|
+
for (const [name, value] of [
|
|
57
|
+
['API_KEY', body.plunkApiKey],
|
|
58
|
+
['STRIPE_SECRET_KEY', stripeSecretKey],
|
|
59
|
+
['STRIPE_WEBHOOK_SECRET', stripeWebhookSecret],
|
|
60
|
+
]) {
|
|
61
|
+
if (typeof value === 'string' && value)
|
|
62
|
+
secrets[name] = value;
|
|
63
|
+
}
|
|
64
|
+
await Promise.all(Object.entries(secrets).map(([secretName, value]) => client.setActionsSecret(secretName, value)));
|
|
65
|
+
const { data: emailSettings, commitSha } = await readJsonConfig(client, EMAIL_SETTINGS_PATH, DEFAULT_EMAIL_SETTINGS);
|
|
66
|
+
await writeJsonConfig(client, EMAIL_SETTINGS_PATH, {
|
|
67
|
+
...emailSettings,
|
|
68
|
+
fromEmail: typeof body.fromEmail === 'string' ? body.fromEmail.trim() : emailSettings.fromEmail,
|
|
69
|
+
fromName: typeof body.fromName === 'string' ? body.fromName.trim() : emailSettings.fromName,
|
|
70
|
+
replyTo: typeof body.replyTo === 'string' ? body.replyTo.trim() : emailSettings.replyTo,
|
|
71
|
+
updatedAt: Date.now(),
|
|
72
|
+
}, 'Configure email sender settings [skip ci]', commitSha);
|
|
73
|
+
if (Object.keys(secrets).length)
|
|
74
|
+
await deploy(c);
|
|
75
|
+
}
|
|
76
|
+
catch (error) {
|
|
77
|
+
console.error('Project setup integrations could not be saved:', error);
|
|
78
|
+
}
|
|
79
|
+
const token = await signWithPayload(c.env.SECRET ?? '', issueSessionForUser({ ...user, password_hash: user.password_hash }));
|
|
80
|
+
const secureCookie = c.req.url.startsWith('https://') ? '; Secure' : '';
|
|
81
|
+
return c.json({ success: true }, 201, { 'Set-Cookie': `token=${token}; HttpOnly; Path=/; SameSite=Strict${secureCookie}` });
|
|
82
|
+
}
|
|
83
|
+
export async function projectNeedsSetup(c) {
|
|
84
|
+
return !(await hasAdminUsers(c));
|
|
85
|
+
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/api/cms/routes/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/api/cms/routes/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAoB5B,OAAO,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AAEzC,QAAA,MAAM,SAAS,sDAAsB,CAAC;AA4BtC,eAAe,SAAS,CAAC"}
|
|
@@ -5,12 +5,15 @@ import authRoutes from './auth.routes';
|
|
|
5
5
|
import { bootstrapAdminUser, createAdminUser, deleteAdminUser, listAdminUsers, patchAdminUser } from '../handlers/admin-users.handler';
|
|
6
6
|
import { deleteConversation, getAgentApiKey, listConversations, upsertConversation } from '../handlers/agent-conversations.handler';
|
|
7
7
|
import { commitWebsiteRepo, headWebsiteRepo, syncWebsiteRepo, uploadWebsiteAsset } from '../handlers/website-repo.handler';
|
|
8
|
+
import { deleteGitHubSecret, listGitHubSecrets, upsertGitHubSecret } from '../handlers/github-secrets.handler';
|
|
9
|
+
import { setupProject } from '../handlers/setup.handler';
|
|
8
10
|
const cmsRoutes = new Hono();
|
|
9
11
|
// Dashboard route (protected)
|
|
10
12
|
cmsRoutes.get('/', authMiddleware, getDashboard);
|
|
11
13
|
// Auth routes (public)
|
|
12
14
|
cmsRoutes.route('/', authRoutes);
|
|
13
15
|
cmsRoutes.post('/bootstrap', bootstrapAdminUser);
|
|
16
|
+
cmsRoutes.post('/setup', setupProject);
|
|
14
17
|
// Protected routes
|
|
15
18
|
cmsRoutes.use('/*', authMiddleware);
|
|
16
19
|
cmsRoutes.get('/users', listAdminUsers);
|
|
@@ -25,4 +28,7 @@ cmsRoutes.get('/website/repo/head', headWebsiteRepo);
|
|
|
25
28
|
cmsRoutes.get('/website/repo/sync', syncWebsiteRepo);
|
|
26
29
|
cmsRoutes.put('/website/repo/commit', commitWebsiteRepo);
|
|
27
30
|
cmsRoutes.put('/website/repo/upload', uploadWebsiteAsset);
|
|
31
|
+
cmsRoutes.get('/github/secrets', listGitHubSecrets);
|
|
32
|
+
cmsRoutes.put('/github/secrets/:name', upsertGitHubSecret);
|
|
33
|
+
cmsRoutes.delete('/github/secrets/:name', deleteGitHubSecret);
|
|
28
34
|
export default cmsRoutes;
|
|
@@ -28,7 +28,7 @@ function getStripe(env) {
|
|
|
28
28
|
*/
|
|
29
29
|
export async function handleStripeWebhook(c) {
|
|
30
30
|
const stripe = getStripe(c.env);
|
|
31
|
-
const webhookSecret = c.env.
|
|
31
|
+
const webhookSecret = c.env.STRIPE_WEBHOOK_SECRET;
|
|
32
32
|
if (!webhookSecret) {
|
|
33
33
|
console.error('Stripe webhook secret not configured');
|
|
34
34
|
return c.json({ error: 'Webhook not configured' }, 500);
|
|
@@ -6,7 +6,7 @@ import { sendEmail } from '../../../lib/email';
|
|
|
6
6
|
import { renderEmailTemplate } from '../../../lib/email-template';
|
|
7
7
|
import { readShopCatalog, readShopSettings } from './products.handler';
|
|
8
8
|
import { readEmailSettings, readEmailTemplateData } from '../../emails/handlers/settings.handler';
|
|
9
|
-
import {
|
|
9
|
+
import { ensureSchema } from '../../../lib/project-db';
|
|
10
10
|
const EUROPEAN_STRIPE_COUNTRIES = [
|
|
11
11
|
'AL', 'AD', 'AT', 'BY', 'BE', 'BA', 'BG', 'HR', 'CY', 'CZ', 'DK', 'EE',
|
|
12
12
|
'FI', 'FR', 'DE', 'GR', 'HU', 'IS', 'IE', 'IT', 'LV', 'LI', 'LT', 'LU',
|
|
@@ -46,7 +46,7 @@ function rowToOrder(row) {
|
|
|
46
46
|
};
|
|
47
47
|
}
|
|
48
48
|
async function insertOrder(env, order) {
|
|
49
|
-
const db = await
|
|
49
|
+
const db = await ensureSchema(env);
|
|
50
50
|
await db
|
|
51
51
|
.prepare(`INSERT INTO shop_orders (
|
|
52
52
|
id, order_number, status, payment_method, customer_json, items_json, totals_json,
|
|
@@ -57,7 +57,7 @@ async function insertOrder(env, order) {
|
|
|
57
57
|
.run();
|
|
58
58
|
}
|
|
59
59
|
async function updateOrder(env, order) {
|
|
60
|
-
const db = await
|
|
60
|
+
const db = await ensureSchema(env);
|
|
61
61
|
await db
|
|
62
62
|
.prepare(`UPDATE shop_orders
|
|
63
63
|
SET status = ?, customer_json = ?, items_json = ?, totals_json = ?, stripe_session_id = ?,
|
|
@@ -67,12 +67,12 @@ async function updateOrder(env, order) {
|
|
|
67
67
|
.run();
|
|
68
68
|
}
|
|
69
69
|
async function getOrderById(env, id) {
|
|
70
|
-
const db = await
|
|
70
|
+
const db = await ensureSchema(env);
|
|
71
71
|
const row = await db.prepare('SELECT * FROM shop_orders WHERE id = ? OR order_number = ?').bind(id, id).first();
|
|
72
72
|
return row ? rowToOrder(row) : null;
|
|
73
73
|
}
|
|
74
74
|
async function getOrderByStripeSession(env, sessionId) {
|
|
75
|
-
const db = await
|
|
75
|
+
const db = await ensureSchema(env);
|
|
76
76
|
const row = await db.prepare('SELECT * FROM shop_orders WHERE stripe_session_id = ?').bind(sessionId).first();
|
|
77
77
|
return row ? rowToOrder(row) : null;
|
|
78
78
|
}
|
|
@@ -180,7 +180,7 @@ export async function createCheckoutSession(c) {
|
|
|
180
180
|
const items = calculateItems(request, catalog.products);
|
|
181
181
|
// Auto-select payment method: prefer Stripe when configured + enabled and no method specified
|
|
182
182
|
if (!request.paymentMethod) {
|
|
183
|
-
const stripeEnabled = settings.enabledPaymentMethods.includes('stripe') && !!c.env.
|
|
183
|
+
const stripeEnabled = settings.enabledPaymentMethods.includes('stripe') && !!c.env.STRIPE_SECRET_KEY;
|
|
184
184
|
request.paymentMethod = stripeEnabled ? 'stripe' : 'bank_transfer';
|
|
185
185
|
}
|
|
186
186
|
const order = createOrder(request, items, settings.orderNumberPrefix);
|
|
@@ -189,13 +189,13 @@ export async function createCheckoutSession(c) {
|
|
|
189
189
|
return c.json({ success: false, error: 'Payment method is not enabled' }, 400);
|
|
190
190
|
}
|
|
191
191
|
if (order.paymentMethod === 'stripe') {
|
|
192
|
-
if (!c.env.
|
|
192
|
+
if (!c.env.STRIPE_SECRET_KEY) {
|
|
193
193
|
return c.json({ success: false, error: 'Stripe is not configured' }, 400);
|
|
194
194
|
}
|
|
195
195
|
if (!request.successUrl || !request.cancelUrl) {
|
|
196
196
|
return c.json({ success: false, error: 'Missing successUrl or cancelUrl' }, 400);
|
|
197
197
|
}
|
|
198
|
-
const stripe = new Stripe(c.env.
|
|
198
|
+
const stripe = new Stripe(c.env.STRIPE_SECRET_KEY, { apiVersion: '2025-02-24.acacia' });
|
|
199
199
|
const session = await stripe.checkout.sessions.create({
|
|
200
200
|
mode: 'payment',
|
|
201
201
|
line_items: [
|
|
@@ -263,7 +263,7 @@ export async function createCheckoutSession(c) {
|
|
|
263
263
|
}
|
|
264
264
|
export async function getOrders(c) {
|
|
265
265
|
try {
|
|
266
|
-
const db = await
|
|
266
|
+
const db = await ensureSchema(c.env);
|
|
267
267
|
const status = c.req.query('status');
|
|
268
268
|
const paymentMethod = c.req.query('paymentMethod');
|
|
269
269
|
const rows = await db
|
|
@@ -527,8 +527,8 @@ export async function sendOrderInvoice(c) {
|
|
|
527
527
|
}
|
|
528
528
|
export async function handleStripeWebhook(c) {
|
|
529
529
|
try {
|
|
530
|
-
const secretKey = c.env.
|
|
531
|
-
const webhookSecret = c.env.
|
|
530
|
+
const secretKey = c.env.STRIPE_SECRET_KEY;
|
|
531
|
+
const webhookSecret = c.env.STRIPE_WEBHOOK_SECRET;
|
|
532
532
|
if (!secretKey || !webhookSecret)
|
|
533
533
|
return c.json({ error: 'Stripe webhook is not configured' }, 500);
|
|
534
534
|
const signature = c.req.header('stripe-signature');
|