|
|
@@ -64,10 +64,24 @@ module.exports = class UserService extends Schmervice.Service {
|
|
64
|
64
|
const pwd = new SecurePassword()
|
|
65
|
65
|
// TODO: Invalidate this cache somehow after a certain time period has
|
|
66
|
66
|
// passed
|
|
|
67
|
+ // TODO: Remove hashedEmails in preference of activeSessions
|
|
67
|
68
|
this.hashedEmails = {
|
|
68
|
69
|
// NOTE: key is email hash and value is timestamp in ms
|
|
69
|
70
|
// abc123456: '123456689',
|
|
70
|
71
|
}
|
|
|
72
|
+
|
|
|
73
|
+ // this.activeSessions = [
|
|
|
74
|
+ // {
|
|
|
75
|
+ // user: {
|
|
|
76
|
+ // useremail: email,
|
|
|
77
|
+ // hashedEmail: hashedEmail,
|
|
|
78
|
+ // username: name,
|
|
|
79
|
+ // },
|
|
|
80
|
+ // expiration: 1203984710234
|
|
|
81
|
+ // },
|
|
|
82
|
+ // token: 'tokenString + expirationDate + salt'
|
|
|
83
|
+ // ]
|
|
|
84
|
+
|
|
71
|
85
|
this.pwd = {
|
|
72
|
86
|
hash: Util.promisify(pwd.hash.bind(pwd)),
|
|
73
|
87
|
verify: Util.promisify(pwd.verify.bind(pwd)),
|
|
|
@@ -210,17 +224,22 @@ module.exports = class UserService extends Schmervice.Service {
|
|
210
|
224
|
* @param {User} user
|
|
211
|
225
|
* @returns {Token}
|
|
212
|
226
|
*/
|
|
213
|
|
-
|
|
|
227
|
+ // TODO: Put this logic in the routes, NOT here
|
|
|
228
|
+ // createSessionToken(user, payload)
|
|
|
229
|
+ // createAccessToken()
|
|
|
230
|
+ //
|
|
214
|
231
|
createToken(user) {
|
|
215
|
232
|
const key = this.server.registrations['main-app-plugin'].options.jwtKey
|
|
216
|
233
|
|
|
217
|
|
- const token = Jwt.token.generate(
|
|
|
234
|
+ let token = Jwt.token.generate(
|
|
218
|
235
|
{
|
|
219
|
236
|
aud: 'urn:audience:test',
|
|
220
|
237
|
iss: 'urn:issuer:test',
|
|
|
238
|
+ // ...payload,
|
|
221
|
239
|
email: user.email,
|
|
222
|
240
|
name: user.name,
|
|
223
|
241
|
seeking: user.seeking,
|
|
|
242
|
+ salt: 'a;ldfkjas;l/dfkafnml;/cjkf',
|
|
224
|
243
|
// profile_id: user.profile_id,
|
|
225
|
244
|
},
|
|
226
|
245
|
{
|
|
|
@@ -228,14 +247,91 @@ module.exports = class UserService extends Schmervice.Service {
|
|
228
|
247
|
algorithm: 'HS256',
|
|
229
|
248
|
},
|
|
230
|
249
|
{
|
|
231
|
|
- // ttlSec: 4 * 60 * 60, // 7 days
|
|
232
|
|
- // ttlSec: 60 * 3, // 3 minutes
|
|
233
|
|
- ttlSec: user.expiration,
|
|
|
250
|
+ ttlSec: 4 * 60 * 60, // 7 days
|
|
234
|
251
|
},
|
|
235
|
252
|
)
|
|
|
253
|
+ console.log('token :=>', token)
|
|
|
254
|
+ token = Jwt.token.generate(
|
|
|
255
|
+ {
|
|
|
256
|
+ aud: 'urn:audience:test',
|
|
|
257
|
+ iss: 'urn:issuer:test',
|
|
|
258
|
+ // ...payload,
|
|
|
259
|
+ email: user.email,
|
|
|
260
|
+ name: user.name,
|
|
|
261
|
+ seeking: user.seeking,
|
|
|
262
|
+ salt: 'qpowieurpqowytqpoieryu',
|
|
|
263
|
+ // profile_id: user.profile_id,
|
|
|
264
|
+ },
|
|
|
265
|
+ {
|
|
|
266
|
+ key: key,
|
|
|
267
|
+ algorithm: 'HS256',
|
|
|
268
|
+ },
|
|
|
269
|
+ {
|
|
|
270
|
+ ttlSec: 4 * 60 * 60, // 7 days
|
|
|
271
|
+ },
|
|
|
272
|
+ )
|
|
|
273
|
+ console.log('\n')
|
|
|
274
|
+ console.log('token :=>', token)
|
|
|
275
|
+ token = Jwt.token.generate(
|
|
|
276
|
+ {
|
|
|
277
|
+ aud: 'urn:audience:test',
|
|
|
278
|
+ iss: 'urn:issuer:test',
|
|
|
279
|
+ // ...payload,
|
|
|
280
|
+ email: user.email,
|
|
|
281
|
+ name: user.name,
|
|
|
282
|
+ seeking: user.seeking,
|
|
|
283
|
+ salt: 'a;ldfkjas;l/dfkafnml;/cjkf',
|
|
|
284
|
+ // profile_id: user.profile_id,
|
|
|
285
|
+ },
|
|
|
286
|
+ {
|
|
|
287
|
+ key: key,
|
|
|
288
|
+ algorithm: 'HS256',
|
|
|
289
|
+ },
|
|
|
290
|
+ {
|
|
|
291
|
+ ttlSec: 6 * 60 * 60, // 7 days
|
|
|
292
|
+ },
|
|
|
293
|
+ )
|
|
|
294
|
+ console.log('token :=>', token)
|
|
|
295
|
+ token = Jwt.token.generate(
|
|
|
296
|
+ {
|
|
|
297
|
+ aud: 'urn:audience:test',
|
|
|
298
|
+ iss: 'urn:issuer:test',
|
|
|
299
|
+ // ...payload,
|
|
|
300
|
+ email: user.email,
|
|
|
301
|
+ name: user.name,
|
|
|
302
|
+ seeking: user.seeking,
|
|
|
303
|
+ salt: 'a;ldfkjas;l/dfkafnml;/cjkf',
|
|
|
304
|
+ // profile_id: user.profile_id,
|
|
|
305
|
+ },
|
|
|
306
|
+ {
|
|
|
307
|
+ key: key,
|
|
|
308
|
+ algorithm: 'HS256',
|
|
|
309
|
+ },
|
|
|
310
|
+ {
|
|
|
311
|
+ ttlSec: 7 * 60 * 60, // 7 days
|
|
|
312
|
+ },
|
|
|
313
|
+ )
|
|
|
314
|
+ console.log('token :=>', token)
|
|
|
315
|
+
|
|
|
316
|
+ // TODO: keep userinfo and it's association with the sessionToken in state/memory
|
|
|
317
|
+ // registerSession(user, sessionToken) // useremail, token
|
|
|
318
|
+ // this.registerSession(user, token)
|
|
236
|
319
|
return token
|
|
237
|
320
|
}
|
|
238
|
321
|
|
|
|
322
|
+ async registerSession(user, hashedEmail, token) {
|
|
|
323
|
+ const sessionRequester = {
|
|
|
324
|
+ user: user,
|
|
|
325
|
+ hashedEmail: hashedEmail,
|
|
|
326
|
+ token: token,
|
|
|
327
|
+ }
|
|
|
328
|
+ const alreadyExists = this.activeSessions.find(
|
|
|
329
|
+ sessionRequester => sessionRequester.hashedEmail === hashedEmail,
|
|
|
330
|
+ )
|
|
|
331
|
+ if (!alreadyExists) {
|
|
|
332
|
+ this.activeSessions.push(sessionRequester)
|
|
|
333
|
+ }
|
|
|
334
|
+ }
|
|
239
|
335
|
/**
|
|
240
|
336
|
* Validates whether a token has expired or not
|
|
241
|
337
|
* @param {User} user
|
|
|
@@ -290,6 +386,7 @@ module.exports = class UserService extends Schmervice.Service {
|
|
290
|
386
|
async checkEmailCache(userEmail) {
|
|
291
|
387
|
const hashedEmail = await hashEmail(userEmail)
|
|
292
|
388
|
const now = Date.now()
|
|
|
389
|
+ // hashedEmail needs to be derived by email, salt
|
|
293
|
390
|
const expiration = this.hashedEmails[hashedEmail]
|
|
294
|
391
|
console.log('this.hashedEmails :=>', this.hashedEmails)
|
|
295
|
392
|
const emailIsInCache = Object.keys(this.hashedEmails).includes(
|