Kaynağa Gözat

:sparkles: Notes added for refactor before merge

tags/0.0.3^2
tomit4 2 yıl önce
ebeveyn
işleme
2cf1669715

+ 6
- 0
backend/lib/auth/strategies/jwt.js Dosyayı Görüntüle

@@ -7,8 +7,14 @@ module.exports = options => {
7 7
         verifyOptions: {
8 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 13
         validate: (decoded, request, h) => {
11 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 18
             const token = request.headers.authorization
13 19
             try {
14 20
                 const validatedJwt = JWT.verify(token, process.env.APP_SECRET)

+ 3
- 0
backend/lib/routes/user/email.js Dosyayı Görüntüle

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

+ 6
- 6
backend/lib/routes/user/verifyactivesession.js Dosyayı Görüntüle

@@ -29,10 +29,13 @@ module.exports = {
29 29
                 ).find(hashedToken => {
30 30
                     return hashedToken === hash
31 31
                 })
32
+                if (!hashToMatch.length) {
33
+                    throw Error('hashToMatch Not Found!')
34
+                }
32 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 39
                 if (now > expiration) {
37 40
                     delete userService.activeSessions[hashToMatch]
38 41
                     throw new Error(
@@ -42,14 +45,11 @@ module.exports = {
42 45
                 if (!hashToMatch) {
43 46
                     throw new Error('no record of email in cache')
44 47
                 }
45
-
46 48
                 return {
47 49
                     ok: true,
48 50
                     handler: pluginConfig.handlerType,
49 51
                     data: {
50 52
                         hashesMatch: hashToMatch === hash,
51
-                        sessionToken:
52
-                            userService.activeSessions[`${hash}`].sessionToken,
53 53
                     },
54 54
                 }
55 55
             } catch (err) {

+ 16
- 16
backend/lib/services/user.js Dosyayı Görüntüle

@@ -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,

+ 5
- 0
frontend/src/views/OnboardingView.vue Dosyayı Görüntüle

@@ -76,6 +76,9 @@ export default {
76 76
             // TODO: Validate All routes hit by these methods using tokens in headers
77 77
             // NOTE: This can be accomplished using sessionData.sessionToken,
78 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 82
             const userId = await this.grabUserIdByEmail(
80 83
                 sessionData.email,
81 84
                 sessionData.sessionToken,
@@ -114,6 +117,8 @@ export default {
114 117
                 cookieKey in cookies ? cookies[`${cookieKey}`] : undefined
115 118
             return cookieVal
116 119
         },
120
+        // NOTE: sessionToken is flying around far too often
121
+        // hashedAccessToken
117 122
         async verifySession(hashedSessionToken) {
118 123
             if (!hashedSessionToken)
119 124
                 return console.warn('WARNING :=> sessionToken is not defined')

+ 1
- 2
frontend/src/views/VerifyView.vue Dosyayı Görüntüle

@@ -52,8 +52,7 @@ export default {
52 52
                 hashedToken,
53 53
             )
54 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 57
         async isSessionTokenValid(hash) {
59 58
             const sessionTokenIsValid =

Loading…
İptal
Kaydet