Преглед изворни кода

:sparkles: Notes added for refactor before merge

tags/0.0.3^2
tomit4 пре 2 година
родитељ
комит
2cf1669715

+ 6
- 0
backend/lib/auth/strategies/jwt.js Прегледај датотеку

7
         verifyOptions: {
7
         verifyOptions: {
8
             algorithms: ['HS256'],
8
             algorithms: ['HS256'],
9
         },
9
         },
10
+        // check the h object to see if the activeSessions is accessible from it
11
+        //
12
+        // check useronlinestatus branch request.server.app
10
         validate: (decoded, request, h) => {
13
         validate: (decoded, request, h) => {
11
             // QUESTION: How can we authenticate both Session and Access Tokens here?
14
             // QUESTION: How can we authenticate both Session and Access Tokens here?
15
+            // Always check rawAccessToken, if it fails, we check the session, if session is valid, then we reissue
16
+            // if session is NOT valid, DELETE the session (and kick user back to login)
17
+            // TODO: set up cron job to occassionaly clean up activeSessions
12
             const token = request.headers.authorization
18
             const token = request.headers.authorization
13
             try {
19
             try {
14
                 const validatedJwt = JWT.verify(token, process.env.APP_SECRET)
20
                 const validatedJwt = JWT.verify(token, process.env.APP_SECRET)

+ 3
- 0
backend/lib/routes/user/email.js Прегледај датотеку

33
                         userCredentials.email
33
                         userCredentials.email
34
                     )
34
                     )
35
                 })
35
                 })
36
+                if (!hashedSessionToken.length) {
37
+                    throw Error('hashedSessionToken not Found!!')
38
+                }
36
                 return {
39
                 return {
37
                     ok: true,
40
                     ok: true,
38
                     handler: pluginConfig.handlerType,
41
                     handler: pluginConfig.handlerType,

+ 6
- 6
backend/lib/routes/user/verifyactivesession.js Прегледај датотеку

29
                 ).find(hashedToken => {
29
                 ).find(hashedToken => {
30
                     return hashedToken === hash
30
                     return hashedToken === hash
31
                 })
31
                 })
32
+                if (!hashToMatch.length) {
33
+                    throw Error('hashToMatch Not Found!')
34
+                }
32
                 const now = Date.now()
35
                 const now = Date.now()
33
-                // TODO: convert this back to a date object
34
-                const expiration =
35
-                    userService.activeSessions[`${hash}`].expiration
36
+                const expiration = new Date(
37
+                    userService.activeSessions[`${hash}`].expiration,
38
+                )
36
                 if (now > expiration) {
39
                 if (now > expiration) {
37
                     delete userService.activeSessions[hashToMatch]
40
                     delete userService.activeSessions[hashToMatch]
38
                     throw new Error(
41
                     throw new Error(
42
                 if (!hashToMatch) {
45
                 if (!hashToMatch) {
43
                     throw new Error('no record of email in cache')
46
                     throw new Error('no record of email in cache')
44
                 }
47
                 }
45
-
46
                 return {
48
                 return {
47
                     ok: true,
49
                     ok: true,
48
                     handler: pluginConfig.handlerType,
50
                     handler: pluginConfig.handlerType,
49
                     data: {
51
                     data: {
50
                         hashesMatch: hashToMatch === hash,
52
                         hashesMatch: hashToMatch === hash,
51
-                        sessionToken:
52
-                            userService.activeSessions[`${hash}`].sessionToken,
53
                     },
53
                     },
54
                 }
54
                 }
55
             } catch (err) {
55
             } catch (err) {

+ 16
- 16
backend/lib/services/user.js Прегледај датотеку

16
 const apiInstance = new SibApiV3Sdk.TransactionalEmailsApi()
16
 const apiInstance = new SibApiV3Sdk.TransactionalEmailsApi()
17
 
17
 
18
 const hashToken = async token => {
18
 const hashToken = async token => {
19
-    // QUESTION: How to best create random salt...?
19
+    // Give it a .env file phrase, NOT RANDOM
20
     const salt = crypto.randomBytes(16).toString('base64')
20
     const salt = crypto.randomBytes(16).toString('base64')
21
+    // const salt = process.env.salt
21
     try {
22
     try {
22
         return crypto.createHmac('sha256', salt).update(token).digest('hex')
23
         return crypto.createHmac('sha256', salt).update(token).digest('hex')
23
     } catch (err) {
24
     } catch (err) {
78
             // expires: expirationTime in seconds
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
         this.pwd = {
91
         this.pwd = {
83
             hash: Util.promisify(pwd.hash.bind(pwd)),
92
             hash: Util.promisify(pwd.hash.bind(pwd)),
249
      * @returns {Token}
258
      * @returns {Token}
250
      */
259
      */
251
     // TODO: remove testing console.log() messages once onboarding auth is working
260
     // TODO: remove testing console.log() messages once onboarding auth is working
261
+    // REFACTOR: Have this function only do one thing (UNIX philsophy)
252
     validateSession(hashedSessionToken) {
262
     validateSession(hashedSessionToken) {
253
         console.log('this.activeSessions :=>', this.activeSessions)
263
         console.log('this.activeSessions :=>', this.activeSessions)
254
         if (!this.activeSessions[hashedSessionToken]) {
264
         if (!this.activeSessions[hashedSessionToken]) {
256
                 'hashedSessionToken not in activeSessions registry!',
266
                 'hashedSessionToken not in activeSessions registry!',
257
             )
267
             )
258
         }
268
         }
259
-
269
+        // BREAK OUT INTO ANOTHER FUNC
260
         const rawSessionToken =
270
         const rawSessionToken =
261
             this.activeSessions[hashedSessionToken].sessionToken
271
             this.activeSessions[hashedSessionToken].sessionToken
262
         const accessToken = this.activeSessions[hashedSessionToken].accessToken
272
         const accessToken = this.activeSessions[hashedSessionToken].accessToken
267
                 'hashedSessionToken is in activeSessions registry, but rawSessionToken does not exist',
277
                 'hashedSessionToken is in activeSessions registry, but rawSessionToken does not exist',
268
             )
278
             )
269
         }
279
         }
280
+        // ANOTHER FUNC HERE
270
         const sessionTokenIsValid = this.validateToken(rawSessionToken)
281
         const sessionTokenIsValid = this.validateToken(rawSessionToken)
271
         console.log('sessionTokenIsValid :=>', sessionTokenIsValid)
282
         console.log('sessionTokenIsValid :=>', sessionTokenIsValid)
272
         const accessTokenIsValid = this.validateToken(accessToken)
283
         const accessTokenIsValid = this.validateToken(accessToken)
273
         console.log('accessTokenIsValid :=>', accessTokenIsValid)
284
         console.log('accessTokenIsValid :=>', accessTokenIsValid)
274
 
285
 
275
         // Both sessionToken and accessToken are expired
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
             console.log(
290
             console.log(
282
                 'sessionToken is valid, but accessToken is null or is expired :=>',
291
                 'sessionToken is valid, but accessToken is null or is expired :=>',
283
             )
292
             )
285
                 payload: sessionTokenIsValid.payload,
294
                 payload: sessionTokenIsValid.payload,
286
             })
295
             })
287
             this.activeSessions[hashedSessionToken].accessToken = accessToken
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
         return {
298
         return {
299
             ...sessionTokenIsValid.payload,
299
             ...sessionTokenIsValid.payload,

+ 5
- 0
frontend/src/views/OnboardingView.vue Прегледај датотеку

76
             // TODO: Validate All routes hit by these methods using tokens in headers
76
             // TODO: Validate All routes hit by these methods using tokens in headers
77
             // NOTE: This can be accomplished using sessionData.sessionToken,
77
             // NOTE: This can be accomplished using sessionData.sessionToken,
78
             // as it currently has the raw session token in it
78
             // as it currently has the raw session token in it
79
+
80
+            // Move this logic onto the backend and have it
81
+            // returned in verifySession(hashedSessionToken)
79
             const userId = await this.grabUserIdByEmail(
82
             const userId = await this.grabUserIdByEmail(
80
                 sessionData.email,
83
                 sessionData.email,
81
                 sessionData.sessionToken,
84
                 sessionData.sessionToken,
114
                 cookieKey in cookies ? cookies[`${cookieKey}`] : undefined
117
                 cookieKey in cookies ? cookies[`${cookieKey}`] : undefined
115
             return cookieVal
118
             return cookieVal
116
         },
119
         },
120
+        // NOTE: sessionToken is flying around far too often
121
+        // hashedAccessToken
117
         async verifySession(hashedSessionToken) {
122
         async verifySession(hashedSessionToken) {
118
             if (!hashedSessionToken)
123
             if (!hashedSessionToken)
119
                 return console.warn('WARNING :=> sessionToken is not defined')
124
                 return console.warn('WARNING :=> sessionToken is not defined')

+ 1
- 2
frontend/src/views/VerifyView.vue Прегледај датотеку

52
                 hashedToken,
52
                 hashedToken,
53
             )
53
             )
54
             if (!sessionData.hashesMatch)
54
             if (!sessionData.hashesMatch)
55
-                throw new Error('Hash is not in registry!')
56
-            else return sessionData.sessionToken
55
+                throw new Error('Hash is not in activeSessions!')
57
         },
56
         },
58
         async isSessionTokenValid(hash) {
57
         async isSessionTokenValid(hash) {
59
             const sessionTokenIsValid =
58
             const sessionTokenIsValid =

Loading…
Откажи
Сачувај