@riocrypto/common-server 1.0.1250 → 1.0.1255

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
@@ -1,10 +1,6 @@
1
1
  export * from "./middlewares/error-handler";
2
2
  export * from "./middlewares/validate-request";
3
- export * from "./middlewares/require-auth";
4
- export * from "./middlewares/verify-admin-api-key";
5
- export * from "./middlewares/require-admin-api-key";
6
3
  export * from "./middlewares/get-order";
7
- export * from "./middlewares/require-priveleges";
8
4
  export * from "./middlewares/authorize";
9
5
  export * from "./middlewares/not-sandbox";
10
6
  export * from "./services/apiKey";
package/build/index.js CHANGED
@@ -16,11 +16,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./middlewares/error-handler"), exports);
18
18
  __exportStar(require("./middlewares/validate-request"), exports);
19
- __exportStar(require("./middlewares/require-auth"), exports);
20
- __exportStar(require("./middlewares/verify-admin-api-key"), exports);
21
- __exportStar(require("./middlewares/require-admin-api-key"), exports);
22
19
  __exportStar(require("./middlewares/get-order"), exports);
23
- __exportStar(require("./middlewares/require-priveleges"), exports);
24
20
  __exportStar(require("./middlewares/authorize"), exports);
25
21
  __exportStar(require("./middlewares/not-sandbox"), exports);
26
22
  __exportStar(require("./services/apiKey"), exports);
@@ -1,13 +1,15 @@
1
1
  import { Request, Response, NextFunction } from "express";
2
2
  import { UserDoc } from "../models/user";
3
3
  import { Mongoose } from "mongoose";
4
- import { AuthorizationType, AuthPayload, AdminAuthPayload } from "@riocrypto/common";
4
+ import { AuthorizationType } from "@riocrypto/common";
5
+ import { AuthDoc } from "../models/auth";
5
6
  import { AdminDoc } from "../models/admin";
7
+ import { AdminAuthDoc } from "../models/admin-auth";
6
8
  declare global {
7
9
  namespace Express {
8
10
  interface Request {
9
- currentAuth?: AuthPayload;
10
- currentAdminAuth?: AdminAuthPayload;
11
+ auth?: AuthDoc;
12
+ adminAuth?: AdminAuthDoc;
11
13
  user?: UserDoc;
12
14
  admin?: AdminDoc;
13
15
  validClusterApiKey?: boolean;
@@ -22,7 +22,7 @@ const secret_manager_client_1 = require("../clients/secret-manager-client");
22
22
  const admin_1 = require("../models/admin");
23
23
  const admin_auth_1 = require("../models/admin-auth");
24
24
  const authorize = (req, res, next, mongoose, authorizationTypes) => __awaiter(void 0, void 0, void 0, function* () {
25
- var _a, _b;
25
+ var _a, _b, _c, _d, _e;
26
26
  if (authorizationTypes.includes(common_1.AuthorizationType.Cluster)) {
27
27
  let apiKey = req.header("x-cluster-api-key");
28
28
  const CLUSTER_API_KEY = yield secret_manager_client_1.secretManagerClient.getSecretValue("CLUSTER_API_KEY");
@@ -36,19 +36,15 @@ const authorize = (req, res, next, mongoose, authorizationTypes) => __awaiter(vo
36
36
  if (authorizationTypes.includes(common_1.AuthorizationType.AdminAuth) ||
37
37
  authorizationTypes.includes(common_1.AuthorizationType.Admin)) {
38
38
  const adminApiKey = req.header("x-admin-api-key");
39
+ const AdminAuth = yield (0, admin_auth_1.buildAdminAuth)(mongoose);
39
40
  if (adminApiKey) {
40
41
  try {
41
42
  const hashedAdminApiKey = yield apiKey_1.ApiKey.toHash(adminApiKey);
42
- const AdminAuth = yield (0, admin_auth_1.buildAdminAuth)(mongoose);
43
43
  const adminAuth = yield AdminAuth.findOne({
44
44
  "apiKeys.value": hashedAdminApiKey,
45
45
  });
46
46
  if (adminAuth) {
47
- const payload = {
48
- id: adminAuth.id,
49
- email: adminAuth.email,
50
- };
51
- req.currentAdminAuth = payload;
47
+ req.adminAuth = adminAuth;
52
48
  }
53
49
  }
54
50
  catch (err) {
@@ -62,95 +58,108 @@ const authorize = (req, res, next, mongoose, authorizationTypes) => __awaiter(vo
62
58
  throw new Error("Unable to get ADMIN_ACCESS_TOKEN_SECRET");
63
59
  }
64
60
  const payload = jsonwebtoken_1.default.verify(req.cookies.adminAccessToken, ADMIN_ACCESS_TOKEN_SECRET);
65
- req.currentAdminAuth = payload;
61
+ const adminAuth = yield AdminAuth.findById(payload.id);
62
+ if (adminAuth) {
63
+ req.adminAuth = adminAuth;
64
+ }
66
65
  }
67
66
  catch (err) { }
68
67
  }
69
68
  }
70
69
  if (authorizationTypes.includes(common_1.AuthorizationType.Admin)) {
71
- if (req.currentAdminAuth) {
72
- let admin;
73
- try {
74
- const Admin = yield (0, admin_1.buildAdmin)(mongoose);
75
- admin = yield Admin.findOne({
76
- authIds: { $in: [req.currentAdminAuth.id] },
77
- });
78
- }
79
- catch (err) {
80
- admin = null;
81
- }
70
+ let admin;
71
+ try {
72
+ const Admin = yield (0, admin_1.buildAdmin)(mongoose);
73
+ admin = yield Admin.findOne({
74
+ authIds: { $in: [(_b = req.adminAuth) === null || _b === void 0 ? void 0 : _b.id] },
75
+ });
76
+ console.log("req.adminAuth?.id", (_c = req.adminAuth) === null || _c === void 0 ? void 0 : _c.id);
77
+ console.log("admin is ", admin);
82
78
  if (admin) {
83
79
  req.admin = admin;
84
80
  }
85
81
  }
82
+ catch (err) { }
86
83
  }
87
84
  if (authorizationTypes.includes(common_1.AuthorizationType.Auth) ||
88
85
  authorizationTypes.includes(common_1.AuthorizationType.UserNoKYC) ||
89
86
  authorizationTypes.includes(common_1.AuthorizationType.User)) {
90
87
  const apiKey = req.header("x-api-key");
88
+ const Auth = yield (0, auth_1.buildAuth)(mongoose);
91
89
  if (apiKey) {
92
90
  try {
93
91
  const hashedApiKey = yield apiKey_1.ApiKey.toHash(apiKey);
94
- const Auth = yield (0, auth_1.buildAuth)(mongoose);
95
92
  const auth = yield Auth.findOne({
96
93
  "apiKeys.value": hashedApiKey,
97
94
  });
98
95
  if (auth) {
99
- const payload = {
100
- id: auth.id,
101
- phoneNumber: auth.phoneNumber,
102
- };
103
- req.currentAuth = payload;
96
+ req.auth = auth;
104
97
  }
105
98
  }
106
- catch (err) {
107
- console.log(err);
108
- }
99
+ catch (err) { }
109
100
  }
110
- else if ((_b = req.cookies) === null || _b === void 0 ? void 0 : _b.accessToken) {
101
+ else if ((_d = req.cookies) === null || _d === void 0 ? void 0 : _d.accessToken) {
111
102
  try {
112
103
  const ACCESS_TOKEN_SECRET = yield secret_manager_client_1.secretManagerClient.getSecretValue("ACCESS_TOKEN_SECRET");
113
104
  if (!ACCESS_TOKEN_SECRET) {
114
105
  throw new common_1.SecretManagerError();
115
106
  }
116
107
  const payload = jsonwebtoken_1.default.verify(req.cookies.accessToken, ACCESS_TOKEN_SECRET);
117
- req.currentAuth = payload;
108
+ const auth = yield Auth.findById(payload.id);
109
+ if (auth) {
110
+ req.auth = auth;
111
+ }
118
112
  }
119
113
  catch (err) { }
120
114
  }
121
115
  }
122
116
  if (authorizationTypes.includes(common_1.AuthorizationType.UserNoKYC) ||
123
117
  authorizationTypes.includes(common_1.AuthorizationType.User)) {
124
- let user;
125
118
  try {
126
119
  const User = yield (0, user_1.buildUser)(mongoose);
127
- user = yield User.findOne({
128
- authIds: { $in: [req.currentAuth.id] },
120
+ const user = yield User.findOne({
121
+ authIds: { $in: [(_e = req.auth) === null || _e === void 0 ? void 0 : _e.id] },
129
122
  });
130
- }
131
- catch (err) {
132
- user = null;
133
- }
134
- if (user) {
135
- if (authorizationTypes.includes(common_1.AuthorizationType.UserNoKYC)) {
136
- req.user = user;
137
- }
138
- else {
139
- if (user.kycStatus === common_1.KYCStatus.Approved) {
123
+ if (user) {
124
+ if (authorizationTypes.includes(common_1.AuthorizationType.UserNoKYC)) {
140
125
  req.user = user;
141
126
  }
127
+ else {
128
+ if (user.kycStatus === common_1.KYCStatus.Approved) {
129
+ req.user = user;
130
+ }
131
+ }
142
132
  }
143
133
  }
134
+ catch (err) { }
144
135
  }
145
- if (!req.currentAdminAuth &&
146
- !req.admin &&
147
- !req.currentAuth &&
148
- !req.user &&
149
- !req.validClusterApiKey) {
150
- if (!authorizationTypes.includes(common_1.AuthorizationType.Public)) {
151
- throw new common_1.NotAuthorizedError();
152
- }
136
+ if (authorizationTypes.includes(common_1.AuthorizationType.Public)) {
137
+ next();
138
+ return;
139
+ }
140
+ if (authorizationTypes.includes(common_1.AuthorizationType.Cluster) &&
141
+ req.validClusterApiKey) {
142
+ next();
143
+ return;
144
+ }
145
+ if (authorizationTypes.includes(common_1.AuthorizationType.Auth) && req.auth) {
146
+ next();
147
+ return;
148
+ }
149
+ if (authorizationTypes.includes(common_1.AuthorizationType.AdminAuth) &&
150
+ req.adminAuth) {
151
+ next();
152
+ return;
153
+ }
154
+ if (authorizationTypes.includes(common_1.AuthorizationType.User) ||
155
+ (authorizationTypes.includes(common_1.AuthorizationType.UserNoKYC) && req.user)) {
156
+ next();
157
+ return;
158
+ }
159
+ if (authorizationTypes.includes(common_1.AuthorizationType.Admin) && req.admin) {
160
+ next();
161
+ return;
153
162
  }
154
- next();
163
+ throw new common_1.NotAuthorizedError();
155
164
  });
156
165
  exports.authorize = authorize;
@@ -51,7 +51,7 @@ const getOrder = (req, res, next, mongoose, onlyBankOrders) => __awaiter(void 0,
51
51
  const User = yield (0, user_1.buildUser)(mongoose);
52
52
  const user = yield User.findById(order.userId);
53
53
  if (req.order.userId !== ((_a = req.user) === null || _a === void 0 ? void 0 : _a.toJSON().id) &&
54
- !req.currentAdminAuth &&
54
+ !req.admin &&
55
55
  (user === null || user === void 0 ? void 0 : user.brokerId) !== ((_b = req.user) === null || _b === void 0 ? void 0 : _b.toJSON().id)) {
56
56
  throw new common_1.NotAuthorizedError();
57
57
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@riocrypto/common-server",
3
- "version": "1.0.1250",
3
+ "version": "1.0.1255",
4
4
  "description": "",
5
5
  "main": "./build/index.js",
6
6
  "types": "./build/index.d.ts",
@@ -27,7 +27,7 @@
27
27
  "@everapi/currencyapi-js": "^1.0.6",
28
28
  "@google-cloud/storage": "^6.9.5",
29
29
  "@google-cloud/vision": "^4.0.2",
30
- "@riocrypto/common": "^1.0.1135",
30
+ "@riocrypto/common": "^1.0.1136",
31
31
  "@types/express": "^4.17.13",
32
32
  "axios": "^0.27.2",
33
33
  "ccxt": "^3.0.30",
@@ -1,2 +0,0 @@
1
- import { Request, Response, NextFunction } from "express";
2
- export declare const requireAdminApiKey: (req: Request, res: Response, next: NextFunction) => void;
@@ -1,11 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.requireAdminApiKey = void 0;
4
- const common_1 = require("@riocrypto/common");
5
- const requireAdminApiKey = (req, res, next) => {
6
- if (!req.validAdminApiKey) {
7
- throw new common_1.NotAuthorizedError();
8
- }
9
- next();
10
- };
11
- exports.requireAdminApiKey = requireAdminApiKey;
@@ -1,2 +0,0 @@
1
- import { Request, Response, NextFunction } from "express";
2
- export declare const requireAuth: (req: Request, res: Response, next: NextFunction) => void;
@@ -1,11 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.requireAuth = void 0;
4
- const common_1 = require("@riocrypto/common");
5
- const requireAuth = (req, res, next) => {
6
- if (!req.currentAuth) {
7
- throw new common_1.NotAuthorizedError();
8
- }
9
- next();
10
- };
11
- exports.requireAuth = requireAuth;
@@ -1,2 +0,0 @@
1
- import { Request, Response, NextFunction } from "express";
2
- export declare const requirePriveleges: (req: Request, res: Response, next: NextFunction) => void;
@@ -1,14 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.requirePriveleges = void 0;
4
- const common_1 = require("@riocrypto/common");
5
- const requirePriveleges = (req, res, next) => {
6
- if (req.currentAuth && !req.user) {
7
- throw new common_1.MissingUserError();
8
- }
9
- if (!req.user && !req.currentAdminAuth) {
10
- throw new common_1.NotAuthorizedError();
11
- }
12
- next();
13
- };
14
- exports.requirePriveleges = requirePriveleges;
@@ -1,9 +0,0 @@
1
- import { Request, Response, NextFunction } from "express";
2
- declare global {
3
- namespace Express {
4
- interface Request {
5
- validAdminApiKey?: boolean;
6
- }
7
- }
8
- }
9
- export declare const verifyAdminApiKey: (req: Request, res: Response, next: NextFunction) => Promise<void>;
@@ -1,26 +0,0 @@
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
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.verifyAdminApiKey = void 0;
13
- const secret_manager_client_1 = require("../clients/secret-manager-client");
14
- const common_1 = require("@riocrypto/common");
15
- const verifyAdminApiKey = (req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
16
- let apiKey = req.header("x-api-key");
17
- const ADMIN_API_KEY = yield secret_manager_client_1.secretManagerClient.getSecretValue("ADMIN_API_KEY");
18
- if (!ADMIN_API_KEY) {
19
- throw new common_1.SecretManagerError();
20
- }
21
- if (apiKey === ADMIN_API_KEY) {
22
- req.validAdminApiKey = true;
23
- }
24
- next();
25
- });
26
- exports.verifyAdminApiKey = verifyAdminApiKey;