Bläddra i källkod

:construction: Set up scaffolding for other jwt lib

tags/0.0.3^2
tomit4 3 år sedan
förälder
incheckning
eac468fcfa

+ 5
- 11
backend/lib/auth/strategies/jwt.js Visa fil

2
 
2
 
3
 module.exports = options => {
3
 module.exports = options => {
4
     return {
4
     return {
5
-        keys: {
6
-            key: options.jwtKey,
5
+        key: options.jwtKey,
6
+        verifyOptions: {
7
             algorithms: ['HS256'],
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
             try {
11
             try {
16
                 // Check if the Access Token is Valid
12
                 // Check if the Access Token is Valid
17
                 // if (!accessTokenIsValid) {
13
                 // if (!accessTokenIsValid) {
25
                 }
21
                 }
26
             } catch (err) {
22
             } catch (err) {
27
                 console.error(err)
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 Visa fil

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

+ 14
- 103
backend/lib/services/user.js Visa fil

2
 require('dotenv').config()
2
 require('dotenv').config()
3
 const crypto = require('crypto')
3
 const crypto = require('crypto')
4
 const Util = require('util')
4
 const Util = require('util')
5
-const Jwt = require('@hapi/jwt')
6
 const JWT = require('jsonwebtoken')
5
 const JWT = require('jsonwebtoken')
7
 const Schmervice = require('@hapipal/schmervice')
6
 const Schmervice = require('@hapipal/schmervice')
8
 const SecurePassword = require('secure-password')
7
 const SecurePassword = require('secure-password')
23
         return undefined
22
         return undefined
24
     }
23
     }
25
 }
24
 }
26
-// const emailsSent = {}
27
 const hasher = async (pwd, steak) => {
25
 const hasher = async (pwd, steak) => {
28
     const hash = await pwd.hash(steak)
26
     const hash = await pwd.hash(steak)
29
     const result = await pwd.verify(steak, hash)
27
     const result = await pwd.verify(steak, hash)
43
             try {
41
             try {
44
                 squirtle = await pwd.hash(steak)
42
                 squirtle = await pwd.hash(steak)
45
                 // console.log('improvedHash', squirtle)
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
                 return squirtle
45
                 return squirtle
48
             } catch (err) {
46
             } catch (err) {
49
                 console.error(
47
                 console.error(
63
     constructor(...args) {
61
     constructor(...args) {
64
         super(...args)
62
         super(...args)
65
         const pwd = new SecurePassword()
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
         // TODO: Remove hashedEmails in preference of activeSessions
65
         // TODO: Remove hashedEmails in preference of activeSessions
69
         this.hashedEmails = {
66
         this.hashedEmails = {
70
             // NOTE: key is email hash and value is timestamp in ms
67
             // NOTE: key is email hash and value is timestamp in ms
231
     //
228
     //
232
     createToken(user) {
229
     createToken(user) {
233
         const key = this.server.registrations['main-app-plugin'].options.jwtKey
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
     async registerSession(user, hashedEmail, token) {
237
     async registerSession(user, hashedEmail, token) {
326
             hashedEmail: hashedEmail,
240
             hashedEmail: hashedEmail,
327
             token: token,
241
             token: token,
328
         }
242
         }
243
+
329
         const alreadyExists = this.activeSessions.find(
244
         const alreadyExists = this.activeSessions.find(
330
             sessionRequester => sessionRequester.hashedEmail === hashedEmail,
245
             sessionRequester => sessionRequester.hashedEmail === hashedEmail,
331
         )
246
         )
339
      * @returns {Token}
254
      * @returns {Token}
340
      */
255
      */
341
     validateToken(token) {
256
     validateToken(token) {
257
+        console.log('token :=>', token)
342
         const key = this.server.registrations['main-app-plugin'].options.jwtKey
258
         const key = this.server.registrations['main-app-plugin'].options.jwtKey
343
-        // NOTE: reveals email...perhaps unhashed email belongs here instead...
344
         try {
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
         } catch (err) {
263
         } catch (err) {
264
+            console.error('ERROR :=>', err)
349
             return { isValid: false, error: err.message }
265
             return { isValid: false, error: err.message }
350
         }
266
         }
351
     }
267
     }
419
         }
335
         }
420
         // Set expiration time for five minutes from now
336
         // Set expiration time for five minutes from now
421
         const duration = 1000 * 60 * 5
337
         const duration = 1000 * 60 * 5
338
+
422
         this.hashedEmails[hashedEmail] = Date.now() + duration
339
         this.hashedEmails[hashedEmail] = Date.now() + duration
423
         const sendSmtpEmail = {
340
         const sendSmtpEmail = {
424
             to: [
341
             to: [
435
 
352
 
436
         await apiInstance.sendTransacEmail(sendSmtpEmail).then(
353
         await apiInstance.sendTransacEmail(sendSmtpEmail).then(
437
             data => {
354
             data => {
438
-                return {
439
-                    wasSuccessfull: true,
440
-                    data: data,
441
-                }
355
+                return { wasSuccessfull: true, data: data }
442
             },
356
             },
443
             error => {
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 Visa fil

12
                 "@hapi/glue": "^8.0.0",
12
                 "@hapi/glue": "^8.0.0",
13
                 "@hapi/hapi": "^20.1.3",
13
                 "@hapi/hapi": "^20.1.3",
14
                 "@hapi/inert": "^6.0.3",
14
                 "@hapi/inert": "^6.0.3",
15
-                "@hapi/jwt": "^2.2.0",
16
                 "@hapi/vision": "^6.0.1",
15
                 "@hapi/vision": "^6.0.1",
17
                 "@hapipal/confidence": "^6.0.1",
16
                 "@hapipal/confidence": "^6.0.1",
18
                 "@hapipal/schmervice": "^2.0.0",
17
                 "@hapipal/schmervice": "^2.0.0",
666
                 "@hapi/hoek": "9.x.x"
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
         "node_modules/@hapi/content": {
668
         "node_modules/@hapi/content": {
679
             "version": "5.0.2",
669
             "version": "5.0.2",
680
             "resolved": "https://registry.npmjs.org/@hapi/content/-/content-5.0.2.tgz",
670
             "resolved": "https://registry.npmjs.org/@hapi/content/-/content-5.0.2.tgz",
777
                 "@hapi/hoek": "9.x.x"
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
         "node_modules/@hapi/mimos": {
770
         "node_modules/@hapi/mimos": {
798
             "version": "6.0.0",
771
             "version": "6.0.0",
799
             "resolved": "https://registry.npmjs.org/@hapi/mimos/-/mimos-6.0.0.tgz",
772
             "resolved": "https://registry.npmjs.org/@hapi/mimos/-/mimos-6.0.0.tgz",
9168
                 "@hapi/hoek": "9.x.x"
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
         "@hapi/content": {
9144
         "@hapi/content": {
9181
             "version": "5.0.2",
9145
             "version": "5.0.2",
9182
             "resolved": "https://registry.npmjs.org/@hapi/content/-/content-5.0.2.tgz",
9146
             "resolved": "https://registry.npmjs.org/@hapi/content/-/content-5.0.2.tgz",
9273
                 "@hapi/hoek": "9.x.x"
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
         "@hapi/mimos": {
9240
         "@hapi/mimos": {
9294
             "version": "6.0.0",
9241
             "version": "6.0.0",
9295
             "resolved": "https://registry.npmjs.org/@hapi/mimos/-/mimos-6.0.0.tgz",
9242
             "resolved": "https://registry.npmjs.org/@hapi/mimos/-/mimos-6.0.0.tgz",

+ 0
- 1
backend/package.json Visa fil

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

+ 5
- 4
frontend/src/views/OnboardingView.vue Visa fil

68
     async created() {
68
     async created() {
69
         this.survey = await surveyFactory.createSurvey()
69
         this.survey = await surveyFactory.createSurvey()
70
         this.authenticator = new Authenticator()
70
         this.authenticator = new Authenticator()
71
+        // TODO: Once tokens are coming through headers, refactor all of this into methods, etc.
71
         // TODO: Consider switch/case() depending on what tokens exist/are valid...
72
         // TODO: Consider switch/case() depending on what tokens exist/are valid...
72
         sessionToken = this.grabStoredCookie('siimee_session')
73
         sessionToken = this.grabStoredCookie('siimee_session')
73
-        console.log('sessionToken :=>', sessionToken)
74
         // if (!sessionToken) {
74
         // if (!sessionToken) {
75
         //     //
75
         //     //
76
         // }
76
         // }
78
         // if (!accessToken) {
78
         // if (!accessToken) {
79
         //     // blow up
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
         // if (sessionData.isValid && !accessToken) {
85
         // if (sessionData.isValid && !accessToken) {
85
         if (sessionData.isValid) {
86
         if (sessionData.isValid) {
86
             this.userEmail = sessionData.payload.email
87
             this.userEmail = sessionData.payload.email

Laddar…
Avbryt
Spara