@midwayjs/captcha 3.6.1
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 +10 -0
- package/dist/config/config.default.d.ts +3 -0
- package/dist/config/config.default.js +17 -0
- package/dist/configuration.d.ts +3 -0
- package/dist/configuration.js +27 -0
- package/dist/constants.d.ts +3 -0
- package/dist/constants.js +7 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +22 -0
- package/dist/interface.d.ts +24 -0
- package/dist/interface.js +3 -0
- package/dist/service.d.ts +22 -0
- package/dist/service.js +105 -0
- package/index.d.ts +8 -0
- package/package.json +35 -0
package/README.md
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.captcha = void 0;
|
|
4
|
+
exports.captcha = {
|
|
5
|
+
size: 4,
|
|
6
|
+
noise: 1,
|
|
7
|
+
width: 120,
|
|
8
|
+
height: 40,
|
|
9
|
+
image: {
|
|
10
|
+
type: 'mixed',
|
|
11
|
+
},
|
|
12
|
+
formula: {},
|
|
13
|
+
text: {},
|
|
14
|
+
expirationTime: 3600,
|
|
15
|
+
idPrefix: 'midway:vc',
|
|
16
|
+
};
|
|
17
|
+
//# sourceMappingURL=config.default.js.map
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.CaptchaConfiguration = void 0;
|
|
10
|
+
const core_1 = require("@midwayjs/core");
|
|
11
|
+
const cacheComponent = require("@midwayjs/cache");
|
|
12
|
+
const DefaultConfig = require("./config/config.default");
|
|
13
|
+
let CaptchaConfiguration = class CaptchaConfiguration {
|
|
14
|
+
};
|
|
15
|
+
CaptchaConfiguration = __decorate([
|
|
16
|
+
(0, core_1.Configuration)({
|
|
17
|
+
namespace: 'captcha',
|
|
18
|
+
imports: [cacheComponent],
|
|
19
|
+
importConfigs: [
|
|
20
|
+
{
|
|
21
|
+
default: DefaultConfig,
|
|
22
|
+
},
|
|
23
|
+
],
|
|
24
|
+
})
|
|
25
|
+
], CaptchaConfiguration);
|
|
26
|
+
exports.CaptchaConfiguration = CaptchaConfiguration;
|
|
27
|
+
//# sourceMappingURL=configuration.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.letters = exports.numbers = void 0;
|
|
4
|
+
exports.numbers = '0123456789';
|
|
5
|
+
const lowerCaseLetters = 'abcdefghijklmnopqrstuvwxyz';
|
|
6
|
+
exports.letters = lowerCaseLetters + lowerCaseLetters.toUpperCase();
|
|
7
|
+
//# sourceMappingURL=constants.js.map
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.Configuration = void 0;
|
|
18
|
+
var configuration_1 = require("./configuration");
|
|
19
|
+
Object.defineProperty(exports, "Configuration", { enumerable: true, get: function () { return configuration_1.CaptchaConfiguration; } });
|
|
20
|
+
__exportStar(require("./interface"), exports);
|
|
21
|
+
__exportStar(require("./service"), exports);
|
|
22
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
interface BaseCaptchaOptions {
|
|
2
|
+
size?: number;
|
|
3
|
+
noise?: number;
|
|
4
|
+
width?: number;
|
|
5
|
+
height?: number;
|
|
6
|
+
}
|
|
7
|
+
export interface CaptchaOptions extends BaseCaptchaOptions {
|
|
8
|
+
image?: ImageCaptchaOptions;
|
|
9
|
+
formula?: FormulaCaptchaOptions;
|
|
10
|
+
text?: TextCaptchaOptions;
|
|
11
|
+
expirationTime?: number;
|
|
12
|
+
idPrefix?: string;
|
|
13
|
+
}
|
|
14
|
+
export interface ImageCaptchaOptions extends BaseCaptchaOptions {
|
|
15
|
+
type?: 'number' | 'letter' | 'mixed';
|
|
16
|
+
}
|
|
17
|
+
export interface FormulaCaptchaOptions extends BaseCaptchaOptions {
|
|
18
|
+
}
|
|
19
|
+
export interface TextCaptchaOptions {
|
|
20
|
+
size?: number;
|
|
21
|
+
type?: 'number' | 'letter' | 'mixed';
|
|
22
|
+
}
|
|
23
|
+
export {};
|
|
24
|
+
//# sourceMappingURL=interface.d.ts.map
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { CacheManager } from '@midwayjs/cache';
|
|
2
|
+
import { FormulaCaptchaOptions, ImageCaptchaOptions, TextCaptchaOptions, CaptchaOptions } from './interface';
|
|
3
|
+
export declare class CaptchaService {
|
|
4
|
+
cacheManager: CacheManager;
|
|
5
|
+
captcha: CaptchaOptions;
|
|
6
|
+
image(options?: ImageCaptchaOptions): Promise<{
|
|
7
|
+
id: string;
|
|
8
|
+
imageBase64: string;
|
|
9
|
+
}>;
|
|
10
|
+
formula(options?: FormulaCaptchaOptions): Promise<{
|
|
11
|
+
id: string;
|
|
12
|
+
imageBase64: string;
|
|
13
|
+
}>;
|
|
14
|
+
text(options?: TextCaptchaOptions): Promise<{
|
|
15
|
+
id: string;
|
|
16
|
+
text: string;
|
|
17
|
+
}>;
|
|
18
|
+
set(text: string): Promise<string>;
|
|
19
|
+
check(id: string, value: string): Promise<boolean>;
|
|
20
|
+
private getStoreId;
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=service.d.ts.map
|
package/dist/service.js
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.CaptchaService = void 0;
|
|
13
|
+
const core_1 = require("@midwayjs/core");
|
|
14
|
+
const cache_1 = require("@midwayjs/cache");
|
|
15
|
+
const svgCaptcha = require("svg-captcha");
|
|
16
|
+
const svgBase64 = require("mini-svg-data-uri");
|
|
17
|
+
const nanoid_1 = require("nanoid");
|
|
18
|
+
const constants_1 = require("./constants");
|
|
19
|
+
let CaptchaService = class CaptchaService {
|
|
20
|
+
async image(options) {
|
|
21
|
+
const { width, height, type } = Object.assign({}, this.captcha, this.captcha.image, options);
|
|
22
|
+
let ignoreChars = '';
|
|
23
|
+
switch (type) {
|
|
24
|
+
case 'letter':
|
|
25
|
+
ignoreChars = constants_1.numbers;
|
|
26
|
+
break;
|
|
27
|
+
case 'number':
|
|
28
|
+
ignoreChars = constants_1.letters;
|
|
29
|
+
break;
|
|
30
|
+
}
|
|
31
|
+
const { data, text } = svgCaptcha.create({
|
|
32
|
+
ignoreChars,
|
|
33
|
+
width,
|
|
34
|
+
height,
|
|
35
|
+
});
|
|
36
|
+
const id = await this.set(text);
|
|
37
|
+
const imageBase64 = svgBase64(data);
|
|
38
|
+
return { id, imageBase64 };
|
|
39
|
+
}
|
|
40
|
+
async formula(options) {
|
|
41
|
+
const { width, height } = Object.assign({}, this.captcha, this.captcha.formula, options);
|
|
42
|
+
const { data, text } = svgCaptcha.createMathExpr({
|
|
43
|
+
width,
|
|
44
|
+
height,
|
|
45
|
+
});
|
|
46
|
+
const id = await this.set(text);
|
|
47
|
+
const imageBase64 = svgBase64(data);
|
|
48
|
+
return { id, imageBase64 };
|
|
49
|
+
}
|
|
50
|
+
async text(options) {
|
|
51
|
+
const textOptions = Object.assign({}, this.captcha, this.captcha.text, options);
|
|
52
|
+
let chars = '';
|
|
53
|
+
switch (textOptions.type) {
|
|
54
|
+
case 'letter':
|
|
55
|
+
chars = constants_1.letters;
|
|
56
|
+
break;
|
|
57
|
+
case 'number':
|
|
58
|
+
chars = constants_1.numbers;
|
|
59
|
+
break;
|
|
60
|
+
default:
|
|
61
|
+
chars = constants_1.letters + constants_1.numbers;
|
|
62
|
+
break;
|
|
63
|
+
}
|
|
64
|
+
let text = '';
|
|
65
|
+
while (textOptions.size--) {
|
|
66
|
+
text += chars[Math.floor(Math.random() * chars.length)];
|
|
67
|
+
}
|
|
68
|
+
const id = await this.set(text);
|
|
69
|
+
return { id, text };
|
|
70
|
+
}
|
|
71
|
+
async set(text) {
|
|
72
|
+
const id = (0, nanoid_1.nanoid)();
|
|
73
|
+
await this.cacheManager.set(this.getStoreId(id), (text || '').toLowerCase(), { ttl: this.captcha.expirationTime });
|
|
74
|
+
return id;
|
|
75
|
+
}
|
|
76
|
+
async check(id, value) {
|
|
77
|
+
if (!id || !value) {
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
const storeId = this.getStoreId(id);
|
|
81
|
+
const storedValue = await this.cacheManager.get(storeId);
|
|
82
|
+
if (value.toLowerCase() !== storedValue) {
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
this.cacheManager.del(storeId);
|
|
86
|
+
return true;
|
|
87
|
+
}
|
|
88
|
+
getStoreId(id) {
|
|
89
|
+
return `${this.captcha}:${id}`;
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
__decorate([
|
|
93
|
+
(0, core_1.Inject)(),
|
|
94
|
+
__metadata("design:type", cache_1.CacheManager)
|
|
95
|
+
], CaptchaService.prototype, "cacheManager", void 0);
|
|
96
|
+
__decorate([
|
|
97
|
+
(0, core_1.Config)('captcha'),
|
|
98
|
+
__metadata("design:type", Object)
|
|
99
|
+
], CaptchaService.prototype, "captcha", void 0);
|
|
100
|
+
CaptchaService = __decorate([
|
|
101
|
+
(0, core_1.Provide)(),
|
|
102
|
+
(0, core_1.Scope)(core_1.ScopeEnum.Singleton)
|
|
103
|
+
], CaptchaService);
|
|
104
|
+
exports.CaptchaService = CaptchaService;
|
|
105
|
+
//# sourceMappingURL=service.js.map
|
package/index.d.ts
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@midwayjs/captcha",
|
|
3
|
+
"version": "3.6.1",
|
|
4
|
+
"description": "Midway Component for Captcha(Verification Code)",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"typings": "index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "tsc",
|
|
9
|
+
"test": "node --require=ts-node/register ../../node_modules/.bin/jest --runInBand",
|
|
10
|
+
"cov": "node --require=ts-node/register ../../node_modules/.bin/jest --runInBand --coverage --forceExit",
|
|
11
|
+
"ci": "npm run test"
|
|
12
|
+
},
|
|
13
|
+
"keywords": [],
|
|
14
|
+
"author": "",
|
|
15
|
+
"files": [
|
|
16
|
+
"dist/**/*.js",
|
|
17
|
+
"dist/**/*.d.ts",
|
|
18
|
+
"index.d.ts"
|
|
19
|
+
],
|
|
20
|
+
"engines": {
|
|
21
|
+
"node": ">=12"
|
|
22
|
+
},
|
|
23
|
+
"license": "MIT",
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"mini-svg-data-uri": "1.4.4",
|
|
26
|
+
"nanoid": "3.3.4",
|
|
27
|
+
"svg-captcha": "1.4.0",
|
|
28
|
+
"@midwayjs/cache": "^3.6.0"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"@midwayjs/core": "^3.6.0",
|
|
32
|
+
"@midwayjs/koa": "^3.6.0",
|
|
33
|
+
"@midwayjs/mock": "^3.6.0"
|
|
34
|
+
}
|
|
35
|
+
}
|