|
|
@@ -14,14 +14,17 @@ apiKey.apiKey = process.env.BREVO_KEY
|
|
14
|
14
|
|
|
15
|
15
|
const apiInstance = new SibApiV3Sdk.TransactionalEmailsApi()
|
|
16
|
16
|
|
|
17
|
|
-const hashEmail = async email => {
|
|
|
17
|
+const hashToken = async token => {
|
|
|
18
|
+ // QUESTION: How to best create random salt...?
|
|
|
19
|
+ const salt = crypto.randomBytes(16).toString('base64')
|
|
18
|
20
|
try {
|
|
19
|
|
- return crypto.createHmac('sha256', '').update(email).digest('hex')
|
|
|
21
|
+ return crypto.createHmac('sha256', salt).update(token).digest('hex')
|
|
20
|
22
|
} catch (err) {
|
|
21
|
23
|
// console.error('ERROR :=>', err)
|
|
22
|
24
|
throw new Error(err.message)
|
|
23
|
25
|
}
|
|
24
|
26
|
}
|
|
|
27
|
+
|
|
25
|
28
|
const hasher = async (pwd, steak) => {
|
|
26
|
29
|
const hash = await pwd.hash(steak)
|
|
27
|
30
|
const result = await pwd.verify(steak, hash)
|
|
|
@@ -41,7 +44,8 @@ const hasher = async (pwd, steak) => {
|
|
41
|
44
|
try {
|
|
42
|
45
|
squirtle = await pwd.hash(steak)
|
|
43
|
46
|
// console.log('improvedHash', squirtle)
|
|
44
|
|
- // const saveHash = Auth.insert({user_email: matchingEmails}).into('token')
|
|
|
47
|
+ // const saveHash = Auth.insert({user_email:
|
|
|
48
|
+ // matchingEmails}).into('token')
|
|
45
|
49
|
return squirtle
|
|
46
|
50
|
} catch (err) {
|
|
47
|
51
|
console.error(
|
|
|
@@ -61,25 +65,19 @@ module.exports = class UserService extends Schmervice.Service {
|
|
61
|
65
|
constructor(...args) {
|
|
62
|
66
|
super(...args)
|
|
63
|
67
|
const pwd = new SecurePassword()
|
|
64
|
|
- // TODO: Invalidate this application state somehow after a certain time period has passed
|
|
65
|
|
- // TODO: Remove hashedEmails in preference of activeSessions
|
|
66
|
|
- this.hashedEmails = {
|
|
67
|
|
- // NOTE: key is email hash and value is timestamp in ms
|
|
|
68
|
+ // TODO: Invalidate this application state somehow after a
|
|
|
69
|
+ // certain time period has passed
|
|
|
70
|
+ this.activeSessions = {
|
|
68
|
71
|
// abc123456: '123456689',
|
|
|
72
|
+ // eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...hashedSessionToken: {
|
|
|
73
|
+ // email: rawEmailString,
|
|
|
74
|
+ // name: 'Joe Doe',
|
|
|
75
|
+ // seeking: 'candidate'
|
|
|
76
|
+ // sessionToken: rawSessionToken, // use for expires instead of expires?
|
|
|
77
|
+ // expires: expirationTime in seconds
|
|
|
78
|
+ // }
|
|
69
|
79
|
}
|
|
70
|
80
|
|
|
71
|
|
- // this.activeSessions = [
|
|
72
|
|
- // {
|
|
73
|
|
- // user: {
|
|
74
|
|
- // useremail: email,
|
|
75
|
|
- // hashedEmail: hashedEmail,
|
|
76
|
|
- // username: name,
|
|
77
|
|
- // },
|
|
78
|
|
- // expiration: 1203984710234
|
|
79
|
|
- // },
|
|
80
|
|
- // token: 'tokenString + expirationDate + salt'
|
|
81
|
|
- // ]
|
|
82
|
|
-
|
|
83
|
81
|
this.pwd = {
|
|
84
|
82
|
hash: Util.promisify(pwd.hash.bind(pwd)),
|
|
85
|
83
|
verify: Util.promisify(pwd.verify.bind(pwd)),
|
|
|
@@ -230,20 +228,6 @@ module.exports = class UserService extends Schmervice.Service {
|
|
230
|
228
|
return JWT.sign(obj, key, { expiresIn: data.expires })
|
|
231
|
229
|
}
|
|
232
|
230
|
|
|
233
|
|
- async registerSession(user, hashedEmail, token) {
|
|
234
|
|
- const sessionRequester = {
|
|
235
|
|
- user: user,
|
|
236
|
|
- hashedEmail: hashedEmail,
|
|
237
|
|
- token: token,
|
|
238
|
|
- }
|
|
239
|
|
-
|
|
240
|
|
- const alreadyExists = this.activeSessions.find(
|
|
241
|
|
- sessionRequester => sessionRequester.hashedEmail === hashedEmail,
|
|
242
|
|
- )
|
|
243
|
|
- if (!alreadyExists) {
|
|
244
|
|
- this.activeSessions.push(sessionRequester)
|
|
245
|
|
- }
|
|
246
|
|
- }
|
|
247
|
231
|
/**
|
|
248
|
232
|
* Validates whether a token has expired or not
|
|
249
|
233
|
* @param {User} user
|
|
|
@@ -293,6 +277,7 @@ module.exports = class UserService extends Schmervice.Service {
|
|
293
|
277
|
return passwordRow ? passwordRow.token : null
|
|
294
|
278
|
}
|
|
295
|
279
|
|
|
|
280
|
+ // TODO: rewrite for new activeSessions object
|
|
296
|
281
|
async checkEmailRegistry(userEmail) {
|
|
297
|
282
|
const hashedEmail = await hashEmail(userEmail)
|
|
298
|
283
|
const now = Date.now()
|
|
|
@@ -316,32 +301,37 @@ module.exports = class UserService extends Schmervice.Service {
|
|
316
|
301
|
* Sends a Transactional Email via Brevo
|
|
317
|
302
|
* @ returns {Object}
|
|
318
|
303
|
*/
|
|
319
|
|
- async emailSent(userEmail) {
|
|
320
|
|
- const hashedEmail = await hashEmail(userEmail)
|
|
321
|
|
- if (Object.keys(this.hashedEmails).includes(hashedEmail)) {
|
|
322
|
|
- return new Error('email address already in cache!!')
|
|
|
304
|
+ async emailSent(userCredentials) {
|
|
|
305
|
+ const hashedSessionToken = await hashToken(userCredentials.sessionToken)
|
|
|
306
|
+ if (Object.keys(this.activeSessions).includes(hashedSessionToken)) {
|
|
|
307
|
+ return new Error('session already in cache!!')
|
|
323
|
308
|
}
|
|
324
|
309
|
// Set expiration time for ten minutes from now
|
|
325
|
|
- const duration = 1000 * 60 * 10
|
|
|
310
|
+ // QUESTION: should we use the sessionToken's expiration time instead?
|
|
|
311
|
+ const duration = 600000
|
|
|
312
|
+
|
|
|
313
|
+ this.activeSessions[hashedSessionToken] = {
|
|
|
314
|
+ email: userCredentials.email,
|
|
|
315
|
+ name: userCredentials.name,
|
|
|
316
|
+ seeking: userCredentials.seeking,
|
|
|
317
|
+ sessionToken: userCredentials.sessionToken,
|
|
|
318
|
+ expiration: Date.now() + duration,
|
|
|
319
|
+ }
|
|
326
|
320
|
|
|
327
|
|
- this.hashedEmails[hashedEmail] = Date.now() + duration
|
|
328
|
|
- // TODO: See FrontEnd in Auth.vue and VerifyView.vue notes:
|
|
329
|
|
- // if user closes browser, they'll need to be issued first session token based off of this:
|
|
330
|
|
- // this.hashedEmails[hashedEmail][email] = userEmail
|
|
331
|
321
|
const sendSmtpEmail = {
|
|
332
|
322
|
to: [
|
|
333
|
323
|
{
|
|
334
|
|
- email: userEmail,
|
|
|
324
|
+ email: userCredentials.email,
|
|
335
|
325
|
},
|
|
336
|
326
|
],
|
|
337
|
327
|
templateId: 1,
|
|
338
|
328
|
params: {
|
|
339
|
329
|
// TODO: Change this in production...
|
|
340
|
|
- link: `localhost:3000/verify/${hashedEmail}`,
|
|
|
330
|
+ link: `localhost:3000/verify/${hashedSessionToken}`,
|
|
341
|
331
|
},
|
|
342
|
332
|
}
|
|
343
|
333
|
|
|
344
|
|
- await apiInstance.sendTransacEmail(sendSmtpEmail).then(
|
|
|
334
|
+ return await apiInstance.sendTransacEmail(sendSmtpEmail).then(
|
|
345
|
335
|
data => {
|
|
346
|
336
|
return { wasSuccessfull: true, data: data }
|
|
347
|
337
|
},
|