@jpbs/common 1.1.3 → 1.1.5
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/build/errors/bad-request-error.js +1 -2
- package/build/errors/custom-error.d.ts +1 -1
- package/build/errors/custom-error.js +2 -2
- package/build/errors/email-error.js +1 -2
- package/build/errors/notauthorized-error.js +1 -2
- package/build/errors/notfound-error.js +1 -2
- package/build/errors/request-validation.js +1 -2
- package/build/middlewares/verifyGatewayToken.js +6 -2
- package/package.json +1 -1
@@ -5,10 +5,9 @@ const http_status_codes_1 = require("http-status-codes");
|
|
5
5
|
const custom_error_1 = require("./custom-error");
|
6
6
|
class BadRequestError extends custom_error_1.CustomError {
|
7
7
|
constructor(message = "Bad request") {
|
8
|
-
super(message);
|
8
|
+
super(message, BadRequestError.prototype);
|
9
9
|
this.message = message;
|
10
10
|
this.statusCode = http_status_codes_1.StatusCodes.BAD_REQUEST;
|
11
|
-
Object.setPrototypeOf(this, BadRequestError.prototype);
|
12
11
|
}
|
13
12
|
serializeErrors() {
|
14
13
|
return [{ message: this.message }];
|
@@ -2,9 +2,9 @@
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.CustomError = void 0;
|
4
4
|
class CustomError extends Error {
|
5
|
-
constructor(message) {
|
5
|
+
constructor(message, prototype) {
|
6
6
|
super(message);
|
7
|
-
Object.setPrototypeOf(this,
|
7
|
+
Object.setPrototypeOf(this, prototype);
|
8
8
|
}
|
9
9
|
}
|
10
10
|
exports.CustomError = CustomError;
|
@@ -5,9 +5,8 @@ const http_status_codes_1 = require("http-status-codes");
|
|
5
5
|
const custom_error_1 = require("./custom-error");
|
6
6
|
class EmailError extends custom_error_1.CustomError {
|
7
7
|
constructor(message) {
|
8
|
-
super(message);
|
8
|
+
super(message, EmailError.prototype);
|
9
9
|
this.statusCode = http_status_codes_1.StatusCodes.INTERNAL_SERVER_ERROR;
|
10
|
-
Object.setPrototypeOf(this, EmailError.prototype);
|
11
10
|
}
|
12
11
|
serializeErrors() {
|
13
12
|
return [{ message: this.message }];
|
@@ -5,10 +5,9 @@ const http_status_codes_1 = require("http-status-codes");
|
|
5
5
|
const custom_error_1 = require("./custom-error");
|
6
6
|
class NotAuthorizedError extends custom_error_1.CustomError {
|
7
7
|
constructor(message = "Not authorized") {
|
8
|
-
super(message);
|
8
|
+
super(message, NotAuthorizedError.prototype);
|
9
9
|
this.message = message;
|
10
10
|
this.statusCode = http_status_codes_1.StatusCodes.UNAUTHORIZED;
|
11
|
-
Object.setPrototypeOf(this, NotAuthorizedError.prototype);
|
12
11
|
}
|
13
12
|
serializeErrors() {
|
14
13
|
return [{ message: this.message }];
|
@@ -5,10 +5,9 @@ const http_status_codes_1 = require("http-status-codes");
|
|
5
5
|
const custom_error_1 = require("./custom-error");
|
6
6
|
class NotFoundError extends custom_error_1.CustomError {
|
7
7
|
constructor(message = "Resource not found") {
|
8
|
-
super(message);
|
8
|
+
super(message, NotFoundError.prototype);
|
9
9
|
this.message = message;
|
10
10
|
this.statusCode = http_status_codes_1.StatusCodes.NOT_FOUND;
|
11
|
-
Object.setPrototypeOf(this, NotFoundError.prototype);
|
12
11
|
}
|
13
12
|
serializeErrors() {
|
14
13
|
return [{ message: this.message }];
|
@@ -5,10 +5,9 @@ const custom_error_1 = require("./custom-error");
|
|
5
5
|
const http_status_codes_1 = require("http-status-codes");
|
6
6
|
class RequestValidationError extends custom_error_1.CustomError {
|
7
7
|
constructor(errors) {
|
8
|
-
super('Invalid request parameters');
|
8
|
+
super('Invalid request parameters', RequestValidationError.prototype);
|
9
9
|
this.errors = errors;
|
10
10
|
this.statusCode = http_status_codes_1.StatusCodes.BAD_REQUEST;
|
11
|
-
Object.setPrototypeOf(this, RequestValidationError.prototype);
|
12
11
|
}
|
13
12
|
serializeErrors() {
|
14
13
|
return this.errors.map(err => {
|
@@ -7,7 +7,8 @@ exports.verifyGatewayToken = void 0;
|
|
7
7
|
const crypto_1 = __importDefault(require("crypto"));
|
8
8
|
const notauthorized_error_1 = require("../errors/notauthorized-error");
|
9
9
|
const verifySignature = (data, secret, timestamp, receivedSignature) => {
|
10
|
-
const
|
10
|
+
const expectedData = data && data.length > 0 ? data : '';
|
11
|
+
const expectedSignature = crypto_1.default.createHmac("sha256", secret).update(`${expectedData}.${timestamp}`).digest("hex");
|
11
12
|
return expectedSignature === receivedSignature;
|
12
13
|
};
|
13
14
|
const verifyGatewayToken = (gatewaySecret) => (req, _res, next) => {
|
@@ -21,7 +22,10 @@ const verifyGatewayToken = (gatewaySecret) => (req, _res, next) => {
|
|
21
22
|
if (Math.abs(currentTime - requestTime) > 2 * 60 * 1000) {
|
22
23
|
throw new notauthorized_error_1.NotAuthorizedError("Unauthorized request source: Expired Request");
|
23
24
|
}
|
24
|
-
|
25
|
+
const serializedData = req.method.toUpperCase() !== 'GET' && req.body && Object.keys(req.body).length > 0
|
26
|
+
? JSON.stringify(req.body)
|
27
|
+
: '';
|
28
|
+
if (!verifySignature(serializedData, gatewaySecret, timestamp, signature)) {
|
25
29
|
throw new notauthorized_error_1.NotAuthorizedError("Unauthorized request source: Invalid Signature");
|
26
30
|
}
|
27
31
|
next();
|