@midwayjs/captcha
Advanced tools
Comparing version 3.19.2 to 3.19.3
@@ -1,19 +0,16 @@ | ||
interface BaseCaptchaOptions { | ||
size?: number; | ||
noise?: number; | ||
width?: number; | ||
height?: number; | ||
import { ConfigObject } from 'svg-captcha-fixed'; | ||
export interface CaptchaCacheOptions { | ||
expirationTime?: number; | ||
idPrefix?: string; | ||
} | ||
export interface CaptchaOptions extends BaseCaptchaOptions { | ||
default?: BaseCaptchaOptions; | ||
export interface CaptchaOptions extends CaptchaCacheOptions { | ||
default?: ConfigObject; | ||
image?: ImageCaptchaOptions; | ||
formula?: FormulaCaptchaOptions; | ||
text?: TextCaptchaOptions; | ||
expirationTime?: number; | ||
idPrefix?: string; | ||
} | ||
export interface ImageCaptchaOptions extends BaseCaptchaOptions { | ||
export interface ImageCaptchaOptions extends ConfigObject { | ||
type?: 'number' | 'letter' | 'mixed'; | ||
} | ||
export interface FormulaCaptchaOptions extends BaseCaptchaOptions { | ||
export interface FormulaCaptchaOptions extends ConfigObject { | ||
} | ||
@@ -24,3 +21,2 @@ export interface TextCaptchaOptions { | ||
} | ||
export {}; | ||
//# sourceMappingURL=interface.d.ts.map |
import { MidwayCache } from '@midwayjs/cache-manager'; | ||
import { FormulaCaptchaOptions, ImageCaptchaOptions, TextCaptchaOptions, CaptchaOptions } from './interface'; | ||
import { FormulaCaptchaOptions, ImageCaptchaOptions, TextCaptchaOptions, CaptchaOptions, CaptchaCacheOptions } from './interface'; | ||
export declare class CaptchaService { | ||
protected captchaCaching: MidwayCache; | ||
protected captcha: CaptchaOptions; | ||
image(options?: ImageCaptchaOptions): Promise<{ | ||
image(options?: ImageCaptchaOptions, cacheOption?: CaptchaCacheOptions): Promise<{ | ||
id: string; | ||
imageBase64: string; | ||
}>; | ||
formula(options?: FormulaCaptchaOptions): Promise<{ | ||
formula(options?: FormulaCaptchaOptions, cacheOption?: CaptchaCacheOptions): Promise<{ | ||
id: string; | ||
imageBase64: string; | ||
}>; | ||
text(options?: TextCaptchaOptions): Promise<{ | ||
text(options?: TextCaptchaOptions, cacheOption?: CaptchaCacheOptions): Promise<{ | ||
id: string; | ||
text: string; | ||
}>; | ||
set(text: string): Promise<string>; | ||
check(id: string, value: string): Promise<boolean>; | ||
set(text: string, cacheOptions?: CaptchaCacheOptions): Promise<string>; | ||
check(id: string, value: string, cacheOptions?: CaptchaCacheOptions): Promise<boolean>; | ||
private getStoreId; | ||
} | ||
//# sourceMappingURL=service.d.ts.map |
@@ -15,44 +15,46 @@ "use strict"; | ||
const cache_manager_1 = require("@midwayjs/cache-manager"); | ||
const svgCaptcha = require("svg-captcha"); | ||
const svgCaptcha = require("svg-captcha-fixed"); | ||
const svgBase64 = require("mini-svg-data-uri"); | ||
const nanoid_1 = require("nanoid"); | ||
const constants_1 = require("./constants"); | ||
const DEFAULT_IMAGE_IGNORE_CHARS = { | ||
letter: constants_1.numbers, | ||
number: constants_1.letters, | ||
}; | ||
let CaptchaService = class CaptchaService { | ||
async image(options) { | ||
const { width, height, type, size, noise } = Object.assign({}, this.captcha, this.captcha.default, this.captcha.image, options); | ||
let ignoreChars = ''; | ||
switch (type) { | ||
case 'letter': | ||
ignoreChars = constants_1.numbers; | ||
break; | ||
case 'number': | ||
ignoreChars = constants_1.letters; | ||
break; | ||
} | ||
async image(options, cacheOption) { | ||
var _a; | ||
// const { expirationTime, idPrefix } = this.captcha; | ||
const { type, ...others } = { | ||
...this.captcha.default, | ||
...this.captcha.image, | ||
...options, | ||
}; | ||
const { data, text } = svgCaptcha.create({ | ||
ignoreChars, | ||
width, | ||
height, | ||
size, | ||
noise, | ||
ignoreChars: (_a = DEFAULT_IMAGE_IGNORE_CHARS[type]) !== null && _a !== void 0 ? _a : '', | ||
...others, | ||
}); | ||
const id = await this.set(text); | ||
const id = await this.set(text, cacheOption); | ||
const imageBase64 = svgBase64(data); | ||
return { id, imageBase64 }; | ||
} | ||
async formula(options) { | ||
const { width, height, noise } = Object.assign({}, this.captcha, this.captcha.default, this.captcha.formula, options); | ||
const { data, text } = svgCaptcha.createMathExpr({ | ||
width, | ||
height, | ||
noise, | ||
}); | ||
const id = await this.set(text); | ||
async formula(options, cacheOption) { | ||
const formulaCaptchaOptions = { | ||
...this.captcha.default, | ||
...this.captcha.formula, | ||
...options, | ||
}; | ||
const { data, text } = svgCaptcha.createMathExpr(formulaCaptchaOptions); | ||
const id = await this.set(text, cacheOption); | ||
const imageBase64 = svgBase64(data); | ||
return { id, imageBase64 }; | ||
} | ||
async text(options) { | ||
const textOptions = Object.assign({}, this.captcha, this.captcha.default, this.captcha.text, options); | ||
async text(options, cacheOption) { | ||
const { type, ...textOptions } = { | ||
...this.captcha.default, | ||
...this.captcha.text, | ||
...options, | ||
}; | ||
let chars = ''; | ||
switch (textOptions.type) { | ||
switch (type) { | ||
case 'letter': | ||
@@ -72,15 +74,16 @@ chars = constants_1.letters; | ||
} | ||
const id = await this.set(text); | ||
const id = await this.set(text, cacheOption); | ||
return { id, text }; | ||
} | ||
async set(text) { | ||
async set(text, cacheOptions) { | ||
var _a; | ||
const id = (0, nanoid_1.nanoid)(); | ||
await this.captchaCaching.set(this.getStoreId(id), (text || '').toLowerCase(), this.captcha.expirationTime * 1000); | ||
await this.captchaCaching.set(this.getStoreId(id, cacheOptions), (text || '').toLowerCase(), ((_a = cacheOptions === null || cacheOptions === void 0 ? void 0 : cacheOptions.expirationTime) !== null && _a !== void 0 ? _a : this.captcha.expirationTime) * 1000); | ||
return id; | ||
} | ||
async check(id, value) { | ||
async check(id, value, cacheOptions) { | ||
if (!id || !value) { | ||
return false; | ||
} | ||
const storeId = this.getStoreId(id); | ||
const storeId = this.getStoreId(id, cacheOptions); | ||
const storedValue = await this.captchaCaching.get(storeId); | ||
@@ -93,7 +96,9 @@ if (value.toLowerCase() !== storedValue) { | ||
} | ||
getStoreId(id) { | ||
if (!this.captcha.idPrefix) { | ||
getStoreId(id, cacheOptions) { | ||
var _a; | ||
const idPrefix = (_a = cacheOptions === null || cacheOptions === void 0 ? void 0 : cacheOptions.idPrefix) !== null && _a !== void 0 ? _a : this.captcha.idPrefix; | ||
if (!idPrefix) { | ||
return id; | ||
} | ||
return `${this.captcha.idPrefix}:${id}`; | ||
return `${idPrefix}:${id}`; | ||
} | ||
@@ -100,0 +105,0 @@ }; |
{ | ||
"name": "@midwayjs/captcha", | ||
"version": "3.19.2", | ||
"version": "3.19.3", | ||
"description": "Midway Component for Captcha(Verification Code)", | ||
@@ -25,6 +25,6 @@ "main": "dist/index.js", | ||
"dependencies": { | ||
"@midwayjs/cache-manager": "^3.19.2", | ||
"@midwayjs/cache-manager": "^3.19.3", | ||
"mini-svg-data-uri": "1.4.4", | ||
"nanoid": "3.3.8", | ||
"svg-captcha": "1.4.0" | ||
"svg-captcha-fixed": "1.5.2" | ||
}, | ||
@@ -36,3 +36,3 @@ "devDependencies": { | ||
}, | ||
"gitHead": "57fd034be94897fb819b0d9ef776de0b9923ab0f" | ||
"gitHead": "7c45ca2710e6495d5555419de5b2ab645696c823" | ||
} |
@@ -9,3 +9,3 @@ # midway captcha(verification code) module | ||
[MIT]((http://github.com/midwayjs/midway/blob/master/LICENSE)) | ||
[MIT]((https://github.com/midwayjs/midway/blob/master/LICENSE)) | ||
12780
263
+ Addedsvg-captcha-fixed@1.5.2
+ Addedsvg-captcha-fixed@1.5.2(transitive)
- Removedsvg-captcha@1.4.0
- Removedsvg-captcha@1.4.0(transitive)