mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-05 02:14:05 +00:00
fix(auth): start no new session for a password change over a Bearer token
A password change ends every session of the user and re-issues one for the device it was made on, handed out as the session cookie. A caller that authenticated with a Bearer token never reads that cookie, so the new session sat in the session list as a live device nobody held until it expired. The controller now asks for the replacement session only when the request came with the session cookie, the same check the sliding renewal makes. The response body is unchanged.
This commit is contained in:
@@ -352,10 +352,20 @@ describe('AuthController (authenticated)', () => {
|
||||
const changePassword = vi.fn().mockReturnValue({ token: 'tk3' });
|
||||
const c = ac(asvc({ changePassword, setAuthCookie } as Partial<AuthService>), rl());
|
||||
expect(await c.changePassword(user, anyBody(), reqCookie, res)).toEqual({ success: true });
|
||||
expect(changePassword).toHaveBeenCalledWith(1, 'u@example.test', anyBody(), true, { userAgent: null });
|
||||
expect(changePassword).toHaveBeenCalledWith(1, 'u@example.test', anyBody(), true, { userAgent: null }, true);
|
||||
expect(setAuthCookie).toHaveBeenCalledWith(res, 'tk3', reqCookie, true);
|
||||
});
|
||||
|
||||
it('change-password starts no new session for a Bearer caller, who has no cookie to receive it', async () => {
|
||||
const setAuthCookie = vi.fn();
|
||||
const changePassword = vi.fn().mockReturnValue({ success: true });
|
||||
const bearer = { ip: '9.9.9.9', headers: { authorization: 'Bearer abc' } } as unknown as Request;
|
||||
const c = ac(asvc({ changePassword, setAuthCookie } as Partial<AuthService>), rl());
|
||||
expect(await c.changePassword(user, anyBody(), bearer, res)).toEqual({ success: true });
|
||||
expect(changePassword).toHaveBeenCalledWith(1, 'u@example.test', anyBody(), undefined, { userAgent: null }, false);
|
||||
expect(setAuthCookie).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('delete-account maps error, else audits and succeeds', async () => {
|
||||
expect(await thrownAsync(() => ac(asvc({ deleteAccount: vi.fn().mockReturnValue({ error: 'Last admin', status: 403 }) } as Partial<AuthService>), rl()).deleteAccount(user, req))).toEqual({ status: 403, body: { error: 'Last admin' } });
|
||||
expect(await ac(asvc({ deleteAccount: vi.fn().mockReturnValue({}) } as Partial<AuthService>), rl()).deleteAccount(user, req)).toEqual({ success: true });
|
||||
|
||||
Reference in New Issue
Block a user