Accounts: Playwright on the real pages; interaction cookie path fix
The interaction address is now /api/v2/interaction/<uid> (redirecting to the sign-in page): oidc-provider scopes its cookie to that path, so every step's API call gets it. Before, a real browser never sent it and every sign-in 'expired'; the test browser now honours cookie paths too. 8 Playwright tests (deep link, wrong password, foreign return address, sign-up, reset mid-sign-in, 2FA set-up, account page, admin) on the real backend with an in-memory database, no Postgres needed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -162,7 +162,7 @@ describe('the steps in between still return to the page', () => {
|
||||
const { url } = await browser.follow(started.url, (u) => isCallback(u) || u.pathname === '/signin');
|
||||
assert.equal(url.pathname, '/signin');
|
||||
const uid = url.searchParams.get('uid')!;
|
||||
const view = await h.api(browser, 'GET', `/api/v2/interaction/${uid}`);
|
||||
const view = await h.api(browser, 'GET', `/api/v2/interaction/${uid}/state`);
|
||||
assert.equal(view.body.stage, 'notice');
|
||||
const done = await h.api(browser, 'POST', `/api/v2/interaction/${uid}/notice`, { version: n.version });
|
||||
const back = await browser.follow(done.body.redirectTo as string, isCallback);
|
||||
|
||||
@@ -23,22 +23,28 @@ const listen = (server: http.Server) => new Promise<number>((resolve) => server.
|
||||
|
||||
/** A browser: cookies per host, redirects followed by hand so each hop can be checked. */
|
||||
export class Browser {
|
||||
private jar = new Map<string, Map<string, string>>();
|
||||
// Per host: name → value and the cookie's Path, sent only on paths under it (as browsers do;
|
||||
// oidc-provider scopes its interaction cookie to the interaction's address).
|
||||
private jar = new Map<string, Map<string, { value: string; path: string }>>();
|
||||
|
||||
private cookieHeader(url: URL) {
|
||||
const c = this.jar.get(url.host);
|
||||
return c ? [...c].map(([k, v]) => `${k}=${v}`).join('; ') : '';
|
||||
if (!c) return '';
|
||||
const under = (p: string) => url.pathname === p || url.pathname.startsWith(p.endsWith('/') ? p : `${p}/`) || p === '/';
|
||||
return [...c].filter(([, v]) => under(v.path)).map(([k, v]) => `${k}=${v.value}`).join('; ');
|
||||
}
|
||||
|
||||
private store(url: URL, res: Response) {
|
||||
for (const line of res.headers.getSetCookie()) {
|
||||
const [pair] = line.split(';');
|
||||
const [pair, ...attrs] = line.split(';');
|
||||
const i = pair!.indexOf('=');
|
||||
const name = pair!.slice(0, i).trim();
|
||||
const value = pair!.slice(i + 1).trim();
|
||||
const pathAttr = attrs.map((a) => a.trim()).find((a) => /^path=/i.test(a));
|
||||
const path = pathAttr ? pathAttr.slice(5) : url.pathname.replace(/\/[^/]*$/, '') || '/';
|
||||
if (!this.jar.has(url.host)) this.jar.set(url.host, new Map());
|
||||
if (/expires=Thu, 01 Jan 1970/i.test(line) || /max-age=0/i.test(line) || value === '') this.jar.get(url.host)!.delete(name);
|
||||
else this.jar.get(url.host)!.set(name, value);
|
||||
else this.jar.get(url.host)!.set(name, { value, path });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user