Skip to content

There can be more than Notion and Miro. AFFiNE(pronounced [ə‘fain]) is a next-gen knowledge base that brings planning, sorting and creating all together. Privacy first, open-source, customizable and ready to use.

feat(server): add captcha runtime flag (#8823)

forehalo9f3dceb22041
diff --git a/packages/backend/server/src/plugins/captcha/config.ts b/packages/backend/server/src/plugins/captcha/config.ts
index 1624002ff2..29bd3d8280 100644
--- a/packages/backend/server/src/plugins/captcha/config.ts
+++ b/packages/backend/server/src/plugins/captcha/config.ts
@@ -1,9 +1,18 @@
-import { defineStartupConfig, ModuleConfig } from '../../fundamentals/config';
+import {
+ defineRuntimeConfig,
+ defineStartupConfig,
+ ModuleConfig,
+} from '../../fundamentals/config';
import { CaptchaConfig } from './types';
declare module '../config' {
interface PluginsConfig {
- captcha: ModuleConfig<CaptchaConfig>;
+ captcha: ModuleConfig<
+ CaptchaConfig,
+ {
+ enable: boolean;
+ }
+ >;
}
}
@@ -21,3 +30,10 @@ defineStartupConfig('plugins.captcha', {
bits: 20,
},
});
+
+defineRuntimeConfig('plugins.captcha', {
+ enable: {
+ desc: 'Check captcha challenge when user authenticating the app.',
+ default: false,
+ },
+});
diff --git a/packages/backend/server/src/plugins/captcha/guard.ts b/packages/backend/server/src/plugins/captcha/guard.ts
index b04bf73ca6..cd925486d4 100644
--- a/packages/backend/server/src/plugins/captcha/guard.ts
+++ b/packages/backend/server/src/plugins/captcha/guard.ts
@@ -6,6 +6,7 @@ import type {
import { Injectable } from '@nestjs/common';
import {
+ Config,
getRequestResponseFromContext,
GuardProvider,
} from '../../fundamentals';
@@ -18,11 +19,18 @@ export class CaptchaGuardProvider
{
name = 'captcha' as const;
- constructor(private readonly captcha: CaptchaService) {
+ constructor(
+ private readonly captcha: CaptchaService,
+ private readonly config: Config
+ ) {
super();
}
async canActivate(context: ExecutionContext) {
+ if (!(await this.config.runtime.fetch('plugins.captcha/enable'))) {
+ return true;
+ }
+
const { req } = getRequestResponseFromContext(context);
// require headers, old client send through query string
diff --git a/packages/backend/server/src/plugins/captcha/index.ts b/packages/backend/server/src/plugins/captcha/index.ts
index ce24e09168..ac27ad7e2a 100644
--- a/packages/backend/server/src/plugins/captcha/index.ts
+++ b/packages/backend/server/src/plugins/captcha/index.ts
@@ -1,3 +1,5 @@
+import './config';
+
import { AuthModule } from '../../core/auth';
import { ServerFeature } from '../../core/config';
import { Plugin } from '../registry';