[grade=B] fix(auth): reuse valid session for cross-host SSO
CI / Test & Lint (push) Canceled after 0s
CI / Security audit (push) Canceled after 0s

This commit is contained in:
Hermes
2026-08-22 04:06:27 -07:00
parent 499fcc2742
commit d313b1e872
6 changed files with 221 additions and 84 deletions
+38
View File
@@ -93,3 +93,41 @@ test('same-origin and tokenless destinations keep their direct URL', () => {
assert.equal(buildHandoffTarget('/settings', 'one-time'), 'https://status.sami/settings');
assert.equal(buildHandoffTarget('https://router.sami/config', ''), 'https://router.sami/config');
});
test('an existing status.sami session returns to a service without another TOTP prompt', async () => {
const query = new URLSearchParams({ auth: 'required', return: 'https://plex.sami/web/' });
let scheduled;
let redirected;
const location = {
origin: 'https://status.sami',
pathname: '/',
search: `?${query.toString()}`,
replace(value) { redirected = value; },
};
const context = {
URL,
URLSearchParams,
SITE: { tld: '.sami' },
sessionStorage: { setItem() {} },
document: { getElementById() { return null; } },
setTimeout(fn) { scheduled = fn; },
console,
fetch: async (url) => {
assert.equal(url, '/api/v1/auth/sso-handoff');
return { ok: true, json: async () => ({ success: true, ssoToken: 'existing-session-token' }) };
},
window: {
location,
history: { replaceState() {} },
},
};
context.window.window = context.window;
vm.runInNewContext(source, context, { filename: 'auth-gate.js' });
assert.equal(typeof scheduled, 'function');
await scheduled();
assert.equal(
redirected,
'https://plex.sami/dashcaddy-sso?token=existing-session-token&return=%2Fweb%2F',
);
});