Captcha Provider
This section analyzes the core source code of the module a-captchasimple to illustrate how to develop a new Captcha Provider
Creating a Captcha Provider
For example, creating a Captcha Provider: imageText within the module a-captchasimple
1. CLI Command
$ vona :create:bean captchaProvider imageText --module=a-captchasimple2. Menu Command
TIP
Context Menu - [Module Path]: Vona Bean/Captcha Provider
Captcha Provider Definition
export type TypeCaptchaProviderImageTextToken = string;
export type TypeCaptchaProviderImageTextPayload = string;
export type TypeCaptchaProviderImageTextData = ICaptchaProviderData<TypeCaptchaProviderImageTextToken, TypeCaptchaProviderImageTextPayload>;
export type TypeCaptchaProviderImageTextType = 'char' | 'math';
const CaptchaProviderImageTextTypes = ['char', 'math'] as const;
export interface ICaptchaProviderOptionsImageText extends IDecoratorCaptchaProviderOptions {
type?: TypeCaptchaProviderImageTextType;
fontPath?: string;
opts: ConfigObject;
}
@CaptchaProvider<ICaptchaProviderOptionsImageText>({
opts: {
size: 4,
color: true,
},
})
class CaptchaProviderImageText {
async create(options) {
this._confirmFont(options);
let type = options.type;
if (!type) {
type = CaptchaProviderImageTextTypes[getRandomInt(2, 0)];
}
const captcha = type === 'char' ? svgCaptcha.create(options.opts) : svgCaptcha.createMathExpr(options.opts);
return { token: captcha.text, payload: svg64(captcha.data) };
}
async verify(
token,
tokenInput,
_options,
) {
return !!tokenInput && !!token && tokenInput.toLowerCase() === token.toLowerCase();
}
}TypeCaptchaProviderImageTextToken: Token type; different providers may have different token typesTypeCaptchaProviderImageTextPayload: Payload type; different providers may have different payload typesTypeCaptchaProviderImageTextData: Captcha Data type corresponding to the current providerICaptchaProviderOptionsImageText: Parameters of the current provider. Different providers can offer different parameter configurations. For example, this provider implements image-text Captcha capabilities based on svg-captcha, therefore it provides the following parameters required by svg-captcha:type/fontPath/optscreate: Creates captcha dataverify: Verifies the captcha token's correctness
App Config
Captcha Provider parameters can be configured in the App Config
src/backend/config/config/config.ts
// onions
config.onions = {
captchaProvider: {
'a-captchasimple:imageText': {
type: 'char',
opts: {
size: 4,
color: true,
},
},
},
};