@hintergrund/worker 0.1.14 → 0.2.3
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.d.ts +0 -1
- package/dist/api/cms/handlers/admin-users.handler.d.ts.map +1 -1
- package/dist/api/cms/handlers/admin-users.handler.js +7 -27
- package/dist/api/cms/handlers/agent-conversations.handler.d.ts +0 -1
- package/dist/api/cms/handlers/agent-conversations.handler.d.ts.map +1 -1
- package/dist/api/cms/handlers/agent-conversations.handler.js +0 -7
- package/dist/api/cms/handlers/agent-provider.handler.d.ts +12 -0
- package/dist/api/cms/handlers/agent-provider.handler.d.ts.map +1 -0
- package/dist/api/cms/handlers/agent-provider.handler.js +64 -0
- package/dist/api/cms/handlers/auth.handler.d.ts +0 -4
- package/dist/api/cms/handlers/auth.handler.d.ts.map +1 -1
- package/dist/api/cms/handlers/auth.handler.js +88 -106
- package/dist/api/cms/handlers/dashboard.handler.d.ts.map +1 -1
- package/dist/api/cms/handlers/dashboard.handler.js +2 -3
- package/dist/api/cms/handlers/github-secrets.handler.d.ts.map +1 -1
- package/dist/api/cms/handlers/github-secrets.handler.js +37 -7
- package/dist/api/cms/handlers/setup.handler.d.ts +2 -0
- package/dist/api/cms/handlers/setup.handler.d.ts.map +1 -1
- package/dist/api/cms/handlers/setup.handler.js +111 -30
- package/dist/api/cms/handlers/website-repo.handler.d.ts.map +1 -1
- package/dist/api/cms/handlers/website-repo.handler.js +11 -4
- package/dist/api/cms/handlers/website-templates.handler.d.ts +6 -0
- package/dist/api/cms/handlers/website-templates.handler.d.ts.map +1 -0
- package/dist/api/cms/handlers/website-templates.handler.js +53 -0
- package/dist/api/cms/routes/auth.routes.d.ts.map +1 -1
- package/dist/api/cms/routes/auth.routes.js +1 -2
- package/dist/api/cms/routes/index.d.ts.map +1 -1
- package/dist/api/cms/routes/index.js +11 -5
- package/dist/hintergrund-assets.d.ts +4 -4
- package/dist/hintergrund-assets.d.ts.map +1 -1
- package/dist/hintergrund-assets.js +4 -4
- package/dist/index.d.ts +7 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -6
- package/dist/lib/config.d.ts +5 -11
- package/dist/lib/config.d.ts.map +1 -1
- package/dist/lib/config.js +21 -36
- package/dist/lib/email.d.ts +3 -5
- package/dist/lib/email.d.ts.map +1 -1
- package/dist/lib/email.js +8 -25
- package/dist/lib/github-app.d.ts.map +1 -1
- package/dist/lib/github-app.js +4 -1
- package/dist/lib/github.d.ts +1 -1
- package/dist/lib/github.d.ts.map +1 -1
- package/dist/lib/github.js +30 -9
- package/dist/lib/jsonc.d.ts +3 -0
- package/dist/lib/jsonc.d.ts.map +1 -0
- package/dist/lib/jsonc.js +76 -0
- package/dist/lib/project-db.d.ts +4 -2
- package/dist/lib/project-db.d.ts.map +1 -1
- package/dist/lib/project-db.js +12 -11
- package/dist/middleware/auth.d.ts.map +1 -1
- package/dist/middleware/auth.js +0 -1
- package/package.json +35 -35
- package/dist/api/emails/handlers/webhook.handler.d.ts +0 -9
- package/dist/api/emails/handlers/webhook.handler.d.ts.map +0 -1
- package/dist/api/emails/handlers/webhook.handler.js +0 -161
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import type { Context } from 'hono';
|
|
2
2
|
import type { HonoEnv } from '../../../index';
|
|
3
3
|
export declare function listAdminUsers(c: Context<HonoEnv>): Promise<Response>;
|
|
4
|
-
export declare function bootstrapAdminUser(c: Context<HonoEnv>): Promise<Response>;
|
|
5
4
|
export declare function createAdminUser(c: Context<HonoEnv>): Promise<Response>;
|
|
6
5
|
export declare function patchAdminUser(c: Context<HonoEnv>): Promise<Response>;
|
|
7
6
|
export declare function deleteAdminUser(c: Context<HonoEnv>): Promise<Response>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"admin-users.handler.d.ts","sourceRoot":"","sources":["../../../../src/api/cms/handlers/admin-users.handler.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AACpC,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;
|
|
1
|
+
{"version":3,"file":"admin-users.handler.d.ts","sourceRoot":"","sources":["../../../../src/api/cms/handlers/admin-users.handler.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AACpC,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AAwB9C,wBAAsB,cAAc,CAAC,CAAC,EAAE,OAAO,CAAC,OAAO,CAAC,GAAG,OAAO,CAAC,QAAQ,CAAC,CAU3E;AAED,wBAAsB,eAAe,CAAC,CAAC,EAAE,OAAO,CAAC,OAAO,CAAC,GAAG,OAAO,CAAC,QAAQ,CAAC,CAE5E;AA8BD,wBAAsB,cAAc,CAAC,CAAC,EAAE,OAAO,CAAC,OAAO,CAAC,GAAG,OAAO,CAAC,QAAQ,CAAC,CAqC3E;AAED,wBAAsB,eAAe,CAAC,CAAC,EAAE,OAAO,CAAC,OAAO,CAAC,GAAG,OAAO,CAAC,QAAQ,CAAC,CAiB5E"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { hashPw } from '../../../lib/auth/crypto';
|
|
2
2
|
import { createId, ensureSchema, nowSeconds } from '../../../lib/project-db';
|
|
3
|
-
const LOGIN_METHODS = ['password', '
|
|
3
|
+
const LOGIN_METHODS = ['password', 'otp'];
|
|
4
4
|
function userToResponse(user) {
|
|
5
5
|
return {
|
|
6
6
|
id: user.id,
|
|
@@ -13,21 +13,6 @@ function userToResponse(user) {
|
|
|
13
13
|
hasPassword: !!user.password_hash,
|
|
14
14
|
};
|
|
15
15
|
}
|
|
16
|
-
async function hasAdminUsers(c) {
|
|
17
|
-
const db = await ensureSchema(c.env);
|
|
18
|
-
const row = await db.prepare('SELECT COUNT(*) AS count FROM admin_users').first();
|
|
19
|
-
return (row?.count ?? 0) > 0;
|
|
20
|
-
}
|
|
21
|
-
function verifyBootstrapToken(c, provided) {
|
|
22
|
-
const configured = c.env.BOOTSTRAP_TOKEN?.trim();
|
|
23
|
-
if (!configured) {
|
|
24
|
-
return c.json({ error: 'Admin bootstrap is not configured' }, 403);
|
|
25
|
-
}
|
|
26
|
-
if (String(provided ?? '') !== configured) {
|
|
27
|
-
return c.json({ error: 'Invalid bootstrap token' }, 403);
|
|
28
|
-
}
|
|
29
|
-
return null;
|
|
30
|
-
}
|
|
31
16
|
export async function listAdminUsers(c) {
|
|
32
17
|
const db = await ensureSchema(c.env);
|
|
33
18
|
const { results } = await db
|
|
@@ -39,23 +24,13 @@ export async function listAdminUsers(c) {
|
|
|
39
24
|
}));
|
|
40
25
|
return c.json({ users });
|
|
41
26
|
}
|
|
42
|
-
export async function bootstrapAdminUser(c) {
|
|
43
|
-
const body = await c.req.json();
|
|
44
|
-
const tokenError = verifyBootstrapToken(c, body.bootstrapToken ?? c.req.header('x-bootstrap-token'));
|
|
45
|
-
if (tokenError)
|
|
46
|
-
return tokenError;
|
|
47
|
-
if (await hasAdminUsers(c)) {
|
|
48
|
-
return c.json({ error: 'Admin users already exist' }, 409);
|
|
49
|
-
}
|
|
50
|
-
return createAdminUserFromBody(c, body, 201);
|
|
51
|
-
}
|
|
52
27
|
export async function createAdminUser(c) {
|
|
53
28
|
return createAdminUserFromBody(c, await c.req.json(), 201);
|
|
54
29
|
}
|
|
55
30
|
async function createAdminUserFromBody(c, body, status) {
|
|
56
31
|
const email = String(body.email ?? '').toLowerCase().trim();
|
|
57
32
|
const name = body.name ? String(body.name).trim() : null;
|
|
58
|
-
const loginMethod = LOGIN_METHODS.includes(body.loginMethod) ? body.loginMethod : '
|
|
33
|
+
const loginMethod = LOGIN_METHODS.includes(body.loginMethod) ? body.loginMethod : 'otp';
|
|
59
34
|
if (!email) {
|
|
60
35
|
return c.json({ error: 'Email is required' }, 400);
|
|
61
36
|
}
|
|
@@ -93,6 +68,11 @@ export async function patchAdminUser(c) {
|
|
|
93
68
|
const passwordHash = body.password
|
|
94
69
|
? await hashPw(String(body.password), c.env.SECRET ?? '')
|
|
95
70
|
: (body.passwordHash === undefined ? existing.password_hash : body.passwordHash);
|
|
71
|
+
// The owner's password is the way back in when email delivery fails, so it can be
|
|
72
|
+
// changed but never removed.
|
|
73
|
+
if (existing.initial_admin === 1 && !passwordHash) {
|
|
74
|
+
return c.json({ error: 'The site owner must keep a password.' }, 400);
|
|
75
|
+
}
|
|
96
76
|
const now = nowSeconds();
|
|
97
77
|
await db
|
|
98
78
|
.prepare(`UPDATE admin_users
|
|
@@ -3,5 +3,4 @@ import type { HonoEnv } from '../../../index';
|
|
|
3
3
|
export declare function listConversations(c: Context<HonoEnv>): Promise<Response>;
|
|
4
4
|
export declare function upsertConversation(c: Context<HonoEnv>): Promise<Response>;
|
|
5
5
|
export declare function deleteConversation(c: Context<HonoEnv>): Promise<Response>;
|
|
6
|
-
export declare function getAgentApiKey(c: Context<HonoEnv>): Promise<Response>;
|
|
7
6
|
//# sourceMappingURL=agent-conversations.handler.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"agent-conversations.handler.d.ts","sourceRoot":"","sources":["../../../../src/api/cms/handlers/agent-conversations.handler.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AACpC,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AA4B9C,wBAAsB,iBAAiB,CAAC,CAAC,EAAE,OAAO,CAAC,OAAO,CAAC,GAAG,OAAO,CAAC,QAAQ,CAAC,CAY9E;AAED,wBAAsB,kBAAkB,CAAC,CAAC,EAAE,OAAO,CAAC,OAAO,CAAC,GAAG,OAAO,CAAC,QAAQ,CAAC,CA8C/E;AAED,wBAAsB,kBAAkB,CAAC,CAAC,EAAE,OAAO,CAAC,OAAO,CAAC,GAAG,OAAO,CAAC,QAAQ,CAAC,CAW/E
|
|
1
|
+
{"version":3,"file":"agent-conversations.handler.d.ts","sourceRoot":"","sources":["../../../../src/api/cms/handlers/agent-conversations.handler.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AACpC,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AA4B9C,wBAAsB,iBAAiB,CAAC,CAAC,EAAE,OAAO,CAAC,OAAO,CAAC,GAAG,OAAO,CAAC,QAAQ,CAAC,CAY9E;AAED,wBAAsB,kBAAkB,CAAC,CAAC,EAAE,OAAO,CAAC,OAAO,CAAC,GAAG,OAAO,CAAC,QAAQ,CAAC,CA8C/E;AAED,wBAAsB,kBAAkB,CAAC,CAAC,EAAE,OAAO,CAAC,OAAO,CAAC,GAAG,OAAO,CAAC,QAAQ,CAAC,CAW/E"}
|
|
@@ -85,10 +85,3 @@ export async function deleteConversation(c) {
|
|
|
85
85
|
.run();
|
|
86
86
|
return c.json({ success: true });
|
|
87
87
|
}
|
|
88
|
-
export async function getAgentApiKey(c) {
|
|
89
|
-
const apiKey = c.env.GEMINI_API_KEY?.trim();
|
|
90
|
-
if (!apiKey) {
|
|
91
|
-
return c.json({ error: 'Builder agent is not configured' }, 500);
|
|
92
|
-
}
|
|
93
|
-
return c.json({ apiKey });
|
|
94
|
-
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Context } from 'hono';
|
|
2
|
+
import { type AgentProviderName } from '@hintergrund/runtime-contracts';
|
|
3
|
+
import type { Bindings, HonoEnv } from '../../../index';
|
|
4
|
+
/** The provider to use, in declaration order, for projects that configure several. */
|
|
5
|
+
export declare function activeAgentProvider(env: Bindings): AgentProviderName | null;
|
|
6
|
+
export declare function getAgentConfig(c: Context<HonoEnv>): Promise<Response>;
|
|
7
|
+
/**
|
|
8
|
+
* Streams an agent request through to the configured provider. The browser speaks
|
|
9
|
+
* the provider's own protocol, so the body is forwarded verbatim both ways.
|
|
10
|
+
*/
|
|
11
|
+
export declare function proxyAgentRequest(c: Context<HonoEnv>): Promise<Response>;
|
|
12
|
+
//# sourceMappingURL=agent-provider.handler.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"agent-provider.handler.d.ts","sourceRoot":"","sources":["../../../../src/api/cms/handlers/agent-provider.handler.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AACpC,OAAO,EAA2C,KAAK,iBAAiB,EAAE,MAAM,gCAAgC,CAAC;AACjH,OAAO,KAAK,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AA4BxD,sFAAsF;AACtF,wBAAgB,mBAAmB,CAAC,GAAG,EAAE,QAAQ,GAAG,iBAAiB,GAAG,IAAI,CAE3E;AAED,wBAAsB,cAAc,CAAC,CAAC,EAAE,OAAO,CAAC,OAAO,CAAC,GAAG,OAAO,CAAC,QAAQ,CAAC,CAE3E;AAED;;;GAGG;AACH,wBAAsB,iBAAiB,CAAC,CAAC,EAAE,OAAO,CAAC,OAAO,CAAC,GAAG,OAAO,CAAC,QAAQ,CAAC,CA8B9E"}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { AGENT_PROVIDERS, AGENT_PROVIDER_SECRETS } from '@hintergrund/runtime-contracts';
|
|
2
|
+
/**
|
|
3
|
+
* Where each provider's requests go, and how its key is presented. Provider keys
|
|
4
|
+
* are Worker secrets and never reach the browser: the admin agents run in the
|
|
5
|
+
* client and call `/api/admin/agent/llm/*`, which attaches the key here.
|
|
6
|
+
*/
|
|
7
|
+
const PROVIDER_ENDPOINTS = {
|
|
8
|
+
google: {
|
|
9
|
+
baseUrl: 'https://generativelanguage.googleapis.com/v1beta',
|
|
10
|
+
authorize: (headers, key) => headers.set('x-goog-api-key', key),
|
|
11
|
+
},
|
|
12
|
+
deepseek: {
|
|
13
|
+
baseUrl: 'https://api.deepseek.com',
|
|
14
|
+
authorize: (headers, key) => headers.set('authorization', `Bearer ${key}`),
|
|
15
|
+
},
|
|
16
|
+
};
|
|
17
|
+
function providerKey(env, provider) {
|
|
18
|
+
const value = env[AGENT_PROVIDER_SECRETS[provider]];
|
|
19
|
+
if (typeof value !== 'string')
|
|
20
|
+
return undefined;
|
|
21
|
+
const key = value.trim();
|
|
22
|
+
return key && key !== 'NOT_CONFIGURED' ? key : undefined;
|
|
23
|
+
}
|
|
24
|
+
/** The provider to use, in declaration order, for projects that configure several. */
|
|
25
|
+
export function activeAgentProvider(env) {
|
|
26
|
+
return AGENT_PROVIDERS.find((provider) => providerKey(env, provider)) ?? null;
|
|
27
|
+
}
|
|
28
|
+
export async function getAgentConfig(c) {
|
|
29
|
+
return c.json({ provider: activeAgentProvider(c.env) });
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Streams an agent request through to the configured provider. The browser speaks
|
|
33
|
+
* the provider's own protocol, so the body is forwarded verbatim both ways.
|
|
34
|
+
*/
|
|
35
|
+
export async function proxyAgentRequest(c) {
|
|
36
|
+
const provider = activeAgentProvider(c.env);
|
|
37
|
+
if (!provider)
|
|
38
|
+
return c.json({ error: 'No AI provider is configured for this project.' }, 503);
|
|
39
|
+
const { baseUrl, authorize } = PROVIDER_ENDPOINTS[provider];
|
|
40
|
+
const headers = new Headers();
|
|
41
|
+
for (const name of ['content-type', 'accept']) {
|
|
42
|
+
const value = c.req.header(name);
|
|
43
|
+
if (value)
|
|
44
|
+
headers.set(name, value);
|
|
45
|
+
}
|
|
46
|
+
authorize(headers, providerKey(c.env, provider));
|
|
47
|
+
const { search } = new URL(c.req.url);
|
|
48
|
+
const upstream = await fetch(`${baseUrl}/${c.req.param('path')}${search}`, {
|
|
49
|
+
method: c.req.method,
|
|
50
|
+
headers,
|
|
51
|
+
body: c.req.raw.body,
|
|
52
|
+
// The Workers runtime requires this when forwarding a streaming request body.
|
|
53
|
+
duplex: 'half',
|
|
54
|
+
signal: c.req.raw.signal,
|
|
55
|
+
});
|
|
56
|
+
// Pass the upstream body straight through so tokens reach the UI as they arrive.
|
|
57
|
+
return new Response(upstream.body, {
|
|
58
|
+
status: upstream.status,
|
|
59
|
+
headers: {
|
|
60
|
+
'content-type': upstream.headers.get('content-type') ?? 'application/json',
|
|
61
|
+
'cache-control': 'no-store',
|
|
62
|
+
},
|
|
63
|
+
});
|
|
64
|
+
}
|
|
@@ -1,9 +1,5 @@
|
|
|
1
1
|
import { Context } from 'hono';
|
|
2
2
|
import { HonoEnv } from '../../../index';
|
|
3
3
|
export declare function login(c: Context<HonoEnv>): Promise<Response>;
|
|
4
|
-
/**
|
|
5
|
-
* Verify magic link token and log user in
|
|
6
|
-
*/
|
|
7
|
-
export declare function verifyMagicLink(c: Context<HonoEnv>): Promise<Response>;
|
|
8
4
|
export declare function logout(c: Context<HonoEnv>): Promise<Response>;
|
|
9
5
|
//# sourceMappingURL=auth.handler.d.ts.map
|
|
@@ -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;AAgBzC,wBAAsB,KAAK,CAAC,CAAC,EAAE,OAAO,CAAC,OAAO,CAAC,GAAG,OAAO,CAAC,QAAQ,CAAC,CAqBlE;AAsHD,wBAAsB,MAAM,CAAC,CAAC,EAAE,OAAO,CAAC,OAAO,CAAC,GAAG,OAAO,CAAC,QAAQ,CAAC,CAOnE"}
|
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import { readRequestBody, verifyPw
|
|
1
|
+
import { readRequestBody, verifyPw } from '../../../lib/auth/crypto';
|
|
2
2
|
import { signWithPayload } from '../../../lib/auth/jwt';
|
|
3
3
|
import { loadConfig } from '../../../lib/config';
|
|
4
|
-
import {
|
|
5
|
-
import { readEmailSettings } from '../../emails/handlers/settings.handler';
|
|
4
|
+
import { sendEmailOtp } from '../../../lib/email';
|
|
6
5
|
import { createId, ensureSchema, getAdminUserByEmail, issueSessionForUser, nowSeconds, sha256Base64Url } from '../../../lib/project-db';
|
|
7
6
|
export async function login(c) {
|
|
8
7
|
try {
|
|
@@ -10,128 +9,114 @@ export async function login(c) {
|
|
|
10
9
|
const body = await readRequestBody(c.req.raw);
|
|
11
10
|
const isSecure = c.req.url.startsWith('https://');
|
|
12
11
|
const email = body.email?.toLowerCase().trim();
|
|
13
|
-
if (!email)
|
|
14
|
-
return
|
|
15
|
-
}
|
|
12
|
+
if (!email)
|
|
13
|
+
return loginError('Email is required', 400);
|
|
16
14
|
const user = await getAdminUserByEmail(c.env, email);
|
|
17
|
-
if (!user)
|
|
18
|
-
return
|
|
19
|
-
|
|
20
|
-
|
|
15
|
+
if (!user)
|
|
16
|
+
return loginError('Invalid credentials', 401);
|
|
17
|
+
// A stored password stays valid as a fallback: an OTP admin can still sign in when
|
|
18
|
+
// email delivery breaks (expired provider key, changed DNS, provider outage).
|
|
19
|
+
if (body.password)
|
|
21
20
|
return await handleProjectPasswordLogin(c, body.password, appConfig.global.hgSecret, user, isSecure);
|
|
22
|
-
}
|
|
23
21
|
if (user.login_method === 'password') {
|
|
24
|
-
return
|
|
22
|
+
return c.json({ success: true, requirePassword: true });
|
|
25
23
|
}
|
|
26
|
-
return await
|
|
24
|
+
return await handleProjectEmailOtpLogin(c, body.otp, user, appConfig, isSecure);
|
|
27
25
|
}
|
|
28
26
|
catch (error) {
|
|
29
27
|
console.error('Login error:', error);
|
|
30
|
-
return
|
|
28
|
+
return loginError('Unable to sign in. Try again.', 401);
|
|
31
29
|
}
|
|
32
30
|
}
|
|
31
|
+
/** The project owner keeps a password fallback; other admins do not. */
|
|
32
|
+
function ownerHasPasswordFallback(user) {
|
|
33
|
+
return user.initial_admin === 1 && !!user.password_hash;
|
|
34
|
+
}
|
|
35
|
+
function loginError(message, status) {
|
|
36
|
+
return new Response(JSON.stringify({ success: false, message }), { status });
|
|
37
|
+
}
|
|
33
38
|
async function handleProjectPasswordLogin(c, password, hgSecret, user, isSecure) {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
+
// The owner always keeps their password as a fallback, so a broken email provider
|
|
40
|
+
// cannot lock the project out. Other admins use only their chosen method, so a
|
|
41
|
+
// forgotten password never lingers as a live credential.
|
|
42
|
+
const ownerFallback = user.initial_admin === 1 && !!user.password_hash;
|
|
43
|
+
if (!ownerFallback && user.login_method !== 'password') {
|
|
44
|
+
return loginError('Password sign-in is not enabled for this user', 400);
|
|
39
45
|
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
'Set-Cookie': `token=${token}; HttpOnly; Path=/; SameSite=Strict${secureCookie}`
|
|
46
|
-
}
|
|
47
|
-
});
|
|
46
|
+
if (!user.password_hash)
|
|
47
|
+
return loginError('Password sign-in is not enabled for this user', 400);
|
|
48
|
+
if (!(await verifyPw(user.password_hash, password, hgSecret)))
|
|
49
|
+
return loginError('Invalid credentials', 401);
|
|
50
|
+
return signedInResponse(hgSecret, user, isSecure);
|
|
48
51
|
}
|
|
49
|
-
async function
|
|
50
|
-
if (user.login_method !== '
|
|
51
|
-
return
|
|
52
|
-
}
|
|
52
|
+
async function handleProjectEmailOtpLogin(c, otp, user, appConfig, isSecure) {
|
|
53
|
+
if (user.login_method !== 'otp')
|
|
54
|
+
return loginError('Email-code sign-in is not enabled for this user', 400);
|
|
53
55
|
const emailsConfig = appConfig.emails;
|
|
54
56
|
if (!emailsConfig?.emailApiKey) {
|
|
55
|
-
return
|
|
57
|
+
return c.json({
|
|
58
|
+
success: false,
|
|
59
|
+
requirePassword: ownerHasPasswordFallback(user),
|
|
60
|
+
message: ownerHasPasswordFallback(user)
|
|
61
|
+
? 'Email codes are unavailable for this project. Sign in with your password instead.'
|
|
62
|
+
: 'Email codes are unavailable because no email provider is configured for this project.',
|
|
63
|
+
}, 503);
|
|
56
64
|
}
|
|
57
|
-
const token = generateRandomToken();
|
|
58
|
-
const tokenHash = await sha256Base64Url(`${token}.${appConfig.global.hgSecret}`);
|
|
59
65
|
const db = await ensureSchema(c.env);
|
|
60
66
|
const now = nowSeconds();
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
.
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
export async function verifyMagicLink(c) {
|
|
67
|
+
const submittedOtp = otp?.trim();
|
|
68
|
+
if (submittedOtp) {
|
|
69
|
+
if (!/^\d{6}$/.test(submittedOtp))
|
|
70
|
+
return loginError('Enter the six-digit code from your email.', 400);
|
|
71
|
+
const codeHash = await otpHash(user.id, submittedOtp, appConfig.global.hgSecret);
|
|
72
|
+
const result = await db.prepare(`UPDATE admin_email_otps
|
|
73
|
+
SET used_at = ?
|
|
74
|
+
WHERE user_id = ? AND code_hash = ? AND used_at IS NULL AND expires_at > ?`).bind(now, user.id, codeHash, now).run();
|
|
75
|
+
if (!result.meta.changes)
|
|
76
|
+
return loginError('That code is invalid or has expired. Request a new one.', 401);
|
|
77
|
+
return signedInResponse(appConfig.global.hgSecret, user, isSecure);
|
|
78
|
+
}
|
|
79
|
+
const code = emailOtp();
|
|
80
|
+
const codeHash = await otpHash(user.id, code, appConfig.global.hgSecret);
|
|
81
|
+
await db.batch([
|
|
82
|
+
db.prepare('DELETE FROM admin_email_otps WHERE user_id = ?').bind(user.id),
|
|
83
|
+
db.prepare(`INSERT INTO admin_email_otps (id, user_id, code_hash, expires_at, used_at, created_at)
|
|
84
|
+
VALUES (?, ?, ?, ?, NULL, ?)`).bind(createId('otp'), user.id, codeHash, now + 15 * 60, now),
|
|
85
|
+
]);
|
|
86
|
+
const { settings } = await import('../../emails/handlers/settings.handler').then(({ readEmailSettings }) => readEmailSettings(c.env));
|
|
82
87
|
try {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
}
|
|
90
|
-
const db = await ensureSchema(c.env);
|
|
91
|
-
const tokenHash = await sha256Base64Url(`${token}.${appConfig.global.hgSecret}`);
|
|
92
|
-
const now = nowSeconds();
|
|
93
|
-
const link = await db
|
|
94
|
-
.prepare(`SELECT * FROM admin_magic_links
|
|
95
|
-
WHERE token_hash = ? AND used_at IS NULL AND expires_at > ?
|
|
96
|
-
LIMIT 1`)
|
|
97
|
-
.bind(tokenHash, now)
|
|
98
|
-
.first();
|
|
99
|
-
if (!link) {
|
|
100
|
-
return redirectToLoginWithError(c, 'Invalid or expired link');
|
|
101
|
-
}
|
|
102
|
-
const user = await db
|
|
103
|
-
.prepare('SELECT * FROM admin_users WHERE id = ? AND disabled_at IS NULL LIMIT 1')
|
|
104
|
-
.bind(link.user_id)
|
|
105
|
-
.first();
|
|
106
|
-
if (!user) {
|
|
107
|
-
return redirectToLoginWithError(c, 'Invalid or expired link');
|
|
108
|
-
}
|
|
109
|
-
await db
|
|
110
|
-
.prepare('UPDATE admin_magic_links SET used_at = ? WHERE id = ? AND used_at IS NULL')
|
|
111
|
-
.bind(now, link.id)
|
|
112
|
-
.run();
|
|
113
|
-
const sessionToken = await signWithPayload(appConfig.global.hgSecret, issueSessionForUser(user));
|
|
114
|
-
const baseUrl = c.req.url.replace(/\/api\/admin\/verify-magic-link.*$/, '');
|
|
115
|
-
return new Response(null, {
|
|
116
|
-
status: 302,
|
|
117
|
-
headers: {
|
|
118
|
-
'Location': `${baseUrl}/admin`,
|
|
119
|
-
'Set-Cookie': `token=${sessionToken}; HttpOnly; Path=/; SameSite=Strict${secureCookie}`
|
|
120
|
-
}
|
|
88
|
+
await sendEmailOtp({
|
|
89
|
+
to: user.email,
|
|
90
|
+
apiKey: emailsConfig.emailApiKey,
|
|
91
|
+
fromEmail: settings.fromEmail ?? '',
|
|
92
|
+
fromName: settings.fromName,
|
|
93
|
+
code,
|
|
121
94
|
});
|
|
122
95
|
}
|
|
123
96
|
catch (error) {
|
|
124
|
-
console.error('
|
|
125
|
-
return
|
|
97
|
+
console.error('Sign-in code delivery failed:', error);
|
|
98
|
+
return c.json({
|
|
99
|
+
success: false,
|
|
100
|
+
requirePassword: ownerHasPasswordFallback(user),
|
|
101
|
+
message: ownerHasPasswordFallback(user)
|
|
102
|
+
? 'We could not send your sign-in code. Sign in with your password instead, then check the email provider settings.'
|
|
103
|
+
: 'We could not send your sign-in code. Ask the site owner to check the email provider settings.',
|
|
104
|
+
}, 503);
|
|
126
105
|
}
|
|
106
|
+
return c.json({ success: true, otpRequired: true, message: 'Check your email for a six-digit sign-in code.' });
|
|
127
107
|
}
|
|
128
|
-
function
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
108
|
+
function emailOtp() {
|
|
109
|
+
return crypto.getRandomValues(new Uint32Array(1))[0].toString().slice(-6).padStart(6, '0');
|
|
110
|
+
}
|
|
111
|
+
async function otpHash(userId, code, secret) {
|
|
112
|
+
return sha256Base64Url(`${userId}.${code}.${secret}`);
|
|
113
|
+
}
|
|
114
|
+
async function signedInResponse(secret, user, isSecure) {
|
|
115
|
+
const token = await signWithPayload(secret, issueSessionForUser(user));
|
|
116
|
+
const secureCookie = isSecure ? '; Secure' : '';
|
|
117
|
+
return new Response(JSON.stringify({ success: true }), {
|
|
118
|
+
status: 200,
|
|
119
|
+
headers: { 'Set-Cookie': `token=${token}; HttpOnly; Path=/; SameSite=Strict${secureCookie}` }
|
|
135
120
|
});
|
|
136
121
|
}
|
|
137
122
|
export async function logout(c) {
|
|
@@ -139,9 +124,6 @@ export async function logout(c) {
|
|
|
139
124
|
const secureCookie = isSecure ? '; Secure' : '';
|
|
140
125
|
return new Response(null, {
|
|
141
126
|
status: 303,
|
|
142
|
-
headers: {
|
|
143
|
-
Location: `/`,
|
|
144
|
-
'Set-Cookie': `token=; HttpOnly; Path=/; Max-Age=0${secureCookie}`
|
|
145
|
-
}
|
|
127
|
+
headers: { Location: '/', 'Set-Cookie': `token=; HttpOnly; Path=/; Max-Age=0${secureCookie}` }
|
|
146
128
|
});
|
|
147
129
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dashboard.handler.d.ts","sourceRoot":"","sources":["../../../../src/api/cms/handlers/dashboard.handler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AAE/B,OAAO,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AAGpC,wBAAsB,YAAY,CAAC,CAAC,EAAE,OAAO,CAAC;IAAE,QAAQ,EAAE,QAAQ,CAAA;CAAE,CAAC,GAAG,OAAO,CAAC,QAAQ,CAAC,
|
|
1
|
+
{"version":3,"file":"dashboard.handler.d.ts","sourceRoot":"","sources":["../../../../src/api/cms/handlers/dashboard.handler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AAE/B,OAAO,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AAGpC,wBAAsB,YAAY,CAAC,CAAC,EAAE,OAAO,CAAC;IAAE,QAAQ,EAAE,QAAQ,CAAA;CAAE,CAAC,GAAG,OAAO,CAAC,QAAQ,CAAC,CAWxF"}
|
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import { getTemplate } from '../../../lib/template';
|
|
2
|
-
import {
|
|
2
|
+
import { getAdminConfig } from '../../../lib/config';
|
|
3
3
|
export async function getDashboard(c) {
|
|
4
4
|
try {
|
|
5
|
-
const
|
|
6
|
-
const adminConfig = getAdminConfig(appConfig);
|
|
5
|
+
const adminConfig = getAdminConfig();
|
|
7
6
|
const configParams = `config='${JSON.stringify(adminConfig)}'`;
|
|
8
7
|
return c.html(getTemplate('admin', c.env.UI_BASE_URL, configParams));
|
|
9
8
|
}
|
|
@@ -1 +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;
|
|
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;AA4C9C,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,CAkB/E;AAED,wBAAsB,kBAAkB,CAAC,CAAC,EAAE,OAAO,CAAC,OAAO,CAAC,GAAG,OAAO,CAAC,QAAQ,CAAC,CAe/E;AAED,YAAY,EAAE,YAAY,EAAE,CAAC"}
|
|
@@ -2,11 +2,33 @@ import { getGitHubConfig } from '../../../lib/config';
|
|
|
2
2
|
import { createGitHubClient } from '../../../lib/github';
|
|
3
3
|
const SECRET_NAME = /^[A-Z_][A-Z0-9_]*$/;
|
|
4
4
|
const MAX_SECRET_BYTES = 64 * 1024;
|
|
5
|
+
// These are the only Actions secrets consumed by the generated deployment
|
|
6
|
+
// workflow. Arbitrary repository secrets must not be presented as Worker
|
|
7
|
+
// configuration because they would never reach the deployed Worker.
|
|
8
|
+
const DEPLOYABLE_ACTION_SECRET_NAMES = new Set([
|
|
9
|
+
'SECRET',
|
|
10
|
+
'APP_PRIVATE_KEY',
|
|
11
|
+
'STRIPE_SECRET_KEY',
|
|
12
|
+
'STRIPE_WEBHOOK_SECRET',
|
|
13
|
+
'API_KEY',
|
|
14
|
+
'SETUP_TOKEN',
|
|
15
|
+
'GEMINI_API_KEY',
|
|
16
|
+
'DEEPSEEK_API_KEY',
|
|
17
|
+
]);
|
|
18
|
+
const DELETABLE_ACTION_SECRET_NAMES = new Set([
|
|
19
|
+
'STRIPE_SECRET_KEY',
|
|
20
|
+
'STRIPE_WEBHOOK_SECRET',
|
|
21
|
+
'API_KEY',
|
|
22
|
+
'GEMINI_API_KEY',
|
|
23
|
+
'DEEPSEEK_API_KEY',
|
|
24
|
+
]);
|
|
5
25
|
function secretName(value) {
|
|
6
26
|
const name = value.trim();
|
|
7
27
|
return SECRET_NAME.test(name) ? name : null;
|
|
8
28
|
}
|
|
9
29
|
async function dispatchDeploy(c) {
|
|
30
|
+
if (c.env.DISABLE_DEPLOYMENTS === 'true')
|
|
31
|
+
return false;
|
|
10
32
|
const client = createGitHubClient(getGitHubConfig(c.env));
|
|
11
33
|
const { owner, repo, branch } = client.getRepoInfo();
|
|
12
34
|
await (await client.getOctokit()).rest.actions.createWorkflowDispatch({
|
|
@@ -15,16 +37,20 @@ async function dispatchDeploy(c) {
|
|
|
15
37
|
workflow_id: 'deploy.yml',
|
|
16
38
|
ref: branch,
|
|
17
39
|
});
|
|
40
|
+
return true;
|
|
18
41
|
}
|
|
19
42
|
export async function listGitHubSecrets(c) {
|
|
20
43
|
const client = createGitHubClient(getGitHubConfig(c.env));
|
|
21
|
-
const secrets = await client.listActionsSecrets();
|
|
44
|
+
const secrets = (await client.listActionsSecrets()).filter((secret) => DEPLOYABLE_ACTION_SECRET_NAMES.has(secret.name));
|
|
22
45
|
return c.json({ secrets });
|
|
23
46
|
}
|
|
24
47
|
export async function upsertGitHubSecret(c) {
|
|
25
48
|
const name = secretName(c.req.param('name') ?? '');
|
|
26
49
|
if (!name)
|
|
27
50
|
return c.json({ error: 'Secret names must use uppercase letters, numbers, and underscores.' }, 400);
|
|
51
|
+
if (!DEPLOYABLE_ACTION_SECRET_NAMES.has(name)) {
|
|
52
|
+
return c.json({ error: `Unsupported deployment secret: ${name}` }, 400);
|
|
53
|
+
}
|
|
28
54
|
const body = await c.req.json();
|
|
29
55
|
if (typeof body.value !== 'string')
|
|
30
56
|
return c.json({ error: 'Secret value is required.' }, 400);
|
|
@@ -34,18 +60,22 @@ export async function upsertGitHubSecret(c) {
|
|
|
34
60
|
const client = createGitHubClient(getGitHubConfig(c.env));
|
|
35
61
|
await client.setActionsSecret(name, body.value);
|
|
36
62
|
const deploy = body.deploy !== false;
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
return c.json({ success: true, deployed: deploy });
|
|
63
|
+
const deploymentStarted = deploy ? await dispatchDeploy(c) : false;
|
|
64
|
+
return c.json({ success: true, deploymentStarted });
|
|
40
65
|
}
|
|
41
66
|
export async function deleteGitHubSecret(c) {
|
|
42
67
|
const name = secretName(c.req.param('name') ?? '');
|
|
43
68
|
if (!name)
|
|
44
69
|
return c.json({ error: 'Secret names must use uppercase letters, numbers, and underscores.' }, 400);
|
|
70
|
+
if (!DEPLOYABLE_ACTION_SECRET_NAMES.has(name)) {
|
|
71
|
+
return c.json({ error: `Unsupported deployment secret: ${name}` }, 400);
|
|
72
|
+
}
|
|
73
|
+
if (!DELETABLE_ACTION_SECRET_NAMES.has(name)) {
|
|
74
|
+
return c.json({ error: `${name} is required project infrastructure and cannot be deleted.` }, 400);
|
|
75
|
+
}
|
|
45
76
|
const client = createGitHubClient(getGitHubConfig(c.env));
|
|
46
77
|
await client.deleteActionsSecret(name);
|
|
47
78
|
const deploy = c.req.query('deploy') !== 'false';
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
return c.json({ success: true, deployed: deploy });
|
|
79
|
+
const deploymentStarted = deploy ? await dispatchDeploy(c) : false;
|
|
80
|
+
return c.json({ success: true, deploymentStarted });
|
|
51
81
|
}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import type { Context } from 'hono';
|
|
2
2
|
import type { HonoEnv } from '../../../index';
|
|
3
|
+
export declare function hasValidSetupToken(env: HonoEnv['Bindings'], provided: unknown): Promise<boolean>;
|
|
3
4
|
/** One-time, project-owned bootstrap. It is unavailable as soon as an admin exists. */
|
|
5
|
+
export declare function setupContext(c: Context<HonoEnv>): Promise<Response>;
|
|
4
6
|
export declare function setupProject(c: Context<HonoEnv>): Promise<Response>;
|
|
5
7
|
export declare function projectNeedsSetup(c: Context<HonoEnv>): Promise<boolean>;
|
|
6
8
|
//# sourceMappingURL=setup.handler.d.ts.map
|
|
@@ -1 +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;
|
|
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;AAUpC,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AAwB9C,wBAAsB,kBAAkB,CAAC,GAAG,EAAE,OAAO,CAAC,UAAU,CAAC,EAAE,QAAQ,EAAE,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,CAQtG;AAQD,uFAAuF;AACvF,wBAAsB,YAAY,CAAC,CAAC,EAAE,OAAO,CAAC,OAAO,CAAC,GAAG,OAAO,CAAC,QAAQ,CAAC,CAIzE;AAED,wBAAsB,YAAY,CAAC,CAAC,EAAE,OAAO,CAAC,OAAO,CAAC,GAAG,OAAO,CAAC,QAAQ,CAAC,CA+IzE;AAED,wBAAsB,iBAAiB,CAAC,CAAC,EAAE,OAAO,CAAC,OAAO,CAAC,GAAG,OAAO,CAAC,OAAO,CAAC,CAE7E"}
|