@riocrypto/common 1.0.387 → 1.0.388
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/index.d.ts
CHANGED
|
@@ -32,6 +32,7 @@ export * from "./middlewares/get-partner";
|
|
|
32
32
|
export * from "./middlewares/get-user";
|
|
33
33
|
export * from "./middlewares/require-priveleges";
|
|
34
34
|
export * from "./middlewares/validate-kyc";
|
|
35
|
+
export * from "./middlewares/authorize";
|
|
35
36
|
export * from "./events/base-listener";
|
|
36
37
|
export * from "./events/base-publisher";
|
|
37
38
|
export * from "./events/subjects";
|
|
@@ -130,6 +131,7 @@ export * from "./types/business-user-update";
|
|
|
130
131
|
export * from "./types/user-type";
|
|
131
132
|
export * from "./types/platform-fee-range";
|
|
132
133
|
export * from "./types/api-key";
|
|
134
|
+
export * from "./types/authorization-type";
|
|
133
135
|
export * from "./classes/Asset";
|
|
134
136
|
export * from "./classes/CountryAsset";
|
|
135
137
|
export * from "./classes/CryptoAsset";
|
package/build/index.js
CHANGED
|
@@ -49,6 +49,7 @@ __exportStar(require("./middlewares/get-user"), exports);
|
|
|
49
49
|
__exportStar(require("./middlewares/require-priveleges"), exports);
|
|
50
50
|
__exportStar(require("./middlewares/validate-kyc"), exports);
|
|
51
51
|
// export * from "./middlewares/validate-order";
|
|
52
|
+
__exportStar(require("./middlewares/authorize"), exports);
|
|
52
53
|
__exportStar(require("./events/base-listener"), exports);
|
|
53
54
|
__exportStar(require("./events/base-publisher"), exports);
|
|
54
55
|
__exportStar(require("./events/subjects"), exports);
|
|
@@ -147,6 +148,7 @@ __exportStar(require("./types/business-user-update"), exports);
|
|
|
147
148
|
__exportStar(require("./types/user-type"), exports);
|
|
148
149
|
__exportStar(require("./types/platform-fee-range"), exports);
|
|
149
150
|
__exportStar(require("./types/api-key"), exports);
|
|
151
|
+
__exportStar(require("./types/authorization-type"), exports);
|
|
150
152
|
__exportStar(require("./classes/Asset"), exports);
|
|
151
153
|
__exportStar(require("./classes/CountryAsset"), exports);
|
|
152
154
|
__exportStar(require("./classes/CryptoAsset"), exports);
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { Request, Response, NextFunction } from "express";
|
|
2
|
+
import { BusinessUserDoc } from "../models/business-user";
|
|
3
|
+
import { UserDoc } from "../models/user";
|
|
4
|
+
import { Mongoose } from "mongoose";
|
|
5
|
+
import { AuthorizationType } from "../types/authorization-type";
|
|
6
|
+
interface AuthPayload {
|
|
7
|
+
id: string;
|
|
8
|
+
phoneNumber: string;
|
|
9
|
+
missing: string[];
|
|
10
|
+
}
|
|
11
|
+
interface AdminAuthPayload {
|
|
12
|
+
id: string;
|
|
13
|
+
email: string;
|
|
14
|
+
}
|
|
15
|
+
declare global {
|
|
16
|
+
namespace Express {
|
|
17
|
+
interface Request {
|
|
18
|
+
currentAuth?: AuthPayload;
|
|
19
|
+
currentAdminAuth?: AdminAuthPayload;
|
|
20
|
+
user?: UserDoc | BusinessUserDoc;
|
|
21
|
+
session?: any;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
export declare const authorize: (req: Request, res: Response, next: NextFunction, mongoose: Mongoose, authorizationTypes: AuthorizationType[]) => Promise<void>;
|
|
26
|
+
export {};
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.authorize = void 0;
|
|
16
|
+
const business_user_1 = require("../models/business-user");
|
|
17
|
+
const user_1 = require("../models/user");
|
|
18
|
+
const user_attribute_1 = require("../types/user-attribute");
|
|
19
|
+
const authorization_type_1 = require("../types/authorization-type");
|
|
20
|
+
const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
|
|
21
|
+
const not_authorized_error_1 = require("../errors/not-authorized-error");
|
|
22
|
+
const kyc_status_1 = require("../types/kyc-status");
|
|
23
|
+
const authorize = (req, res, next, mongoose, authorizationTypes) => __awaiter(void 0, void 0, void 0, function* () {
|
|
24
|
+
var _a, _b;
|
|
25
|
+
if (authorizationTypes.includes(authorization_type_1.AuthorizationType.Auth)) {
|
|
26
|
+
if ((_a = req.cookies) === null || _a === void 0 ? void 0 : _a.jwt) {
|
|
27
|
+
try {
|
|
28
|
+
const payload = jsonwebtoken_1.default.verify(req.cookies.jwt, process.env.JWT_KEY);
|
|
29
|
+
req.currentAuth = payload;
|
|
30
|
+
}
|
|
31
|
+
catch (err) { }
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
if (authorizationTypes.includes(authorization_type_1.AuthorizationType.Admin)) {
|
|
35
|
+
if ((_b = req.cookies) === null || _b === void 0 ? void 0 : _b.adminJwt) {
|
|
36
|
+
try {
|
|
37
|
+
const payload = jsonwebtoken_1.default.verify(req.cookies.adminJwt, process.env.JWT_KEY_ADMIN);
|
|
38
|
+
req.currentAdminAuth = payload;
|
|
39
|
+
}
|
|
40
|
+
catch (err) { }
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
if (authorizationTypes.includes(authorization_type_1.AuthorizationType.UserNoKYC)) {
|
|
44
|
+
let individualUser;
|
|
45
|
+
let businessUser;
|
|
46
|
+
try {
|
|
47
|
+
const User = yield (0, user_1.buildUser)(mongoose);
|
|
48
|
+
individualUser = yield User.findOne({
|
|
49
|
+
phoneNumber: req.currentAuth.phoneNumber,
|
|
50
|
+
attributes: { $ne: user_attribute_1.UserAttribute.deleted },
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
catch (err) {
|
|
54
|
+
individualUser = null;
|
|
55
|
+
}
|
|
56
|
+
try {
|
|
57
|
+
const BusinessUser = yield (0, business_user_1.buildBusinessUser)(mongoose);
|
|
58
|
+
businessUser = yield BusinessUser.findOne({
|
|
59
|
+
phoneNumber: req.currentAuth.phoneNumber,
|
|
60
|
+
attributes: { $ne: user_attribute_1.UserAttribute.deleted },
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
catch (err) {
|
|
64
|
+
businessUser = null;
|
|
65
|
+
}
|
|
66
|
+
const user = individualUser || businessUser;
|
|
67
|
+
if (user) {
|
|
68
|
+
req.user = user;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
if (authorizationTypes.includes(authorization_type_1.AuthorizationType.User)) {
|
|
72
|
+
let individualUser;
|
|
73
|
+
let businessUser;
|
|
74
|
+
try {
|
|
75
|
+
const User = yield (0, user_1.buildUser)(mongoose);
|
|
76
|
+
individualUser = yield User.findOne({
|
|
77
|
+
phoneNumber: req.currentAuth.phoneNumber,
|
|
78
|
+
attributes: { $ne: user_attribute_1.UserAttribute.deleted },
|
|
79
|
+
});
|
|
80
|
+
if ((individualUser === null || individualUser === void 0 ? void 0 : individualUser.kycStatus) !== kyc_status_1.KYCStatus.Passed) {
|
|
81
|
+
throw new Error();
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
catch (err) {
|
|
85
|
+
individualUser = null;
|
|
86
|
+
}
|
|
87
|
+
try {
|
|
88
|
+
const BusinessUser = yield (0, business_user_1.buildBusinessUser)(mongoose);
|
|
89
|
+
businessUser = yield BusinessUser.findOne({
|
|
90
|
+
phoneNumber: req.currentAuth.phoneNumber,
|
|
91
|
+
attributes: { $ne: user_attribute_1.UserAttribute.deleted },
|
|
92
|
+
});
|
|
93
|
+
if ((businessUser === null || businessUser === void 0 ? void 0 : businessUser.kycStatus) !== kyc_status_1.KYCStatus.businessApproved) {
|
|
94
|
+
throw new Error();
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
catch (err) {
|
|
98
|
+
businessUser = null;
|
|
99
|
+
}
|
|
100
|
+
const user = individualUser || businessUser;
|
|
101
|
+
if (user) {
|
|
102
|
+
req.user = user;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
if (!req.currentAdminAuth && !req.currentAuth && !req.user) {
|
|
106
|
+
throw new not_authorized_error_1.NotAuthorizedError();
|
|
107
|
+
}
|
|
108
|
+
next();
|
|
109
|
+
});
|
|
110
|
+
exports.authorize = authorize;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AuthorizationType = void 0;
|
|
4
|
+
var AuthorizationType;
|
|
5
|
+
(function (AuthorizationType) {
|
|
6
|
+
AuthorizationType["UserNoKYC"] = "newUserNoKYC";
|
|
7
|
+
AuthorizationType["User"] = "user";
|
|
8
|
+
AuthorizationType["Admin"] = "admin";
|
|
9
|
+
AuthorizationType["Partner"] = "partner";
|
|
10
|
+
AuthorizationType["Auth"] = "auth";
|
|
11
|
+
})(AuthorizationType = exports.AuthorizationType || (exports.AuthorizationType = {}));
|