Queue ordinary game permissions when Lobby accounts are linked #650

Merged
unimatrix merged 1 commit from fix/lobby-account-permission-sync-20261202 into main 2026-12-02 12:12:18 +01:00
2 changed files with 6 additions and 2 deletions

View file

@ -1,6 +1,7 @@
import {randomBytes,randomUUID,createHash,createCipheriv,createDecipheriv,scrypt as rawScrypt,timingSafeEqual} from 'node:crypto';
import {promisify} from 'node:util';
import {validUuid} from './game-link.mjs';
import {nativeSyncMutation} from './native-sync-outbox.mjs';
const scrypt=promisify(rawScrypt),digest=v=>createHash('sha256').update(v).digest('hex');
const fault=(message,status=409)=>Object.assign(Error(message),{status});
export const LOBBY_ACCOUNT_SCHEMA=`CREATE TABLE IF NOT EXISTS site_lobby_account_provisions (
@ -66,6 +67,7 @@ export function createLobbyAccountService(db,{encryptionKey,clock=Date.now,onPro
await tx.query('INSERT INTO site_profiles(user_id,bio,public_info,socials_json,updated_at) VALUES(?,?,?,?,?)',[id,'DuneRealms player '+input.name+'.','','{}',now]);
await tx.query('INSERT INTO site_game_links(user_id,minecraft_uuid,minecraft_name,linked_at) VALUES(?,?,?,?)',[id,uuid,input.name,now]);
await tx.query('INSERT INTO site_game_identities(minecraft_uuid,first_linked_at,last_name) VALUES(?,?,?)',[uuid,now,input.name]);
const sync=nativeSyncMutation(now);await tx.query(sync.sql,sync.values);
await tx.query('INSERT INTO site_contact_emails(user_id,address,verified_at) VALUES(?,?,NULL)',[id,LOBBY_PLACEHOLDER_EMAIL]);
await tx.query('INSERT INTO site_first_login(user_id,password_changed,created_at) VALUES(?,0,?)',[id,now]);
await tx.query('INSERT INTO site_lobby_account_provisions(minecraft_uuid,request_id,minecraft_name,account_id,credential_hash,ciphertext,created_at,expires_at,delivered_at) VALUES(?,?,?,?,?,?,?,?,NULL)',[uuid,requestId,input.name,id,digest(hash),seal(password,key,binding),now,now+ttl]);

View file

@ -4,6 +4,7 @@ import {DatabaseSync} from 'node:sqlite';
import {scryptSync,randomUUID} from 'node:crypto';
import {writeFile} from 'node:fs/promises';
import {LOBBY_ACCOUNT_SCHEMA,createLobbyAccountService,createLobbyAccountAPI} from '../node/lobby-account.mjs';
import {NATIVE_SYNC_SCHEMA} from '../node/native-sync-schema.mjs';
const key='ab'.repeat(32),token='X'.repeat(43),uuid='5195da0c-ba50-4f82-baa7-1c99a73c810b';
function fixture(){
const sql=new DatabaseSync(':memory:');sql.exec(`
@ -16,7 +17,7 @@ function fixture(){
CREATE TABLE site_first_login(user_id TEXT PRIMARY KEY,password_changed INTEGER,created_at INTEGER);
CREATE TABLE site_age_declarations(user_id TEXT PRIMARY KEY,age_band TEXT,declared_at INTEGER);
CREATE TABLE account_audit(id TEXT PRIMARY KEY,actor_id TEXT,action TEXT,details TEXT,created_at INTEGER);
${LOBBY_ACCOUNT_SCHEMA.replace(' ENGINE=InnoDB DEFAULT CHARSET=utf8mb4','')}`);
${LOBBY_ACCOUNT_SCHEMA.replace(' ENGINE=InnoDB DEFAULT CHARSET=utf8mb4','')};${NATIVE_SYNC_SCHEMA}`);
const clean=s=>s.replace(/ FOR UPDATE/g,'');
const tx={async first(s,v=[]){return sql.prepare(clean(s)).get(...v)||null;},async query(s,v=[]){return sql.prepare(clean(s)).run(...v);}};
const db={async transaction(work){sql.exec('BEGIN');try{const r=await work(tx);sql.exec('COMMIT');return r;}catch(e){sql.exec('ROLLBACK');throw e;}}};
@ -33,6 +34,7 @@ test('new account, verified game link and encrypted replay commit together; age
const email=f.sql.prepare('SELECT * FROM site_contact_emails').get();assert.equal(email.address,'no_reply@dunerealms.com');assert.equal(email.verified_at,null);
assert.equal(f.sql.prepare('SELECT COUNT(*) n FROM site_age_declarations').get().n,0);
assert.equal(f.sql.prepare('SELECT password_changed FROM site_first_login').get().password_changed,0);
const sync=f.sql.prepare('SELECT * FROM native_sync_outbox').get();assert.equal(sync.job_key,'memberships');assert.equal(sync.attempts,0);assert.equal(sync.next_attempt_at,0);
const raw=f.sql.prepare('SELECT * FROM site_lobby_account_provisions').get();assert.ok(!JSON.stringify(raw).includes(a.password));assert.ok(!f.sql.prepare('SELECT details FROM account_audit').get().details.includes(a.password));
if(process.env.LOBBY_ACCOUNT_WIRE_OUTPUT){
const ack=await f.service.execute({schema:1,action:'ack',uuid,requestId:f.input.requestId});
@ -76,7 +78,7 @@ test('failed link insertion rolls the entire new account back',async()=>{
const f=fixture();try{
f.sql.exec("CREATE TRIGGER broken_link BEFORE INSERT ON site_game_links BEGIN SELECT RAISE(ABORT,'link unavailable');END");
await assert.rejects(f.service.execute(f.input),/link unavailable/);
for(const name of ['site_accounts','player_profiles','site_profiles','site_lobby_account_provisions','account_audit'])assert.equal(f.sql.prepare('SELECT COUNT(*) n FROM '+name).get().n,0);
for(const name of ['site_accounts','player_profiles','site_profiles','site_lobby_account_provisions','account_audit','native_sync_outbox'])assert.equal(f.sql.prepare('SELECT COUNT(*) n FROM '+name).get().n,0);
}finally{f.sql.close();}
});
test('bank outage retains the account and original secret; retry completes readiness without another account',async()=>{