From f1e4fdf17566ed61e6266eadaad565ea8aed7c00 Mon Sep 17 00:00:00 2001 From: martin <74269598+martabal@users.noreply.github.com> Date: Tue, 13 Feb 2024 02:47:26 +0100 Subject: [PATCH] refactor: authentication on public routes (#6765) * refactor: authentication on public routes * fix: remove public user * pr feedback * pr feedback * pr feedback * pr feedback * remove unused method * fix: tests * fix: useless methods * fix: tests * pr feedback * pr feedback * chore: cleanup --------- Co-authored-by: Jason Rasmussen --- server/e2e/api/specs/auth.e2e-spec.ts | 3 +- server/src/domain/auth/auth.constant.ts | 1 + server/src/domain/auth/auth.service.ts | 6 +- .../src/immich/controllers/auth.controller.ts | 2 + server/test/fixtures/auth.stub.ts | 3 + .../lib/components/forms/login-form.svelte | 1 - .../navigation-bar/account-info-panel.svelte | 27 ++++----- .../navigation-bar/navigation-bar.svelte | 2 +- .../user-profile-settings.svelte | 32 +++++----- .../user-settings-list.svelte | 2 +- web/src/lib/stores/user.store.ts | 10 +--- web/src/lib/stores/websocket.ts | 7 +-- web/src/lib/utils/auth.ts | 58 +++++++++++-------- web/src/routes/(user)/share/[key]/+page.ts | 4 +- web/src/routes/+page.ts | 4 +- web/src/routes/auth/change-password/+page.ts | 5 +- 16 files changed, 92 insertions(+), 75 deletions(-) diff --git a/server/e2e/api/specs/auth.e2e-spec.ts b/server/e2e/api/specs/auth.e2e-spec.ts index eeb4723faa..e514d2b803 100644 --- a/server/e2e/api/specs/auth.e2e-spec.ts +++ b/server/e2e/api/specs/auth.e2e-spec.ts @@ -153,9 +153,10 @@ describe(`${AuthController.name} (e2e)`, () => { expect(token).toBeDefined(); const cookies = headers['set-cookie']; - expect(cookies).toHaveLength(2); + expect(cookies).toHaveLength(3); expect(cookies[0]).toEqual(`immich_access_token=${token}; HttpOnly; Path=/; Max-Age=34560000; SameSite=Lax;`); expect(cookies[1]).toEqual('immich_auth_type=password; HttpOnly; Path=/; Max-Age=34560000; SameSite=Lax;'); + expect(cookies[2]).toEqual('immich_is_authenticated=true; Path=/; Max-Age=34560000; SameSite=Lax;'); }); }); diff --git a/server/src/domain/auth/auth.constant.ts b/server/src/domain/auth/auth.constant.ts index d237a19cd5..f29fc92741 100644 --- a/server/src/domain/auth/auth.constant.ts +++ b/server/src/domain/auth/auth.constant.ts @@ -1,6 +1,7 @@ export const MOBILE_REDIRECT = 'app.immich:/'; export const LOGIN_URL = '/auth/login?autoLaunch=0'; export const IMMICH_ACCESS_COOKIE = 'immich_access_token'; +export const IMMICH_IS_AUTHENTICATED = 'immich_is_authenticated'; export const IMMICH_AUTH_TYPE_COOKIE = 'immich_auth_type'; export const IMMICH_API_KEY_NAME = 'api_key'; export const IMMICH_API_KEY_HEADER = 'x-api-key'; diff --git a/server/src/domain/auth/auth.service.ts b/server/src/domain/auth/auth.service.ts index d4c2e4e189..a2c0d2df93 100644 --- a/server/src/domain/auth/auth.service.ts +++ b/server/src/domain/auth/auth.service.ts @@ -29,6 +29,7 @@ import { IMMICH_ACCESS_COOKIE, IMMICH_API_KEY_HEADER, IMMICH_AUTH_TYPE_COOKIE, + IMMICH_IS_AUTHENTICATED, LOGIN_URL, MOBILE_REDIRECT, } from './auth.constant'; @@ -429,14 +430,17 @@ export class AuthService { let authTypeCookie = ''; let accessTokenCookie = ''; + let isAuthenticatedCookie = ''; if (isSecure) { accessTokenCookie = `${IMMICH_ACCESS_COOKIE}=${loginResponse.accessToken}; HttpOnly; Secure; Path=/; Max-Age=${maxAge}; SameSite=Lax;`; authTypeCookie = `${IMMICH_AUTH_TYPE_COOKIE}=${authType}; HttpOnly; Secure; Path=/; Max-Age=${maxAge}; SameSite=Lax;`; + isAuthenticatedCookie = `${IMMICH_IS_AUTHENTICATED}=true; Secure; Path=/; Max-Age=${maxAge}; SameSite=Lax;`; } else { accessTokenCookie = `${IMMICH_ACCESS_COOKIE}=${loginResponse.accessToken}; HttpOnly; Path=/; Max-Age=${maxAge}; SameSite=Lax;`; authTypeCookie = `${IMMICH_AUTH_TYPE_COOKIE}=${authType}; HttpOnly; Path=/; Max-Age=${maxAge}; SameSite=Lax;`; + isAuthenticatedCookie = `${IMMICH_IS_AUTHENTICATED}=true; Path=/; Max-Age=${maxAge}; SameSite=Lax;`; } - return [accessTokenCookie, authTypeCookie]; + return [accessTokenCookie, authTypeCookie, isAuthenticatedCookie]; } } diff --git a/server/src/immich/controllers/auth.controller.ts b/server/src/immich/controllers/auth.controller.ts index 15018c10de..c8ffd52fd1 100644 --- a/server/src/immich/controllers/auth.controller.ts +++ b/server/src/immich/controllers/auth.controller.ts @@ -5,6 +5,7 @@ import { ChangePasswordDto, IMMICH_ACCESS_COOKIE, IMMICH_AUTH_TYPE_COOKIE, + IMMICH_IS_AUTHENTICATED, LoginCredentialDto, LoginDetails, LoginResponseDto, @@ -84,6 +85,7 @@ export class AuthController { ): Promise { res.clearCookie(IMMICH_ACCESS_COOKIE); res.clearCookie(IMMICH_AUTH_TYPE_COOKIE); + res.clearCookie(IMMICH_IS_AUTHENTICATED); return this.service.logout(auth, (request.cookies || {})[IMMICH_AUTH_TYPE_COOKIE]); } diff --git a/server/test/fixtures/auth.stub.ts b/server/test/fixtures/auth.stub.ts index 3dbbdcbf12..4b0c06baf3 100644 --- a/server/test/fixtures/auth.stub.ts +++ b/server/test/fixtures/auth.stub.ts @@ -145,6 +145,7 @@ export const loginResponseStub = { cookie: [ 'immich_access_token=cmFuZG9tLWJ5dGVz; HttpOnly; Secure; Path=/; Max-Age=34560000; SameSite=Lax;', 'immich_auth_type=oauth; HttpOnly; Secure; Path=/; Max-Age=34560000; SameSite=Lax;', + 'immich_is_authenticated=true; Secure; Path=/; Max-Age=34560000; SameSite=Lax;', ], }, user1password: { @@ -160,6 +161,7 @@ export const loginResponseStub = { cookie: [ 'immich_access_token=cmFuZG9tLWJ5dGVz; HttpOnly; Secure; Path=/; Max-Age=34560000; SameSite=Lax;', 'immich_auth_type=password; HttpOnly; Secure; Path=/; Max-Age=34560000; SameSite=Lax;', + 'immich_is_authenticated=true; Secure; Path=/; Max-Age=34560000; SameSite=Lax;', ], }, user1insecure: { @@ -175,6 +177,7 @@ export const loginResponseStub = { cookie: [ 'immich_access_token=cmFuZG9tLWJ5dGVz; HttpOnly; Path=/; Max-Age=34560000; SameSite=Lax;', 'immich_auth_type=password; HttpOnly; Path=/; Max-Age=34560000; SameSite=Lax;', + 'immich_is_authenticated=true; Path=/; Max-Age=34560000; SameSite=Lax;', ], }, }; diff --git a/web/src/lib/components/forms/login-form.svelte b/web/src/lib/components/forms/login-form.svelte index d749decf42..1eb01cd199 100644 --- a/web/src/lib/components/forms/login-form.svelte +++ b/web/src/lib/components/forms/login-form.svelte @@ -76,7 +76,6 @@ dispatch('firstLogin'); return; } - dispatch('success'); return; } catch (error) { diff --git a/web/src/lib/components/shared-components/navigation-bar/account-info-panel.svelte b/web/src/lib/components/shared-components/navigation-bar/account-info-panel.svelte index 0c790c2851..3d4a89ab5a 100644 --- a/web/src/lib/components/shared-components/navigation-bar/account-info-panel.svelte +++ b/web/src/lib/components/shared-components/navigation-bar/account-info-panel.svelte @@ -1,7 +1,7 @@