27 lines
3.3 KiB
JavaScript
27 lines
3.3 KiB
JavaScript
'use strict';
|
|
const {test}=require('node:test'),assert=require('node:assert/strict'),crypto=require('node:crypto'),fs=require('node:fs'),os=require('node:os'),path=require('node:path');
|
|
const {Store}=require('../../src/modules/dashboard/store.cjs'),{createDashboard}=require('../../src/app/dashboard.cjs');
|
|
function fixture(t,cleanup=true){const dir=fs.mkdtempSync(path.join(os.tmpdir(),'async-auth-')),store=new Store(dir),admin=store.bootstrap('admin','Test-password-123');if(cleanup)t.after(()=>fs.rmSync(dir,{recursive:true,force:true}));return {store,dir,admin};}
|
|
test('HTTP login validates real stored credentials without synchronous password hashing',async t=>{
|
|
const {store,dir}=fixture(t,false),server=createDashboard({store,outputDir:dir});await new Promise(r=>server.listen(0,'127.0.0.1',r));t.after(async()=>{server.closeAllConnections();await new Promise(r=>server.close(r));fs.rmSync(dir,{recursive:true,force:true});});
|
|
const base='http://127.0.0.1:'+server.address().port;
|
|
const call=async(username,password)=>{const response=await fetch(base+'/api/login',{method:'POST',headers:{Origin:base,'Content-Type':'application/json'},body:JSON.stringify({username,password})});return {status:response.status,body:await response.json(),cookie:response.headers.get('set-cookie')};};
|
|
t.mock.method(crypto,'scryptSync',()=>{throw Error('synchronous password computation blocks other pages');});
|
|
const good=await call('ADMIN','Test-password-123');assert.equal(good.status,200);assert.equal(good.body.user.username,'admin');assert.ok(good.cookie);
|
|
assert.equal((await call('missing','Test-password-123')).status,401);assert.equal((await call('admin','Wrong-password-123')).status,401);
|
|
const authenticate=store.authenticateAsync.bind(store);store.authenticateAsync=async(...args)=>{const user=await authenticate(...args);store.updateUser(user.id,{version:user.version,enabled:true,role:user.role},user);return user;};
|
|
const stale=await call('admin','Test-password-123');assert.equal(stale.status,401,'account revocation between validation and session issuance must win');assert.equal(stale.cookie,null);
|
|
});
|
|
test('a disabled or changed account cannot complete a queued asynchronous login',async t=>{
|
|
const {store,admin}=fixture(t),worker=store.addUser({username:'worker',password:'Test-password-123',role:'operator'},'admin');
|
|
assert.equal(typeof store.authenticateAsync,'function');
|
|
const pending=store.authenticateAsync('worker','Test-password-123');store.updateUser(worker.id,{version:1,enabled:false,role:'operator'},admin);assert.equal(await pending,null);
|
|
const reset=store.authenticateAsync('admin','Test-password-123');store.updatePassword(admin.id,'New-password-456',admin);assert.equal(await reset,null);
|
|
});
|
|
test('asynchronous password jobs yield to the event loop and reject excessive queued work',async t=>{
|
|
const {store}=fixture(t);assert.equal(typeof store.authenticateAsync,'function');
|
|
let completed=0;const jobs=Array.from({length:60},()=>store.authenticateAsync('admin','Test-password-123').finally(()=>completed++));
|
|
const settled=Promise.allSettled(jobs);await new Promise(r=>setImmediate(r));assert.ok(completed<60,'HTTP/event loop must run while scrypt works');
|
|
const results=await settled;assert.ok(results.some(item=>item.status==='fulfilled'&&item.value?.username==='admin'));assert.ok(results.some(item=>item.status==='rejected'&&item.reason.status===429));
|
|
});
|