forked from unimatrix/web
feat(postbank): central UUID client numbers and My Banking Information
This commit is contained in:
parent
d065aef3c8
commit
ad97dcd233
2 changed files with 94 additions and 3 deletions
51
website/node/bank-clients.mjs
Normal file
51
website/node/bank-clients.mjs
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
import {createHash} from 'node:crypto';
|
||||
import {identity} from './auth.mjs';
|
||||
|
||||
export function canonicalClientUuid(value){
|
||||
if(typeof value!=='string'||! /^[a-f0-9]{8}(?:-[a-f0-9]{4}){3}-[a-f0-9]{12}$/i.test(value))throw new TypeError('A canonical player UUID is required.');
|
||||
return value.toLowerCase();
|
||||
}
|
||||
export function clientNumberCandidate(uuid,attempt=0){
|
||||
uuid=canonicalClientUuid(uuid);
|
||||
if(!Number.isSafeInteger(attempt)||attempt<0||attempt>=32)throw new RangeError('Invalid client number attempt.');
|
||||
const hash=createHash('sha256').update('dunerealms:postbank:client:v1:'+uuid+':'+attempt).digest('hex');
|
||||
return (BigInt('0x'+hash)%1000000000000n).toString().padStart(12,'0');
|
||||
}
|
||||
// A truncated UUID hash alone cannot guarantee uniqueness. Persist the assigned
|
||||
// number, with independent unique keys for UUID and number, and retry collisions.
|
||||
export async function ensureBankClient(db,uuid,{candidate=clientNumberCandidate}={}){
|
||||
uuid=canonicalClientUuid(uuid);
|
||||
const find=()=>db.prepare('SELECT client_number FROM bank_clients WHERE minecraft_uuid=?').bind(uuid).first();
|
||||
const existing=await find();if(existing)return existing.client_number;
|
||||
for(let attempt=0;attempt<32;attempt++){
|
||||
const number=candidate(uuid,attempt);
|
||||
if(typeof number!=='string'||!/^\d{12}$/.test(number))throw Error('Invalid client number candidate.');
|
||||
try{
|
||||
await db.prepare('INSERT INTO bank_clients(minecraft_uuid,client_number,created_at) VALUES(?,?,?)').bind(uuid,number,Date.now()).run();
|
||||
return number;
|
||||
}catch(error){
|
||||
if(error.code!=='ER_DUP_ENTRY'&&!String(error.message).includes('UNIQUE constraint'))throw error;
|
||||
const assigned=await find();if(assigned)return assigned.client_number;
|
||||
}
|
||||
}
|
||||
throw Error('Client number allocation is temporarily unavailable.');
|
||||
}
|
||||
export function createBankClientApi(db,{authenticate=r=>identity(r,db)}={}){
|
||||
const reply=(data,status=200)=>Response.json(data,{status,headers:{'Cache-Control':'private, no-store',Vary:'Cookie'}});
|
||||
const linked=id=>db.prepare('SELECT minecraft_uuid FROM site_game_links WHERE user_id=?').bind(id).first();
|
||||
return async request=>{
|
||||
if(request.method!=='GET')return reply({message:'Method not allowed.'},405);
|
||||
if(new URL(request.url).search)return reply({message:'Client identity does not accept account overrides.'},400);
|
||||
const user=await authenticate(request);if(!user)return reply({message:'Sign in first.'},401);
|
||||
const link=await linked(user.id);
|
||||
const policy=await db.prepare('SELECT game_link_exempt FROM site_account_policies WHERE user_id=?').bind(user.id).first();
|
||||
if(policy?.game_link_exempt)return reply({clientNumber:null,linked:false});
|
||||
if(!link?.minecraft_uuid)return reply({clientNumber:null,linked:false});
|
||||
try{
|
||||
const uuid=canonicalClientUuid(link.minecraft_uuid),number=await ensureBankClient(db,uuid);
|
||||
const current=await authenticate(request),fresh=current?.id===user.id?await linked(user.id):null;
|
||||
if(!fresh?.minecraft_uuid||canonicalClientUuid(fresh.minecraft_uuid)!==uuid)return reply({message:'Your linked account changed. Refresh your client identity.'},409);
|
||||
return reply({clientNumber:number,linked:true});
|
||||
}catch{return reply({message:'Client identity is temporarily unavailable.'},503);}
|
||||
};
|
||||
}
|
||||
|
|
@ -54,6 +54,11 @@ CREATE TABLE IF NOT EXISTS site_game_links (
|
|||
CREATE TABLE IF NOT EXISTS site_game_identities (
|
||||
minecraft_uuid CHAR(36) PRIMARY KEY, first_linked_at BIGINT NOT NULL, last_name VARCHAR(32) NOT NULL
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||
CREATE TABLE IF NOT EXISTS bank_clients (
|
||||
minecraft_uuid CHAR(36) PRIMARY KEY,
|
||||
client_number CHAR(12) NOT NULL UNIQUE,
|
||||
created_at BIGINT NOT NULL
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||
CREATE TABLE IF NOT EXISTS site_link_challenges (
|
||||
user_id CHAR(36) PRIMARY KEY, token_hash CHAR(64) NOT NULL UNIQUE, expires_at BIGINT NOT NULL,
|
||||
FOREIGN KEY(user_id) REFERENCES site_accounts(id)
|
||||
|
|
|
|||
Loading…
Reference in a new issue

Staff