plus-midwayjs
Version:
85 lines (84 loc) • 3.62 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
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;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SignatureMiddleware = void 0;
const core_1 = require("@midwayjs/core");
const tag_1 = require("../decorator/tag");
const data_1 = require("../tag/data");
const crypto = require("crypto-js");
let SignatureMiddleware = class SignatureMiddleware {
constructor() {
this.ignoreUrls = [];
}
static getName() {
return 'signature';
}
async init() {
var _a;
this.ignoreUrls = (_a = this.plusUrlTagData.byKey(tag_1.TagTypes.IGNORE_SIGN)) !== null && _a !== void 0 ? _a : [];
}
resolve() {
return async (ctx, next) => {
let { url } = ctx;
url = url.replace(this.prefix, '').split('?')[0];
if (this.ignoreUrls.includes(url)) {
await next();
return;
}
// 合并sign配置
this.signConfig = this.config.module[url.split('/')[1]]
? this.config.module[url.split('/')[1]]['sign']
: this.config.plus.sign;
this.signConfig = Object.assign(this.config.plus.sign, this.signConfig);
if (this.signConfig.enable && ctx.request.method !== 'GET') {
const sign = ctx.headers[this.signConfig.key];
if (!sign) {
throw new core_1.httpError.UnauthorizedError('Signature authentication failed');
}
let newParams = {};
Object.keys(ctx.request.body)
.reverse()
.forEach((key) => {
newParams[key] = ctx.request.body[key];
});
const encParams = crypto
.MD5(crypto.SHA512(JSON.stringify(newParams)).toString() + this.signConfig.salt)
.toString();
if (sign != encParams) {
throw new core_1.httpError.UnauthorizedError('Signature authentication failed');
}
}
await next();
};
}
};
__decorate([
(0, core_1.Config)('koa.globalPrefix'),
__metadata("design:type", String)
], SignatureMiddleware.prototype, "prefix", void 0);
__decorate([
(0, core_1.Inject)(),
__metadata("design:type", data_1.PlusUrlTagData)
], SignatureMiddleware.prototype, "plusUrlTagData", void 0);
__decorate([
(0, core_1.Config)(core_1.ALL),
__metadata("design:type", Object)
], SignatureMiddleware.prototype, "config", void 0);
__decorate([
(0, core_1.Init)(),
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", Promise)
], SignatureMiddleware.prototype, "init", null);
SignatureMiddleware = __decorate([
(0, core_1.Middleware)()
], SignatureMiddleware);
exports.SignatureMiddleware = SignatureMiddleware;