@midwayjs/captcha 4.0.0-alpha.1 → 4.0.0-beta.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/interface.d.ts +8 -12
- package/dist/service.d.ts +6 -6
- package/dist/service.js +41 -37
- package/package.json +9 -9
package/README.md
CHANGED
package/dist/interface.d.ts
CHANGED
|
@@ -1,25 +1,21 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
height?: number;
|
|
1
|
+
import { ConfigObject } from 'svg-captcha-fixed';
|
|
2
|
+
export interface CaptchaCacheOptions {
|
|
3
|
+
expirationTime?: number;
|
|
4
|
+
idPrefix?: string;
|
|
6
5
|
}
|
|
7
|
-
export interface CaptchaOptions extends
|
|
8
|
-
default?:
|
|
6
|
+
export interface CaptchaOptions extends CaptchaCacheOptions {
|
|
7
|
+
default?: ConfigObject;
|
|
9
8
|
image?: ImageCaptchaOptions;
|
|
10
9
|
formula?: FormulaCaptchaOptions;
|
|
11
10
|
text?: TextCaptchaOptions;
|
|
12
|
-
expirationTime?: number;
|
|
13
|
-
idPrefix?: string;
|
|
14
11
|
}
|
|
15
|
-
export interface ImageCaptchaOptions extends
|
|
12
|
+
export interface ImageCaptchaOptions extends ConfigObject {
|
|
16
13
|
type?: 'number' | 'letter' | 'mixed';
|
|
17
14
|
}
|
|
18
|
-
export interface FormulaCaptchaOptions extends
|
|
15
|
+
export interface FormulaCaptchaOptions extends ConfigObject {
|
|
19
16
|
}
|
|
20
17
|
export interface TextCaptchaOptions {
|
|
21
18
|
size?: number;
|
|
22
19
|
type?: 'number' | 'letter' | 'mixed';
|
|
23
20
|
}
|
|
24
|
-
export {};
|
|
25
21
|
//# sourceMappingURL=interface.d.ts.map
|
package/dist/service.d.ts
CHANGED
|
@@ -1,22 +1,22 @@
|
|
|
1
1
|
import { MidwayCache } from '@midwayjs/cache-manager';
|
|
2
|
-
import { FormulaCaptchaOptions, ImageCaptchaOptions, TextCaptchaOptions, CaptchaOptions } from './interface';
|
|
2
|
+
import { FormulaCaptchaOptions, ImageCaptchaOptions, TextCaptchaOptions, CaptchaOptions, CaptchaCacheOptions } from './interface';
|
|
3
3
|
export declare class CaptchaService {
|
|
4
4
|
protected captchaCaching: MidwayCache;
|
|
5
5
|
protected captcha: CaptchaOptions;
|
|
6
|
-
image(options?: ImageCaptchaOptions): Promise<{
|
|
6
|
+
image(options?: ImageCaptchaOptions, cacheOption?: CaptchaCacheOptions): Promise<{
|
|
7
7
|
id: string;
|
|
8
8
|
imageBase64: string;
|
|
9
9
|
}>;
|
|
10
|
-
formula(options?: FormulaCaptchaOptions): Promise<{
|
|
10
|
+
formula(options?: FormulaCaptchaOptions, cacheOption?: CaptchaCacheOptions): Promise<{
|
|
11
11
|
id: string;
|
|
12
12
|
imageBase64: string;
|
|
13
13
|
}>;
|
|
14
|
-
text(options?: TextCaptchaOptions): Promise<{
|
|
14
|
+
text(options?: TextCaptchaOptions, cacheOption?: CaptchaCacheOptions): Promise<{
|
|
15
15
|
id: string;
|
|
16
16
|
text: string;
|
|
17
17
|
}>;
|
|
18
|
-
set(text: string): Promise<string>;
|
|
19
|
-
check(id: string, value: string): Promise<boolean>;
|
|
18
|
+
set(text: string, cacheOptions?: CaptchaCacheOptions): Promise<string>;
|
|
19
|
+
check(id: string, value: string, cacheOptions?: CaptchaCacheOptions): Promise<boolean>;
|
|
20
20
|
private getStoreId;
|
|
21
21
|
}
|
|
22
22
|
//# sourceMappingURL=service.d.ts.map
|
package/dist/service.js
CHANGED
|
@@ -12,48 +12,51 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
12
12
|
exports.CaptchaService = void 0;
|
|
13
13
|
const core_1 = require("@midwayjs/core");
|
|
14
14
|
const cache_manager_1 = require("@midwayjs/cache-manager");
|
|
15
|
-
const svgCaptcha = require("svg-captcha");
|
|
15
|
+
const svgCaptcha = require("svg-captcha-fixed");
|
|
16
16
|
const svgBase64 = require("mini-svg-data-uri");
|
|
17
17
|
const nanoid_1 = require("nanoid");
|
|
18
18
|
const constants_1 = require("./constants");
|
|
19
|
+
const DEFAULT_IMAGE_IGNORE_CHARS = {
|
|
20
|
+
letter: constants_1.numbers,
|
|
21
|
+
number: constants_1.letters,
|
|
22
|
+
};
|
|
19
23
|
let CaptchaService = class CaptchaService {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
break;
|
|
30
|
-
}
|
|
24
|
+
captchaCaching;
|
|
25
|
+
captcha;
|
|
26
|
+
async image(options, cacheOption) {
|
|
27
|
+
// const { expirationTime, idPrefix } = this.captcha;
|
|
28
|
+
const { type, ...others } = {
|
|
29
|
+
...this.captcha.default,
|
|
30
|
+
...this.captcha.image,
|
|
31
|
+
...options,
|
|
32
|
+
};
|
|
31
33
|
const { data, text } = svgCaptcha.create({
|
|
32
|
-
ignoreChars,
|
|
33
|
-
|
|
34
|
-
height,
|
|
35
|
-
size,
|
|
36
|
-
noise,
|
|
34
|
+
ignoreChars: DEFAULT_IMAGE_IGNORE_CHARS[type] ?? '',
|
|
35
|
+
...others,
|
|
37
36
|
});
|
|
38
|
-
const id = await this.set(text);
|
|
37
|
+
const id = await this.set(text, cacheOption);
|
|
39
38
|
const imageBase64 = svgBase64(data);
|
|
40
39
|
return { id, imageBase64 };
|
|
41
40
|
}
|
|
42
|
-
async formula(options) {
|
|
43
|
-
const
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
});
|
|
49
|
-
const id = await this.set(text);
|
|
41
|
+
async formula(options, cacheOption) {
|
|
42
|
+
const formulaCaptchaOptions = {
|
|
43
|
+
...this.captcha.default,
|
|
44
|
+
...this.captcha.formula,
|
|
45
|
+
...options,
|
|
46
|
+
};
|
|
47
|
+
const { data, text } = svgCaptcha.createMathExpr(formulaCaptchaOptions);
|
|
48
|
+
const id = await this.set(text, cacheOption);
|
|
50
49
|
const imageBase64 = svgBase64(data);
|
|
51
50
|
return { id, imageBase64 };
|
|
52
51
|
}
|
|
53
|
-
async text(options) {
|
|
54
|
-
const
|
|
52
|
+
async text(options, cacheOption) {
|
|
53
|
+
const { type, ...textOptions } = {
|
|
54
|
+
...this.captcha.default,
|
|
55
|
+
...this.captcha.text,
|
|
56
|
+
...options,
|
|
57
|
+
};
|
|
55
58
|
let chars = '';
|
|
56
|
-
switch (
|
|
59
|
+
switch (type) {
|
|
57
60
|
case 'letter':
|
|
58
61
|
chars = constants_1.letters;
|
|
59
62
|
break;
|
|
@@ -68,19 +71,19 @@ let CaptchaService = class CaptchaService {
|
|
|
68
71
|
while (textOptions.size--) {
|
|
69
72
|
text += chars[Math.floor(Math.random() * chars.length)];
|
|
70
73
|
}
|
|
71
|
-
const id = await this.set(text);
|
|
74
|
+
const id = await this.set(text, cacheOption);
|
|
72
75
|
return { id, text };
|
|
73
76
|
}
|
|
74
|
-
async set(text) {
|
|
77
|
+
async set(text, cacheOptions) {
|
|
75
78
|
const id = (0, nanoid_1.nanoid)();
|
|
76
|
-
await this.captchaCaching.set(this.getStoreId(id), (text || '').toLowerCase(), this.captcha.expirationTime * 1000);
|
|
79
|
+
await this.captchaCaching.set(this.getStoreId(id, cacheOptions), (text || '').toLowerCase(), (cacheOptions?.expirationTime ?? this.captcha.expirationTime) * 1000);
|
|
77
80
|
return id;
|
|
78
81
|
}
|
|
79
|
-
async check(id, value) {
|
|
82
|
+
async check(id, value, cacheOptions) {
|
|
80
83
|
if (!id || !value) {
|
|
81
84
|
return false;
|
|
82
85
|
}
|
|
83
|
-
const storeId = this.getStoreId(id);
|
|
86
|
+
const storeId = this.getStoreId(id, cacheOptions);
|
|
84
87
|
const storedValue = await this.captchaCaching.get(storeId);
|
|
85
88
|
if (value.toLowerCase() !== storedValue) {
|
|
86
89
|
return false;
|
|
@@ -88,11 +91,12 @@ let CaptchaService = class CaptchaService {
|
|
|
88
91
|
await this.captchaCaching.del(storeId);
|
|
89
92
|
return true;
|
|
90
93
|
}
|
|
91
|
-
getStoreId(id) {
|
|
92
|
-
|
|
94
|
+
getStoreId(id, cacheOptions) {
|
|
95
|
+
const idPrefix = cacheOptions?.idPrefix ?? this.captcha.idPrefix;
|
|
96
|
+
if (!idPrefix) {
|
|
93
97
|
return id;
|
|
94
98
|
}
|
|
95
|
-
return `${
|
|
99
|
+
return `${idPrefix}:${id}`;
|
|
96
100
|
}
|
|
97
101
|
};
|
|
98
102
|
exports.CaptchaService = CaptchaService;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@midwayjs/captcha",
|
|
3
|
-
"version": "4.0.0-
|
|
3
|
+
"version": "4.0.0-beta.10",
|
|
4
4
|
"description": "Midway Component for Captcha(Verification Code)",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"typings": "index.d.ts",
|
|
@@ -18,19 +18,19 @@
|
|
|
18
18
|
"index.d.ts"
|
|
19
19
|
],
|
|
20
20
|
"engines": {
|
|
21
|
-
"node": ">=
|
|
21
|
+
"node": ">=20"
|
|
22
22
|
},
|
|
23
23
|
"license": "MIT",
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@midwayjs/cache-manager": "^4.0.0-
|
|
25
|
+
"@midwayjs/cache-manager": "^4.0.0-beta.10",
|
|
26
26
|
"mini-svg-data-uri": "1.4.4",
|
|
27
|
-
"nanoid": "3.3.
|
|
28
|
-
"svg-captcha": "1.
|
|
27
|
+
"nanoid": "3.3.11",
|
|
28
|
+
"svg-captcha-fixed": "1.5.2"
|
|
29
29
|
},
|
|
30
30
|
"devDependencies": {
|
|
31
|
-
"@midwayjs/core": "^4.0.0-
|
|
32
|
-
"@midwayjs/koa": "^4.0.0-
|
|
33
|
-
"@midwayjs/mock": "^4.0.0-
|
|
31
|
+
"@midwayjs/core": "^4.0.0-beta.10",
|
|
32
|
+
"@midwayjs/koa": "^4.0.0-beta.10",
|
|
33
|
+
"@midwayjs/mock": "^4.0.0-beta.10"
|
|
34
34
|
},
|
|
35
|
-
"gitHead": "
|
|
35
|
+
"gitHead": "1b1856629913703f67304155aaf611ec936a81ac"
|
|
36
36
|
}
|