Pārlūkot izejas kodu

:recycle: moving around stuff more for jwt protection

master
TOJ 5 gadus atpakaļ
vecāks
revīzija
6c36a88142

+ 22
- 0
backend/lib/auth/strategies/jwt.js Parādīt failu

@@ -0,0 +1,22 @@
1
+'use strict';
2
+
3
+module.exports = (options) => {
4
+    return {
5
+        keys: {
6
+            key: options.jwtKey,
7
+            algorithms: ['HS256']
8
+        },
9
+        verify: {
10
+            aud: false,
11
+            iss: false,
12
+            sub: false
13
+        },
14
+        httpAuthScheme: 'Token',
15
+        validate: (artifacts, request, h) => {
16
+            return {
17
+                isValid: true,
18
+                credentials: { user: artifacts.decoded.payload.user }
19
+            }
20
+        }
21
+    }
22
+}

+ 16
- 0
backend/lib/index.js Parādīt failu

@@ -0,0 +1,16 @@
1
+const Jwt = require('@hapi/jwt');
2
+const AuthStratgey = require('./auth/strategies/jwt')
3
+const UserPlugin = require('./plugins/user');
4
+const TestPlugin = require('./plugins/test');
5
+
6
+exports.plugin = {
7
+    name: 'main-app-plugin',
8
+    register: async (server, options) => {
9
+
10
+        await server.register(TestPlugin, {})
11
+
12
+        await server.register(UserPlugin, {})
13
+
14
+
15
+    },
16
+}

+ 0
- 15
backend/lib/plugins/index.js Parādīt failu

@@ -1,15 +0,0 @@
1
-
2
-const UserPlugin = require('./user');
3
-const TestPlugin = require('./test');
4
-
5
-const pluginOptions = {
6
-    routes: { prefix: `/api` }
7
-}
8
-
9
-module.exports = {
10
-    name: 'my-app-plugin',
11
-    register: async (server, options) => {
12
-        await server.register(TestPlugin, pluginOptions)
13
-        await server.register(UserPlugin, pluginOptions)
14
-    }
15
-}

+ 11
- 3
backend/lib/plugins/user.js Parādīt failu

@@ -1,14 +1,22 @@
1 1
 const Schwifty = require('@hapipal/schwifty');
2
-
2
+const Jwt = require('@hapi/jwt');
3
+const JwtStrategy = require('../auth/strategies/jwt')
3 4
 const UserModel = require('../models/user');
4 5
 const UserCurrentRoute = require('../routes/user/current');
5 6
 
6 7
 module.exports = {
7
-    name: 'my-user-plugin',
8
+    name: 'user-plugin',
8 9
     version: '1.0.0',
9
-    register: async server => {
10
+    register: async (server, options) => {
11
+        await server.register(Jwt)
10 12
         await server.register(Schwifty)
11 13
         await server.registerModel(UserModel)
14
+
15
+        const mainApp = server.registrations['main-app-plugin']
16
+        const jwtOptions = JwtStrategy(mainApp.options)
17
+        server.auth.strategy('default_jwt', 'jwt', jwtOptions)
18
+        server.auth.default('default_jwt')
19
+
12 20
         await server.route(UserCurrentRoute)
13 21
     }
14 22
 }

+ 1
- 1
backend/lib/routes/user/current.js Parādīt failu

@@ -26,7 +26,6 @@ const validators = {
26 26
 module.exports = {
27 27
     method: 'get',
28 28
     path: '/user/{name}',
29
-    // auth: 'jwt',
30 29
     handler: async request => {
31 30
         try {
32 31
             /** Get the data for your endpoint */
@@ -50,6 +49,7 @@ module.exports = {
50 49
     options: {
51 50
         ...pluginConfig.docs.get,
52 51
         tags: ['api'],
52
+        auth: 'default_jwt',
53 53
         validate: validators.get,
54 54
         response: {
55 55
             schema: Joi.object({

+ 69
- 0
backend/package-lock.json Parādīt failu

@@ -12,6 +12,7 @@
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.0.1",
15 16
         "@hapi/vision": "^6.0.1",
16 17
         "@hapipal/confidence": "^6.0.1",
17 18
         "@hapipal/schwifty": "^6.0.0",
@@ -115,6 +116,15 @@
115 116
         "@hapi/hoek": "9.x.x"
116 117
       }
117 118
     },
119
+    "node_modules/@hapi/catbox-object": {
120
+      "version": "2.0.0",
121
+      "resolved": "https://registry.npmjs.org/@hapi/catbox-object/-/catbox-object-2.0.0.tgz",
122
+      "integrity": "sha512-tzTo5q9UVqwqtpNkIz0VNSmJTbaGyD9ZQmw4a91BBWB+YJWYa066KkxOTHGmmWJzjZEhG2CsNYKu34J25pA5aw==",
123
+      "dependencies": {
124
+        "@hapi/boom": "9.x.x",
125
+        "@hapi/hoek": "9.x.x"
126
+      }
127
+    },
118 128
     "node_modules/@hapi/content": {
119 129
       "version": "5.0.2",
120 130
       "resolved": "https://registry.npmjs.org/@hapi/content/-/content-5.0.2.tgz",
@@ -217,6 +227,23 @@
217 227
         "@hapi/hoek": "9.x.x"
218 228
       }
219 229
     },
230
+    "node_modules/@hapi/jwt": {
231
+      "version": "2.0.1",
232
+      "resolved": "https://registry.npmjs.org/@hapi/jwt/-/jwt-2.0.1.tgz",
233
+      "integrity": "sha512-6/nX/yOIk9mvs+r72LFhF177yOB4yVv3e0Nqn7cIx2CU+VruBHxMKkHraARXx6oUAtiwNuyhW+trO5QeGm9ESQ==",
234
+      "dependencies": {
235
+        "@hapi/b64": "5.x.x",
236
+        "@hapi/boom": "9.x.x",
237
+        "@hapi/bounce": "2.x.x",
238
+        "@hapi/bourne": "2.x.x",
239
+        "@hapi/catbox-object": "2.x.x",
240
+        "@hapi/cryptiles": "5.x.x",
241
+        "@hapi/hoek": "9.x.x",
242
+        "@hapi/wreck": "17.x.x",
243
+        "ecdsa-sig-formatter": "1.x.x",
244
+        "joi": "^17.2.1"
245
+      }
246
+    },
220 247
     "node_modules/@hapi/mimos": {
221 248
       "version": "6.0.0",
222 249
       "resolved": "https://registry.npmjs.org/@hapi/mimos/-/mimos-6.0.0.tgz",
@@ -1179,6 +1206,14 @@
1179 1206
       "integrity": "sha1-7gHdHKwO08vH/b6jfcCo8c4ALOI=",
1180 1207
       "dev": true
1181 1208
     },
1209
+    "node_modules/ecdsa-sig-formatter": {
1210
+      "version": "1.0.11",
1211
+      "resolved": "https://registry.npmjs.org/ecdsa-sig-formatter/-/ecdsa-sig-formatter-1.0.11.tgz",
1212
+      "integrity": "sha512-nagl3RYrbNv6kQkeJIpt6NJZy8twLB/2vtz6yN9Z4vRKHN4/QZJIEbqohALSgwKdnksuY3k5Addp5lg8sVoVcQ==",
1213
+      "dependencies": {
1214
+        "safe-buffer": "^5.0.1"
1215
+      }
1216
+    },
1182 1217
     "node_modules/emoji-regex": {
1183 1218
       "version": "8.0.0",
1184 1219
       "resolved": "https://registry.npmjs.org/emoji-regex/-/emoji-regex-8.0.0.tgz",
@@ -4088,6 +4123,15 @@
4088 4123
         "@hapi/hoek": "9.x.x"
4089 4124
       }
4090 4125
     },
4126
+    "@hapi/catbox-object": {
4127
+      "version": "2.0.0",
4128
+      "resolved": "https://registry.npmjs.org/@hapi/catbox-object/-/catbox-object-2.0.0.tgz",
4129
+      "integrity": "sha512-tzTo5q9UVqwqtpNkIz0VNSmJTbaGyD9ZQmw4a91BBWB+YJWYa066KkxOTHGmmWJzjZEhG2CsNYKu34J25pA5aw==",
4130
+      "requires": {
4131
+        "@hapi/boom": "9.x.x",
4132
+        "@hapi/hoek": "9.x.x"
4133
+      }
4134
+    },
4091 4135
     "@hapi/content": {
4092 4136
       "version": "5.0.2",
4093 4137
       "resolved": "https://registry.npmjs.org/@hapi/content/-/content-5.0.2.tgz",
@@ -4184,6 +4228,23 @@
4184 4228
         "@hapi/hoek": "9.x.x"
4185 4229
       }
4186 4230
     },
4231
+    "@hapi/jwt": {
4232
+      "version": "2.0.1",
4233
+      "resolved": "https://registry.npmjs.org/@hapi/jwt/-/jwt-2.0.1.tgz",
4234
+      "integrity": "sha512-6/nX/yOIk9mvs+r72LFhF177yOB4yVv3e0Nqn7cIx2CU+VruBHxMKkHraARXx6oUAtiwNuyhW+trO5QeGm9ESQ==",
4235
+      "requires": {
4236
+        "@hapi/b64": "5.x.x",
4237
+        "@hapi/boom": "9.x.x",
4238
+        "@hapi/bounce": "2.x.x",
4239
+        "@hapi/bourne": "2.x.x",
4240
+        "@hapi/catbox-object": "2.x.x",
4241
+        "@hapi/cryptiles": "5.x.x",
4242
+        "@hapi/hoek": "9.x.x",
4243
+        "@hapi/wreck": "17.x.x",
4244
+        "ecdsa-sig-formatter": "1.x.x",
4245
+        "joi": "^17.2.1"
4246
+      }
4247
+    },
4187 4248
     "@hapi/mimos": {
4188 4249
       "version": "6.0.0",
4189 4250
       "resolved": "https://registry.npmjs.org/@hapi/mimos/-/mimos-6.0.0.tgz",
@@ -4946,6 +5007,14 @@
4946 5007
       "integrity": "sha1-7gHdHKwO08vH/b6jfcCo8c4ALOI=",
4947 5008
       "dev": true
4948 5009
     },
5010
+    "ecdsa-sig-formatter": {
5011
+      "version": "1.0.11",
5012
+      "resolved": "https://registry.npmjs.org/ecdsa-sig-formatter/-/ecdsa-sig-formatter-1.0.11.tgz",
5013
+      "integrity": "sha512-nagl3RYrbNv6kQkeJIpt6NJZy8twLB/2vtz6yN9Z4vRKHN4/QZJIEbqohALSgwKdnksuY3k5Addp5lg8sVoVcQ==",
5014
+      "requires": {
5015
+        "safe-buffer": "^5.0.1"
5016
+      }
5017
+    },
4949 5018
     "emoji-regex": {
4950 5019
       "version": "8.0.0",
4951 5020
       "resolved": "https://registry.npmjs.org/emoji-regex/-/emoji-regex-8.0.0.tgz",

+ 1
- 0
backend/package.json Parādīt failu

@@ -14,6 +14,7 @@
14 14
     "@hapi/glue": "^8.0.0",
15 15
     "@hapi/hapi": "^20.1.3",
16 16
     "@hapi/inert": "^6.0.3",
17
+    "@hapi/jwt": "^2.0.1",
17 18
     "@hapi/vision": "^6.0.1",
18 19
     "@hapipal/confidence": "^6.0.1",
19 20
     "@hapipal/schwifty": "^6.0.0",

+ 21
- 3
backend/server/manifest.js Parādīt failu

@@ -4,7 +4,7 @@ const Inert = require('@hapi/inert');
4 4
 const Vision = require('@hapi/vision');
5 5
 const Schwifty = require('@hapipal/schwifty');
6 6
 const HapiSwagger = require('hapi-swagger');
7
-const AppPlugin = require('../lib/plugins');
7
+
8 8
 // Pull .env into process.env
9 9
 Dotenv.config({ path: `${__dirname}/.env` });
10 10
 
@@ -34,7 +34,24 @@ module.exports = new Confidence.Store({
34 34
     },
35 35
     register: {
36 36
         plugins: [
37
-            AppPlugin,
37
+            {
38
+                plugin: '../lib', // Main plugin
39
+                routes: {
40
+                    prefix: '/api'
41
+                },
42
+                options: {
43
+                    jwtKey: {
44
+                        $filter: 'NODE_ENV',
45
+                        $default: {
46
+                            $param: 'APP_SECRET',
47
+                            $default: 'app-secret'
48
+                        },
49
+                        production: { // In production do not default to "app-secret"
50
+                            $param: 'APP_SECRET'
51
+                        }
52
+                    }
53
+                }
54
+            },
38 55
             // Documentaion deps
39 56
             Inert,
40 57
             Vision,
@@ -69,4 +86,5 @@ module.exports = new Confidence.Store({
69 86
             }
70 87
         ]
71 88
     }
72
-});
89
+})
90
+

Notiek ielāde…
Atcelt
Saglabāt