web/website/node/jarvis-identity-provision.mjs
2026-11-27 23:43:05 +00:00

11 lines
1.2 KiB
JavaScript

import {randomBytes,randomUUID} from 'node:crypto';import {JARVIS_IDENTITY} from './jarvis-identity.mjs';
export async function ensureJarvisIdentity(db,{clock=Date.now}={}){
return db.transaction(async tx=>{
const named=await tx.first('SELECT id,username,role FROM site_accounts WHERE username=? FOR UPDATE',[JARVIS_IDENTITY.username]);
if(named){if(named.id!==JARVIS_IDENTITY.id||named.role!==JARVIS_IDENTITY.role)throw Error('Jarvis username belongs to another account; manual review required');return{created:false};}
const idConflict=await tx.first('SELECT id FROM site_accounts WHERE id=?',[JARVIS_IDENTITY.id]);if(idConflict)throw Error('Jarvis account identity conflict');
await tx.query('INSERT INTO site_accounts(id,username,display_name,password_salt,password_hash,role,created_at) VALUES(?,?,?,?,?,?,?)',[JARVIS_IDENTITY.id,JARVIS_IDENTITY.username,JARVIS_IDENTITY.displayName,randomBytes(16).toString('hex'),'!service',JARVIS_IDENTITY.role,clock()]);
await tx.query('INSERT INTO account_audit(id,actor_id,action,details,created_at) VALUES(?,?,?,?,?)',[randomUUID(),JARVIS_IDENTITY.id,'project-identity-created',JSON.stringify({kind:'official-project-identity',sitePermissions:[],staff:false}),clock()]);
return{created:true};
});
}