payme-integration
Version:
NestJS integration module for Payme payment system
85 lines (84 loc) • 3.46 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);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.PaymeBasicAuthGuard = void 0;
const common_1 = require("@nestjs/common");
const errors_1 = require("../errors");
let PaymeBasicAuthGuard = class PaymeBasicAuthGuard {
constructor(config) {
this.config = config;
}
async canActivate(context) {
const request = context.switchToHttp().getRequest();
const response = context.switchToHttp().getResponse();
const token = this.extractTokenFromHeader(request);
const trans_id = request?.body?.id;
if (!token) {
response.status(200).send({
id: trans_id,
error: errors_1.PaymeError.INVALID_AUTHORIZATION,
});
return false;
}
try {
const decoded = this.decodeToken(token);
if (!decoded) {
response.status(200).send({
id: trans_id,
error: errors_1.PaymeError.INVALID_AUTHORIZATION,
});
return false;
}
const [username, password] = decoded.split(':');
const is_valid_username = this.config.username === username;
const is_valid_password = this.config.password === password;
if (!is_valid_username || !is_valid_password) {
response.status(200).send({
id: trans_id,
error: errors_1.PaymeError.INVALID_AUTHORIZATION,
});
return false;
}
}
catch (error) {
response.status(200).send({
id: trans_id,
error: errors_1.PaymeError.INVALID_AUTHORIZATION,
});
return false;
}
return true;
}
extractTokenFromHeader(request) {
const auth_header = request.headers['authorization'];
if (!auth_header)
return undefined;
const [type, token] = auth_header.split(' ');
return type === 'Basic' ? token : undefined;
}
decodeToken(token) {
try {
return Buffer.from(token, 'base64').toString('utf8');
}
catch (error) {
return undefined;
}
}
};
exports.PaymeBasicAuthGuard = PaymeBasicAuthGuard;
exports.PaymeBasicAuthGuard = PaymeBasicAuthGuard = __decorate([
(0, common_1.Injectable)(),
__param(0, (0, common_1.Inject)('PAYME_GUARD_OPTIONS')),
__metadata("design:paramtypes", [Object])
], PaymeBasicAuthGuard);