web/website/node/account-test-identity.mjs
2026-11-21 20:22:37 +01:00

38 lines
3.7 KiB
JavaScript

import {randomUUID} from 'node:crypto';
export const TEST_IDENTITY_SCHEMA=`CREATE TABLE IF NOT EXISTS site_test_game_identities(minecraft_uuid CHAR(36) PRIMARY KEY,user_id CHAR(36) NOT NULL,key_id CHAR(36) NOT NULL UNIQUE,issued_by CHAR(36) NOT NULL,created_at BIGINT NOT NULL)`;
export const TEST_IDENTITY_ACTION='accounts.test-minecraft-identity';
export const TEST_IDENTITY_PERMISSION='web.jarvis.accounts.test-identity';
export async function testIdentityTargets(db,prefix){
if(typeof prefix!=='string'||! /^[A-Za-z0-9_]{2,32}$/.test(prefix))throw TypeError('Enter at least two account-name characters.');
const pattern=prefix.toLowerCase().replace(/[_%!]/g,c=>'!'+c)+'%';
const rows=await db.query("SELECT a.username,a.display_name FROM site_accounts a WHERE LOWER(a.username) LIKE ? ESCAPE '!' AND a.password_hash NOT LIKE '!%' AND a.role NOT IN ('memorial','official_account','system_account') AND NOT EXISTS(SELECT 1 FROM site_game_links l WHERE l.user_id=a.id) AND NOT EXISTS(SELECT 1 FROM site_link_challenges c WHERE c.user_id=a.id) ORDER BY a.username LIMIT 25",[pattern]);
return rows.map(r=>({value:r.username,label:r.display_name?r.username+' · '+r.display_name:r.username}));
}
async function target(tx,username){
if(typeof username!=='string'||! /^[A-Za-z0-9_]{1,32}$/.test(username))throw TypeError('Invalid target account name');
const row=await tx.first("SELECT id,username,role FROM site_accounts WHERE LOWER(username)=LOWER(?) AND password_hash NOT LIKE '!%' FOR UPDATE",[username]);
if(!row||['memorial','official_account','system_account'].includes(row.role))throw Error('Instruction requires an active ordinary test account');
const link=await tx.first('SELECT * FROM site_game_links WHERE user_id=? FOR UPDATE',[row.id]);
const challenge=await tx.first('SELECT * FROM site_link_challenges WHERE user_id=? FOR UPDATE',[row.id]);
if(link||challenge)throw Error('Instruction cannot overwrite an existing game link or pending link challenge');
return row;
}
export async function prepareTestIdentity(tx,username){
const account=await target(tx,username),minecraftUuid=randomUUID();
return {version:1,action:TEST_IDENTITY_ACTION,account,minecraftUuid,minecraftName:('TEST_'+account.username).slice(0,32),synthetic:true};
}
export async function applyTestIdentity(tx,manifest,{keyId,actorId,now}){
if(manifest?.version!==1||manifest.action!==TEST_IDENTITY_ACTION||manifest.synthetic!==true||! /^[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12}$/.test(manifest.minecraftUuid||''))throw Error('Instruction test identity scope is invalid');
const account=await target(tx,manifest.account?.username);
if(JSON.stringify(account)!==JSON.stringify(manifest.account)||manifest.minecraftName!==('TEST_'+account.username).slice(0,32))throw Error('Instruction target account changed');
if(await tx.first('SELECT minecraft_uuid FROM site_game_identities WHERE minecraft_uuid=? FOR UPDATE',[manifest.minecraftUuid]))throw Error('Instruction UUID is already registered');
await tx.query('INSERT INTO site_game_links(user_id,minecraft_uuid,minecraft_name,linked_at) VALUES(?,?,?,?)',[account.id,manifest.minecraftUuid,manifest.minecraftName,now]);
await tx.query('INSERT INTO site_game_identities(minecraft_uuid,first_linked_at,last_name) VALUES(?,?,?)',[manifest.minecraftUuid,now,manifest.minecraftName]);
await tx.query('INSERT INTO site_test_game_identities(minecraft_uuid,user_id,key_id,issued_by,created_at) VALUES(?,?,?,?,?)',[manifest.minecraftUuid,account.id,keyId,actorId,now]);
await tx.query('INSERT INTO account_audit(id,actor_id,action,details,created_at) VALUES(?,?,?,?,?)',[randomUUID(),actorId,'jarvis-test-game-identity-assigned',JSON.stringify({keyId,accountId:account.id,minecraftUuid:manifest.minecraftUuid,synthetic:true}),now]);
}