Explorar el Código

:recycle: moving around stuff more for jwt protection

master
TOJ hace 5 años
padre
commit
6c36a88142

+ 22
- 0
backend/lib/auth/strategies/jwt.js Ver fichero

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 Ver fichero

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 Ver fichero

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 Ver fichero

1
 const Schwifty = require('@hapipal/schwifty');
1
 const Schwifty = require('@hapipal/schwifty');
2
-
2
+const Jwt = require('@hapi/jwt');
3
+const JwtStrategy = require('../auth/strategies/jwt')
3
 const UserModel = require('../models/user');
4
 const UserModel = require('../models/user');
4
 const UserCurrentRoute = require('../routes/user/current');
5
 const UserCurrentRoute = require('../routes/user/current');
5
 
6
 
6
 module.exports = {
7
 module.exports = {
7
-    name: 'my-user-plugin',
8
+    name: 'user-plugin',
8
     version: '1.0.0',
9
     version: '1.0.0',
9
-    register: async server => {
10
+    register: async (server, options) => {
11
+        await server.register(Jwt)
10
         await server.register(Schwifty)
12
         await server.register(Schwifty)
11
         await server.registerModel(UserModel)
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
         await server.route(UserCurrentRoute)
20
         await server.route(UserCurrentRoute)
13
     }
21
     }
14
 }
22
 }

+ 1
- 1
backend/lib/routes/user/current.js Ver fichero

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

+ 69
- 0
backend/package-lock.json Ver fichero

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.0.1",
15
         "@hapi/vision": "^6.0.1",
16
         "@hapi/vision": "^6.0.1",
16
         "@hapipal/confidence": "^6.0.1",
17
         "@hapipal/confidence": "^6.0.1",
17
         "@hapipal/schwifty": "^6.0.0",
18
         "@hapipal/schwifty": "^6.0.0",
115
         "@hapi/hoek": "9.x.x"
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
     "node_modules/@hapi/content": {
128
     "node_modules/@hapi/content": {
119
       "version": "5.0.2",
129
       "version": "5.0.2",
120
       "resolved": "https://registry.npmjs.org/@hapi/content/-/content-5.0.2.tgz",
130
       "resolved": "https://registry.npmjs.org/@hapi/content/-/content-5.0.2.tgz",
217
         "@hapi/hoek": "9.x.x"
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
     "node_modules/@hapi/mimos": {
247
     "node_modules/@hapi/mimos": {
221
       "version": "6.0.0",
248
       "version": "6.0.0",
222
       "resolved": "https://registry.npmjs.org/@hapi/mimos/-/mimos-6.0.0.tgz",
249
       "resolved": "https://registry.npmjs.org/@hapi/mimos/-/mimos-6.0.0.tgz",
1179
       "integrity": "sha1-7gHdHKwO08vH/b6jfcCo8c4ALOI=",
1206
       "integrity": "sha1-7gHdHKwO08vH/b6jfcCo8c4ALOI=",
1180
       "dev": true
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
     "node_modules/emoji-regex": {
1217
     "node_modules/emoji-regex": {
1183
       "version": "8.0.0",
1218
       "version": "8.0.0",
1184
       "resolved": "https://registry.npmjs.org/emoji-regex/-/emoji-regex-8.0.0.tgz",
1219
       "resolved": "https://registry.npmjs.org/emoji-regex/-/emoji-regex-8.0.0.tgz",
4088
         "@hapi/hoek": "9.x.x"
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
     "@hapi/content": {
4135
     "@hapi/content": {
4092
       "version": "5.0.2",
4136
       "version": "5.0.2",
4093
       "resolved": "https://registry.npmjs.org/@hapi/content/-/content-5.0.2.tgz",
4137
       "resolved": "https://registry.npmjs.org/@hapi/content/-/content-5.0.2.tgz",
4184
         "@hapi/hoek": "9.x.x"
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
     "@hapi/mimos": {
4248
     "@hapi/mimos": {
4188
       "version": "6.0.0",
4249
       "version": "6.0.0",
4189
       "resolved": "https://registry.npmjs.org/@hapi/mimos/-/mimos-6.0.0.tgz",
4250
       "resolved": "https://registry.npmjs.org/@hapi/mimos/-/mimos-6.0.0.tgz",
4946
       "integrity": "sha1-7gHdHKwO08vH/b6jfcCo8c4ALOI=",
5007
       "integrity": "sha1-7gHdHKwO08vH/b6jfcCo8c4ALOI=",
4947
       "dev": true
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
     "emoji-regex": {
5018
     "emoji-regex": {
4950
       "version": "8.0.0",
5019
       "version": "8.0.0",
4951
       "resolved": "https://registry.npmjs.org/emoji-regex/-/emoji-regex-8.0.0.tgz",
5020
       "resolved": "https://registry.npmjs.org/emoji-regex/-/emoji-regex-8.0.0.tgz",

+ 1
- 0
backend/package.json Ver fichero

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

+ 21
- 3
backend/server/manifest.js Ver fichero

4
 const Vision = require('@hapi/vision');
4
 const Vision = require('@hapi/vision');
5
 const Schwifty = require('@hapipal/schwifty');
5
 const Schwifty = require('@hapipal/schwifty');
6
 const HapiSwagger = require('hapi-swagger');
6
 const HapiSwagger = require('hapi-swagger');
7
-const AppPlugin = require('../lib/plugins');
7
+
8
 // Pull .env into process.env
8
 // Pull .env into process.env
9
 Dotenv.config({ path: `${__dirname}/.env` });
9
 Dotenv.config({ path: `${__dirname}/.env` });
10
 
10
 
34
     },
34
     },
35
     register: {
35
     register: {
36
         plugins: [
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
             // Documentaion deps
55
             // Documentaion deps
39
             Inert,
56
             Inert,
40
             Vision,
57
             Vision,
69
             }
86
             }
70
         ]
87
         ]
71
     }
88
     }
72
-});
89
+})
90
+

Loading…
Cancelar
Guardar