Add account password changes and administrator resets
This commit is contained in:
1 parent
23e28a7dcb
commit
060b36682d
14 files changed
+263
-26
No files matched your search
@@ -2,6 +2,7 @@ const fs=require('node:fs');
|
||||
const path=require('node:path');
|
||||
const crypto=require('node:crypto');
|
||||
const {DatabaseSync}=require('node:sqlite');
|
||||
const {validatePassword,hashPassword,matchesPassword}=require('./passwords.cjs');
|
||||
const copy=value=>structuredClone(value);
|
||||
const safeUser=({id,username,role,createdAt})=>({id,username,role,createdAt});
|
||||
const fail=(message,status=400)=>{throw Object.assign(new Error(message),{status});};
|
||||
@@ -69,11 +70,10 @@ class Store {
|
||||
user(id){const user=this.data.users.find(user=>user.id===id);return user?safeUser(user):null;}
|
||||
makeUser(username,password,role) {
|
||||
if(typeof username!=='string' || !/^[a-zA-Z0-9_.-]{3,40}$/.test(username))fail('账号需为 3–40 位英文、数字、点、下划线或短横线。');
|
||||
if(typeof password!=='string' || password.length<12 || password.length>128)fail('密码长度需为 12–128 位。');
|
||||
validatePassword(password);
|
||||
if(!['admin','operator','viewer'].includes(role))fail('账号角色不正确。');
|
||||
if(this.data.users.some(user=>user.username.toLowerCase()===username.toLowerCase()))fail('账号已存在。',409);
|
||||
const salt=crypto.randomBytes(16).toString('hex');
|
||||
return {id:crypto.randomUUID(),username,role,createdAt:new Date().toISOString(),passwordHash:salt+':'+crypto.scryptSync(password,salt,32).toString('hex')};
|
||||
return {id:crypto.randomUUID(),username,role,createdAt:new Date().toISOString(),passwordHash:hashPassword(password)};
|
||||
}
|
||||
bootstrap(username,password) {
|
||||
if(this.data.users.length)fail('管理员已经创建,请登录。',409);
|
||||
@@ -87,11 +87,21 @@ class Store {
|
||||
authenticate(username,password) {
|
||||
if(typeof username!=='string' || typeof password!=='string' || password.length>128)return null;
|
||||
const user=this.data.users.find(user=>user.username.toLowerCase()===username.toLowerCase());
|
||||
const [salt,hash]=(user?.passwordHash || '00000000000000000000000000000000:'+ '0'.repeat(64)).split(':');
|
||||
const actual=crypto.scryptSync(password,salt,32);
|
||||
if(!crypto.timingSafeEqual(actual,Buffer.from(hash,'hex')) || !user)return null;
|
||||
if(!matchesPassword(password,user?.passwordHash) || !user)return null;
|
||||
return safeUser(user);
|
||||
}
|
||||
passwordVersion(id){return this.data.users.find(user=>user.id===id)?.passwordVersion||0;}
|
||||
updatePassword(id,password,actor){
|
||||
const user=this.data.users.find(user=>user.id===id);if(!user)fail('账号不存在。',404);
|
||||
validatePassword(password);if(matchesPassword(password,user.passwordHash))fail('新密码不能与原密码相同。');
|
||||
const passwordHash=hashPassword(password);
|
||||
return this.commit(draft=>{
|
||||
const target=draft.users.find(user=>user.id===id);target.passwordHash=passwordHash;
|
||||
target.passwordVersion=(target.passwordVersion||0)+1;
|
||||
this.addAudit(draft,actor.username,actor.id===id?'修改密码':'重置密码',target.username);
|
||||
return safeUser(target);
|
||||
});
|
||||
}
|
||||
recordJob(record) {
|
||||
return this.commit(draft=>{
|
||||
const index=draft.jobs.findIndex(job=>job.jobId===record.jobId);
|
||||
|
||||
Reference in new issue
Block a user