From e06b96925ebe6bb440ed00305e91dda71d212cd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Guti=C3=A9rrez?= Date: Mon, 23 Nov 2026 11:26:52 +0100 Subject: [PATCH 1/3] feat: send a Turnstile token on the console logins --- src/components/auth/LoginPageView.tsx | 3 ++ .../services/management-auth.service.ts | 10 ++-- .../services/partners-auth.service.ts | 12 +++-- src/services/captcha.service.ts | 12 +++++ src/services/login-captcha.test.ts | 49 +++++++++++++++++++ .../services/sub-account-auth.service.ts | 4 +- .../services/wholesalers-auth.service.ts | 10 ++-- 7 files changed, 88 insertions(+), 12 deletions(-) create mode 100644 src/services/login-captcha.test.ts diff --git a/src/components/auth/LoginPageView.tsx b/src/components/auth/LoginPageView.tsx index 08360416..dbdf0a0b 100644 --- a/src/components/auth/LoginPageView.tsx +++ b/src/components/auth/LoginPageView.tsx @@ -8,6 +8,7 @@ import TextInput from './TextInput'; import PasswordInput, { IFormValues } from '../PasswordInput'; import { BrandLogo } from '../BrandLogo'; import { getLockedUntil, isTwoFactorRequiredError } from '../../utils/apiError'; +import { CaptchaUnavailableError } from '../../services/captcha.service'; // Only app-internal paths are allowed as a post-login destination, so a crafted // ?redirect= can never send the user to another origin. @@ -108,6 +109,8 @@ export const LoginPageView = ({ time: dayjs(lockedUntil).format('HH:mm'), }), ); + } else if (err instanceof CaptchaUnavailableError) { + setLoginError(t('login.captchaUnavailableError')); } else if (supportsTwoFactor && !twoFactorRequired && isTwoFactorRequiredError(err)) { setTwoFactorRequired(true); } else { diff --git a/src/management/services/management-auth.service.ts b/src/management/services/management-auth.service.ts index ab565327..77cddf6f 100644 --- a/src/management/services/management-auth.service.ts +++ b/src/management/services/management-auth.service.ts @@ -1,4 +1,5 @@ import managementAxios from '../core/management-axios'; +import { captchaService } from '../../services/captcha.service'; const MANAGEMENT_TOKEN_KEY = 'managementToken'; @@ -15,9 +16,12 @@ function removeToken(): void { } async function logIn(email: string, password: string, code?: string): Promise<{ twoFactorSetupRequired: boolean }> { - const response = await managementAxios.post<{ token: string; twoFactorSetupRequired?: boolean }>( - '/management/login', - { email, password, code }, + const response = await captchaService.withCaptcha('Login', (headers) => + managementAxios.post<{ token: string; twoFactorSetupRequired?: boolean }>( + '/management/login', + { email, password, code }, + { headers }, + ), ); setToken(response.data.token); return { twoFactorSetupRequired: !!response.data.twoFactorSetupRequired }; diff --git a/src/partners/services/partners-auth.service.ts b/src/partners/services/partners-auth.service.ts index 72d5eacf..a22d405d 100644 --- a/src/partners/services/partners-auth.service.ts +++ b/src/partners/services/partners-auth.service.ts @@ -16,11 +16,13 @@ function removeToken(): void { } async function logIn(email: string, password: string, code?: string): Promise<{ twoFactorSetupRequired: boolean }> { - const response = await partnersAxios.post<{ token: string; twoFactorSetupRequired: boolean }>('/partners/login', { - email, - password, - code, - }); + const response = await captchaService.withCaptcha('Login', (headers) => + partnersAxios.post<{ token: string; twoFactorSetupRequired: boolean }>( + '/partners/login', + { email, password, code }, + { headers }, + ), + ); setToken(response.data.token); return { twoFactorSetupRequired: response.data.twoFactorSetupRequired }; } diff --git a/src/services/captcha.service.ts b/src/services/captcha.service.ts index f930e0bc..2b4c561b 100644 --- a/src/services/captcha.service.ts +++ b/src/services/captcha.service.ts @@ -106,8 +106,20 @@ function isCaptchaRejection(err: unknown): boolean { ); } +async function withCaptcha(action: string, request: (headers: Record) => Promise): Promise { + const headers = await tryGetHeaders(action); + + try { + return await request(headers); + } catch (err) { + if (isCaptchaRejection(err)) throw new CaptchaUnavailableError(); + throw err; + } +} + export const captchaService = { getHeaders, tryGetHeaders, isCaptchaRejection, + withCaptcha, }; diff --git a/src/services/login-captcha.test.ts b/src/services/login-captcha.test.ts new file mode 100644 index 00000000..47704adf --- /dev/null +++ b/src/services/login-captcha.test.ts @@ -0,0 +1,49 @@ +import { describe, expect, test } from 'vitest'; +import managementAxios from '../management/core/management-axios'; +import partnersAxios from '../partners/core/partners-axios'; +import subAccountAxios from '../sub-account/core/sub-account-axios'; +import wholesalersAxios from '../wholesalers/core/wholesalers-axios'; +import { managementAuthService } from '../management/services/management-auth.service'; +import { partnersAuthService } from '../partners/services/partners-auth.service'; +import { subAccountAuthService } from '../sub-account/services/sub-account-auth.service'; +import { wholesalersAuthService } from '../wholesalers/services/wholesalers-auth.service'; +import { mockAdapter } from '../testing/mock-axios-adapter'; +import { stubLocation } from '../testing/stub-location'; +import { CaptchaUnavailableError, captchaService } from './captcha.service'; + +describe('console logins', () => { + stubLocation('/login'); + + describe.each([ + ['management', managementAxios, managementAuthService], + ['partners', partnersAxios, partnersAuthService], + ['sub-account', subAccountAxios, subAccountAuthService], + ['wholesalers', wholesalersAxios, wholesalersAuthService], + ])('when the %s backend rejects the captcha', (_, axiosInstance, authService) => { + test('then logIn fails with a CaptchaUnavailableError', async () => { + axiosInstance.defaults.adapter = mockAdapter(() => ({ status: 403, data: { code: 'CAPTCHA_FAILED' } })); + + const error = await authService.logIn('user@internxt.com', 'secret').catch((e: unknown) => e); + + expect(error).toBeInstanceOf(CaptchaUnavailableError); + }); + }); +}); + +describe('captchaService.withCaptcha', () => { + test('when the widget is not available, then the request still runs without the captcha header', async () => { + const result = await captchaService.withCaptcha('Login', async (headers) => headers); + + expect(result).toEqual({}); + }); + + test('when the request fails for another reason, then the error is rethrown untouched', async () => { + const failure = new Error('boom'); + + await expect( + captchaService.withCaptcha('Login', async () => { + throw failure; + }), + ).rejects.toBe(failure); + }); +}); diff --git a/src/sub-account/services/sub-account-auth.service.ts b/src/sub-account/services/sub-account-auth.service.ts index 9b02c36f..5bf57a7d 100644 --- a/src/sub-account/services/sub-account-auth.service.ts +++ b/src/sub-account/services/sub-account-auth.service.ts @@ -18,7 +18,9 @@ function removeToken(): void { } async function logIn(email: string, password: string, code?: string): Promise { - const response = await subAccountAxios.post<{ token: string }>('/subaccount/login', { email, password, code }); + const response = await captchaService.withCaptcha('Login', (headers) => + subAccountAxios.post<{ token: string }>('/subaccount/login', { email, password, code }, { headers }), + ); setToken(response.data.token); localStorage.setItem(EMAIL_KEY, email); } diff --git a/src/wholesalers/services/wholesalers-auth.service.ts b/src/wholesalers/services/wholesalers-auth.service.ts index ec5f42df..b9babdd6 100644 --- a/src/wholesalers/services/wholesalers-auth.service.ts +++ b/src/wholesalers/services/wholesalers-auth.service.ts @@ -1,4 +1,5 @@ import wholesalersAxios from '../core/wholesalers-axios'; +import { captchaService } from '../../services/captcha.service'; const WHOLESALERS_TOKEN_KEY = 'wholesalersToken'; @@ -15,9 +16,12 @@ function removeToken(): void { } async function logIn(email: string, password: string, code?: string): Promise<{ twoFactorSetupRequired: boolean }> { - const response = await wholesalersAxios.post<{ token: string; twoFactorSetupRequired?: boolean }>( - '/wholesalers/login', - { email, password, code }, + const response = await captchaService.withCaptcha('Login', (headers) => + wholesalersAxios.post<{ token: string; twoFactorSetupRequired?: boolean }>( + '/wholesalers/login', + { email, password, code }, + { headers }, + ), ); setToken(response.data.token); return { twoFactorSetupRequired: !!response.data.twoFactorSetupRequired }; From b796951d355e2ebbc1eecc4027d6a0afc87b892b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Guti=C3=A9rrez?= Date: Mon, 23 Nov 2026 11:42:55 +0100 Subject: [PATCH 2/3] refactor: limit Turnstile on login to the management console --- .../services/partners-auth.service.ts | 12 ++++---- src/services/login-captcha.test.ts | 28 +++++++------------ .../services/sub-account-auth.service.ts | 4 +-- .../services/wholesalers-auth.service.ts | 10 ++----- 4 files changed, 19 insertions(+), 35 deletions(-) diff --git a/src/partners/services/partners-auth.service.ts b/src/partners/services/partners-auth.service.ts index a22d405d..72d5eacf 100644 --- a/src/partners/services/partners-auth.service.ts +++ b/src/partners/services/partners-auth.service.ts @@ -16,13 +16,11 @@ function removeToken(): void { } async function logIn(email: string, password: string, code?: string): Promise<{ twoFactorSetupRequired: boolean }> { - const response = await captchaService.withCaptcha('Login', (headers) => - partnersAxios.post<{ token: string; twoFactorSetupRequired: boolean }>( - '/partners/login', - { email, password, code }, - { headers }, - ), - ); + const response = await partnersAxios.post<{ token: string; twoFactorSetupRequired: boolean }>('/partners/login', { + email, + password, + code, + }); setToken(response.data.token); return { twoFactorSetupRequired: response.data.twoFactorSetupRequired }; } diff --git a/src/services/login-captcha.test.ts b/src/services/login-captcha.test.ts index 47704adf..ba736d8b 100644 --- a/src/services/login-captcha.test.ts +++ b/src/services/login-captcha.test.ts @@ -1,12 +1,6 @@ import { describe, expect, test } from 'vitest'; import managementAxios from '../management/core/management-axios'; -import partnersAxios from '../partners/core/partners-axios'; -import subAccountAxios from '../sub-account/core/sub-account-axios'; -import wholesalersAxios from '../wholesalers/core/wholesalers-axios'; import { managementAuthService } from '../management/services/management-auth.service'; -import { partnersAuthService } from '../partners/services/partners-auth.service'; -import { subAccountAuthService } from '../sub-account/services/sub-account-auth.service'; -import { wholesalersAuthService } from '../wholesalers/services/wholesalers-auth.service'; import { mockAdapter } from '../testing/mock-axios-adapter'; import { stubLocation } from '../testing/stub-location'; import { CaptchaUnavailableError, captchaService } from './captcha.service'; @@ -14,20 +8,18 @@ import { CaptchaUnavailableError, captchaService } from './captcha.service'; describe('console logins', () => { stubLocation('/login'); - describe.each([ - ['management', managementAxios, managementAuthService], - ['partners', partnersAxios, partnersAuthService], - ['sub-account', subAccountAxios, subAccountAuthService], - ['wholesalers', wholesalersAxios, wholesalersAuthService], - ])('when the %s backend rejects the captcha', (_, axiosInstance, authService) => { - test('then logIn fails with a CaptchaUnavailableError', async () => { - axiosInstance.defaults.adapter = mockAdapter(() => ({ status: 403, data: { code: 'CAPTCHA_FAILED' } })); + describe.each([['management', managementAxios, managementAuthService]])( + 'when the %s backend rejects the captcha', + (_, axiosInstance, authService) => { + test('then logIn fails with a CaptchaUnavailableError', async () => { + axiosInstance.defaults.adapter = mockAdapter(() => ({ status: 403, data: { code: 'CAPTCHA_FAILED' } })); - const error = await authService.logIn('user@internxt.com', 'secret').catch((e: unknown) => e); + const error = await authService.logIn('user@internxt.com', 'secret').catch((e: unknown) => e); - expect(error).toBeInstanceOf(CaptchaUnavailableError); - }); - }); + expect(error).toBeInstanceOf(CaptchaUnavailableError); + }); + }, + ); }); describe('captchaService.withCaptcha', () => { diff --git a/src/sub-account/services/sub-account-auth.service.ts b/src/sub-account/services/sub-account-auth.service.ts index 5bf57a7d..9b02c36f 100644 --- a/src/sub-account/services/sub-account-auth.service.ts +++ b/src/sub-account/services/sub-account-auth.service.ts @@ -18,9 +18,7 @@ function removeToken(): void { } async function logIn(email: string, password: string, code?: string): Promise { - const response = await captchaService.withCaptcha('Login', (headers) => - subAccountAxios.post<{ token: string }>('/subaccount/login', { email, password, code }, { headers }), - ); + const response = await subAccountAxios.post<{ token: string }>('/subaccount/login', { email, password, code }); setToken(response.data.token); localStorage.setItem(EMAIL_KEY, email); } diff --git a/src/wholesalers/services/wholesalers-auth.service.ts b/src/wholesalers/services/wholesalers-auth.service.ts index b9babdd6..ec5f42df 100644 --- a/src/wholesalers/services/wholesalers-auth.service.ts +++ b/src/wholesalers/services/wholesalers-auth.service.ts @@ -1,5 +1,4 @@ import wholesalersAxios from '../core/wholesalers-axios'; -import { captchaService } from '../../services/captcha.service'; const WHOLESALERS_TOKEN_KEY = 'wholesalersToken'; @@ -16,12 +15,9 @@ function removeToken(): void { } async function logIn(email: string, password: string, code?: string): Promise<{ twoFactorSetupRequired: boolean }> { - const response = await captchaService.withCaptcha('Login', (headers) => - wholesalersAxios.post<{ token: string; twoFactorSetupRequired?: boolean }>( - '/wholesalers/login', - { email, password, code }, - { headers }, - ), + const response = await wholesalersAxios.post<{ token: string; twoFactorSetupRequired?: boolean }>( + '/wholesalers/login', + { email, password, code }, ); setToken(response.data.token); return { twoFactorSetupRequired: !!response.data.twoFactorSetupRequired }; From a9333cad6e187d589091d57a2bb1829eb26a8cb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Guti=C3=A9rrez?= Date: Mon, 23 Nov 2026 11:43:10 +0100 Subject: [PATCH 3/3] test: simplify the management login captcha test --- src/services/login-captcha.test.ts | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/src/services/login-captcha.test.ts b/src/services/login-captcha.test.ts index ba736d8b..b6ec821c 100644 --- a/src/services/login-captcha.test.ts +++ b/src/services/login-captcha.test.ts @@ -5,21 +5,16 @@ import { mockAdapter } from '../testing/mock-axios-adapter'; import { stubLocation } from '../testing/stub-location'; import { CaptchaUnavailableError, captchaService } from './captcha.service'; -describe('console logins', () => { - stubLocation('/login'); +describe('management login', () => { + stubLocation('/management/login'); - describe.each([['management', managementAxios, managementAuthService]])( - 'when the %s backend rejects the captcha', - (_, axiosInstance, authService) => { - test('then logIn fails with a CaptchaUnavailableError', async () => { - axiosInstance.defaults.adapter = mockAdapter(() => ({ status: 403, data: { code: 'CAPTCHA_FAILED' } })); + test('when the backend rejects the captcha, then logIn fails with a CaptchaUnavailableError', async () => { + managementAxios.defaults.adapter = mockAdapter(() => ({ status: 403, data: { code: 'CAPTCHA_FAILED' } })); - const error = await authService.logIn('user@internxt.com', 'secret').catch((e: unknown) => e); + const error = await managementAuthService.logIn('user@internxt.com', 'secret').catch((e: unknown) => e); - expect(error).toBeInstanceOf(CaptchaUnavailableError); - }); - }, - ); + expect(error).toBeInstanceOf(CaptchaUnavailableError); + }); }); describe('captchaService.withCaptcha', () => {