浏览代码

:recycle: Started refactor by breaking out validateSession into helper funcs

tags/0.0.3^2
tomit4 2 年前
父节点
当前提交
efaf03889e
共有 2 个文件被更改,包括 53 次插入40 次删除
  1. 5
    6
      backend/lib/auth/strategies/jwt.js
  2. 48
    34
      backend/lib/services/user.js

+ 5
- 6
backend/lib/auth/strategies/jwt.js 查看文件

@@ -7,14 +7,13 @@ 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
-        //
10
+        // TODO: check the h object to see if the activeSessions is accessible from it
12 11
         // check useronlinestatus branch request.server.app
12
+        // Always check rawAccessToken, if it fails, we check the session, if session
13
+        // is valid, then we reissue it
14
+        // if session is NOT valid, DELETE the session (and kick user back to login)
15
+        // TODO: set up cron job to occassionaly clean up activeSessions
13 16
         validate: (decoded, request, h) => {
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
18 17
             const token = request.headers.authorization
19 18
             try {
20 19
                 const validatedJwt = JWT.verify(token, process.env.APP_SECRET)

+ 48
- 34
backend/lib/services/user.js 查看文件

@@ -248,51 +248,65 @@ module.exports = class UserService extends Schmervice.Service {
248 248
             return { payload: null, message: err.message }
249 249
         }
250 250
     }
251
-
251
+    /*
252
+     * Grabs the sessionToken and accessToken from the
253
+     * this.activeSessions object based off of provided hashedToken
254
+     * @params {UserSession}
255
+     * @returns {grabTokensFromActiveSession}
256
+     */
257
+    _grabTokensFromActiveSessions(userSession) {
258
+        const rawSessionToken = userSession.sessionToken
259
+        const accessToken = userSession.accessToken
260
+        return { rawSessionToken: rawSessionToken, accessToken: accessToken }
261
+    }
262
+    /**
263
+     * Helper function to validate both tokens grabbed from this.activeSessions
264
+     * @params {Tokens}
265
+     * @returns {ValidatedTokens}
266
+     */
267
+    _validateTokens(tokens) {
268
+        const sessionTokenIsValid = this.validateToken(tokens.rawSessionToken)
269
+        const accessTokenIsValid = this.validateToken(tokens.accessToken)
270
+        return {
271
+            sessionTokenIsValid: sessionTokenIsValid,
272
+            accessTokenIsValid: accessTokenIsValid,
273
+        }
274
+    }
275
+    /**
276
+     * Checks to see if the activeSession accessToken is expired
277
+     * If it is, it creates a new one and stores it in activeSession
278
+     * @ params {UserSession} {ValidatedTokens}
279
+     * @returns Void
280
+     */
281
+    _createAccessTokenIfExpired(userSession, validatedTokens) {
282
+        if (!validatedTokens.accessTokenIsValid.payload) {
283
+            const accessToken = this.createToken({
284
+                payload: validatedTokens.sessionTokenIsValid.payload,
285
+            })
286
+            userSession.accessToken = accessToken
287
+        }
288
+    }
252 289
     /**
253 290
      * Uses this.validateToken() to verify hashedSessionToken's
254 291
      * existence, expiry, and also valdiates accessToken
255
-     * @param {User} user
256
-     * @returns {Token}
292
+     * @param {HashedSessionToken} hashedSessionToken
293
+     * @returns {PayloadFromActiveSessions}
257 294
      */
258
-    // TODO: remove testing console.log() messages once onboarding auth is working
259
-    // REFACTOR: Have this function only do one thing (UNIX philsophy)
260 295
     validateSession(hashedSessionToken) {
296
+        // TODO: Remove this console.log() prior to release to production,
297
+        // (useful for testing application state)
261 298
         console.log('this.activeSessions :=>', this.activeSessions)
262
-        if (!this.activeSessions[hashedSessionToken]) {
299
+        const userSession = this.activeSessions[hashedSessionToken]
300
+        if (!userSession) {
263 301
             throw new Error(
264 302
                 'hashedSessionToken not in activeSessions registry!',
265 303
             )
266 304
         }
267
-        // BREAK OUT INTO ANOTHER FUNC
268
-        const rawSessionToken =
269
-            this.activeSessions[hashedSessionToken].sessionToken
270
-        const accessToken = this.activeSessions[hashedSessionToken].accessToken
271
-
272
-        // Weird Edge case...
273
-        if (!rawSessionToken) {
274
-            throw new Error(
275
-                'hashedSessionToken is in activeSessions registry, but rawSessionToken does not exist',
276
-            )
277
-        }
278
-        // ANOTHER FUNC HERE
279
-        const sessionTokenIsValid = this.validateToken(rawSessionToken)
280
-        const accessTokenIsValid = this.validateToken(accessToken)
281
-
282
-        // Both sessionToken and accessToken are expired
283
-        // createAccessToken()
284
-        //
285
-        if (!accessTokenIsValid.payload) {
286
-            console.log(
287
-                'sessionToken is valid, but accessToken is null or is expired :=>',
288
-            )
289
-            const accessToken = this.createToken({
290
-                payload: sessionTokenIsValid.payload,
291
-            })
292
-            this.activeSessions[hashedSessionToken].accessToken = accessToken
293
-        }
305
+        const tokens = this._grabTokensFromActiveSessions(userSession)
306
+        const validatedTokens = this._validateTokens(tokens)
307
+        this._createAccessTokenIfExpired(userSession, validatedTokens)
294 308
         return {
295
-            ...sessionTokenIsValid.payload,
309
+            ...validatedTokens.sessionTokenIsValid.payload,
296 310
             sessionToken: this.activeSessions[hashedSessionToken].sessionToken,
297 311
         }
298 312
     }

正在加载...
取消
保存