17 lines
3.9 KiB
JavaScript
17 lines
3.9 KiB
JavaScript
import {validateKitEquipment} from './public/kit-design-model.mjs';
|
|
import {randomUUID} from 'node:crypto';
|
|
import {identity} from '../node/auth.mjs';
|
|
import {accessFor} from '../node/permissions.mjs';
|
|
const uuid=/^[a-f0-9]{8}(?:-[a-f0-9]{4}){3}-[a-f0-9]{12}$/i;
|
|
const reply=(body,status=200)=>Response.json(body,{status,headers:{'Cache-Control':'private, no-store','Vary':'Cookie'}});
|
|
export function createImperialAtelier(db,settings={},dependencies={}){const auth=dependencies.authenticate||(r=>identity(r,db)),access=dependencies.access||(u=>accessFor(db,u));return async request=>{
|
|
const url=new URL(request.url);if(url.search)return reply({message:'Player overrides are not accepted.'},400);const user=await auth(request);if(!user)return reply({message:'Sign in first.'},401);if(!(await access(user)).nodes.includes('web.menu.game'))return reply({message:'Game account access required.'},403);
|
|
const link=await db.prepare('SELECT minecraft_uuid FROM site_game_links WHERE user_id=?').bind(user.id).first();if(!link)return reply({linked:false,canDesign:false,offices:[]});const player=link.minecraft_uuid;if(!uuid.test(player))return reply({message:'Verified game identity unavailable.'},503);
|
|
try{if((settings.SERVER_CONTROL_TOKEN||'').length<64)throw Error();let path='/kits/player?uuid='+encodeURIComponent(player),body;
|
|
if(request.method==='POST'){if(request.headers.get('origin')!==new URL(settings.CP_ORIGIN||'https://cp.dunerealms.com').origin)return reply({message:'Origin rejected.'},403);let b;try{const text=await request.text();if(Buffer.byteLength(text)>32768)throw Error();b=JSON.parse(text);}catch{return reply({message:'Invalid design.'},400);}const plain=(v,n)=>typeof v==='string'&&v.length>0&&v.length<=n&&!/[<>§\x00-\x1f\x7f]/.test(v);if(!plain(b.office,64)||!plain(b.term,200)||!Number.isSafeInteger(b.version)||b.version<0||!plain(b.name,64)||!/^#[a-f0-9]{6}$/i.test(b.color)||!Array.isArray(b.lore)||b.lore.length>6||b.lore.some(l=>!plain(l,120)))return reply({message:'Check the kit name, colour and lore.'},400);let equipment;if(b.equipment!==undefined){try{equipment=validateKitEquipment(b.equipment);}catch{return reply({message:'Check the armour, banner and item lore.'},400);}}body={...(equipment?{equipment}:{}),uuid:player,office:b.office,term:b.term,version:b.version,name:b.name,color:b.color,lore:b.lore};path='/kits/player/definition';await db.prepare('INSERT INTO account_audit(id,actor_id,action,details,created_at) VALUES(?,?,?,?,?)').bind(randomUUID(),user.id,'office-kit-design-requested',JSON.stringify({office:b.office,term:b.term,version:b.version}),Date.now()).run();
|
|
}else if(request.method!=='GET')return reply({message:'Method not allowed.'},405);
|
|
const r=await(dependencies.fetcher||fetch)('http://127.0.0.1:15433'+path,{method:body?'POST':'GET',headers:{Authorization:'Bearer '+settings.SERVER_CONTROL_TOKEN,...(body?{'Content-Type':'application/json'}:{})},...(body?{body:JSON.stringify(body)}:{}),signal:AbortSignal.timeout(8000)});const d=await r.json();if(r.status===403)return reply({message:'This office is no longer held by your linked game account.'},403);if(r.status===409)return reply({message:'The office term or design changed. Refresh before saving.'},409);if(!r.ok)throw Error();
|
|
if(body){if(d.ok!==true||d.uuid?.toLowerCase()!==player.toLowerCase()||d.office!==body.office||d.term!==body.term||d.version!==body.version+1)throw Error();return reply({ok:true,version:d.version});}
|
|
if(d.playerUuid?.toLowerCase()!==player.toLowerCase()||typeof d.canDesign!=='boolean'||!Array.isArray(d.offices)||d.offices.length>20)throw Error();return reply({linked:true,canDesign:d.canDesign,offices:d.offices.map(o=>({office:o.office,term:o.term,version:o.version,name:o.name,color:o.color,lore:o.lore,quality:o.quality,equipment:o.equipment?validateKitEquipment(o.equipment):undefined}))});
|
|
}catch{return reply({message:'The Imperial Atelier is temporarily unavailable. Refresh to check your current office.'},503);}
|
|
};}
|

Staff