@riocrypto/common-server 1.0.1249 → 1.0.1254

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.
@@ -175,14 +175,7 @@ declare class BridgeClient {
175
175
  last_4: string;
176
176
  }[]>;
177
177
  exchangePlaidPublicToken(linkToken: string, publicToken: string): Promise<void>;
178
- createExternalAccount(bridgeCustomerId: string, bankName: string, accountNumber: string, routingNumber: string, accountOwnerName: string, address: {
179
- streetLine1: string;
180
- streetLine2?: string;
181
- city: string;
182
- state: string;
183
- postalCode: string;
184
- country: string;
185
- }): Promise<{
178
+ createExternalAccount(bridgeCustomerId: string, bankName: string, accountNumber: string, routingNumber: string, accountOwnerName: string): Promise<{
186
179
  id: string;
187
180
  bank_name: string;
188
181
  account_name: string;
@@ -179,7 +179,7 @@ class BridgeClient {
179
179
  return data;
180
180
  });
181
181
  }
182
- createExternalAccount(bridgeCustomerId, bankName, accountNumber, routingNumber, accountOwnerName, address) {
182
+ createExternalAccount(bridgeCustomerId, bankName, accountNumber, routingNumber, accountOwnerName) {
183
183
  return __awaiter(this, void 0, void 0, function* () {
184
184
  const { data } = yield axios_1.default.post(`${this.baseUrl}/v0/customers/${bridgeCustomerId}/external_accounts`, {
185
185
  type: "wire",
@@ -187,14 +187,6 @@ class BridgeClient {
187
187
  account_number: accountNumber,
188
188
  routing_number: routingNumber,
189
189
  account_owner_name: accountOwnerName,
190
- address: {
191
- street_line_1: address.streetLine1,
192
- street_line_2: address.streetLine2,
193
- city: address.city,
194
- state: address.state,
195
- postal_code: address.postalCode,
196
- country: (0, common_1.convertAlpha2ToAlpha3)(address.country),
197
- },
198
190
  }, {
199
191
  headers: {
200
192
  "Api-Key": this.apiKey,
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;
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,106 @@ 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
+ });
82
76
  if (admin) {
83
77
  req.admin = admin;
84
78
  }
85
79
  }
80
+ catch (err) { }
86
81
  }
87
82
  if (authorizationTypes.includes(common_1.AuthorizationType.Auth) ||
88
83
  authorizationTypes.includes(common_1.AuthorizationType.UserNoKYC) ||
89
84
  authorizationTypes.includes(common_1.AuthorizationType.User)) {
90
85
  const apiKey = req.header("x-api-key");
86
+ const Auth = yield (0, auth_1.buildAuth)(mongoose);
91
87
  if (apiKey) {
92
88
  try {
93
89
  const hashedApiKey = yield apiKey_1.ApiKey.toHash(apiKey);
94
- const Auth = yield (0, auth_1.buildAuth)(mongoose);
95
90
  const auth = yield Auth.findOne({
96
91
  "apiKeys.value": hashedApiKey,
97
92
  });
98
93
  if (auth) {
99
- const payload = {
100
- id: auth.id,
101
- phoneNumber: auth.phoneNumber,
102
- };
103
- req.currentAuth = payload;
94
+ req.auth = auth;
104
95
  }
105
96
  }
106
- catch (err) {
107
- console.log(err);
108
- }
97
+ catch (err) { }
109
98
  }
110
- else if ((_b = req.cookies) === null || _b === void 0 ? void 0 : _b.accessToken) {
99
+ else if ((_c = req.cookies) === null || _c === void 0 ? void 0 : _c.accessToken) {
111
100
  try {
112
101
  const ACCESS_TOKEN_SECRET = yield secret_manager_client_1.secretManagerClient.getSecretValue("ACCESS_TOKEN_SECRET");
113
102
  if (!ACCESS_TOKEN_SECRET) {
114
103
  throw new common_1.SecretManagerError();
115
104
  }
116
105
  const payload = jsonwebtoken_1.default.verify(req.cookies.accessToken, ACCESS_TOKEN_SECRET);
117
- req.currentAuth = payload;
106
+ const auth = yield Auth.findById(payload.id);
107
+ if (auth) {
108
+ req.auth = auth;
109
+ }
118
110
  }
119
111
  catch (err) { }
120
112
  }
121
113
  }
122
114
  if (authorizationTypes.includes(common_1.AuthorizationType.UserNoKYC) ||
123
115
  authorizationTypes.includes(common_1.AuthorizationType.User)) {
124
- let user;
125
116
  try {
126
117
  const User = yield (0, user_1.buildUser)(mongoose);
127
- user = yield User.findOne({
128
- authIds: { $in: [req.currentAuth.id] },
118
+ const user = yield User.findOne({
119
+ authIds: { $in: [(_d = req.auth) === null || _d === void 0 ? void 0 : _d.id] },
129
120
  });
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) {
121
+ if (user) {
122
+ if (authorizationTypes.includes(common_1.AuthorizationType.UserNoKYC)) {
140
123
  req.user = user;
141
124
  }
125
+ else {
126
+ if (user.kycStatus === common_1.KYCStatus.Approved) {
127
+ req.user = user;
128
+ }
129
+ }
142
130
  }
143
131
  }
132
+ catch (err) { }
144
133
  }
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
- }
134
+ if (authorizationTypes.includes(common_1.AuthorizationType.Public)) {
135
+ next();
136
+ return;
137
+ }
138
+ if (authorizationTypes.includes(common_1.AuthorizationType.Cluster) &&
139
+ req.validClusterApiKey) {
140
+ next();
141
+ return;
142
+ }
143
+ if (authorizationTypes.includes(common_1.AuthorizationType.Auth) && req.auth) {
144
+ next();
145
+ return;
146
+ }
147
+ if (authorizationTypes.includes(common_1.AuthorizationType.AdminAuth) &&
148
+ req.adminAuth) {
149
+ next();
150
+ return;
151
+ }
152
+ if (authorizationTypes.includes(common_1.AuthorizationType.User) ||
153
+ (authorizationTypes.includes(common_1.AuthorizationType.UserNoKYC) && req.user)) {
154
+ next();
155
+ return;
156
+ }
157
+ if (authorizationTypes.includes(common_1.AuthorizationType.Admin) && req.admin) {
158
+ next();
159
+ return;
153
160
  }
154
- next();
161
+ throw new common_1.NotAuthorizedError();
155
162
  });
156
163
  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.1249",
3
+ "version": "1.0.1254",
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;