Quellcode durchsuchen

:construction: Set up scaffolding for other jwt lib

tags/0.0.3^2
tomit4 vor 3 Jahren
Ursprung
Commit
eac468fcfa

+ 5
- 11
backend/lib/auth/strategies/jwt.js Datei anzeigen

@@ -2,16 +2,12 @@
2 2
 
3 3
 module.exports = options => {
4 4
     return {
5
-        keys: {
6
-            key: options.jwtKey,
5
+        key: options.jwtKey,
6
+        verifyOptions: {
7 7
             algorithms: ['HS256'],
8 8
         },
9
-        verify: {
10
-            aud: 'urn:audience:test',
11
-            iss: 'urn:issuer:test',
12
-            sub: false,
13
-        },
14
-        validate: (artifacts, request, h) => {
9
+        validate: (decoded, request, h) => {
10
+            console.log('decoded :>>', decoded)
15 11
             try {
16 12
                 // Check if the Access Token is Valid
17 13
                 // if (!accessTokenIsValid) {
@@ -25,9 +21,7 @@ module.exports = options => {
25 21
                 }
26 22
             } catch (err) {
27 23
                 console.error(err)
28
-                return {
29
-                    isValid: false,
30
-                }
24
+                return { isValid: false }
31 25
             }
32 26
         },
33 27
     }

+ 1
- 1
backend/lib/plugins/user.js Datei anzeigen

@@ -1,7 +1,7 @@
1 1
 const Objection = require('objection')
2 2
 const Schmervice = require('@hapipal/schmervice')
3 3
 const Schwifty = require('@hapipal/schwifty')
4
-const Jwt = require('@hapi/jwt')
4
+const Jwt = require('hapi-auth-jwt2')
5 5
 const JwtStrategy = require('../auth/strategies/jwt')
6 6
 
7 7
 const UserModel = require('../models/user')

+ 14
- 103
backend/lib/services/user.js Datei anzeigen

@@ -2,7 +2,6 @@
2 2
 require('dotenv').config()
3 3
 const crypto = require('crypto')
4 4
 const Util = require('util')
5
-const Jwt = require('@hapi/jwt')
6 5
 const JWT = require('jsonwebtoken')
7 6
 const Schmervice = require('@hapipal/schmervice')
8 7
 const SecurePassword = require('secure-password')
@@ -23,7 +22,6 @@ const hashEmail = async email => {
23 22
         return undefined
24 23
     }
25 24
 }
26
-// const emailsSent = {}
27 25
 const hasher = async (pwd, steak) => {
28 26
     const hash = await pwd.hash(steak)
29 27
     const result = await pwd.verify(steak, hash)
@@ -43,7 +41,7 @@ const hasher = async (pwd, steak) => {
43 41
             try {
44 42
                 squirtle = await pwd.hash(steak)
45 43
                 // console.log('improvedHash', squirtle)
46
-                // const saveHash = Auth.insert({user_email: matchingEmails}, ).into('token')
44
+                // const saveHash = Auth.insert({user_email: matchingEmails}).into('token')
47 45
                 return squirtle
48 46
             } catch (err) {
49 47
                 console.error(
@@ -63,8 +61,7 @@ module.exports = class UserService extends Schmervice.Service {
63 61
     constructor(...args) {
64 62
         super(...args)
65 63
         const pwd = new SecurePassword()
66
-        // TODO: Invalidate this cache somehow after a certain time period has
67
-        // passed
64
+        // TODO: Invalidate this application state somehow after a certain time period has passed
68 65
         // TODO: Remove hashedEmails in preference of activeSessions
69 66
         this.hashedEmails = {
70 67
             // NOTE: key is email hash and value is timestamp in ms
@@ -231,93 +228,10 @@ module.exports = class UserService extends Schmervice.Service {
231 228
     //
232 229
     createToken(user) {
233 230
         const key = this.server.registrations['main-app-plugin'].options.jwtKey
231
+        const obj = {}
234 232
 
235
-        let token = Jwt.token.generate(
236
-            {
237
-                aud: 'urn:audience:test',
238
-                iss: 'urn:issuer:test',
239
-                // ...payload,
240
-                email: user.email,
241
-                name: user.name,
242
-                seeking: user.seeking,
243
-                salt: 'a;ldfkjas;l/dfkafnml;/cjkf',
244
-                // profile_id: user.profile_id,
245
-            },
246
-            {
247
-                key: key,
248
-                algorithm: 'HS256',
249
-            },
250
-            {
251
-                ttlSec: 4 * 60 * 60, // 7 days
252
-            },
253
-        )
254
-        console.log('token :=>', token)
255
-        token = Jwt.token.generate(
256
-            {
257
-                aud: 'urn:audience:test',
258
-                iss: 'urn:issuer:test',
259
-                // ...payload,
260
-                email: user.email,
261
-                name: user.name,
262
-                seeking: user.seeking,
263
-                salt: 'qpowieurpqowytqpoieryu',
264
-                // profile_id: user.profile_id,
265
-            },
266
-            {
267
-                key: key,
268
-                algorithm: 'HS256',
269
-            },
270
-            {
271
-                ttlSec: 4 * 60 * 60, // 7 days
272
-            },
273
-        )
274
-        console.log('\n')
275
-        console.log('token :=>', token)
276
-        token = Jwt.token.generate(
277
-            {
278
-                aud: 'urn:audience:test',
279
-                iss: 'urn:issuer:test',
280
-                // ...payload,
281
-                email: user.email,
282
-                name: user.name,
283
-                seeking: user.seeking,
284
-                salt: 'a;ldfkjas;l/dfkafnml;/cjkf',
285
-                // profile_id: user.profile_id,
286
-            },
287
-            {
288
-                key: key,
289
-                algorithm: 'HS256',
290
-            },
291
-            {
292
-                ttlSec: 6 * 60 * 60, // 7 days
293
-            },
294
-        )
295
-        console.log('token :=>', token)
296
-        token = Jwt.token.generate(
297
-            {
298
-                aud: 'urn:audience:test',
299
-                iss: 'urn:issuer:test',
300
-                // ...payload,
301
-                email: user.email,
302
-                name: user.name,
303
-                seeking: user.seeking,
304
-                salt: 'a;ldfkjas;l/dfkafnml;/cjkf',
305
-                // profile_id: user.profile_id,
306
-            },
307
-            {
308
-                key: key,
309
-                algorithm: 'HS256',
310
-            },
311
-            {
312
-                ttlSec: 7 * 60 * 60, // 7 days
313
-            },
314
-        )
315
-        console.log('token :=>', token)
316
-
317
-        // TODO: keep userinfo and it's association with the sessionToken in state/memory
318
-        // registerSession(user, sessionToken) // useremail, token
319
-        // this.registerSession(user, token)
320
-        return token
233
+        Object.assign(obj, { ...user })
234
+        return JWT.sign(obj, key)
321 235
     }
322 236
 
323 237
     async registerSession(user, hashedEmail, token) {
@@ -326,6 +240,7 @@ module.exports = class UserService extends Schmervice.Service {
326 240
             hashedEmail: hashedEmail,
327 241
             token: token,
328 242
         }
243
+
329 244
         const alreadyExists = this.activeSessions.find(
330 245
             sessionRequester => sessionRequester.hashedEmail === hashedEmail,
331 246
         )
@@ -339,13 +254,14 @@ module.exports = class UserService extends Schmervice.Service {
339 254
      * @returns {Token}
340 255
      */
341 256
     validateToken(token) {
257
+        console.log('token :=>', token)
342 258
         const key = this.server.registrations['main-app-plugin'].options.jwtKey
343
-        // NOTE: reveals email...perhaps unhashed email belongs here instead...
344 259
         try {
345
-            const decodedToken = Jwt.token.decode(token)
346
-            Jwt.token.verify(decodedToken, key)
347
-            return { isValid: true, payload: decodedToken.decoded.payload }
260
+            const decodedToken = JWT.verify(token, key)
261
+            console.log('decodedToken :=>', decodedToken)
262
+            return { isValid: true, payload: decodedToken }
348 263
         } catch (err) {
264
+            console.error('ERROR :=>', err)
349 265
             return { isValid: false, error: err.message }
350 266
         }
351 267
     }
@@ -419,6 +335,7 @@ module.exports = class UserService extends Schmervice.Service {
419 335
         }
420 336
         // Set expiration time for five minutes from now
421 337
         const duration = 1000 * 60 * 5
338
+
422 339
         this.hashedEmails[hashedEmail] = Date.now() + duration
423 340
         const sendSmtpEmail = {
424 341
             to: [
@@ -435,16 +352,10 @@ module.exports = class UserService extends Schmervice.Service {
435 352
 
436 353
         await apiInstance.sendTransacEmail(sendSmtpEmail).then(
437 354
             data => {
438
-                return {
439
-                    wasSuccessfull: true,
440
-                    data: data,
441
-                }
355
+                return { wasSuccessfull: true, data: data }
442 356
             },
443 357
             error => {
444
-                return {
445
-                    wasSuccessfull: false,
446
-                    error: error,
447
-                }
358
+                return { wasSuccessfull: false, error: error }
448 359
             },
449 360
         )
450 361
     }

+ 0
- 53
backend/package-lock.json Datei anzeigen

@@ -12,7 +12,6 @@
12 12
                 "@hapi/glue": "^8.0.0",
13 13
                 "@hapi/hapi": "^20.1.3",
14 14
                 "@hapi/inert": "^6.0.3",
15
-                "@hapi/jwt": "^2.2.0",
16 15
                 "@hapi/vision": "^6.0.1",
17 16
                 "@hapipal/confidence": "^6.0.1",
18 17
                 "@hapipal/schmervice": "^2.0.0",
@@ -666,15 +665,6 @@
666 665
                 "@hapi/hoek": "9.x.x"
667 666
             }
668 667
         },
669
-        "node_modules/@hapi/catbox-object": {
670
-            "version": "2.0.0",
671
-            "resolved": "https://registry.npmjs.org/@hapi/catbox-object/-/catbox-object-2.0.0.tgz",
672
-            "integrity": "sha512-tzTo5q9UVqwqtpNkIz0VNSmJTbaGyD9ZQmw4a91BBWB+YJWYa066KkxOTHGmmWJzjZEhG2CsNYKu34J25pA5aw==",
673
-            "dependencies": {
674
-                "@hapi/boom": "9.x.x",
675
-                "@hapi/hoek": "9.x.x"
676
-            }
677
-        },
678 668
         "node_modules/@hapi/content": {
679 669
             "version": "5.0.2",
680 670
             "resolved": "https://registry.npmjs.org/@hapi/content/-/content-5.0.2.tgz",
@@ -777,23 +767,6 @@
777 767
                 "@hapi/hoek": "9.x.x"
778 768
             }
779 769
         },
780
-        "node_modules/@hapi/jwt": {
781
-            "version": "2.2.0",
782
-            "resolved": "https://registry.npmjs.org/@hapi/jwt/-/jwt-2.2.0.tgz",
783
-            "integrity": "sha512-hOzQ/E0O9XemapjYddGH4ReCG5JEHz62zLeNou4Mt282yx7JknCPTTsnsqkxRE+EPVWNGXGz2E3SDlST80hjMw==",
784
-            "dependencies": {
785
-                "@hapi/b64": "5.x.x",
786
-                "@hapi/boom": "9.x.x",
787
-                "@hapi/bounce": "2.x.x",
788
-                "@hapi/bourne": "2.x.x",
789
-                "@hapi/catbox-object": "2.x.x",
790
-                "@hapi/cryptiles": "5.x.x",
791
-                "@hapi/hoek": "9.x.x",
792
-                "@hapi/wreck": "17.x.x",
793
-                "ecdsa-sig-formatter": "1.x.x",
794
-                "joi": "^17.2.1"
795
-            }
796
-        },
797 770
         "node_modules/@hapi/mimos": {
798 771
             "version": "6.0.0",
799 772
             "resolved": "https://registry.npmjs.org/@hapi/mimos/-/mimos-6.0.0.tgz",
@@ -9168,15 +9141,6 @@
9168 9141
                 "@hapi/hoek": "9.x.x"
9169 9142
             }
9170 9143
         },
9171
-        "@hapi/catbox-object": {
9172
-            "version": "2.0.0",
9173
-            "resolved": "https://registry.npmjs.org/@hapi/catbox-object/-/catbox-object-2.0.0.tgz",
9174
-            "integrity": "sha512-tzTo5q9UVqwqtpNkIz0VNSmJTbaGyD9ZQmw4a91BBWB+YJWYa066KkxOTHGmmWJzjZEhG2CsNYKu34J25pA5aw==",
9175
-            "requires": {
9176
-                "@hapi/boom": "9.x.x",
9177
-                "@hapi/hoek": "9.x.x"
9178
-            }
9179
-        },
9180 9144
         "@hapi/content": {
9181 9145
             "version": "5.0.2",
9182 9146
             "resolved": "https://registry.npmjs.org/@hapi/content/-/content-5.0.2.tgz",
@@ -9273,23 +9237,6 @@
9273 9237
                 "@hapi/hoek": "9.x.x"
9274 9238
             }
9275 9239
         },
9276
-        "@hapi/jwt": {
9277
-            "version": "2.2.0",
9278
-            "resolved": "https://registry.npmjs.org/@hapi/jwt/-/jwt-2.2.0.tgz",
9279
-            "integrity": "sha512-hOzQ/E0O9XemapjYddGH4ReCG5JEHz62zLeNou4Mt282yx7JknCPTTsnsqkxRE+EPVWNGXGz2E3SDlST80hjMw==",
9280
-            "requires": {
9281
-                "@hapi/b64": "5.x.x",
9282
-                "@hapi/boom": "9.x.x",
9283
-                "@hapi/bounce": "2.x.x",
9284
-                "@hapi/bourne": "2.x.x",
9285
-                "@hapi/catbox-object": "2.x.x",
9286
-                "@hapi/cryptiles": "5.x.x",
9287
-                "@hapi/hoek": "9.x.x",
9288
-                "@hapi/wreck": "17.x.x",
9289
-                "ecdsa-sig-formatter": "1.x.x",
9290
-                "joi": "^17.2.1"
9291
-            }
9292
-        },
9293 9240
         "@hapi/mimos": {
9294 9241
             "version": "6.0.0",
9295 9242
             "resolved": "https://registry.npmjs.org/@hapi/mimos/-/mimos-6.0.0.tgz",

+ 0
- 1
backend/package.json Datei anzeigen

@@ -20,7 +20,6 @@
20 20
         "@hapi/glue": "^8.0.0",
21 21
         "@hapi/hapi": "^20.1.3",
22 22
         "@hapi/inert": "^6.0.3",
23
-        "@hapi/jwt": "^2.2.0",
24 23
         "@hapi/vision": "^6.0.1",
25 24
         "@hapipal/confidence": "^6.0.1",
26 25
         "@hapipal/schmervice": "^2.0.0",

+ 5
- 4
frontend/src/views/OnboardingView.vue Datei anzeigen

@@ -68,9 +68,9 @@ export default {
68 68
     async created() {
69 69
         this.survey = await surveyFactory.createSurvey()
70 70
         this.authenticator = new Authenticator()
71
+        // TODO: Once tokens are coming through headers, refactor all of this into methods, etc.
71 72
         // TODO: Consider switch/case() depending on what tokens exist/are valid...
72 73
         sessionToken = this.grabStoredCookie('siimee_session')
73
-        console.log('sessionToken :=>', sessionToken)
74 74
         // if (!sessionToken) {
75 75
         //     //
76 76
         // }
@@ -78,9 +78,10 @@ export default {
78 78
         // if (!accessToken) {
79 79
         //     // blow up
80 80
         // }
81
-        const sessionData = await this.authenticator.validateSession(
82
-            sessionToken,
83
-        )
81
+        let sessionData
82
+        if (sessionToken) {
83
+            sessionData = await this.authenticator.validateSession(sessionToken)
84
+        }
84 85
         // if (sessionData.isValid && !accessToken) {
85 86
         if (sessionData.isValid) {
86 87
             this.userEmail = sessionData.payload.email

Laden…
Abbrechen
Speichern