mirror of
https://github.com/fluxerapp/fluxer
synced 2026-10-19 05:55:34 +09:00
feat(captcha): make ALTCHA the only captcha (#3035)
This commit is contained in:
1 parent
d433a039b5
commit
4f968bbc47
209 files changed
+4097
-8692
No files matched your search
@@ -84,7 +84,7 @@ export function OAuth2ApplicationsController(app: HonoApp) {
|
||||
security: ['bearerToken', 'sessionToken'],
|
||||
tags: ['OAuth2'],
|
||||
description:
|
||||
'Creates a new bot-backed OAuth2 application (client). Requires CAPTCHA verification. Returns client credentials including ID and secret. Application can be used for authorization flows and API access.',
|
||||
'Creates a new bot-backed OAuth2 application (client). Requires a solved captcha challenge (X-Captcha-Token). Returns client credentials including ID and secret. Application can be used for authorization flows and API access.',
|
||||
}),
|
||||
async (ctx) => {
|
||||
const userId = ctx.get('user').id;
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import {createTestAccount} from '@app/api/auth/tests/AuthTestUtils';
|
||||
import {Config} from '@app/api/Config';
|
||||
import {createOAuth2Application, createUniqueApplicationName} from '@app/api/oauth/tests/OAuth2TestUtils';
|
||||
import {type ApiTestHarness, createApiTestHarness} from '@app/api/test/ApiTestHarness';
|
||||
import {CAPTCHA_TEST_HEADER, issueSolvedCaptchaToken, useCheapCaptcha} from '@app/api/test/CaptchaTestUtils';
|
||||
import {HTTP_STATUS} from '@app/api/test/TestConstants';
|
||||
import {createBuilder} from '@app/api/test/TestRequestBuilder';
|
||||
import {APIErrorCodes} from '@fluxer/constants/src/ApiErrorCodes';
|
||||
@@ -18,19 +18,6 @@ interface ValidationErrorResponse {
|
||||
}>;
|
||||
}
|
||||
|
||||
async function withCaptchaEnabled<T>(run: () => Promise<T>): Promise<T> {
|
||||
const previousEnabled = Config.captcha.enabled;
|
||||
const previousTestModeEnabled = Config.dev.testModeEnabled;
|
||||
Config.captcha.enabled = true;
|
||||
Config.dev.testModeEnabled = true;
|
||||
try {
|
||||
return await run();
|
||||
} finally {
|
||||
Config.captcha.enabled = previousEnabled;
|
||||
Config.dev.testModeEnabled = previousTestModeEnabled;
|
||||
}
|
||||
}
|
||||
|
||||
describe('OAuth2 Application Create', () => {
|
||||
let harness: ApiTestHarness;
|
||||
beforeEach(async () => {
|
||||
@@ -94,24 +81,24 @@ describe('OAuth2 Application Create', () => {
|
||||
});
|
||||
test('requires captcha when creating a bot application', async () => {
|
||||
const account = await createTestAccount(harness);
|
||||
await withCaptchaEnabled(async () =>
|
||||
createBuilder(harness, account.token)
|
||||
.post('/oauth2/applications')
|
||||
.body({name: createUniqueApplicationName()})
|
||||
.expect(HTTP_STATUS.BAD_REQUEST, APIErrorCodes.CAPTCHA_REQUIRED)
|
||||
.execute(),
|
||||
);
|
||||
await useCheapCaptcha();
|
||||
await createBuilder(harness, account.token)
|
||||
.post('/oauth2/applications')
|
||||
.header(CAPTCHA_TEST_HEADER, 'true')
|
||||
.body({name: createUniqueApplicationName()})
|
||||
.expect(HTTP_STATUS.BAD_REQUEST, APIErrorCodes.CAPTCHA_REQUIRED)
|
||||
.execute();
|
||||
});
|
||||
test('creates a bot application with a valid captcha token', async () => {
|
||||
const account = await createTestAccount(harness);
|
||||
await withCaptchaEnabled(async () =>
|
||||
createBuilder(harness, account.token)
|
||||
.post('/oauth2/applications')
|
||||
.header('x-captcha-token', 'test-captcha-token')
|
||||
.body({name: createUniqueApplicationName()})
|
||||
.expect(HTTP_STATUS.OK)
|
||||
.execute(),
|
||||
);
|
||||
await useCheapCaptcha();
|
||||
await createBuilder(harness, account.token)
|
||||
.post('/oauth2/applications')
|
||||
.header(CAPTCHA_TEST_HEADER, 'true')
|
||||
.header('x-captcha-token', await issueSolvedCaptchaToken(harness))
|
||||
.body({name: createUniqueApplicationName()})
|
||||
.expect(HTTP_STATUS.OK)
|
||||
.execute();
|
||||
});
|
||||
test('rejects missing name', async () => {
|
||||
const account = await createTestAccount(harness);
|
||||
|
||||
Reference in new issue
Block a user