Sfoglia il codice sorgente

:construction: Verification of JWT through headers/strategies achieved

juan_spike
tomit4 2 anni fa
parent
commit
d27a69a062

+ 6
- 10
backend/lib/auth/strategies/jwt.js Vedi File

1
 'use strict'
1
 'use strict'
2
+const JWT = require('jsonwebtoken')
2
 
3
 
3
 module.exports = options => {
4
 module.exports = options => {
4
     return {
5
     return {
7
             algorithms: ['HS256'],
8
             algorithms: ['HS256'],
8
         },
9
         },
9
         validate: (decoded, request, h) => {
10
         validate: (decoded, request, h) => {
10
-            console.log('decoded :>>', decoded) // doesn't log to console...
11
+            const token = request.headers.authorization
11
             try {
12
             try {
12
-                // Check if the Access Token is Valid
13
-                // if (!accessTokenIsValid) {
14
-                // Look up if the Session is Active
15
-                // } else {
16
-                // isValid: true
17
-                // }
13
+                const validatedJwt = JWT.verify(token, process.env.APP_SECRET)
18
                 return {
14
                 return {
19
                     isValid: true,
15
                     isValid: true,
20
-                    credentials: { user: artifacts.decoded.payload.user },
16
+                    credentials: validatedJwt.payload.email,
21
                 }
17
                 }
22
             } catch (err) {
18
             } catch (err) {
23
-                console.error(err)
24
-                return { isValid: false }
19
+                console.error('ERROR :=>', err)
20
+                return { isValid: false, error: err.message }
25
             }
21
             }
26
         },
22
         },
27
     }
23
     }

+ 3
- 4
backend/lib/routes/user/validatesession.js Vedi File

15
 
15
 
16
 module.exports = {
16
 module.exports = {
17
     method: 'GET',
17
     method: 'GET',
18
-    path: '/validatesession/{sessionToken}',
19
-    // method: 'GET' sessionToken in header ?
18
+    path: '/validatesession',
20
     options: {
19
     options: {
21
         ...pluginConfig.docs.get,
20
         ...pluginConfig.docs.get,
22
         tags: ['api'],
21
         tags: ['api'],
23
-        auth: false, // set to jwt strategy
22
+        auth: 'default_jwt',
24
         cors: true,
23
         cors: true,
25
         handler: async function (request, h) {
24
         handler: async function (request, h) {
26
-            const sessionToken = request.params.sessionToken
25
+            const sessionToken = request.headers.authorization
27
             const { userService } = request.server.services()
26
             const { userService } = request.server.services()
28
             try {
27
             try {
29
                 const validatedSessionToken =
28
                 const validatedSessionToken =

+ 1
- 1
backend/lib/services/user.js Vedi File

251
      */
251
      */
252
     // TODO: Move this ino the auth strategies
252
     // TODO: Move this ino the auth strategies
253
     validateToken(token) {
253
     validateToken(token) {
254
-        const key = this.server.registrations['main-app-plugin'].options.jwtKey // mysecret
254
+        const key = this.server.registrations['main-app-plugin'].options.jwtKey
255
         try {
255
         try {
256
             return JWT.verify(token, key)
256
             return JWT.verify(token, key)
257
         } catch (err) {
257
         } catch (err) {

+ 1
- 1
frontend/src/services/auth.service.js Vedi File

21
         return await db.post('/user/getaccess', req, true)
21
         return await db.post('/user/getaccess', req, true)
22
     }
22
     }
23
     async validateSession(sessionToken) {
23
     async validateSession(sessionToken) {
24
-        return await db.get(`/user/validatesession/${sessionToken}`)
24
+        return await db.get('/user/validatesession', sessionToken)
25
     }
25
     }
26
 }
26
 }
27
 
27
 

+ 19
- 6
frontend/src/utils/db.js Vedi File

27
             patch: 'PATCH',
27
             patch: 'PATCH',
28
         }
28
         }
29
     }
29
     }
30
-    _makeHeader({ method, payload }) {
30
+    _makeHeader({ method, payload, authorization }) {
31
         const header = { ...headerTemplate }
31
         const header = { ...headerTemplate }
32
         header.method = method
32
         header.method = method
33
         if (payload) {
33
         if (payload) {
34
             header.body = JSON.stringify(payload)
34
             header.body = JSON.stringify(payload)
35
         }
35
         }
36
+        if (authorization) {
37
+            header.headers.authorization = authorization
38
+        }
36
         return header
39
         return header
37
     }
40
     }
38
     async _tryFetch({ endpoint, header }, returnHeaders = false) {
41
     async _tryFetch({ endpoint, header }, returnHeaders = false) {
51
             console.error(`[API Util]: ${error}\nroute:`, endpoint)
54
             console.error(`[API Util]: ${error}\nroute:`, endpoint)
52
         }
55
         }
53
     }
56
     }
54
-    async get(endpoint) {
55
-        return await this._tryFetch({
56
-            endpoint,
57
-            header: this._makeHeader({ method: this._verbs.get }),
58
-        })
57
+    async get(endpoint, authHeaders = false) {
58
+        if (authHeaders) {
59
+            return await this._tryFetch({
60
+                endpoint,
61
+                header: this._makeHeader({
62
+                    method: this._verbs.get,
63
+                    authorization: `${authHeaders}`,
64
+                }),
65
+            })
66
+        } else {
67
+            return await this._tryFetch({
68
+                endpoint,
69
+                header: this._makeHeader({ method: this._verbs.get }),
70
+            })
71
+        }
59
     }
72
     }
60
     async post(endpoint, payload = {}, returnHeaders = false) {
73
     async post(endpoint, payload = {}, returnHeaders = false) {
61
         return await this._tryFetch({
74
         return await this._tryFetch({

Loading…
Annulla
Salva