Add WebUI dev mode auth & dev script
Introduce a development-only WebUI auth mode controlled by SNOWLUMA_DEV_MODE. When enabled, a fixed in-memory password ("snowluma-dev") is used, config/webui.json is not read or written, and password changes are disabled. Expose WebuiAuth.isDevMode() and WebuiAuth.devPassword, add startup logs when dev mode is active, and wire up dev:mode npm scripts (root and packages/core) using cross-env to set the env flags.
This commit is contained in:
1 parent
466a10a00a
commit
19a564991c
4 files changed
+55
-4
No files matched your search
@@ -9,6 +9,7 @@
|
|||||||
"build:webui": "pnpm --filter webui build",
|
"build:webui": "pnpm --filter webui build",
|
||||||
"build:all": "pnpm --filter @snowluma/core build:all && pnpm --filter webui build",
|
"build:all": "pnpm --filter @snowluma/core build:all && pnpm --filter webui build",
|
||||||
"dev": "pnpm --filter @snowluma/core dev",
|
"dev": "pnpm --filter @snowluma/core dev",
|
||||||
|
"dev:mode": "cd dist && cross-env SNOWLUMA_DEV_MODE=1 SNOWLUMA_DEV_WEBUI=1 node index.mjs",
|
||||||
"dev:web": "pnpm --filter webui dev",
|
"dev:web": "pnpm --filter webui dev",
|
||||||
"test": "pnpm --filter @snowluma/core test",
|
"test": "pnpm --filter @snowluma/core test",
|
||||||
"test:watch": "pnpm --filter @snowluma/core test:watch",
|
"test:watch": "pnpm --filter @snowluma/core test:watch",
|
||||||
@@ -17,6 +18,7 @@
|
|||||||
"start": "node dist/index.mjs"
|
"start": "node dist/index.mjs"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
"cross-env": "^10.1.0",
|
||||||
"typescript": "catalog:"
|
"typescript": "catalog:"
|
||||||
},
|
},
|
||||||
"pnpm": {
|
"pnpm": {
|
||||||
|
|||||||
@@ -10,6 +10,17 @@ const SCRYPT_N = 16384; // cost
|
|||||||
const SCRYPT_R = 8;
|
const SCRYPT_R = 8;
|
||||||
const SCRYPT_P = 1;
|
const SCRYPT_P = 1;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fixed credentials used when SNOWLUMA_DEV_MODE=1 is set. The state is kept
|
||||||
|
* entirely in memory and never written to `config/webui.json`, so toggling
|
||||||
|
* dev mode on/off never disturbs the real persisted password.
|
||||||
|
*/
|
||||||
|
const DEV_PASSWORD = 'snowluma-dev';
|
||||||
|
|
||||||
|
export function isDevAuthMode(): boolean {
|
||||||
|
return process.env.SNOWLUMA_DEV_MODE === '1';
|
||||||
|
}
|
||||||
|
|
||||||
export interface WebuiAuthState {
|
export interface WebuiAuthState {
|
||||||
passwordHash: string; // hex
|
passwordHash: string; // hex
|
||||||
passwordSalt: string; // hex
|
passwordSalt: string; // hex
|
||||||
@@ -89,20 +100,35 @@ function atomicWrite(state: WebuiAuthState): void {
|
|||||||
export class WebuiAuth {
|
export class WebuiAuth {
|
||||||
private state: WebuiAuthState;
|
private state: WebuiAuthState;
|
||||||
private initialPlain: string | null;
|
private initialPlain: string | null;
|
||||||
|
private readonly devMode: boolean;
|
||||||
|
|
||||||
private constructor(state: WebuiAuthState, initialPlain: string | null) {
|
private constructor(state: WebuiAuthState, initialPlain: string | null, devMode: boolean) {
|
||||||
this.state = state;
|
this.state = state;
|
||||||
this.initialPlain = initialPlain;
|
this.initialPlain = initialPlain;
|
||||||
|
this.devMode = devMode;
|
||||||
}
|
}
|
||||||
|
|
||||||
static load(): WebuiAuth {
|
static load(): WebuiAuth {
|
||||||
|
if (isDevAuthMode()) {
|
||||||
|
const salt = randomBytes(16);
|
||||||
|
const hash = hashPassword(DEV_PASSWORD, salt);
|
||||||
|
const now = new Date().toISOString();
|
||||||
|
const state: WebuiAuthState = {
|
||||||
|
passwordHash: hash.toString('hex'),
|
||||||
|
passwordSalt: salt.toString('hex'),
|
||||||
|
mustChangePassword: false,
|
||||||
|
generatedAt: now,
|
||||||
|
updatedAt: now,
|
||||||
|
};
|
||||||
|
return new WebuiAuth(state, null, true);
|
||||||
|
}
|
||||||
ensureConfigDir();
|
ensureConfigDir();
|
||||||
if (fs.existsSync(WEBUI_CONFIG_PATH)) {
|
if (fs.existsSync(WEBUI_CONFIG_PATH)) {
|
||||||
try {
|
try {
|
||||||
const raw = fs.readFileSync(WEBUI_CONFIG_PATH, 'utf8');
|
const raw = fs.readFileSync(WEBUI_CONFIG_PATH, 'utf8');
|
||||||
const parsed = JSON.parse(raw) as unknown;
|
const parsed = JSON.parse(raw) as unknown;
|
||||||
if (isValidState(parsed)) {
|
if (isValidState(parsed)) {
|
||||||
return new WebuiAuth(parsed, null);
|
return new WebuiAuth(parsed, null, false);
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
/* fallthrough — regenerate */
|
/* fallthrough — regenerate */
|
||||||
@@ -111,7 +137,17 @@ export class WebuiAuth {
|
|||||||
const initialPassword = randomBytes(8).toString('hex');
|
const initialPassword = randomBytes(8).toString('hex');
|
||||||
const state = generateInitialState(initialPassword);
|
const state = generateInitialState(initialPassword);
|
||||||
atomicWrite(state);
|
atomicWrite(state);
|
||||||
return new WebuiAuth(state, initialPassword);
|
return new WebuiAuth(state, initialPassword, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** True when SNOWLUMA_DEV_MODE was active at load time. */
|
||||||
|
isDevMode(): boolean {
|
||||||
|
return this.devMode;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Fixed dev password (only meaningful when {@link isDevMode} is true). */
|
||||||
|
static get devPassword(): string {
|
||||||
|
return DEV_PASSWORD;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Returns the auto-generated initial password if this is a fresh install, else null. */
|
/** Returns the auto-generated initial password if this is a fresh install, else null. */
|
||||||
@@ -143,6 +179,9 @@ export class WebuiAuth {
|
|||||||
* After success, mustChangePassword=false and the file is rewritten on disk.
|
* After success, mustChangePassword=false and the file is rewritten on disk.
|
||||||
*/
|
*/
|
||||||
setPassword(newPassword: string): void {
|
setPassword(newPassword: string): void {
|
||||||
|
if (this.devMode) {
|
||||||
|
throw new Error('开发模式 (SNOWLUMA_DEV_MODE=1) 已禁用密码修改');
|
||||||
|
}
|
||||||
if (!isStrongPassword(newPassword)) {
|
if (!isStrongPassword(newPassword)) {
|
||||||
throw new Error('密码不符合强度要求');
|
throw new Error('密码不符合强度要求');
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -77,7 +77,14 @@ export async function initWebUI(
|
|||||||
): Promise<{ port: number }> {
|
): Promise<{ port: number }> {
|
||||||
const auth = WebuiAuth.load();
|
const auth = WebuiAuth.load();
|
||||||
const initialPassword = auth.takeInitialPassword();
|
const initialPassword = auth.takeInitialPassword();
|
||||||
if (initialPassword) {
|
if (auth.isDevMode()) {
|
||||||
|
log.warn('=========================================');
|
||||||
|
log.warn('WebUI 已进入开发模式 (SNOWLUMA_DEV_MODE=1)');
|
||||||
|
log.warn('固定密码: %s', WebuiAuth.devPassword);
|
||||||
|
log.warn('config/webui.json 不会被读写,强制改密流程已跳过');
|
||||||
|
log.warn('请勿在生产环境启用此模式');
|
||||||
|
log.warn('=========================================');
|
||||||
|
} else if (initialPassword) {
|
||||||
log.info('=========================================');
|
log.info('=========================================');
|
||||||
log.info('WebUI 安全认证(首次启动,已生成临时密码)');
|
log.info('WebUI 安全认证(首次启动,已生成临时密码)');
|
||||||
log.info('默认用户: admin');
|
log.info('默认用户: admin');
|
||||||
|
|||||||
Generated
+3
@@ -20,6 +20,9 @@ importers:
|
|||||||
|
|
||||||
.:
|
.:
|
||||||
devDependencies:
|
devDependencies:
|
||||||
|
cross-env:
|
||||||
|
specifier: ^10.1.0
|
||||||
|
version: 10.1.0
|
||||||
typescript:
|
typescript:
|
||||||
specifier: 'catalog:'
|
specifier: 'catalog:'
|
||||||
version: 6.0.2
|
version: 6.0.2
|
||||||
|
|||||||
Reference in new issue
Block a user