@steedos/accounts 3.0.13-beta.8 → 3.0.13

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.
Files changed (112) hide show
  1. package/lib/core/index.js +177 -249
  2. package/lib/core/index.js.map +1 -1
  3. package/lib/database-mongo/index.js +1 -1
  4. package/lib/database-mongo/index.js.map +1 -1
  5. package/lib/database-mongo/mongo.js +760 -1278
  6. package/lib/database-mongo/mongo.js.map +1 -1
  7. package/lib/db.js +2 -2
  8. package/lib/db.js.map +1 -1
  9. package/lib/index.js +107 -143
  10. package/lib/index.js.map +1 -1
  11. package/lib/mail.js +36 -47
  12. package/lib/mail.js.map +1 -1
  13. package/lib/password/accounts-password.js +505 -763
  14. package/lib/password/accounts-password.js.map +1 -1
  15. package/lib/password/index.js +2 -2
  16. package/lib/password/index.js.map +1 -1
  17. package/lib/password/utils/encryption.js +10 -22
  18. package/lib/password/utils/encryption.js.map +1 -1
  19. package/lib/password/utils/is-email.js +2 -2
  20. package/lib/password/utils/is-email.js.map +1 -1
  21. package/lib/password/utils/user.js +3 -3
  22. package/lib/password/utils/user.js.map +1 -1
  23. package/lib/rest-express/endpoints/authorize.js +47 -54
  24. package/lib/rest-express/endpoints/authorize.js.map +1 -1
  25. package/lib/rest-express/endpoints/geetestV3/geetest-init.js +85 -131
  26. package/lib/rest-express/endpoints/geetestV3/geetest-init.js.map +1 -1
  27. package/lib/rest-express/endpoints/geetestV3/sdk/geetest_config.js +1 -1
  28. package/lib/rest-express/endpoints/geetestV3/sdk/geetest_config.js.map +1 -1
  29. package/lib/rest-express/endpoints/geetestV3/sdk/geetest_lib.js +112 -160
  30. package/lib/rest-express/endpoints/geetestV3/sdk/geetest_lib.js.map +1 -1
  31. package/lib/rest-express/endpoints/geetestV3/sdk/geetest_lib_result.js +8 -9
  32. package/lib/rest-express/endpoints/geetestV3/sdk/geetest_lib_result.js.map +1 -1
  33. package/lib/rest-express/endpoints/get-user.js +7 -12
  34. package/lib/rest-express/endpoints/get-user.js.map +1 -1
  35. package/lib/rest-express/endpoints/impersonate.js +14 -27
  36. package/lib/rest-express/endpoints/impersonate.js.map +1 -1
  37. package/lib/rest-express/endpoints/initServer.js +9 -13
  38. package/lib/rest-express/endpoints/initServer.js.map +1 -1
  39. package/lib/rest-express/endpoints/login.js +82 -104
  40. package/lib/rest-express/endpoints/login.js.map +1 -1
  41. package/lib/rest-express/endpoints/logout.js +68 -86
  42. package/lib/rest-express/endpoints/logout.js.map +1 -1
  43. package/lib/rest-express/endpoints/oauth/provider-callback.js +29 -37
  44. package/lib/rest-express/endpoints/oauth/provider-callback.js.map +1 -1
  45. package/lib/rest-express/endpoints/password/change-password.js +73 -102
  46. package/lib/rest-express/endpoints/password/change-password.js.map +1 -1
  47. package/lib/rest-express/endpoints/password/register.js +63 -95
  48. package/lib/rest-express/endpoints/password/register.js.map +1 -1
  49. package/lib/rest-express/endpoints/password/reset.js +27 -48
  50. package/lib/rest-express/endpoints/password/reset.js.map +1 -1
  51. package/lib/rest-express/endpoints/password/setSpaceUserPassword.js +112 -144
  52. package/lib/rest-express/endpoints/password/setSpaceUserPassword.js.map +1 -1
  53. package/lib/rest-express/endpoints/password/two-factor.js +39 -70
  54. package/lib/rest-express/endpoints/password/two-factor.js.map +1 -1
  55. package/lib/rest-express/endpoints/password/verify-email.js +34 -65
  56. package/lib/rest-express/endpoints/password/verify-email.js.map +1 -1
  57. package/lib/rest-express/endpoints/password/verify.js +45 -74
  58. package/lib/rest-express/endpoints/password/verify.js.map +1 -1
  59. package/lib/rest-express/endpoints/put-user-name.js +19 -32
  60. package/lib/rest-express/endpoints/put-user-name.js.map +1 -1
  61. package/lib/rest-express/endpoints/refresh-access-token.js +14 -27
  62. package/lib/rest-express/endpoints/refresh-access-token.js.map +1 -1
  63. package/lib/rest-express/endpoints/service-authenticate.js +55 -79
  64. package/lib/rest-express/endpoints/service-authenticate.js.map +1 -1
  65. package/lib/rest-express/endpoints/spaces.js +13 -30
  66. package/lib/rest-express/endpoints/spaces.js.map +1 -1
  67. package/lib/rest-express/endpoints/steedos/accept_invitation.js +28 -42
  68. package/lib/rest-express/endpoints/steedos/accept_invitation.js.map +1 -1
  69. package/lib/rest-express/endpoints/steedos/create-tenant.js +81 -102
  70. package/lib/rest-express/endpoints/steedos/create-tenant.js.map +1 -1
  71. package/lib/rest-express/endpoints/steedos/decline_invitation.js +28 -42
  72. package/lib/rest-express/endpoints/steedos/decline_invitation.js.map +1 -1
  73. package/lib/rest-express/endpoints/steedos/get-tenant.js +54 -67
  74. package/lib/rest-express/endpoints/steedos/get-tenant.js.map +1 -1
  75. package/lib/rest-express/endpoints/steedos/settings.js +110 -127
  76. package/lib/rest-express/endpoints/steedos/settings.js.map +1 -1
  77. package/lib/rest-express/endpoints/update-session.js +33 -48
  78. package/lib/rest-express/endpoints/update-session.js.map +1 -1
  79. package/lib/rest-express/express-middleware.js +57 -59
  80. package/lib/rest-express/express-middleware.js.map +1 -1
  81. package/lib/rest-express/index.js +1 -1
  82. package/lib/rest-express/index.js.map +1 -1
  83. package/lib/rest-express/user-loader.js +67 -86
  84. package/lib/rest-express/user-loader.js.map +1 -1
  85. package/lib/rest-express/utils/get-user-agent.js +2 -2
  86. package/lib/rest-express/utils/get-user-agent.js.map +1 -1
  87. package/lib/rest-express/utils/getClientIp.js +3 -3
  88. package/lib/rest-express/utils/getClientIp.js.map +1 -1
  89. package/lib/rest-express/utils/send-error.js +5 -7
  90. package/lib/rest-express/utils/send-error.js.map +1 -1
  91. package/lib/rest-express/utils/steedos-auth.js +21 -21
  92. package/lib/rest-express/utils/steedos-auth.js.map +1 -1
  93. package/lib/rest-express/utils/users.js +17 -30
  94. package/lib/rest-express/utils/users.js.map +1 -1
  95. package/lib/server/accounts-server.js +413 -574
  96. package/lib/server/accounts-server.js.map +1 -1
  97. package/lib/server/index.js +1 -1
  98. package/lib/server/index.js.map +1 -1
  99. package/lib/server/utils/email.js +19 -35
  100. package/lib/server/utils/email.js.map +1 -1
  101. package/lib/server/utils/get-first-user-email.js +3 -3
  102. package/lib/server/utils/get-first-user-email.js.map +1 -1
  103. package/lib/server/utils/tokens.js +14 -24
  104. package/lib/server/utils/tokens.js.map +1 -1
  105. package/lib/types/index.js +1 -1
  106. package/package.json +5 -5
  107. package/src/database-mongo/mongo.ts +6 -2
  108. package/src/password/accounts-password.ts +61 -0
  109. package/src/rest-express/endpoints/password/change-password.ts +1 -1
  110. package/src/rest-express/endpoints/password/setSpaceUserPassword.ts +5 -0
  111. package/src/rest-express/endpoints/service-authenticate.ts +2 -5
  112. package/src/types/types/user.ts +2 -0
@@ -1,20 +1,19 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Mongo = void 0;
4
- var tslib_1 = require("tslib");
5
- var lodash_1 = require("lodash");
6
- var mongodb_1 = require("mongodb");
7
- var auth_1 = require("@steedos/auth");
8
- var lodash_2 = require("lodash");
9
- var objectql_1 = require("@steedos/objectql");
10
- var moment = require("moment");
11
- var toMongoID = function (objectId) {
4
+ const lodash_1 = require("lodash");
5
+ const mongodb_1 = require("mongodb");
6
+ const auth_1 = require("@steedos/auth");
7
+ const lodash_2 = require("lodash");
8
+ const objectql_1 = require("@steedos/objectql");
9
+ const moment = require("moment");
10
+ const toMongoID = (objectId) => {
12
11
  if (typeof objectId === "string") {
13
12
  return new mongodb_1.ObjectId(objectId);
14
13
  }
15
14
  return objectId;
16
15
  };
17
- var defaultOptions = {
16
+ const defaultOptions = {
18
17
  collectionName: "users",
19
18
  sessionCollectionName: "sessions",
20
19
  codeCollectionName: "users_verify_code",
@@ -27,11 +26,18 @@ var defaultOptions = {
27
26
  convertUserIdToMongoObjectId: false,
28
27
  convertSessionIdToMongoObjectId: false,
29
28
  caseSensitiveUserName: true,
30
- dateProvider: function (date) { return (date ? date.getTime() : Date.now()); },
29
+ dateProvider: (date) => (date ? date.getTime() : Date.now()),
31
30
  };
32
- var Mongo = (function () {
33
- function Mongo(db, options) {
34
- this.options = (0, lodash_1.merge)(tslib_1.__assign({}, defaultOptions), options);
31
+ class Mongo {
32
+ options;
33
+ db;
34
+ collection;
35
+ sessionCollection;
36
+ codeCollection;
37
+ inviteCollection;
38
+ spaceUserCollection;
39
+ constructor(db, options) {
40
+ this.options = (0, lodash_1.merge)({ ...defaultOptions }, options);
35
41
  if (!db) {
36
42
  throw new Error("A database connection is required");
37
43
  }
@@ -42,725 +48,432 @@ var Mongo = (function () {
42
48
  this.inviteCollection = this.db.collection(this.options.inviteCollectionName);
43
49
  this.spaceUserCollection = this.db.collection(this.options.spaceUserCollectionName);
44
50
  }
45
- Mongo.prototype.findValidSessionsByUserId = function (userId, is_phone) {
46
- return tslib_1.__awaiter(this, void 0, void 0, function () {
47
- var query, sessions;
48
- return tslib_1.__generator(this, function (_a) {
49
- switch (_a.label) {
50
- case 0:
51
- query = {
52
- userId: userId,
53
- valid: true,
54
- };
55
- if (is_phone) {
56
- query.is_phone = true;
57
- }
58
- else {
59
- query.is_phone = { $ne: true };
60
- }
61
- return [4, this.sessionCollection
62
- .find(query)
63
- .project({ _id: 1 })
64
- .toArray()];
65
- case 1:
66
- sessions = _a.sent();
67
- if (sessions) {
68
- sessions.forEach(function (session) {
69
- session.id = session._id.toString();
70
- });
71
- }
72
- return [2, sessions];
73
- }
74
- });
75
- });
76
- };
77
- Mongo.prototype.setupIndexes = function () {
78
- return tslib_1.__awaiter(this, void 0, void 0, function () {
79
- return tslib_1.__generator(this, function (_a) {
80
- switch (_a.label) {
81
- case 0: return [4, this.sessionCollection.createIndex("token", {
82
- unique: true,
83
- sparse: true,
84
- })];
85
- case 1:
86
- _a.sent();
87
- return [4, this.collection.createIndex("username", {
88
- unique: true,
89
- sparse: true,
90
- })];
91
- case 2:
92
- _a.sent();
93
- return [4, this.collection.createIndex("emails.address", {
94
- unique: true,
95
- sparse: true,
96
- })];
97
- case 3:
98
- _a.sent();
99
- return [2];
100
- }
101
- });
102
- });
103
- };
104
- Mongo.prototype.createUser = function (_a) {
105
- return tslib_1.__awaiter(this, void 0, void 0, function () {
106
- var user, encryptedMobile, encryptedName, ret;
107
- var _b;
108
- var password = _a.password, username = _a.username, email = _a.email, email_verified = _a.email_verified, mobile = _a.mobile, mobile_verified = _a.mobile_verified, cleanUser = tslib_1.__rest(_a, ["password", "username", "email", "email_verified", "mobile", "mobile_verified"]);
109
- return tslib_1.__generator(this, function (_c) {
110
- switch (_c.label) {
111
- case 0:
112
- user = tslib_1.__assign(tslib_1.__assign({}, cleanUser), (_b = { services: {} }, _b[this.options.timestamps.createdAt] = this.options.dateProvider(), _b[this.options.timestamps.updatedAt] = this.options.dateProvider(), _b));
113
- if (password) {
114
- user.services.password = { bcrypt: password };
115
- }
116
- if (username) {
117
- user.username = username;
118
- }
119
- if (email) {
120
- user.email = email.toLowerCase();
121
- user.email_verified = email_verified;
122
- user.emails = [
123
- { address: email.toLowerCase(), verified: email_verified },
124
- ];
125
- }
126
- if (!mobile) return [3, 2];
127
- user.mobile = mobile;
128
- return [4, this.getEncryptedSpaceUserFieldValue(mobile, "mobile")];
129
- case 1:
130
- encryptedMobile = _c.sent();
131
- if (encryptedMobile) {
132
- user.mobile = encryptedMobile;
133
- }
134
- user.mobile_verified = mobile_verified;
135
- _c.label = 2;
136
- case 2:
137
- if (!user.name) return [3, 4];
138
- return [4, this.getEncryptedSpaceUserFieldValue(user.name, "name")];
139
- case 3:
140
- encryptedName = _c.sent();
141
- if (encryptedName) {
142
- user.name = encryptedName;
143
- }
144
- _c.label = 4;
145
- case 4:
146
- if (this.options.idProvider) {
147
- user._id = this.options.idProvider();
148
- }
149
- user.steedos_id = user._id;
150
- return [4, this.collection.insertOne(user)];
151
- case 5:
152
- ret = _c.sent();
153
- return [2, user._id];
154
- }
155
- });
156
- });
157
- };
158
- Mongo.prototype.findUserById = function (userId) {
159
- return tslib_1.__awaiter(this, void 0, void 0, function () {
160
- var id, user;
161
- return tslib_1.__generator(this, function (_a) {
162
- switch (_a.label) {
163
- case 0:
164
- id = this.options.convertUserIdToMongoObjectId
165
- ? toMongoID(userId)
166
- : userId;
167
- return [4, this.collection.findOne({ _id: id })];
168
- case 1:
169
- user = _a.sent();
170
- if (user) {
171
- user.id = user._id.toString();
172
- }
173
- return [2, user];
174
- }
175
- });
176
- });
177
- };
178
- Mongo.prototype.findUserByEmail = function (email) {
179
- return tslib_1.__awaiter(this, void 0, void 0, function () {
180
- var user;
181
- return tslib_1.__generator(this, function (_a) {
182
- switch (_a.label) {
183
- case 0:
184
- if (!(0, lodash_2.isString)(email)) {
185
- return [2, null];
186
- }
187
- return [4, this.collection.findOne({
188
- email: email.toLowerCase(),
189
- })];
190
- case 1:
191
- user = _a.sent();
192
- if (user) {
193
- user.id = user._id.toString();
194
- }
195
- return [2, user];
196
- }
197
- });
198
- });
199
- };
200
- Mongo.prototype.findUserByMobile = function (mobile) {
201
- return tslib_1.__awaiter(this, void 0, void 0, function () {
202
- var selector, encryptedMobile, user;
203
- return tslib_1.__generator(this, function (_a) {
204
- switch (_a.label) {
205
- case 0:
206
- if (!(0, lodash_2.isString)(mobile)) {
207
- return [2, null];
208
- }
209
- selector = {
210
- mobile: mobile,
211
- };
212
- return [4, this.getEncryptedSpaceUserFieldValue(mobile, "mobile")];
213
- case 1:
214
- encryptedMobile = _a.sent();
215
- if (encryptedMobile) {
216
- selector.mobile = encryptedMobile;
217
- }
218
- return [4, this.collection.findOne(selector)];
219
- case 2:
220
- user = _a.sent();
221
- if (user) {
222
- user.id = user._id.toString();
223
- }
224
- return [2, user];
225
- }
226
- });
227
- });
228
- };
229
- Mongo.prototype.findUserByUsername = function (username) {
230
- return tslib_1.__awaiter(this, void 0, void 0, function () {
231
- var filter, user;
232
- return tslib_1.__generator(this, function (_a) {
233
- switch (_a.label) {
234
- case 0:
235
- if (!(0, lodash_2.isString)(username)) {
236
- return [2, null];
237
- }
238
- filter = this.options.caseSensitiveUserName
239
- ? { username: username }
240
- : {
241
- $where: "obj.username && (obj.username.toLowerCase() === \"".concat(username.toLowerCase(), "\")"),
242
- };
243
- return [4, this.collection.findOne(filter)];
244
- case 1:
245
- user = _a.sent();
246
- if (user) {
247
- user.id = user._id.toString();
248
- }
249
- return [2, user];
250
- }
251
- });
252
- });
253
- };
254
- Mongo.prototype.findPasswordHash = function (userId) {
255
- return tslib_1.__awaiter(this, void 0, void 0, function () {
256
- var user;
257
- return tslib_1.__generator(this, function (_a) {
258
- switch (_a.label) {
259
- case 0: return [4, this.findUserById(userId)];
260
- case 1:
261
- user = _a.sent();
262
- if (user) {
263
- return [2, (0, lodash_1.get)(user, "services.password.bcrypt")];
264
- }
265
- return [2, null];
266
- }
267
- });
268
- });
269
- };
270
- Mongo.prototype.findUserByEmailVerificationToken = function (token) {
271
- return tslib_1.__awaiter(this, void 0, void 0, function () {
272
- var user;
273
- return tslib_1.__generator(this, function (_a) {
274
- switch (_a.label) {
275
- case 0:
276
- if (!(0, lodash_2.isString)(token)) {
277
- return [2, null];
278
- }
279
- return [4, this.collection.findOne({
280
- "services.email.verificationTokens.token": token,
281
- })];
282
- case 1:
283
- user = _a.sent();
284
- if (user) {
285
- user.id = user._id.toString();
286
- }
287
- return [2, user];
288
- }
289
- });
290
- });
291
- };
292
- Mongo.prototype.findUserByResetPasswordToken = function (token) {
293
- return tslib_1.__awaiter(this, void 0, void 0, function () {
294
- var user;
295
- return tslib_1.__generator(this, function (_a) {
296
- switch (_a.label) {
297
- case 0:
298
- if (!(0, lodash_2.isString)(token)) {
299
- return [2, null];
300
- }
301
- return [4, this.collection.findOne({
302
- "services.password.reset.token": token,
303
- })];
304
- case 1:
305
- user = _a.sent();
306
- if (user) {
307
- user.id = user._id.toString();
308
- }
309
- return [2, user];
310
- }
311
- });
312
- });
313
- };
314
- Mongo.prototype.findUserByServiceId = function (serviceName, serviceId) {
315
- return tslib_1.__awaiter(this, void 0, void 0, function () {
316
- var user;
317
- var _a;
318
- return tslib_1.__generator(this, function (_b) {
319
- switch (_b.label) {
320
- case 0:
321
- if (!(0, lodash_2.isString)(serviceId)) {
322
- return [2, null];
323
- }
324
- return [4, this.collection.findOne((_a = {},
325
- _a["services.".concat(serviceName, ".id")] = serviceId,
326
- _a))];
327
- case 1:
328
- user = _b.sent();
329
- if (user) {
330
- user.id = user._id.toString();
331
- }
332
- return [2, user];
333
- }
334
- });
335
- });
336
- };
337
- Mongo.prototype.addEmail = function (userId, newEmail, verified) {
338
- return tslib_1.__awaiter(this, void 0, void 0, function () {
339
- var id, ret;
340
- var _a;
341
- return tslib_1.__generator(this, function (_b) {
342
- switch (_b.label) {
343
- case 0:
344
- id = this.options.convertUserIdToMongoObjectId
345
- ? toMongoID(userId)
346
- : userId;
347
- return [4, this.collection.updateOne({ _id: id }, {
348
- $addToSet: {
349
- emails: {
350
- address: newEmail.toLowerCase(),
351
- verified: verified,
352
- },
353
- },
354
- $set: (_a = {},
355
- _a[this.options.timestamps.updatedAt] = this.options.dateProvider(),
356
- _a),
357
- })];
358
- case 1:
359
- ret = _b.sent();
360
- if (ret.matchedCount === 0) {
361
- throw new Error("User not found");
362
- }
363
- return [2];
364
- }
365
- });
366
- });
367
- };
368
- Mongo.prototype.removeEmail = function (userId, email) {
369
- return tslib_1.__awaiter(this, void 0, void 0, function () {
370
- var id, ret;
371
- var _a;
372
- return tslib_1.__generator(this, function (_b) {
373
- switch (_b.label) {
374
- case 0:
375
- id = this.options.convertUserIdToMongoObjectId
376
- ? toMongoID(userId)
377
- : userId;
378
- return [4, this.collection.updateOne({ _id: id }, {
379
- $pull: { emails: { address: email.toLowerCase() } },
380
- $set: (_a = {},
381
- _a[this.options.timestamps.updatedAt] = this.options.dateProvider(),
382
- _a),
383
- })];
384
- case 1:
385
- ret = _b.sent();
386
- if (ret.matchedCount === 0) {
387
- throw new Error("User not found");
388
- }
389
- return [2];
390
- }
51
+ async findValidSessionsByUserId(userId, is_phone) {
52
+ let query = {
53
+ userId,
54
+ valid: true,
55
+ };
56
+ if (is_phone) {
57
+ query.is_phone = true;
58
+ }
59
+ else {
60
+ query.is_phone = { $ne: true };
61
+ }
62
+ const sessions = await this.sessionCollection
63
+ .find(query)
64
+ .project({ _id: 1 })
65
+ .toArray();
66
+ if (sessions) {
67
+ sessions.forEach(function (session) {
68
+ session.id = session._id.toString();
391
69
  });
70
+ }
71
+ return sessions;
72
+ }
73
+ async setupIndexes() {
74
+ await this.sessionCollection.createIndex("token", {
75
+ unique: true,
76
+ sparse: true,
392
77
  });
393
- };
394
- Mongo.prototype.verifyEmail = function (userId, email) {
395
- return tslib_1.__awaiter(this, void 0, void 0, function () {
396
- var id, ret;
397
- var _a;
398
- return tslib_1.__generator(this, function (_b) {
399
- switch (_b.label) {
400
- case 0:
401
- id = this.options.convertUserIdToMongoObjectId
402
- ? toMongoID(userId)
403
- : userId;
404
- return [4, this.collection.updateOne({ _id: id, email: email }, {
405
- $set: (_a = {
406
- email_verified: true
407
- },
408
- _a[this.options.timestamps.updatedAt] = this.options.dateProvider(),
409
- _a),
410
- $pull: {
411
- "services.email.verificationTokens": { address: email },
412
- },
413
- })];
414
- case 1:
415
- ret = _b.sent();
416
- return [4, this.spaceUserCollection.updateMany({ user: id }, {
417
- $set: {
418
- email_verified: true,
419
- modified: this.options.dateProvider(),
420
- modified_by: id,
421
- },
422
- })];
423
- case 2:
424
- _b.sent();
425
- if (ret.matchedCount === 0) {
426
- throw new Error("User not found");
427
- }
428
- return [2];
429
- }
430
- });
78
+ await this.collection.createIndex("username", {
79
+ unique: true,
80
+ sparse: true,
431
81
  });
432
- };
433
- Mongo.prototype.getEncryptedSpaceUserFieldValue = function (value, fieldName) {
434
- return tslib_1.__awaiter(this, void 0, void 0, function () {
435
- var objectql, objFields, datasource, encryptedValue;
436
- return tslib_1.__generator(this, function (_a) {
437
- switch (_a.label) {
438
- case 0:
439
- objectql = require("@steedos/objectql");
440
- return [4, objectql.getObject("space_users").getFields()];
441
- case 1:
442
- objFields = _a.sent();
443
- if (!objFields[fieldName].enable_encryption) return [3, 3];
444
- datasource = (0, objectql_1.getDataSource)("default");
445
- return [4, datasource.adapter.encryptValue(value)];
446
- case 2:
447
- encryptedValue = _a.sent();
448
- if (encryptedValue) {
449
- return [2, encryptedValue];
450
- }
451
- _a.label = 3;
452
- case 3: return [2];
453
- }
454
- });
82
+ await this.collection.createIndex("emails.address", {
83
+ unique: true,
84
+ sparse: true,
455
85
  });
456
- };
457
- Mongo.prototype.verifyMobile = function (userId, mobile) {
458
- return tslib_1.__awaiter(this, void 0, void 0, function () {
459
- var id, encryptedMobile, ret;
460
- var _a;
461
- return tslib_1.__generator(this, function (_b) {
462
- switch (_b.label) {
463
- case 0:
464
- if (!(0, lodash_2.isString)(mobile)) {
465
- return [2, null];
466
- }
467
- id = this.options.convertUserIdToMongoObjectId
468
- ? toMongoID(userId)
469
- : userId;
470
- return [4, this.getEncryptedSpaceUserFieldValue(mobile, "mobile")];
471
- case 1:
472
- encryptedMobile = _b.sent();
473
- return [4, this.collection.updateOne({ _id: id, mobile: encryptedMobile || mobile }, {
474
- $set: (_a = {
475
- mobile_verified: true
476
- },
477
- _a[this.options.timestamps.updatedAt] = this.options.dateProvider(),
478
- _a),
479
- $pull: {
480
- "services.mobile.verificationTokens": {
481
- mobile: encryptedMobile || mobile,
482
- },
483
- },
484
- })];
485
- case 2:
486
- ret = _b.sent();
487
- return [4, this.spaceUserCollection.updateMany({ user: id }, {
488
- $set: {
489
- mobile_verified: true,
490
- modified: this.options.dateProvider(),
491
- modified_by: id,
492
- },
493
- })];
494
- case 3:
495
- _b.sent();
496
- if (ret.matchedCount === 0) {
497
- throw new Error("User not found");
498
- }
499
- return [2];
500
- }
501
- });
86
+ }
87
+ async createUser({ password, username, email, email_verified, mobile, mobile_verified, ...cleanUser }) {
88
+ const user = {
89
+ ...cleanUser,
90
+ services: {},
91
+ [this.options.timestamps.createdAt]: this.options.dateProvider(),
92
+ [this.options.timestamps.updatedAt]: this.options.dateProvider(),
93
+ };
94
+ if (password) {
95
+ user.services.password = { bcrypt: password };
96
+ }
97
+ if (username) {
98
+ user.username = username;
99
+ }
100
+ if (email) {
101
+ user.email = email.toLowerCase();
102
+ user.email_verified = email_verified;
103
+ user.emails = [
104
+ { address: email.toLowerCase(), verified: email_verified },
105
+ ];
106
+ }
107
+ if (mobile) {
108
+ user.mobile = mobile;
109
+ const encryptedMobile = await this.getEncryptedSpaceUserFieldValue(mobile, "mobile");
110
+ if (encryptedMobile) {
111
+ user.mobile = encryptedMobile;
112
+ }
113
+ user.mobile_verified = mobile_verified;
114
+ }
115
+ if (user.name) {
116
+ const encryptedName = await this.getEncryptedSpaceUserFieldValue(user.name, "name");
117
+ if (encryptedName) {
118
+ user.name = encryptedName;
119
+ }
120
+ }
121
+ if (this.options.idProvider) {
122
+ user._id = this.options.idProvider();
123
+ }
124
+ user.steedos_id = user._id;
125
+ const ret = await this.collection.insertOne(user);
126
+ return user._id;
127
+ }
128
+ async findUserById(userId) {
129
+ const id = this.options.convertUserIdToMongoObjectId
130
+ ? toMongoID(userId)
131
+ : userId;
132
+ const user = await this.collection.findOne({ _id: id });
133
+ if (user) {
134
+ user.id = user._id.toString();
135
+ }
136
+ return user;
137
+ }
138
+ async findUserByEmail(email) {
139
+ if (!(0, lodash_2.isString)(email)) {
140
+ return null;
141
+ }
142
+ const user = await this.collection.findOne({
143
+ email: email.toLowerCase(),
502
144
  });
503
- };
504
- Mongo.prototype.setMobile = function (userId, newMobile) {
505
- return tslib_1.__awaiter(this, void 0, void 0, function () {
506
- var id, encryptedMobile, existed, user, ret;
507
- var _a;
508
- return tslib_1.__generator(this, function (_b) {
509
- switch (_b.label) {
510
- case 0:
511
- if (!(0, lodash_2.isString)(newMobile)) {
512
- return [2, null];
513
- }
514
- id = this.options.convertUserIdToMongoObjectId
515
- ? toMongoID(userId)
516
- : userId;
517
- return [4, this.getEncryptedSpaceUserFieldValue(newMobile, "mobile")];
518
- case 1:
519
- encryptedMobile = (_b.sent()) ||
520
- newMobile;
521
- return [4, this.collection
522
- .find({ _id: { $ne: id }, mobile: encryptedMobile })
523
- .count()];
524
- case 2:
525
- existed = _b.sent();
526
- if (existed > 0) {
527
- throw new Error("该手机号已被其他用户注册");
528
- }
529
- return [4, this.collection.findOne({ _id: id }, { projection: { mobile: 1 } })];
530
- case 3:
531
- user = _b.sent();
532
- if (!(user && user.mobile != newMobile)) return [3, 6];
533
- return [4, this.collection.updateOne({ _id: id }, {
534
- $set: (_a = {
535
- mobile: encryptedMobile
536
- },
537
- _a[this.options.timestamps.updatedAt] = this.options.dateProvider(),
538
- _a),
539
- $pull: {
540
- "services.mobile.verificationTokens": { mobile: encryptedMobile },
541
- },
542
- })];
543
- case 4:
544
- ret = _b.sent();
545
- return [4, this.spaceUserCollection.updateMany({ user: id }, {
546
- $set: {
547
- mobile: encryptedMobile,
548
- modified: this.options.dateProvider(),
549
- modified_by: id,
550
- },
551
- })];
552
- case 5:
553
- _b.sent();
554
- _b.label = 6;
555
- case 6:
556
- if (!user) {
557
- throw new Error("User not found");
558
- }
559
- return [2];
560
- }
561
- });
145
+ if (user) {
146
+ user.id = user._id.toString();
147
+ }
148
+ return user;
149
+ }
150
+ async findUserByMobile(mobile) {
151
+ if (!(0, lodash_2.isString)(mobile)) {
152
+ return null;
153
+ }
154
+ const selector = {
155
+ mobile,
156
+ };
157
+ const encryptedMobile = await this.getEncryptedSpaceUserFieldValue(mobile, "mobile");
158
+ if (encryptedMobile) {
159
+ selector.mobile = encryptedMobile;
160
+ }
161
+ const user = await this.collection.findOne(selector);
162
+ if (user) {
163
+ user.id = user._id.toString();
164
+ }
165
+ return user;
166
+ }
167
+ async findUserByUsername(username) {
168
+ if (!(0, lodash_2.isString)(username)) {
169
+ return null;
170
+ }
171
+ const filter = this.options.caseSensitiveUserName
172
+ ? { username }
173
+ : {
174
+ $where: `obj.username && (obj.username.toLowerCase() === "${username.toLowerCase()}")`,
175
+ };
176
+ const user = await this.collection.findOne(filter);
177
+ if (user) {
178
+ user.id = user._id.toString();
179
+ }
180
+ return user;
181
+ }
182
+ async findPasswordHash(userId) {
183
+ const user = await this.findUserById(userId);
184
+ if (user) {
185
+ return (0, lodash_1.get)(user, "services.password.bcrypt");
186
+ }
187
+ return null;
188
+ }
189
+ async findUserByEmailVerificationToken(token) {
190
+ if (!(0, lodash_2.isString)(token)) {
191
+ return null;
192
+ }
193
+ const user = await this.collection.findOne({
194
+ "services.email.verificationTokens.token": token,
562
195
  });
563
- };
564
- Mongo.prototype.setEmail = function (userId, newEmail) {
565
- return tslib_1.__awaiter(this, void 0, void 0, function () {
566
- var id, existed, user, ret;
567
- var _a;
568
- return tslib_1.__generator(this, function (_b) {
569
- switch (_b.label) {
570
- case 0:
571
- if (!(0, lodash_2.isString)(newEmail)) {
572
- return [2, null];
573
- }
574
- id = this.options.convertUserIdToMongoObjectId
575
- ? toMongoID(userId)
576
- : userId;
577
- return [4, this.collection
578
- .find({ _id: { $ne: id }, email: newEmail })
579
- .count()];
580
- case 1:
581
- existed = _b.sent();
582
- if (existed > 0) {
583
- throw new Error("该邮箱已被其他用户注册");
584
- }
585
- return [4, this.collection.findOne({ _id: id }, { projection: { email: 1 } })];
586
- case 2:
587
- user = _b.sent();
588
- if (!(user && user.email != newEmail)) return [3, 5];
589
- return [4, this.collection.updateOne({ _id: id }, {
590
- $set: (_a = {
591
- email: newEmail
592
- },
593
- _a[this.options.timestamps.updatedAt] = this.options.dateProvider(),
594
- _a),
595
- $pull: {
596
- "services.email.verificationTokens": { address: newEmail },
597
- },
598
- })];
599
- case 3:
600
- ret = _b.sent();
601
- return [4, this.spaceUserCollection.updateMany({ user: id }, {
602
- $set: {
603
- email: newEmail,
604
- modified: this.options.dateProvider(),
605
- modified_by: id,
606
- },
607
- })];
608
- case 4:
609
- _b.sent();
610
- _b.label = 5;
611
- case 5:
612
- if (!user) {
613
- throw new Error("User not found");
614
- }
615
- return [2];
616
- }
617
- });
196
+ if (user) {
197
+ user.id = user._id.toString();
198
+ }
199
+ return user;
200
+ }
201
+ async findUserByResetPasswordToken(token) {
202
+ if (!(0, lodash_2.isString)(token)) {
203
+ return null;
204
+ }
205
+ const user = await this.collection.findOne({
206
+ "services.password.reset.token": token,
618
207
  });
619
- };
620
- Mongo.prototype.setUsername = function (userId, newUsername) {
621
- return tslib_1.__awaiter(this, void 0, void 0, function () {
622
- var id, ret;
623
- var _a;
624
- return tslib_1.__generator(this, function (_b) {
625
- switch (_b.label) {
626
- case 0:
627
- id = this.options.convertUserIdToMongoObjectId
628
- ? toMongoID(userId)
629
- : userId;
630
- return [4, this.collection.updateOne({ _id: id }, {
631
- $set: (_a = {
632
- username: newUsername
633
- },
634
- _a[this.options.timestamps.updatedAt] = this.options.dateProvider(),
635
- _a),
636
- })];
637
- case 1:
638
- ret = _b.sent();
639
- if (ret.matchedCount === 0) {
640
- throw new Error("User not found");
641
- }
642
- return [2];
643
- }
644
- });
208
+ if (user) {
209
+ user.id = user._id.toString();
210
+ }
211
+ return user;
212
+ }
213
+ async findUserByServiceId(serviceName, serviceId) {
214
+ if (!(0, lodash_2.isString)(serviceId)) {
215
+ return null;
216
+ }
217
+ const user = await this.collection.findOne({
218
+ [`services.${serviceName}.id`]: serviceId,
645
219
  });
646
- };
647
- Mongo.prototype.setPassword = function (userId, newPassword) {
648
- return tslib_1.__awaiter(this, void 0, void 0, function () {
649
- var id, ret;
650
- var _a;
651
- return tslib_1.__generator(this, function (_b) {
652
- switch (_b.label) {
653
- case 0:
654
- id = this.options.convertUserIdToMongoObjectId
655
- ? toMongoID(userId)
656
- : userId;
657
- return [4, this.collection.updateOne({ _id: id }, {
658
- $set: (_a = {
659
- "services.password.bcrypt": newPassword
660
- },
661
- _a[this.options.timestamps.updatedAt] = this.options.dateProvider(),
662
- _a),
663
- $push: {
664
- "services.password_history": newPassword,
665
- },
666
- $unset: {
667
- "services.password.reset": "",
668
- },
669
- })];
670
- case 1:
671
- ret = _b.sent();
672
- if (ret.matchedCount === 0) {
673
- throw new Error("User not found");
674
- }
675
- return [2];
676
- }
220
+ if (user) {
221
+ user.id = user._id.toString();
222
+ }
223
+ return user;
224
+ }
225
+ async addEmail(userId, newEmail, verified) {
226
+ const id = this.options.convertUserIdToMongoObjectId
227
+ ? toMongoID(userId)
228
+ : userId;
229
+ const ret = await this.collection.updateOne({ _id: id }, {
230
+ $addToSet: {
231
+ emails: {
232
+ address: newEmail.toLowerCase(),
233
+ verified,
234
+ },
235
+ },
236
+ $set: {
237
+ [this.options.timestamps.updatedAt]: this.options.dateProvider(),
238
+ },
239
+ });
240
+ if (ret.matchedCount === 0) {
241
+ throw new Error("User not found");
242
+ }
243
+ }
244
+ async removeEmail(userId, email) {
245
+ const id = this.options.convertUserIdToMongoObjectId
246
+ ? toMongoID(userId)
247
+ : userId;
248
+ const ret = await this.collection.updateOne({ _id: id }, {
249
+ $pull: { emails: { address: email.toLowerCase() } },
250
+ $set: {
251
+ [this.options.timestamps.updatedAt]: this.options.dateProvider(),
252
+ },
253
+ });
254
+ if (ret.matchedCount === 0) {
255
+ throw new Error("User not found");
256
+ }
257
+ }
258
+ async verifyEmail(userId, email) {
259
+ const id = this.options.convertUserIdToMongoObjectId
260
+ ? toMongoID(userId)
261
+ : userId;
262
+ const ret = await this.collection.updateOne({ _id: id, email: email }, {
263
+ $set: {
264
+ email_verified: true,
265
+ [this.options.timestamps.updatedAt]: this.options.dateProvider(),
266
+ },
267
+ $pull: {
268
+ "services.email.verificationTokens": { address: email },
269
+ },
270
+ });
271
+ await this.spaceUserCollection.updateMany({ user: id }, {
272
+ $set: {
273
+ email_verified: true,
274
+ modified: this.options.dateProvider(),
275
+ modified_by: id,
276
+ },
277
+ });
278
+ if (ret.matchedCount === 0) {
279
+ throw new Error("User not found");
280
+ }
281
+ }
282
+ async getEncryptedSpaceUserFieldValue(value, fieldName) {
283
+ const objectql = require("@steedos/objectql");
284
+ const objFields = await objectql.getObject("space_users").getFields();
285
+ if (objFields[fieldName].enable_encryption) {
286
+ const datasource = (0, objectql_1.getDataSource)("default");
287
+ const encryptedValue = await datasource.adapter.encryptValue(value);
288
+ if (encryptedValue) {
289
+ return encryptedValue;
290
+ }
291
+ }
292
+ }
293
+ async verifyMobile(userId, mobile) {
294
+ if (!(0, lodash_2.isString)(mobile)) {
295
+ return null;
296
+ }
297
+ const id = this.options.convertUserIdToMongoObjectId
298
+ ? toMongoID(userId)
299
+ : userId;
300
+ const encryptedMobile = await this.getEncryptedSpaceUserFieldValue(mobile, "mobile");
301
+ const ret = await this.collection.updateOne({ _id: id, mobile: encryptedMobile || mobile }, {
302
+ $set: {
303
+ mobile_verified: true,
304
+ [this.options.timestamps.updatedAt]: this.options.dateProvider(),
305
+ },
306
+ $pull: {
307
+ "services.mobile.verificationTokens": {
308
+ mobile: encryptedMobile || mobile,
309
+ },
310
+ },
311
+ });
312
+ await this.spaceUserCollection.updateMany({ user: id }, {
313
+ $set: {
314
+ mobile_verified: true,
315
+ modified: this.options.dateProvider(),
316
+ modified_by: id,
317
+ },
318
+ });
319
+ if (ret.matchedCount === 0) {
320
+ throw new Error("User not found");
321
+ }
322
+ }
323
+ async setMobile(userId, newMobile) {
324
+ if (!(0, lodash_2.isString)(newMobile)) {
325
+ return null;
326
+ }
327
+ const id = this.options.convertUserIdToMongoObjectId
328
+ ? toMongoID(userId)
329
+ : userId;
330
+ const encryptedMobile = (await this.getEncryptedSpaceUserFieldValue(newMobile, "mobile")) ||
331
+ newMobile;
332
+ let existed = await this.collection
333
+ .find({ _id: { $ne: id }, mobile: encryptedMobile })
334
+ .count();
335
+ if (existed > 0) {
336
+ throw new Error("该手机号已被其他用户注册");
337
+ }
338
+ let user = await this.collection.findOne({ _id: id }, { projection: { mobile: 1 } });
339
+ if (user && user.mobile != newMobile) {
340
+ const ret = await this.collection.updateOne({ _id: id }, {
341
+ $set: {
342
+ mobile: encryptedMobile,
343
+ [this.options.timestamps.updatedAt]: this.options.dateProvider(),
344
+ },
345
+ $pull: {
346
+ "services.mobile.verificationTokens": { mobile: encryptedMobile },
347
+ },
348
+ });
349
+ await this.spaceUserCollection.updateMany({ user: id }, {
350
+ $set: {
351
+ mobile: encryptedMobile,
352
+ modified: this.options.dateProvider(),
353
+ modified_by: id,
354
+ },
677
355
  });
678
- });
679
- };
680
- Mongo.prototype.setService = function (userId, serviceName, service) {
681
- return tslib_1.__awaiter(this, void 0, void 0, function () {
682
- var id;
683
- var _a;
684
- return tslib_1.__generator(this, function (_b) {
685
- switch (_b.label) {
686
- case 0:
687
- id = this.options.convertUserIdToMongoObjectId
688
- ? toMongoID(userId)
689
- : userId;
690
- return [4, this.collection.updateOne({ _id: id }, {
691
- $set: (_a = {},
692
- _a["services.".concat(serviceName)] = service,
693
- _a[this.options.timestamps.updatedAt] = this.options.dateProvider(),
694
- _a),
695
- })];
696
- case 1:
697
- _b.sent();
698
- return [2];
699
- }
356
+ }
357
+ if (!user) {
358
+ throw new Error("User not found");
359
+ }
360
+ }
361
+ async setEmail(userId, newEmail) {
362
+ if (!(0, lodash_2.isString)(newEmail)) {
363
+ return null;
364
+ }
365
+ const id = this.options.convertUserIdToMongoObjectId
366
+ ? toMongoID(userId)
367
+ : userId;
368
+ let existed = await this.collection
369
+ .find({ _id: { $ne: id }, email: newEmail })
370
+ .count();
371
+ if (existed > 0) {
372
+ throw new Error("该邮箱已被其他用户注册");
373
+ }
374
+ let user = await this.collection.findOne({ _id: id }, { projection: { email: 1 } });
375
+ if (user && user.email != newEmail) {
376
+ const ret = await this.collection.updateOne({ _id: id }, {
377
+ $set: {
378
+ email: newEmail,
379
+ [this.options.timestamps.updatedAt]: this.options.dateProvider(),
380
+ },
381
+ $pull: {
382
+ "services.email.verificationTokens": { address: newEmail },
383
+ },
384
+ });
385
+ await this.spaceUserCollection.updateMany({ user: id }, {
386
+ $set: {
387
+ email: newEmail,
388
+ modified: this.options.dateProvider(),
389
+ modified_by: id,
390
+ },
700
391
  });
392
+ }
393
+ if (!user) {
394
+ throw new Error("User not found");
395
+ }
396
+ }
397
+ async setUsername(userId, newUsername) {
398
+ const id = this.options.convertUserIdToMongoObjectId
399
+ ? toMongoID(userId)
400
+ : userId;
401
+ const ret = await this.collection.updateOne({ _id: id }, {
402
+ $set: {
403
+ username: newUsername,
404
+ [this.options.timestamps.updatedAt]: this.options.dateProvider(),
405
+ },
406
+ });
407
+ if (ret.matchedCount === 0) {
408
+ throw new Error("User not found");
409
+ }
410
+ }
411
+ async setPassword(userId, newPassword) {
412
+ const id = this.options.convertUserIdToMongoObjectId
413
+ ? toMongoID(userId)
414
+ : userId;
415
+ const ret = await this.collection.updateOne({ _id: id }, {
416
+ $set: {
417
+ "services.password.bcrypt": newPassword,
418
+ [this.options.timestamps.updatedAt]: this.options.dateProvider(),
419
+ },
420
+ $push: {
421
+ "services.password_history": newPassword,
422
+ },
423
+ $unset: {
424
+ "services.password.reset": "",
425
+ },
426
+ });
427
+ if (ret.matchedCount === 0) {
428
+ throw new Error("User not found");
429
+ }
430
+ }
431
+ async setService(userId, serviceName, service) {
432
+ const id = this.options.convertUserIdToMongoObjectId
433
+ ? toMongoID(userId)
434
+ : userId;
435
+ await this.collection.updateOne({ _id: id }, {
436
+ $set: {
437
+ [`services.${serviceName}`]: service,
438
+ [this.options.timestamps.updatedAt]: this.options.dateProvider(),
439
+ },
701
440
  });
702
- };
703
- Mongo.prototype.unsetService = function (userId, serviceName) {
704
- return tslib_1.__awaiter(this, void 0, void 0, function () {
705
- var id;
706
- var _a, _b;
707
- return tslib_1.__generator(this, function (_c) {
708
- switch (_c.label) {
709
- case 0:
710
- id = this.options.convertUserIdToMongoObjectId
711
- ? toMongoID(userId)
712
- : userId;
713
- return [4, this.collection.updateOne({ _id: id }, {
714
- $set: (_a = {},
715
- _a[this.options.timestamps.updatedAt] = this.options.dateProvider(),
716
- _a),
717
- $unset: (_b = {},
718
- _b["services.".concat(serviceName)] = "",
719
- _b),
720
- })];
721
- case 1:
722
- _c.sent();
723
- return [2];
724
- }
725
- });
441
+ }
442
+ async unsetService(userId, serviceName) {
443
+ const id = this.options.convertUserIdToMongoObjectId
444
+ ? toMongoID(userId)
445
+ : userId;
446
+ await this.collection.updateOne({ _id: id }, {
447
+ $set: {
448
+ [this.options.timestamps.updatedAt]: this.options.dateProvider(),
449
+ },
450
+ $unset: {
451
+ [`services.${serviceName}`]: "",
452
+ },
726
453
  });
727
- };
728
- Mongo.prototype.setUserDeactivated = function (userId, deactivated) {
729
- return tslib_1.__awaiter(this, void 0, void 0, function () {
730
- var id;
731
- var _a;
732
- return tslib_1.__generator(this, function (_b) {
733
- switch (_b.label) {
734
- case 0:
735
- id = this.options.convertUserIdToMongoObjectId
736
- ? toMongoID(userId)
737
- : userId;
738
- return [4, this.collection.updateOne({ _id: id }, {
739
- $set: (_a = {
740
- deactivated: deactivated
741
- },
742
- _a[this.options.timestamps.updatedAt] = this.options.dateProvider(),
743
- _a),
744
- })];
745
- case 1:
746
- _b.sent();
747
- return [2];
748
- }
749
- });
454
+ }
455
+ async setUserDeactivated(userId, deactivated) {
456
+ const id = this.options.convertUserIdToMongoObjectId
457
+ ? toMongoID(userId)
458
+ : userId;
459
+ await this.collection.updateOne({ _id: id }, {
460
+ $set: {
461
+ deactivated,
462
+ [this.options.timestamps.updatedAt]: this.options.dateProvider(),
463
+ },
750
464
  });
751
- };
752
- Mongo.prototype.resolveInfo = function (connection) {
753
- if (connection === void 0) { connection = {}; }
754
- var ip = connection.ip;
755
- var userAgent = connection.userAgent;
756
- var login_expiration_in_days = connection.login_expiration_in_days;
757
- var phone_login_expiration_in_days = connection.phone_login_expiration_in_days;
758
- var is_phone = connection.is_phone;
759
- var is_tablet = connection.is_tablet;
760
- var space = connection.space;
761
- var provider = connection.provider;
465
+ }
466
+ resolveInfo(connection = {}) {
467
+ let ip = connection.ip;
468
+ let userAgent = connection.userAgent;
469
+ let login_expiration_in_days = connection.login_expiration_in_days;
470
+ let phone_login_expiration_in_days = connection.phone_login_expiration_in_days;
471
+ let is_phone = connection.is_phone;
472
+ let is_tablet = connection.is_tablet;
473
+ let space = connection.space;
474
+ let provider = connection.provider;
762
475
  if (userAgent) {
763
- var foo = userAgent.split(" Space/");
476
+ const foo = userAgent.split(" Space/");
764
477
  if (foo.length > 1) {
765
478
  userAgent = foo[0];
766
479
  space = foo[1];
@@ -771,577 +484,346 @@ var Mongo = (function () {
771
484
  }
772
485
  if (space) {
773
486
  return {
774
- ip: ip,
775
- userAgent: userAgent,
776
- space: space,
777
- is_phone: is_phone,
778
- is_tablet: is_tablet,
779
- login_expiration_in_days: login_expiration_in_days,
487
+ ip,
488
+ userAgent,
489
+ space,
490
+ is_phone,
491
+ is_tablet,
492
+ login_expiration_in_days,
780
493
  user_provider: provider,
781
494
  };
782
495
  }
783
496
  return {
784
- ip: ip,
785
- userAgent: userAgent,
786
- is_phone: is_phone,
787
- is_tablet: is_tablet,
788
- login_expiration_in_days: login_expiration_in_days,
497
+ ip,
498
+ userAgent,
499
+ is_phone,
500
+ is_tablet,
501
+ login_expiration_in_days,
789
502
  user_provider: provider,
790
503
  };
791
- };
792
- Mongo.prototype.createSession = function (userId_1, token_1) {
793
- return tslib_1.__awaiter(this, arguments, void 0, function (userId, token, connection, extraData) {
794
- var infos, session, ret;
795
- var _a;
796
- if (connection === void 0) { connection = {}; }
797
- return tslib_1.__generator(this, function (_b) {
798
- switch (_b.label) {
799
- case 0:
800
- infos = this.resolveInfo(connection);
801
- session = tslib_1.__assign(tslib_1.__assign({ userId: userId, token: token }, infos), (_a = { extraData: extraData, valid: true }, _a[this.options.timestamps.createdAt] = this.options.dateProvider(), _a[this.options.timestamps.updatedAt] = this.options.dateProvider(), _a));
802
- if (this.options.idProvider) {
803
- session._id = this.options.idProvider();
804
- }
805
- return [4, this.sessionCollection.insertOne(session)];
806
- case 1:
807
- ret = _b.sent();
808
- return [4, this.updateMeteorSession(userId, token, infos)];
809
- case 2:
810
- _b.sent();
811
- return [2, ret.insertedId.toString()];
812
- }
813
- });
814
- });
815
- };
816
- Mongo.prototype.updateSession = function (sessionId, connection) {
817
- return tslib_1.__awaiter(this, void 0, void 0, function () {
818
- var _id, infos, _set;
819
- var _a;
820
- return tslib_1.__generator(this, function (_b) {
821
- switch (_b.label) {
822
- case 0:
823
- _id = this.options.convertSessionIdToMongoObjectId
824
- ? toMongoID(sessionId)
825
- : sessionId;
826
- infos = this.resolveInfo(connection);
827
- _set = (_a = {
828
- userAgent: infos.userAgent,
829
- ip: infos.ip
830
- },
831
- _a[this.options.timestamps.updatedAt] = this.options.dateProvider(),
832
- _a);
833
- if (infos.space) {
834
- _set.space = infos.space;
835
- }
836
- return [4, this.sessionCollection.updateOne({ _id: _id }, {
837
- $set: _set,
838
- })];
839
- case 1:
840
- _b.sent();
841
- return [2];
842
- }
843
- });
844
- });
845
- };
846
- Mongo.prototype.invalidateSession = function (sessionId) {
847
- return tslib_1.__awaiter(this, void 0, void 0, function () {
848
- var _id, session;
849
- var _a;
850
- return tslib_1.__generator(this, function (_b) {
851
- switch (_b.label) {
852
- case 0:
853
- _id = this.options.convertSessionIdToMongoObjectId
854
- ? toMongoID(sessionId)
855
- : sessionId;
856
- return [4, this.sessionCollection.updateOne({ _id: _id }, {
857
- $set: (_a = {
858
- valid: false
859
- },
860
- _a[this.options.timestamps.updatedAt] = this.options.dateProvider(),
861
- _a),
862
- })];
863
- case 1:
864
- _b.sent();
865
- return [4, this.sessionCollection.findOne({
866
- _id: _id,
867
- })];
868
- case 2:
869
- session = _b.sent();
870
- return [4, this.destroyMeteorToken(session.userId, session.token)];
871
- case 3:
872
- _b.sent();
873
- return [2];
874
- }
875
- });
876
- });
877
- };
878
- Mongo.prototype.invalidateAllSessions = function (userId) {
879
- return tslib_1.__awaiter(this, void 0, void 0, function () {
880
- var _a;
881
- return tslib_1.__generator(this, function (_b) {
882
- switch (_b.label) {
883
- case 0: return [4, this.sessionCollection.updateMany({ userId: userId }, {
884
- $set: (_a = {
885
- valid: false
886
- },
887
- _a[this.options.timestamps.updatedAt] = this.options.dateProvider(),
888
- _a),
889
- })];
890
- case 1:
891
- _b.sent();
892
- return [2];
893
- }
894
- });
895
- });
896
- };
897
- Mongo.prototype.findSessionByToken = function (token) {
898
- return tslib_1.__awaiter(this, void 0, void 0, function () {
899
- var session;
900
- return tslib_1.__generator(this, function (_a) {
901
- switch (_a.label) {
902
- case 0: return [4, this.sessionCollection.findOne({ token: token })];
903
- case 1:
904
- session = _a.sent();
905
- if (session) {
906
- session.id = session._id.toString();
907
- }
908
- return [2, session];
909
- }
910
- });
911
- });
912
- };
913
- Mongo.prototype.findSessionById = function (sessionId) {
914
- return tslib_1.__awaiter(this, void 0, void 0, function () {
915
- var _id, session;
916
- return tslib_1.__generator(this, function (_a) {
917
- switch (_a.label) {
918
- case 0:
919
- _id = this.options.convertSessionIdToMongoObjectId
920
- ? toMongoID(sessionId)
921
- : sessionId;
922
- return [4, this.sessionCollection.findOne({ _id: _id })];
923
- case 1:
924
- session = _a.sent();
925
- if (session) {
926
- session.id = session._id.toString();
927
- }
928
- return [2, session];
929
- }
930
- });
931
- });
932
- };
933
- Mongo.prototype.addEmailVerificationToken = function (userId, email, token) {
934
- return tslib_1.__awaiter(this, void 0, void 0, function () {
935
- var _id;
936
- return tslib_1.__generator(this, function (_a) {
937
- switch (_a.label) {
938
- case 0:
939
- _id = this.options.convertUserIdToMongoObjectId
940
- ? toMongoID(userId)
941
- : userId;
942
- return [4, this.collection.updateOne({ _id: _id }, {
943
- $push: {
944
- "services.email.verificationTokens": {
945
- token: token,
946
- address: email.toLowerCase(),
947
- when: this.options.dateProvider(),
948
- },
949
- },
950
- })];
951
- case 1:
952
- _a.sent();
953
- return [2];
954
- }
955
- });
956
- });
957
- };
958
- Mongo.prototype.addResetPasswordToken = function (userId, email, token, reason) {
959
- return tslib_1.__awaiter(this, void 0, void 0, function () {
960
- var _id;
961
- return tslib_1.__generator(this, function (_a) {
962
- switch (_a.label) {
963
- case 0:
964
- _id = this.options.convertUserIdToMongoObjectId
965
- ? toMongoID(userId)
966
- : userId;
967
- return [4, this.collection.updateOne({ _id: _id }, {
968
- $push: {
969
- "services.password.reset": {
970
- token: token,
971
- address: email.toLowerCase(),
972
- when: this.options.dateProvider(),
973
- reason: reason,
974
- },
975
- },
976
- })];
977
- case 1:
978
- _a.sent();
979
- return [2];
980
- }
981
- });
982
- });
983
- };
984
- Mongo.prototype.setResetPassword = function (userId, email, newPassword) {
985
- return tslib_1.__awaiter(this, void 0, void 0, function () {
986
- return tslib_1.__generator(this, function (_a) {
987
- switch (_a.label) {
988
- case 0: return [4, this.setPassword(userId, newPassword)];
989
- case 1:
990
- _a.sent();
991
- return [2];
992
- }
993
- });
994
- });
995
- };
996
- Mongo.prototype.applyCode = function (name, owner, nextCode, MAX_FAILURE_COUNT, EFFECTIVE_TIME) {
997
- return tslib_1.__awaiter(this, void 0, void 0, function () {
998
- var now, query, record, doc, result;
999
- var _a;
1000
- return tslib_1.__generator(this, function (_b) {
1001
- switch (_b.label) {
1002
- case 0:
1003
- now = new Date();
1004
- if (!(0, lodash_2.isString)(name)) {
1005
- throw new Error("accounts.invalidCode");
1006
- }
1007
- query = {
1008
- name: name,
1009
- verifiedAt: null,
1010
- expiredAt: { $gt: now },
1011
- };
1012
- if (owner) {
1013
- query.owner = owner;
1014
- }
1015
- return [4, this.codeCollection.findOne(query)];
1016
- case 1:
1017
- record = _b.sent();
1018
- if (!record) return [3, 2];
1019
- return [3, 4];
1020
- case 2:
1021
- doc = (_a = {
1022
- name: name,
1023
- owner: owner,
1024
- code: nextCode,
1025
- expiredAt: new Date(moment().add(EFFECTIVE_TIME, "m"))
1026
- },
1027
- _a[this.options.timestamps.createdAt] = this.options.dateProvider(),
1028
- _a);
1029
- if (this.options.idProvider) {
1030
- doc._id = this.options.idProvider();
1031
- }
1032
- return [4, this.codeCollection.insertOne(doc)];
1033
- case 3:
1034
- result = _b.sent();
1035
- record = result.ops[0];
1036
- _b.label = 4;
1037
- case 4: return [2, record];
1038
- }
1039
- });
1040
- });
1041
- };
1042
- Mongo.prototype.addVerificationCode = function (user, code, options) {
1043
- return tslib_1.__awaiter(this, void 0, void 0, function () {
1044
- var foundedUser, owner, ret;
1045
- return tslib_1.__generator(this, function (_a) {
1046
- switch (_a.label) {
1047
- case 0:
1048
- foundedUser = null;
1049
- if (!user.email) return [3, 2];
1050
- return [4, this.findUserByEmail(user.email)];
1051
- case 1:
1052
- foundedUser = _a.sent();
1053
- return [3, 4];
1054
- case 2:
1055
- if (!user.mobile) return [3, 4];
1056
- return [4, this.findUserByMobile(user.mobile)];
1057
- case 3:
1058
- foundedUser = _a.sent();
1059
- _a.label = 4;
1060
- case 4:
1061
- owner = foundedUser ? foundedUser.id : null;
1062
- return [4, this.applyCode(user.email ? user.email : user.mobile, owner, code, options.MAX_FAILURE_COUNT, options.EFFECTIVE_TIME)];
1063
- case 5:
1064
- ret = _a.sent();
1065
- return [2, ret];
1066
- }
1067
- });
1068
- });
1069
- };
1070
- Mongo.prototype.verifyCodeByName = function (name, code) {
1071
- return tslib_1.__awaiter(this, void 0, void 0, function () {
1072
- var now, query, result;
1073
- return tslib_1.__generator(this, function (_a) {
1074
- switch (_a.label) {
1075
- case 0:
1076
- if (!(0, lodash_2.isString)(code)) {
1077
- throw new Error("accounts.invalidCode");
1078
- }
1079
- if (!(0, lodash_2.isString)(name)) {
1080
- throw new Error("accounts.invalidCode");
1081
- }
1082
- now = new Date();
1083
- query = {
1084
- name: name,
1085
- code: code,
1086
- verifiedAt: null,
1087
- expiredAt: { $gt: now },
1088
- };
1089
- return [4, this.codeCollection.findOne(query)];
1090
- case 1:
1091
- result = _a.sent();
1092
- if (!result) return [3, 3];
1093
- return [4, this.codeCollection.updateOne({ _id: result._id }, { $set: { verifiedAt: now } })];
1094
- case 2:
1095
- _a.sent();
1096
- return [2, result];
1097
- case 3: throw new Error("accounts.invalidCode");
1098
- }
1099
- });
1100
- });
1101
- };
1102
- Mongo.prototype.verifyCodeByOwner = function (owner, code) {
1103
- return tslib_1.__awaiter(this, void 0, void 0, function () {
1104
- var now, query, result;
1105
- return tslib_1.__generator(this, function (_a) {
1106
- switch (_a.label) {
1107
- case 0:
1108
- if (!(0, lodash_2.isString)(code)) {
1109
- throw new Error("accounts.invalidCode");
1110
- }
1111
- if (!(0, lodash_2.isString)(owner)) {
1112
- throw new Error("accounts.invalidCode");
1113
- }
1114
- now = new Date();
1115
- query = {
1116
- owner: owner,
1117
- code: code,
1118
- verifiedAt: null,
1119
- expiredAt: { $gt: now },
1120
- };
1121
- return [4, this.codeCollection.findOne(query)];
1122
- case 1:
1123
- result = _a.sent();
1124
- if (!result) return [3, 3];
1125
- return [4, this.codeCollection.updateOne({ _id: result._id }, { $set: { verifiedAt: now } })];
1126
- case 2:
1127
- _a.sent();
1128
- return [2, result];
1129
- case 3:
1130
- console.log("verifyCodeByOwner throw new Error accounts.invalidCode");
1131
- throw new Error("accounts.invalidCode");
1132
- }
1133
- });
1134
- });
1135
- };
1136
- Mongo.prototype.checkVerificationCode = function (user, code) {
1137
- return tslib_1.__awaiter(this, void 0, void 0, function () {
1138
- var name, record;
1139
- return tslib_1.__generator(this, function (_a) {
1140
- switch (_a.label) {
1141
- case 0:
1142
- name = null;
1143
- if (user.email)
1144
- name = user.email;
1145
- else if (user.mobile)
1146
- name = user.mobile;
1147
- if (!name)
1148
- return [2, false];
1149
- return [4, this.verifyCodeByName(name, code)];
1150
- case 1:
1151
- record = _a.sent();
1152
- if (!record)
1153
- return [2, false];
1154
- return [2, true];
1155
- }
1156
- });
1157
- });
1158
- };
1159
- Mongo.prototype.findUserByVerificationCode = function (user, code) {
1160
- return tslib_1.__awaiter(this, void 0, void 0, function () {
1161
- var foundedUser, owner, record;
1162
- return tslib_1.__generator(this, function (_a) {
1163
- switch (_a.label) {
1164
- case 0:
1165
- foundedUser = null;
1166
- if (!user.email) return [3, 2];
1167
- return [4, this.findUserByEmail(user.email)];
1168
- case 1:
1169
- foundedUser = _a.sent();
1170
- return [3, 4];
1171
- case 2:
1172
- if (!user.mobile) return [3, 4];
1173
- return [4, this.findUserByMobile(user.mobile)];
1174
- case 3:
1175
- foundedUser = _a.sent();
1176
- _a.label = 4;
1177
- case 4:
1178
- if (!foundedUser)
1179
- return [2, null];
1180
- owner = foundedUser.id;
1181
- return [4, this.verifyCodeByOwner(owner, code)];
1182
- case 5:
1183
- record = _a.sent();
1184
- if (!record)
1185
- return [2, null];
1186
- if (!(user.email && foundedUser.email_verified != true)) return [3, 8];
1187
- return [4, this.verifyEmail(owner, user.email)];
1188
- case 6:
1189
- _a.sent();
1190
- return [4, this.findUserById(owner)];
1191
- case 7:
1192
- foundedUser = _a.sent();
1193
- return [3, 11];
1194
- case 8:
1195
- if (!(user.mobile && foundedUser.mobile_verified != true)) return [3, 11];
1196
- return [4, this.verifyMobile(owner, user.mobile)];
1197
- case 9:
1198
- _a.sent();
1199
- return [4, this.findUserById(owner)];
1200
- case 10:
1201
- foundedUser = _a.sent();
1202
- _a.label = 11;
1203
- case 11: return [2, foundedUser];
1204
- }
1205
- });
1206
- });
1207
- };
1208
- Mongo.prototype.getMySpaces = function (userId) {
1209
- return tslib_1.__awaiter(this, void 0, void 0, function () {
1210
- var userSpaces, spaceIds, spaces;
1211
- return tslib_1.__generator(this, function (_a) {
1212
- switch (_a.label) {
1213
- case 0: return [4, this.db
1214
- .collection("space_users")
1215
- .find({ user: userId, invite_state: { $ne: "refused" } })
1216
- .project({ space: 1, user_accepted: 1, invite_state: 1 })
1217
- .toArray()];
1218
- case 1:
1219
- userSpaces = _a.sent();
1220
- spaceIds = (0, lodash_1.map)(userSpaces, "space");
1221
- return [4, this.db
1222
- .collection("spaces")
1223
- .find({ _id: { $in: spaceIds } })
1224
- .project({
1225
- name: 1,
1226
- favicon: 1,
1227
- account_logo: 1,
1228
- avatar_square: 1,
1229
- avatar: 1,
1230
- background: 1,
1231
- enable_register: 1,
1232
- })
1233
- .toArray()];
1234
- case 2:
1235
- spaces = _a.sent();
1236
- return [2, (0, lodash_1.map)(spaces, function (space) {
1237
- var spaceUser = (0, lodash_1.find)(userSpaces, function (item) {
1238
- return item.space == space._id;
1239
- });
1240
- return tslib_1.__assign(tslib_1.__assign({}, space), { user_accepted: spaceUser.user_accepted, invite_state: spaceUser.invite_state });
1241
- })];
1242
- }
1243
- });
1244
- });
1245
- };
1246
- Mongo.prototype.getFirstSpace = function () {
1247
- return tslib_1.__awaiter(this, void 0, void 0, function () {
1248
- var space;
1249
- return tslib_1.__generator(this, function (_a) {
1250
- switch (_a.label) {
1251
- case 0: return [4, this.db.collection("spaces").findOne()];
1252
- case 1:
1253
- space = _a.sent();
1254
- return [2, space];
1255
- }
1256
- });
504
+ }
505
+ async createSession(userId, token, connection = {}, extraData) {
506
+ const infos = this.resolveInfo(connection);
507
+ const session = {
508
+ userId,
509
+ token,
510
+ ...infos,
511
+ extraData,
512
+ valid: true,
513
+ [this.options.timestamps.createdAt]: this.options.dateProvider(),
514
+ [this.options.timestamps.updatedAt]: this.options.dateProvider(),
515
+ };
516
+ if (this.options.idProvider) {
517
+ session._id = this.options.idProvider();
518
+ }
519
+ const ret = await this.sessionCollection.insertOne(session);
520
+ await this.updateMeteorSession(userId, token, infos);
521
+ return ret.insertedId.toString();
522
+ }
523
+ async updateSession(sessionId, connection) {
524
+ const _id = this.options.convertSessionIdToMongoObjectId
525
+ ? toMongoID(sessionId)
526
+ : sessionId;
527
+ const infos = this.resolveInfo(connection);
528
+ let _set = {
529
+ userAgent: infos.userAgent,
530
+ ip: infos.ip,
531
+ [this.options.timestamps.updatedAt]: this.options.dateProvider(),
532
+ };
533
+ if (infos.space) {
534
+ _set.space = infos.space;
535
+ }
536
+ await this.sessionCollection.updateOne({ _id }, {
537
+ $set: _set,
1257
538
  });
1258
- };
1259
- Mongo.prototype.updateMeteorSession = function (userId, token, infos) {
1260
- return tslib_1.__awaiter(this, void 0, void 0, function () {
1261
- var when, login_expiration_in_days, is_phone, is_tablet, stampedAuthToken, hashedToken;
1262
- return tslib_1.__generator(this, function (_a) {
1263
- switch (_a.label) {
1264
- case 0:
1265
- when = new Date();
1266
- login_expiration_in_days = infos.login_expiration_in_days, is_phone = infos.is_phone, is_tablet = infos.is_tablet;
1267
- if (login_expiration_in_days &&
1268
- (0, lodash_2.isNumber)(login_expiration_in_days) &&
1269
- login_expiration_in_days > 0) {
1270
- when = moment()
1271
- .subtract((90 - login_expiration_in_days) * 24 * 60, "minute")
1272
- .toDate();
1273
- }
1274
- stampedAuthToken = {
1275
- token: token,
1276
- when: when,
1277
- };
1278
- hashedToken = (0, auth_1.hashStampedToken)(stampedAuthToken);
1279
- hashedToken.created = new Date();
1280
- hashedToken.is_phone = is_phone;
1281
- hashedToken.is_tablet = is_tablet;
1282
- return [4, this.collection.updateOne({ _id: userId }, {
1283
- $push: {
1284
- "services.resume.loginTokens": hashedToken,
1285
- },
1286
- })];
1287
- case 1:
1288
- _a.sent();
1289
- return [4, this.spaceUserCollection.updateMany({ user: userId }, {
1290
- $set: {
1291
- last_logon: new Date(),
1292
- },
1293
- })];
1294
- case 2:
1295
- _a.sent();
1296
- return [2, true];
1297
- }
1298
- });
539
+ }
540
+ async invalidateSession(sessionId) {
541
+ const _id = this.options.convertSessionIdToMongoObjectId
542
+ ? toMongoID(sessionId)
543
+ : sessionId;
544
+ await this.sessionCollection.updateOne({ _id }, {
545
+ $set: {
546
+ valid: false,
547
+ [this.options.timestamps.updatedAt]: this.options.dateProvider(),
548
+ },
549
+ });
550
+ const session = await this.sessionCollection.findOne({
551
+ _id: _id,
552
+ });
553
+ await this.destroyMeteorToken(session.userId, session.token);
554
+ }
555
+ async invalidateAllSessions(userId) {
556
+ await this.sessionCollection.updateMany({ userId }, {
557
+ $set: {
558
+ valid: false,
559
+ [this.options.timestamps.updatedAt]: this.options.dateProvider(),
560
+ },
1299
561
  });
1300
- };
1301
- Mongo.prototype.destroyMeteorToken = function (userId, token) {
1302
- return tslib_1.__awaiter(this, void 0, void 0, function () {
1303
- var stampedAuthToken, hashedTokenDoc, loginToken;
1304
- return tslib_1.__generator(this, function (_a) {
1305
- switch (_a.label) {
1306
- case 0:
1307
- stampedAuthToken = {
1308
- token: token,
1309
- when: new Date(),
1310
- };
1311
- hashedTokenDoc = (0, auth_1.hashStampedToken)(stampedAuthToken);
1312
- loginToken = hashedTokenDoc.hashedToken;
1313
- return [4, this.collection.updateOne({ _id: userId }, {
1314
- $pull: {
1315
- "services.resume.loginTokens": {
1316
- $or: [{ hashedToken: loginToken }, { token: loginToken }],
1317
- },
1318
- },
1319
- })];
1320
- case 1:
1321
- _a.sent();
1322
- return [2, true];
1323
- }
1324
- });
562
+ }
563
+ async findSessionByToken(token) {
564
+ const session = await this.sessionCollection.findOne({ token });
565
+ if (session) {
566
+ session.id = session._id.toString();
567
+ }
568
+ return session;
569
+ }
570
+ async findSessionById(sessionId) {
571
+ const _id = this.options.convertSessionIdToMongoObjectId
572
+ ? toMongoID(sessionId)
573
+ : sessionId;
574
+ const session = await this.sessionCollection.findOne({ _id });
575
+ if (session) {
576
+ session.id = session._id.toString();
577
+ }
578
+ return session;
579
+ }
580
+ async addEmailVerificationToken(userId, email, token) {
581
+ const _id = this.options.convertUserIdToMongoObjectId
582
+ ? toMongoID(userId)
583
+ : userId;
584
+ await this.collection.updateOne({ _id }, {
585
+ $push: {
586
+ "services.email.verificationTokens": {
587
+ token,
588
+ address: email.toLowerCase(),
589
+ when: this.options.dateProvider(),
590
+ },
591
+ },
1325
592
  });
1326
- };
1327
- Mongo.prototype.getInviteInfo = function (id) {
1328
- return tslib_1.__awaiter(this, void 0, void 0, function () {
1329
- return tslib_1.__generator(this, function (_a) {
1330
- switch (_a.label) {
1331
- case 0: return [4, this.inviteCollection.findOne({ _id: id })];
1332
- case 1: return [2, _a.sent()];
1333
- }
1334
- });
593
+ }
594
+ async addResetPasswordToken(userId, email, token, reason) {
595
+ const _id = this.options.convertUserIdToMongoObjectId
596
+ ? toMongoID(userId)
597
+ : userId;
598
+ await this.collection.updateOne({ _id }, {
599
+ $push: {
600
+ "services.password.reset": {
601
+ token,
602
+ address: email.toLowerCase(),
603
+ when: this.options.dateProvider(),
604
+ reason,
605
+ },
606
+ },
1335
607
  });
1336
- };
1337
- Mongo.prototype.updateUser = function (userId, options) {
1338
- return tslib_1.__awaiter(this, void 0, void 0, function () {
1339
- return tslib_1.__generator(this, function (_a) {
1340
- return [2, this.collection.updateOne({ _id: userId }, options)];
608
+ }
609
+ async setResetPassword(userId, email, newPassword) {
610
+ await this.setPassword(userId, newPassword);
611
+ }
612
+ async applyCode(name, owner, nextCode, MAX_FAILURE_COUNT, EFFECTIVE_TIME) {
613
+ const now = new Date();
614
+ if (!(0, lodash_2.isString)(name)) {
615
+ throw new Error("accounts.invalidCode");
616
+ }
617
+ const query = {
618
+ name: name,
619
+ verifiedAt: null,
620
+ expiredAt: { $gt: now },
621
+ };
622
+ if (owner) {
623
+ query.owner = owner;
624
+ }
625
+ let record = await this.codeCollection.findOne(query);
626
+ if (record) {
627
+ }
628
+ else {
629
+ let doc = {
630
+ name,
631
+ owner,
632
+ code: nextCode,
633
+ expiredAt: new Date(moment().add(EFFECTIVE_TIME, "m")),
634
+ [this.options.timestamps.createdAt]: this.options.dateProvider(),
635
+ };
636
+ if (this.options.idProvider) {
637
+ doc._id = this.options.idProvider();
638
+ }
639
+ let result = await this.codeCollection.insertOne(doc);
640
+ record = result.ops[0];
641
+ }
642
+ return record;
643
+ }
644
+ async addVerificationCode(user, code, options) {
645
+ let foundedUser = null;
646
+ if (user.email)
647
+ foundedUser = await this.findUserByEmail(user.email);
648
+ else if (user.mobile)
649
+ foundedUser = await this.findUserByMobile(user.mobile);
650
+ const owner = foundedUser ? foundedUser.id : null;
651
+ const ret = await this.applyCode(user.email ? user.email : user.mobile, owner, code, options.MAX_FAILURE_COUNT, options.EFFECTIVE_TIME);
652
+ return ret;
653
+ }
654
+ async verifyCodeByName(name, code) {
655
+ if (!(0, lodash_2.isString)(code)) {
656
+ throw new Error("accounts.invalidCode");
657
+ }
658
+ if (!(0, lodash_2.isString)(name)) {
659
+ throw new Error("accounts.invalidCode");
660
+ }
661
+ const now = new Date();
662
+ let query = {
663
+ name: name,
664
+ code: code,
665
+ verifiedAt: null,
666
+ expiredAt: { $gt: now },
667
+ };
668
+ let result = await this.codeCollection.findOne(query);
669
+ if (result) {
670
+ await this.codeCollection.updateOne({ _id: result._id }, { $set: { verifiedAt: now } });
671
+ return result;
672
+ }
673
+ else {
674
+ throw new Error("accounts.invalidCode");
675
+ }
676
+ }
677
+ async verifyCodeByOwner(owner, code) {
678
+ if (!(0, lodash_2.isString)(code)) {
679
+ throw new Error("accounts.invalidCode");
680
+ }
681
+ if (!(0, lodash_2.isString)(owner)) {
682
+ throw new Error("accounts.invalidCode");
683
+ }
684
+ const now = new Date();
685
+ let query = {
686
+ owner: owner,
687
+ code: code,
688
+ verifiedAt: null,
689
+ expiredAt: { $gt: now },
690
+ };
691
+ let result = await this.codeCollection.findOne(query);
692
+ if (result) {
693
+ await this.codeCollection.updateOne({ _id: result._id }, { $set: { verifiedAt: now } });
694
+ return result;
695
+ }
696
+ else {
697
+ console.log("verifyCodeByOwner throw new Error accounts.invalidCode");
698
+ throw new Error("accounts.invalidCode");
699
+ }
700
+ }
701
+ async checkVerificationCode(user, code) {
702
+ let name = null;
703
+ if (user.email)
704
+ name = user.email;
705
+ else if (user.mobile)
706
+ name = user.mobile;
707
+ if (!name)
708
+ return false;
709
+ const record = await this.verifyCodeByName(name, code);
710
+ if (!record)
711
+ return false;
712
+ return true;
713
+ }
714
+ async findUserByVerificationCode(user, code) {
715
+ let foundedUser = null;
716
+ if (user.email)
717
+ foundedUser = await this.findUserByEmail(user.email);
718
+ else if (user.mobile)
719
+ foundedUser = await this.findUserByMobile(user.mobile);
720
+ if (!foundedUser)
721
+ return null;
722
+ const owner = foundedUser.id;
723
+ const record = await this.verifyCodeByOwner(owner, code);
724
+ if (!record)
725
+ return null;
726
+ if (user.email && foundedUser.email_verified != true) {
727
+ await this.verifyEmail(owner, user.email);
728
+ foundedUser = await this.findUserById(owner);
729
+ }
730
+ else if (user.mobile && foundedUser.mobile_verified != true) {
731
+ await this.verifyMobile(owner, user.mobile);
732
+ foundedUser = await this.findUserById(owner);
733
+ }
734
+ return foundedUser;
735
+ }
736
+ async getMySpaces(userId) {
737
+ const userSpaces = await this.db
738
+ .collection("space_users")
739
+ .find({ user: userId, invite_state: { $ne: "refused" } })
740
+ .project({ space: 1, user_accepted: 1, invite_state: 1 })
741
+ .toArray();
742
+ const spaceIds = (0, lodash_1.map)(userSpaces, "space");
743
+ const spaces = await this.db
744
+ .collection("spaces")
745
+ .find({ _id: { $in: spaceIds } })
746
+ .project({
747
+ name: 1,
748
+ favicon: 1,
749
+ account_logo: 1,
750
+ avatar_square: 1,
751
+ avatar: 1,
752
+ background: 1,
753
+ enable_register: 1,
754
+ })
755
+ .toArray();
756
+ return (0, lodash_1.map)(spaces, function (space) {
757
+ const spaceUser = (0, lodash_1.find)(userSpaces, (item) => {
758
+ return item.space == space._id;
1341
759
  });
760
+ return {
761
+ ...space,
762
+ user_accepted: spaceUser.user_accepted,
763
+ invite_state: spaceUser.invite_state,
764
+ };
1342
765
  });
1343
- };
1344
- return Mongo;
1345
- }());
766
+ }
767
+ async getFirstSpace() {
768
+ const space = await this.db.collection("spaces").findOne();
769
+ return space;
770
+ }
771
+ async updateMeteorSession(userId, token, infos) {
772
+ let when = new Date();
773
+ const { login_expiration_in_days, is_phone, is_tablet } = infos;
774
+ if (login_expiration_in_days &&
775
+ (0, lodash_2.isNumber)(login_expiration_in_days) &&
776
+ login_expiration_in_days > 0) {
777
+ when = moment()
778
+ .subtract((90 - login_expiration_in_days) * 24 * 60, "minute")
779
+ .toDate();
780
+ }
781
+ let stampedAuthToken = {
782
+ token: token,
783
+ when: when,
784
+ };
785
+ const now = new Date();
786
+ let hashedToken = (0, auth_1.hashStampedToken)(stampedAuthToken);
787
+ hashedToken.created = now;
788
+ hashedToken.is_phone = is_phone;
789
+ hashedToken.is_tablet = is_tablet;
790
+ await this.collection.updateOne({ _id: userId }, {
791
+ $push: {
792
+ "services.resume.loginTokens": hashedToken,
793
+ },
794
+ $set: {
795
+ last_logon: now,
796
+ },
797
+ });
798
+ await this.spaceUserCollection.updateMany({ user: userId }, {
799
+ $set: {
800
+ last_logon: now,
801
+ },
802
+ });
803
+ return true;
804
+ }
805
+ async destroyMeteorToken(userId, token) {
806
+ let stampedAuthToken = {
807
+ token: token,
808
+ when: new Date(),
809
+ };
810
+ let hashedTokenDoc = (0, auth_1.hashStampedToken)(stampedAuthToken);
811
+ let loginToken = hashedTokenDoc.hashedToken;
812
+ await this.collection.updateOne({ _id: userId }, {
813
+ $pull: {
814
+ "services.resume.loginTokens": {
815
+ $or: [{ hashedToken: loginToken }, { token: loginToken }],
816
+ },
817
+ },
818
+ });
819
+ return true;
820
+ }
821
+ async getInviteInfo(id) {
822
+ return await this.inviteCollection.findOne({ _id: id });
823
+ }
824
+ async updateUser(userId, options) {
825
+ return this.collection.updateOne({ _id: userId }, options);
826
+ }
827
+ }
1346
828
  exports.Mongo = Mongo;
1347
829
  //# sourceMappingURL=mongo.js.map