added svcapi ui and camunda code
[it/otf.git] / otf-frontend / server / src / feathers / authentication.js
1 /*  Copyright (c) 2019 AT&T Intellectual Property.                             #\r
2 #                                                                              #\r
3 #   Licensed under the Apache License, Version 2.0 (the "License");            #\r
4 #   you may not use this file except in compliance with the License.           #\r
5 #   You may obtain a copy of the License at                                    #\r
6 #                                                                              #\r
7 #       http://www.apache.org/licenses/LICENSE-2.0                             #\r
8 #                                                                              #\r
9 #   Unless required by applicable law or agreed to in writing, software        #\r
10 #   distributed under the License is distributed on an "AS IS" BASIS,          #\r
11 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #\r
12 #   See the License for the specific language governing permissions and        #\r
13 #   limitations under the License.                                             #\r
14 ##############################################################################*/\r
15 \r
16 \r
17 const authentication = require('@feathersjs/authentication');\r
18 const jwt = require('@feathersjs/authentication-jwt');\r
19 const local = require('@feathersjs/authentication-local');\r
20 const { permissions } = require('./hooks/permissions/permissions');\r
21 // const { hashPassword, protect } = require('@feathersjs/authentication-local').hooks;\r
22 \r
23 module.exports = function (app) {\r
24         const config = app.get('authentication');\r
25 \r
26         // Set up authentication with the secret\r
27         app.configure(authentication(config));\r
28         app.configure(jwt());\r
29         app.configure(local());\r
30 \r
31         // The `authentication` service is used to create a JWT.\r
32         // The before `create` hook registers strategies that can be used\r
33         // to create a new valid JWT (e.g. local or oauth2)\r
34         app.service(config.path).hooks({\r
35                 before: {\r
36                         create: [\r
37                                 function(context){\r
38                                          //console.log(context.data)\r
39                                         // console.log('authing');\r
40                                 },\r
41                                 authentication.hooks.authenticate(config.strategies),\r
42                                 permissions('authentication')\r
43                         ],\r
44                         remove: [\r
45                                 authentication.hooks.authenticate('jwt')\r
46                         ]\r
47                 },\r
48                 after: {\r
49                         create: [\r
50                                 // Send the user profile back with access token\r
51                                 async function (context) {\r
52                                         if (!context.params.user.enabled) {\r
53                                                 context.result.accessToken = null;\r
54                                         }\r
55 \r
56                                         context.result['user'] = context.params.user;\r
57 \r
58                                         //Send Back the users rules\r
59                                         if(context.params.ability){\r
60                                                 context.result.user['rules'] = context.params.ability.rules;\r
61                                         }\r
62 \r
63                                         delete context.result.user.password;\r
64                                         return context;\r
65                                 }\r
66                         ]\r
67                 }\r
68         });\r
69 };\r