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

fix AF-1702
This commit is contained in:
forehalo
2024-11-14 09:41:36 +00:00
parent 6a64055886
commit 9f3dceb220
3 changed files with 29 additions and 3 deletions

View File

@@ -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,
},
});

View File

@@ -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

View File

@@ -1,3 +1,5 @@
import './config';
import { AuthModule } from '../../core/auth';
import { ServerFeature } from '../../core/config';
import { Plugin } from '../registry';