Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/components/auth/LoginPageView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down
10 changes: 7 additions & 3 deletions src/management/services/management-auth.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import managementAxios from '../core/management-axios';
import { captchaService } from '../../services/captcha.service';

const MANAGEMENT_TOKEN_KEY = 'managementToken';

Expand All @@ -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 };
Expand Down
12 changes: 12 additions & 0 deletions src/services/captcha.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,20 @@ function isCaptchaRejection(err: unknown): boolean {
);
}

async function withCaptcha<T>(action: string, request: (headers: Record<string, string>) => Promise<T>): Promise<T> {
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,
};
36 changes: 36 additions & 0 deletions src/services/login-captcha.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { describe, expect, test } from 'vitest';
import managementAxios from '../management/core/management-axios';
import { managementAuthService } from '../management/services/management-auth.service';
import { mockAdapter } from '../testing/mock-axios-adapter';
import { stubLocation } from '../testing/stub-location';
import { CaptchaUnavailableError, captchaService } from './captcha.service';

describe('management login', () => {
stubLocation('/management/login');

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 managementAuthService.logIn('[email protected]', '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);
});
});
Loading