Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

Console: address review on the password change (CHOO-4224)

Simon Flack12370f9f6a64
diff --git a/console/apps/switch-console-desktop/src/main/core/switch-servers/gateway-client.account.test.ts b/console/apps/switch-console-desktop/src/main/core/switch-servers/gateway-client.account.test.ts
index afaaed15b..db473017d 100644
--- a/console/apps/switch-console-desktop/src/main/core/switch-servers/gateway-client.account.test.ts
+++ b/console/apps/switch-console-desktop/src/main/core/switch-servers/gateway-client.account.test.ts
@@ -121,6 +121,19 @@ describe('account', () => {
expect(setSessionCookie).toHaveBeenCalledWith('srv-1', 'fresh-session');
});
+ it('says the password changed when only keeping its new session failed', async () => {
+ fetchMock.mockImplementation(async () =>
+ response(200, { ok: true }, ['switch_auth=fresh-session; HttpOnly; Path=/'])
+ );
+ setSessionCookie.mockRejectedValueOnce(new Error('disk full'));
+ const failure = await changeMyPassword(SERVER, 'old-pass', 'new-password-1').catch(
+ (error: unknown) => error
+ );
+ expect(failure).toBeInstanceOf(Error);
+ expect((failure as Error).name).toBe('PasswordChangedSessionNotSavedError');
+ expect((failure as Error).message).toMatch(/^Your password was changed/);
+ });
+
it('leaves the stored session alone when the server sends no new one', async () => {
fetchMock.mockImplementation(async () => response(200, { ok: true }));
await changeMyPassword(SERVER, 'old-pass', 'new-password-1');
diff --git a/console/apps/switch-console-desktop/src/main/core/switch-servers/gateway-client.ts b/console/apps/switch-console-desktop/src/main/core/switch-servers/gateway-client.ts
index 82e5fe69c..7d80be127 100644
--- a/console/apps/switch-console-desktop/src/main/core/switch-servers/gateway-client.ts
+++ b/console/apps/switch-console-desktop/src/main/core/switch-servers/gateway-client.ts
@@ -786,24 +786,49 @@ export async function updateMyName(server: SwitchServer, name: string): Promise<
* included, and answers with a new session cookie for this caller. Keeping it
* is what leaves Console signed in: the stored cookie is now refused. A server
* that sends none leaves the old one, and the next call finds it signed out.
+ *
+ * Failing to keep the new cookie is not a failed change: the password has
+ * changed by then, so it is reported as what it is, with what to do next.
*/
export async function changeMyPassword(
server: SwitchServer,
currentPassword: string,
newPassword: string
): Promise<void> {
+ let res: Response;
try {
- const res = await gatewayFetch(server, '/auth/me/password', {
+ res = await gatewayFetch(server, '/auth/me/password', {
authenticated: true,
method: 'PUT',
body: { current_password: currentPassword, new_password: newPassword },
});
- const cookie = extractAuthCookie(res.headers.getSetCookie());
- if (cookie) await setSessionCookie(server.id, cookie);
} catch (error) {
if (error instanceof GatewayError && error.detail) throw new Error(error.detail);
throw error;
}
+ const cookie = extractAuthCookie(res.headers.getSetCookie());
+ if (!cookie) return;
+ try {
+ await setSessionCookie(server.id, cookie);
+ } catch (cause) {
+ throw new PasswordChangedSessionNotSavedError(cause);
+ }
+}
+
+/**
+ * The password changed, but Console could not keep the session the server
+ * answered with. Named, so the window says it as it is rather than leading
+ * with "Could not change your password", which would leave the person
+ * believing the old password still works.
+ */
+export class PasswordChangedSessionNotSavedError extends Error {
+ constructor(cause: unknown) {
+ super(
+ 'Your password was changed, but Console couldn’t save its new session. Sign in again with your new password.',
+ { cause }
+ );
+ this.name = 'PasswordChangedSessionNotSavedError';
+ }
}
export async function fetchMe(server: SwitchServer): Promise<SwitchUser> {
diff --git a/console/apps/switch-console-desktop/src/renderer/features/settings/pages/account-page.tsx b/console/apps/switch-console-desktop/src/renderer/features/settings/pages/account-page.tsx
index d8bbe6288..3c11ba911 100644
--- a/console/apps/switch-console-desktop/src/renderer/features/settings/pages/account-page.tsx
+++ b/console/apps/switch-console-desktop/src/renderer/features/settings/pages/account-page.tsx
@@ -195,6 +195,10 @@ export function ChangePassword({ serverId }: { serverId: string }) {
onSuccess: () => {
reset();
setChanged(true);
+ // Anything refused in the moment between the server ending the old
+ // session and Console keeping the new one can have shown the server as
+ // signed out; read its status again now the new session is in place.
+ void switchServersStore.refreshStatus(serverId);
},
onError: (error) => {
const { headline, detail } = describeFailure(error, 'Could not change your password.');
diff --git a/console/apps/switch-console-desktop/src/renderer/features/switch-servers/switch-servers-store.test.ts b/console/apps/switch-console-desktop/src/renderer/features/switch-servers/switch-servers-store.test.ts
index 008090ccc..350c6d311 100644
--- a/console/apps/switch-console-desktop/src/renderer/features/switch-servers/switch-servers-store.test.ts
+++ b/console/apps/switch-console-desktop/src/renderer/features/switch-servers/switch-servers-store.test.ts
@@ -809,6 +809,22 @@ describe('a session the server has ended', () => {
await first;
});
+ it('comes back signed in when a hint that raced a password change is followed by a re-read', async () => {
+ // The old session ended a moment before Console kept the new one: the
+ // hint's re-check used the old cookie and read signed out. The account
+ // page re-reads the status once the change is done, with the new cookie.
+ const store = newStore([server('a')]);
+ getConnectionStatus.mockResolvedValueOnce({ serverId: 'a', connected: true, user });
+ await store.refreshStatus('a');
+ getConnectionStatus.mockResolvedValueOnce({ serverId: 'a', connected: false, user: null });
+ await store.noteSessionEnded('a');
+ expect(store.isConnected('a')).toBe(false);
+
+ getConnectionStatus.mockResolvedValueOnce({ serverId: 'a', connected: true, user });
+ await store.refreshStatus('a');
+ expect(store.isConnected('a')).toBe(true);
+ });
+
it('listens for the main process saying so', async () => {
expect(onSessionEnded).toBeDefined();
switchServersStore.servers = [server('a')];
diff --git a/console/apps/switch-console-desktop/src/renderer/lib/errors/describe-failure.test.ts b/console/apps/switch-console-desktop/src/renderer/lib/errors/describe-failure.test.ts
index f06e6db5b..9e4a382cd 100644
--- a/console/apps/switch-console-desktop/src/renderer/lib/errors/describe-failure.test.ts
+++ b/console/apps/switch-console-desktop/src/renderer/lib/errors/describe-failure.test.ts
@@ -138,6 +138,17 @@ describe('describeFailure', () => {
});
});
+ it('says a password changed whose new session could not be kept, not that it failed', () => {
+ const message =
+ 'Your password was changed, but Console couldn’t save its new session. Sign in again with your new password.';
+ expect(
+ describeFailure(
+ rpcError('PasswordChangedSessionNotSavedError', message),
+ 'Could not change your password.'
+ )
+ ).toEqual({ headline: message, detail: null });
+ });
+
it('passes a refusal to remove a machine running moved agents straight through', () => {
const message =
'build-box runs builder as managed agents for this Console, so it cannot be removed yet. Delete those agents first.';
diff --git a/console/apps/switch-console-desktop/src/renderer/lib/errors/describe-failure.ts b/console/apps/switch-console-desktop/src/renderer/lib/errors/describe-failure.ts
index 6be368866..7abab8a5f 100644
--- a/console/apps/switch-console-desktop/src/renderer/lib/errors/describe-failure.ts
+++ b/console/apps/switch-console-desktop/src/renderer/lib/errors/describe-failure.ts
@@ -94,9 +94,11 @@ export function describeFailure(error: unknown, fallback: string): FailureDescri
case 'DuplicateServerUrlError':
case 'NoDashboardError':
case 'NoManagementApiError':
+ case 'PasswordChangedSessionNotSavedError':
// Already a modeled, actionable sentence: the server and the page to
// fix it on, who is changing the server and when to try again, which
- // agents to bring back first, or which address to enter instead.
+ // agents to bring back first, which address to enter instead, or that a
+ // password changed but its new session could not be kept.
return { headline: error.message, detail: null };
}
}
diff --git a/console/apps/switch-console-desktop/src/renderer/tests/browser/change-password.test.tsx b/console/apps/switch-console-desktop/src/renderer/tests/browser/change-password.test.tsx
index c4b8c3091..581acd8e2 100644
--- a/console/apps/switch-console-desktop/src/renderer/tests/browser/change-password.test.tsx
+++ b/console/apps/switch-console-desktop/src/renderer/tests/browser/change-password.test.tsx
@@ -16,13 +16,14 @@ vi.hoisted(() => {
});
const changeMyPassword = vi.hoisted(() => vi.fn());
+const refreshStatus = vi.hoisted(() => vi.fn());
vi.mock('@renderer/lib/ipc', () => ({
events: { on: () => () => {} },
rpc: { switchServers: { changeMyPassword } },
}));
vi.mock('@renderer/features/switch-servers/switch-servers-store', () => ({
- switchServersStore: {},
+ switchServersStore: { refreshStatus },
}));
import { ChangePassword } from '@renderer/features/settings/pages/account-page';
@@ -94,6 +95,9 @@ describe('ChangePassword', () => {
expect(document.querySelector('[role="alert"]')?.textContent).toBe(
'Password changed successfully'
);
+ // Whatever was refused while the old session was ending can have shown the
+ // server as signed out; it is read again with the new session in place.
+ expect(refreshStatus).toHaveBeenCalledWith('srv-1');
});
it('does not carry the message over once the page is left and opened again', async () => {
@@ -113,5 +117,6 @@ describe('ChangePassword', () => {
await render();
await changePassword();
expect(document.body.textContent).not.toContain('Password changed successfully');
+ expect(refreshStatus).not.toHaveBeenCalled();
});
});