避免部署账号异常
This commit is contained in:
@@ -21,6 +21,8 @@ import { id } from "./util";
|
|||||||
export async function ensureAdminUser(env: Env): Promise<void> {
|
export async function ensureAdminUser(env: Env): Promise<void> {
|
||||||
const existing = await env.DB.prepare("SELECT id FROM users WHERE username = ?").bind(env.ADMIN_USERNAME).first<{ id: string }>();
|
const existing = await env.DB.prepare("SELECT id FROM users WHERE username = ?").bind(env.ADMIN_USERNAME).first<{ id: string }>();
|
||||||
if (existing) return;
|
if (existing) return;
|
||||||
|
const adminPassword = env.ADMIN_PASSWORD;
|
||||||
|
if (!adminPassword?.trim()) throw new Error("ADMIN_PASSWORD secret is required before creating the admin user");
|
||||||
|
|
||||||
const keyPair = await crypto.subtle.generateKey(
|
const keyPair = await crypto.subtle.generateKey(
|
||||||
{ name: "RSASSA-PKCS1-v1_5", modulusLength: 2048, publicExponent: new Uint8Array([1, 0, 1]), hash: "SHA-256" },
|
{ name: "RSASSA-PKCS1-v1_5", modulusLength: 2048, publicExponent: new Uint8Array([1, 0, 1]), hash: "SHA-256" },
|
||||||
@@ -34,7 +36,7 @@ export async function ensureAdminUser(env: Env): Promise<void> {
|
|||||||
await env.DB.prepare(
|
await env.DB.prepare(
|
||||||
"INSERT OR IGNORE INTO users (id, username, display_name, note, password_hash, private_key_jwk, public_key_jwk, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"
|
"INSERT OR IGNORE INTO users (id, username, display_name, note, password_hash, private_key_jwk, public_key_jwk, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"
|
||||||
)
|
)
|
||||||
.bind(id(), env.ADMIN_USERNAME, env.ADMIN_USERNAME, "", await hashPassword(env.ADMIN_PASSWORD), JSON.stringify(privateKey), JSON.stringify(publicKey), now)
|
.bind(id(), env.ADMIN_USERNAME, env.ADMIN_USERNAME, "", await hashPassword(adminPassword), JSON.stringify(privateKey), JSON.stringify(publicKey), now)
|
||||||
.run();
|
.run();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,6 +9,9 @@
|
|||||||
"ADMIN_USERNAME": "sun"
|
"ADMIN_USERNAME": "sun"
|
||||||
//"ADMIN_PASSWORD": "change-me-before-deploy"
|
//"ADMIN_PASSWORD": "change-me-before-deploy"
|
||||||
},
|
},
|
||||||
|
"secrets": {
|
||||||
|
"required": ["ADMIN_PASSWORD"]
|
||||||
|
},
|
||||||
"d1_databases": [
|
"d1_databases": [
|
||||||
{
|
{
|
||||||
"binding": "DB",
|
"binding": "DB",
|
||||||
|
|||||||
Reference in New Issue
Block a user