web/website/node/jarvis-master-generator-api.mjs
unimatrixgit 3dee0ee6b3
All checks were successful
DuneRealms Web CI / web (push) Successful in 2m44s
Reuse verified bureau authority for keygen and make JARVIS registers readable with copy references
2026-11-19 20:05:41 +01:00

27 lines
3 KiB
JavaScript

import {identity} from './auth.mjs';import {jarvisGeneratorCan} from './jarvis-generator-access.mjs';import {createMasterKeyGenerator} from './jarvis-master-generator.mjs';
export function createMasterKeyGeneratorAPI(db,keys,{origin='https://cp.dunerealms.com',authenticate=r=>identity(r,db),authorize=(u,node)=>jarvisGeneratorCan(db,u,node)}={}){
const service=keys?createMasterKeyGenerator(db,keys,{authorize}):null;
const reply=(d,s=200)=>Response.json(d,{status:s,headers:{'Cache-Control':'private, no-store',Vary:'Cookie','X-Content-Type-Options':'nosniff'}});
return async request=>{
const user=await authenticate(request);if(!user)return reply({message:'Sign in to use J.A.R.V.I.S.'},401);
const url=new URL(request.url);if(url.search)return reply({message:'Query overrides are not accepted.'},400);
if(!await authorize(user,'web.jarvis.keys.view'))return reply({message:'Masterkey access required.'},403);
if(!service)return reply({message:'The key service is unavailable.'},503);
try{
if(request.method==='GET'){
if(url.pathname.endsWith('/history')){const rows=(await db.prepare('SELECT action_id,template_version,state,created_at FROM jarvis_generated_instructions WHERE account_id=? ORDER BY created_at DESC LIMIT 100').bind(user.id).all()).results;return reply({records:rows});}
return reply(await service.catalogue(user));
}
if(request.method!=='POST')return reply({message:'Method not allowed.'},405);
if(request.headers.get('origin')!==origin)return reply({message:'Origin rejected.'},403);
if(!/^application\/json(?:;|$)/i.test(request.headers.get('content-type')||''))return reply({message:'JSON required.'},415);
const reader=request.body?.getReader(),parts=[];let size=0;if(!reader)return reply({message:'Instruction required.'},400);
while(true){const r=await reader.read();if(r.done)break;size+=r.value.byteLength;if(size>8192){await reader.cancel();return reply({message:'Instruction too large.'},413);}parts.push(r.value);}
let input;try{input=JSON.parse(Buffer.concat(parts).toString('utf8'));}catch{return reply({message:'Invalid instruction.'},400);}
const redeem=url.pathname.endsWith('/redeem'),fields=redeem?['code','requestId','confirmed']:['actionId','templateVersion','values','requestId','confirmed'];
if(!input||Array.isArray(input)||input.confirmed!==true||Object.keys(input).some(k=>!fields.includes(k)))return reply({message:'Review and confirm the instruction.'},400);
const fresh=await authenticate(request);if(fresh?.id!==user.id||!await authorize(fresh,'web.jarvis.keys.view'))return reply({message:'Your session or authority changed.'},403);
return reply({ok:true,...await service[redeem?'redeem':'issue'](fresh,input)});
}catch(error){const message=error.message||'';const safe=/^(Devkeys|Masterkey|Choose |Invalid |Unknown |Instruction |Protected |Clan inventory|Key unavailable|Enter |Dossier)/.test(message);return reply({message:safe?message:'The instruction could not be confirmed. Reload before retrying.'},/authority/.test(message)?403:409);}
};
}