39 lines
6.3 KiB
JavaScript
39 lines
6.3 KiB
JavaScript
import {randomUUID} from 'node:crypto';
|
|
import {STAFF_OFFICE_ACCOUNT_ID} from './organisation-keys.mjs';
|
|
import {boardDesiredState} from './board-state-keys.mjs';
|
|
export const BOARD_DELEGATION_SCHEMA=`CREATE TABLE IF NOT EXISTS jarvis_board_delegations(organisation_kind VARCHAR(12) NOT NULL,organisation_id CHAR(36) NOT NULL,key_id CHAR(36) NOT NULL UNIQUE,request_id CHAR(36) NOT NULL UNIQUE,member_id CHAR(36) NOT NULL,member_binding VARCHAR(120) NOT NULL,source_kind VARCHAR(24) NOT NULL,source_id CHAR(36) NOT NULL,source_owner_id CHAR(36) NULL,expires_at BIGINT NOT NULL,actor_id CHAR(36) NOT NULL,ticket_id VARCHAR(80) NOT NULL,reason VARCHAR(240) NOT NULL,created_at BIGINT NOT NULL,PRIMARY KEY(organisation_kind,organisation_id))`;
|
|
export async function initializeBoardDelegations(db){await db.query(BOARD_DELEGATION_SCHEMA+' ENGINE=InnoDB DEFAULT CHARSET=utf8mb4');}
|
|
const uuid=v=>typeof v==='string'&&/^[a-f0-9]{8}(?:-[a-f0-9]{4}){3}-[a-f0-9]{12}$/i.test(v);
|
|
const first=async(store,sql,args=[])=>store.first?store.first(sql,args):(await store.query(sql,args))[0]||null;
|
|
const fail=message=>{throw Error(message);};
|
|
// Uses an existing scoped, expiring staff overwrite key. It remains unconsumed
|
|
// evidence, so ordinary staff-key revocation works without rewriting history.
|
|
export function createBoardDelegations(db,keys,{resolveSource,authorizeStaff,clock=Date.now}={}){
|
|
if(typeof resolveSource!=='function'||typeof authorizeStaff!=='function')throw TypeError('Existing staff authority and resolved board deed adapters required');
|
|
const allowed=async(actor,store)=>{if(!uuid(actor?.id)||await authorizeStaff(actor,store)!==true)fail('Current forum staff overwrite authority required');};
|
|
const eligible=(store,id)=>first(store,"SELECT id FROM site_accounts WHERE id=? AND password_hash NOT LIKE '!%' AND role NOT IN ('official_account','system_account','memorial')",[id]);
|
|
return Object.freeze({
|
|
async issue(actor,input){
|
|
await allowed(actor,db);
|
|
if(!input||Object.keys(input).some(k=>!['kind','id','memberId','requestId','expiresAt','ticketId','reason'].includes(k))||!['clan','company','politics'].includes(input.kind)||![input.id,input.memberId,input.requestId].every(uuid)||!Number.isSafeInteger(input.expiresAt)||input.expiresAt<=clock()||typeof input.ticketId!=='string'||!input.ticketId.trim()||input.ticketId.length>80||typeof input.reason!=='string'||!input.reason.trim()||input.reason.length>240||/[\x00-\x1f]/.test(input.reason))throw TypeError('Choose a current board member, expiry and staff ticket');
|
|
const scope='board-control:'+input.kind+':'+input.id+':'+input.requestId;
|
|
const grant=await keys.issue({accountId:STAFF_OFFICE_ACCOUNT_ID,keyType:'staff-parent-link',scopeId:scope,expiresAt:input.expiresAt,authorize:async(tx,{keyId})=>{
|
|
await allowed(actor,tx);const source=boardDesiredState(await resolveSource(tx,input.kind,input.id),{kind:input.kind,id:input.id});
|
|
const member=source.policy.members.find(m=>m.id===input.memberId);
|
|
if(source.organisation.lifecycle!=='active'||!member?.binding||!await eligible(tx,input.memberId)||source.source.expiresAt!==null&&input.expiresAt>source.source.expiresAt)fail('Temporary board control requires current membership within the deed term');
|
|
const old=await first(tx,'SELECT * FROM jarvis_board_delegations WHERE organisation_kind=? AND organisation_id=? FOR UPDATE',[input.kind,input.id]);
|
|
if(old?.request_id===input.requestId){if(old.member_id!==input.memberId||old.member_binding!==member.binding||old.source_owner_id!==source.source.ownerId||old.source_id!==source.source.id||Number(old.expires_at)!==input.expiresAt||old.ticket_id!==input.ticketId.trim()||old.reason!==input.reason.trim()||old.actor_id!==actor.id)fail('Board overwrite reference conflict');return;}
|
|
await tx.query('INSERT INTO jarvis_board_delegations(organisation_kind,organisation_id,key_id,request_id,member_id,member_binding,source_kind,source_id,source_owner_id,expires_at,actor_id,ticket_id,reason,created_at) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?) ON DUPLICATE KEY UPDATE key_id=VALUES(key_id),request_id=VALUES(request_id),member_id=VALUES(member_id),member_binding=VALUES(member_binding),source_kind=VALUES(source_kind),source_id=VALUES(source_id),source_owner_id=VALUES(source_owner_id),expires_at=VALUES(expires_at),actor_id=VALUES(actor_id),ticket_id=VALUES(ticket_id),reason=VALUES(reason),created_at=VALUES(created_at)',[input.kind,input.id,keyId,input.requestId,input.memberId,member.binding,source.source.kind,source.source.id,source.source.ownerId,input.expiresAt,actor.id,input.ticketId.trim(),input.reason.trim(),clock()]);
|
|
await tx.query('INSERT INTO account_audit(id,actor_id,action,details,created_at) VALUES(?,?,?,?,?)',[randomUUID(),actor.id,'board-control-delegated',JSON.stringify({keyId,kind:input.kind,id:input.id,memberId:input.memberId,expiresAt:input.expiresAt,ticketId:input.ticketId.trim(),reason:input.reason.trim(),boardOnly:true}),clock()]);
|
|
}});
|
|
const saved=await first(db,'SELECT key_id FROM jarvis_board_delegations WHERE organisation_kind=? AND organisation_id=?',[input.kind,input.id]);if(saved?.key_id!==grant.id)fail('A newer board overwrite exists; the old reference was not restored');return {keyId:grant.id,created:grant.created,boardOnly:true};
|
|
},
|
|
async effective(kind,id,resolved,store=db){
|
|
const source=boardDesiredState(resolved,{kind,id});
|
|
const row=await first(store,"SELECT d.*,k.key_type,k.scope_id,k.account_id,k.revoked_at,k.used_at,k.expires_at key_expiry FROM jarvis_board_delegations d JOIN organisation_keys k ON k.id=d.key_id WHERE d.organisation_kind=? AND d.organisation_id=?",[kind,id]);
|
|
if(!row||source.organisation.lifecycle!=='active'||row.key_type!=='staff-parent-link'||row.account_id!==STAFF_OFFICE_ACCOUNT_ID||row.scope_id!=='board-control:'+kind+':'+id+':'+row.request_id||row.revoked_at!=null||row.used_at!=null||Number(row.expires_at)<=clock()||Number(row.key_expiry)!==Number(row.expires_at)||source.source.ownerId!==row.source_owner_id||source.source.kind!==row.source_kind||source.source.id!==row.source_id||!source.policy.members.some(m=>m.id===row.member_id&&m.binding===row.member_binding)||source.source.expiresAt!==null&&clock()>=source.source.expiresAt)return source;
|
|
if(!await eligible(store,row.member_id))return source;
|
|
return {...source,source:{...source.source,refs:{...source.source.refs,boardControlKeyId:row.key_id}},policy:{...source.policy,managers:[row.member_id]}};
|
|
}
|
|
});
|
|
}
|

Staff