|
|
@@ -16,8 +16,9 @@ apiKey.apiKey = process.env.BREVO_KEY
|
|
16
|
16
|
const apiInstance = new SibApiV3Sdk.TransactionalEmailsApi()
|
|
17
|
17
|
|
|
18
|
18
|
const hashToken = async token => {
|
|
19
|
|
- // QUESTION: How to best create random salt...?
|
|
|
19
|
+ // Give it a .env file phrase, NOT RANDOM
|
|
20
|
20
|
const salt = crypto.randomBytes(16).toString('base64')
|
|
|
21
|
+ // const salt = process.env.salt
|
|
21
|
22
|
try {
|
|
22
|
23
|
return crypto.createHmac('sha256', salt).update(token).digest('hex')
|
|
23
|
24
|
} catch (err) {
|
|
|
@@ -78,6 +79,14 @@ module.exports = class UserService extends Schmervice.Service {
|
|
78
|
79
|
// expires: expirationTime in seconds
|
|
79
|
80
|
// }
|
|
80
|
81
|
}
|
|
|
82
|
+ // Check the hashedCookie which is our hashedSessionToken string
|
|
|
83
|
+ // validate whether or not the rawAccessToken is still valid, if valid good to go.
|
|
|
84
|
+ // if NOT valid, then we need to reassign accessToken to a newAccessToken
|
|
|
85
|
+ // this.activeSessions = {
|
|
|
86
|
+ // eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...hashedSessionToken: {
|
|
|
87
|
+ // accessToken: 'as;dflkja;;dlfkja;sldkf... rawAccessToken'
|
|
|
88
|
+ // }
|
|
|
89
|
+ // }
|
|
81
|
90
|
|
|
82
|
91
|
this.pwd = {
|
|
83
|
92
|
hash: Util.promisify(pwd.hash.bind(pwd)),
|
|
|
@@ -249,6 +258,7 @@ module.exports = class UserService extends Schmervice.Service {
|
|
249
|
258
|
* @returns {Token}
|
|
250
|
259
|
*/
|
|
251
|
260
|
// TODO: remove testing console.log() messages once onboarding auth is working
|
|
|
261
|
+ // REFACTOR: Have this function only do one thing (UNIX philsophy)
|
|
252
|
262
|
validateSession(hashedSessionToken) {
|
|
253
|
263
|
console.log('this.activeSessions :=>', this.activeSessions)
|
|
254
|
264
|
if (!this.activeSessions[hashedSessionToken]) {
|
|
|
@@ -256,7 +266,7 @@ module.exports = class UserService extends Schmervice.Service {
|
|
256
|
266
|
'hashedSessionToken not in activeSessions registry!',
|
|
257
|
267
|
)
|
|
258
|
268
|
}
|
|
259
|
|
-
|
|
|
269
|
+ // BREAK OUT INTO ANOTHER FUNC
|
|
260
|
270
|
const rawSessionToken =
|
|
261
|
271
|
this.activeSessions[hashedSessionToken].sessionToken
|
|
262
|
272
|
const accessToken = this.activeSessions[hashedSessionToken].accessToken
|
|
|
@@ -267,17 +277,16 @@ module.exports = class UserService extends Schmervice.Service {
|
|
267
|
277
|
'hashedSessionToken is in activeSessions registry, but rawSessionToken does not exist',
|
|
268
|
278
|
)
|
|
269
|
279
|
}
|
|
|
280
|
+ // ANOTHER FUNC HERE
|
|
270
|
281
|
const sessionTokenIsValid = this.validateToken(rawSessionToken)
|
|
271
|
282
|
console.log('sessionTokenIsValid :=>', sessionTokenIsValid)
|
|
272
|
283
|
const accessTokenIsValid = this.validateToken(accessToken)
|
|
273
|
284
|
console.log('accessTokenIsValid :=>', accessTokenIsValid)
|
|
274
|
285
|
|
|
275
|
286
|
// Both sessionToken and accessToken are expired
|
|
276
|
|
- if (!sessionTokenIsValid.payload && !accessTokenIsValid.payload) {
|
|
277
|
|
- console.log('session is expired! kicking you off!')
|
|
278
|
|
- return sessionTokenIsValid
|
|
279
|
|
- }
|
|
280
|
|
- if (sessionTokenIsValid.payload && !accessTokenIsValid.payload) {
|
|
|
287
|
+ // createAccessToken()
|
|
|
288
|
+ //
|
|
|
289
|
+ if (!accessTokenIsValid.payload) {
|
|
281
|
290
|
console.log(
|
|
282
|
291
|
'sessionToken is valid, but accessToken is null or is expired :=>',
|
|
283
|
292
|
)
|
|
|
@@ -285,15 +294,6 @@ module.exports = class UserService extends Schmervice.Service {
|
|
285
|
294
|
payload: sessionTokenIsValid.payload,
|
|
286
|
295
|
})
|
|
287
|
296
|
this.activeSessions[hashedSessionToken].accessToken = accessToken
|
|
288
|
|
- } else if (!sessionTokenIsValid.payload && accessTokenIsValid.payload) {
|
|
289
|
|
- console.log(
|
|
290
|
|
- 'accessToken is valid, but sessionToken has expired :=>',
|
|
291
|
|
- )
|
|
292
|
|
- const newSessionToken = this.createToken({
|
|
293
|
|
- payload: accessTokenIsValid.payload,
|
|
294
|
|
- })
|
|
295
|
|
- this.activeSessions[hashedSessionToken].sessionToken =
|
|
296
|
|
- newSessionToken
|
|
297
|
297
|
}
|
|
298
|
298
|
return {
|
|
299
|
299
|
...sessionTokenIsValid.payload,
|