aloux-iam 0.0.20 → 0.0.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/auth.js CHANGED
@@ -1,91 +1,91 @@
1
- const jwt = require('jsonwebtoken')
2
- const User = require('./models/User')
3
- const Permission = require('./models/Permission')
4
-
5
- const getAccess = (user, resource) => {
6
- for(let i in user._functions){
7
- for(let j in user._functions[i]._permissions){
8
- if(user._functions[i]._permissions[j].status === 'Activo'){
9
- const permissionBack = user._functions[i]._permissions[j].method + ' ' + user._functions[i]._permissions[j].endpoint
10
- if(permissionBack === resource.method + ' ' + resource.endpoint){
11
- return true
12
- }
13
- }
14
- }
15
- }
16
- return false
17
- }
18
-
19
- const auth = async(req, res, next) => {
20
-
21
- try {
22
-
23
- let token = req.cookies.token || req.header('Authorization')
24
-
25
- if (!token) {
26
- throw {
27
- code: 401,
28
- title: 'Error de autenticación',
29
- detail: 'Endpoint requiere token',
30
- suggestion: 'Vuelve a iniciar sesion',
31
- error: new Error()
32
- }
33
- }
34
-
35
- token = token.replace('Bearer ', '')
36
-
37
- const data = jwt.verify(token, process.env.AUTH_SECRET)
38
- const user = await User.findOne({ _id: data._id, 'tokens.token': token, status: 'Activo' }, {"tokens":0,pwd:0}).populate({ path: "_functions", populate: [{ path: "_permissions"}] }).lean()
39
-
40
- if (!user) {
41
- throw {
42
- code: 401,
43
- title: 'Error de autenticación',
44
- detail: 'No se encontró el usuario',
45
- suggestion: 'Vuelve a iniciar sesion',
46
- error: new Error()
47
- }
48
- }
49
-
50
- const resource = await Permission.findOne({ method: req.originalMethod, endpoint: req.route.path }).lean()
51
- if(!resource){
52
- throw {
53
- code: 403,
54
- title: 'Error de recurso',
55
- detail: 'No se encontro dado de alta el privilegio del endpoint: [' + req.route.path + ']',
56
- suggestion: 'Contacta con el administrador',
57
- error: new Error()
58
- }
59
- }
60
-
61
- if(!resource.default){
62
- const access = getAccess(user, resource)
63
- if (!access) {
64
- throw {
65
- code: 403,
66
- title: 'Error de permisos',
67
- detail: 'No cuentas con permisos para el recurso [' + resource.api +'] que: ' + (resource ? resource.description : 'Recurso indefinido' ),
68
- suggestion: 'Contacta con el administrador',
69
- error: new Error()
70
- }
71
- }
72
- }
73
-
74
- req.user = user
75
- req.token = token
76
- next()
77
- } catch (error) {
78
- let obj = error
79
- if(!error.code){
80
- obj = {
81
- code: 401,
82
- title: 'Error de autenticación',
83
- detail: error.message,
84
- suggestion: 'Vuelve a iniciar sesion'
85
- }
86
- }
87
- res.status(obj.code).send(obj)
88
- }
89
- }
90
-
1
+ const jwt = require('jsonwebtoken')
2
+ const User = require('./models/User')
3
+ const Permission = require('./models/Permission')
4
+
5
+ const getAccess = (user, resource) => {
6
+ for(let i in user._functions){
7
+ for(let j in user._functions[i]._permissions){
8
+ if(user._functions[i]._permissions[j].status === 'Activo'){
9
+ const permissionBack = user._functions[i]._permissions[j].method + ' ' + user._functions[i]._permissions[j].endpoint
10
+ if(permissionBack === resource.method + ' ' + resource.endpoint){
11
+ return true
12
+ }
13
+ }
14
+ }
15
+ }
16
+ return false
17
+ }
18
+
19
+ const auth = async(req, res, next) => {
20
+
21
+ try {
22
+
23
+ let token = req.cookies.token || req.header('Authorization')
24
+
25
+ if (!token) {
26
+ throw {
27
+ code: 401,
28
+ title: 'Error de autenticación',
29
+ detail: 'Endpoint requiere token',
30
+ suggestion: 'Vuelve a iniciar sesion',
31
+ error: new Error()
32
+ }
33
+ }
34
+
35
+ token = token.replace('Bearer ', '')
36
+
37
+ const data = jwt.verify(token, process.env.AUTH_SECRET)
38
+ const user = await User.findOne({ _id: data._id, 'tokens.token': token, status: 'Activo' }, {"tokens":0,pwd:0}).populate({ path: "_functions", populate: [{ path: "_permissions"}] }).lean()
39
+
40
+ if (!user) {
41
+ throw {
42
+ code: 401,
43
+ title: 'Error de autenticación',
44
+ detail: 'No se encontró el usuario',
45
+ suggestion: 'Vuelve a iniciar sesion',
46
+ error: new Error()
47
+ }
48
+ }
49
+
50
+ const resource = await Permission.findOne({ method: req.originalMethod, endpoint: req.route.path }).lean()
51
+ if(!resource){
52
+ throw {
53
+ code: 403,
54
+ title: 'Error de recurso',
55
+ detail: 'No se encontro dado de alta el privilegio del endpoint: [' + req.route.path + ']',
56
+ suggestion: 'Contacta con el administrador',
57
+ error: new Error()
58
+ }
59
+ }
60
+
61
+ if(!resource.default){
62
+ const access = getAccess(user, resource)
63
+ if (!access) {
64
+ throw {
65
+ code: 403,
66
+ title: 'Error de permisos',
67
+ detail: 'No cuentas con permisos para el recurso [' + resource.api +'] que: ' + (resource ? resource.description : 'Recurso indefinido' ),
68
+ suggestion: 'Contacta con el administrador',
69
+ error: new Error()
70
+ }
71
+ }
72
+ }
73
+
74
+ req.user = user
75
+ req.token = token
76
+ next()
77
+ } catch (error) {
78
+ let obj = error
79
+ if(!error.code){
80
+ obj = {
81
+ code: 401,
82
+ title: 'Error de autenticación',
83
+ detail: error.message,
84
+ suggestion: 'Vuelve a iniciar sesion'
85
+ }
86
+ }
87
+ res.status(obj.code).send(obj)
88
+ }
89
+ }
90
+
91
91
  module.exports = auth
@@ -1,14 +1,14 @@
1
- const self = module.exports
2
-
3
- self.responseError = async (res, error) => {
4
- let obj = error
5
- if(!error.code){
6
- obj = {
7
- code: 400,
8
- title: 'Error',
9
- detail: error.message,
10
- suggestion: 'Revisar el detalle'
11
- }
12
- }
13
- res.status(obj.code).send(obj)
1
+ const self = module.exports
2
+
3
+ self.responseError = async (res, error) => {
4
+ let obj = error
5
+ if(!error.code){
6
+ obj = {
7
+ code: 400,
8
+ title: 'Error',
9
+ detail: error.message,
10
+ suggestion: 'Revisar el detalle'
11
+ }
12
+ }
13
+ res.status(obj.code).send(obj)
14
14
  }
@@ -1,148 +1,148 @@
1
- const Auth = require('../services/auth')
2
- const utils = require('../config/utils')
3
-
4
- const self = module.exports
5
-
6
- self.email = async (req, res) => {
7
- try {
8
- const response = await Auth.searchEmail(req.body.email)
9
- res.status(200).send({ email: response })
10
- } catch (error) {
11
- await utils.responseError(res,error)
12
- }
13
- }
14
-
15
- self.login = async (req, res) => {
16
- try {
17
- const response = await Auth.login(req.body, res)
18
- res.status(200).send(response)
19
- } catch (error) {
20
- await utils.responseError(res,error)
21
- }
22
- }
23
-
24
- self.logout = async (req, res) => {
25
- try {
26
- await Auth.logout(req, res)
27
- res.status(200).send()
28
- } catch (error) {
29
- await utils.responseError(res,error)
30
- }
31
- }
32
-
33
- self.logoutAll = async (req, res) => {
34
- try {
35
- await Auth.logoutAll(req.body)
36
- res.status(200).send()
37
- } catch (error) {
38
- await utils.responseError(res,error)
39
- }
40
- }
41
-
42
- self.me = async (req, res) => {
43
- try {
44
- const response = await Auth.me(req, res)
45
- res.status(200).send(response)
46
- } catch (error) {
47
- await utils.responseError(res,error)
48
- }
49
- }
50
-
51
- self.resetPass = async (req, res) => {
52
- try {
53
- const response = await Auth.resetPass(req, res)
54
- res.status(200).send(response)
55
- } catch (error) {
56
- await utils.responseError(res,error)
57
- }
58
- }
59
-
60
- self.updateAny = async (req, res) => {
61
- try {
62
- const response = await Auth.updateAny(req, res)
63
- res.status(200).send(response)
64
- } catch (error) {
65
- await utils.responseError(res,error)
66
- }
67
- }
68
-
69
- self.recoverpassword = async (req, res) => {
70
- try {
71
- await Auth.recoverpassword(req, res)
72
- res.status(200).send()
73
- } catch (error) {
74
- await utils.responseError(res,error)
75
- }
76
- }
77
-
78
- self.verifyCode = async (req, res) => {
79
- try {
80
- await Auth.verifyCode(req, res)
81
- res.status(200).send()
82
- } catch (error) {
83
- await utils.responseError(res,error)
84
- }
85
- }
86
-
87
- self.resetPassword = async (req, res) => {
88
- try {
89
- const response = await Auth.resetPassword(req, res)
90
- res.status(200).send(response)
91
- } catch (error) {
92
- await utils.responseError(res,error)
93
- }
94
- }
95
-
96
- self.sendVerifyMailAccount = async (req, res) => {
97
- try {
98
- await Auth.sendVerifyMailAccountJob(req, true)
99
- res.status(200).send()
100
- }catch(error) {
101
- await utils.responseError(res,error)
102
- }
103
- }
104
-
105
- self.verifyMailTokenAccount = async (req, res) => {
106
- try {
107
- const response = await Auth.verifyMailTokenAccount(req, res)
108
- res.status(200).send(response)
109
- } catch (error) {
110
- await utils.responseError(res,error)
111
- }
112
- }
113
-
114
- self.updatePicture = async (req, res) => {
115
- try {
116
- const response = await Auth.updatePicture(req, res)
117
- res.status(202).send(response)
118
- } catch (error) {
119
- await utils.responseError(res,error)
120
- }
121
- }
122
-
123
- self.verifyPhone = async (req, res) => {
124
- try {
125
- await Auth.verifyPhone(req, res)
126
- res.status(200).send()
127
- } catch (error) {
128
- await utils.responseError(res,error)
129
- }
130
- }
131
-
132
- self.validatePhone = async (req, res) => {
133
- try {
134
- const response = await Auth.validatePhone(req, res)
135
- res.status(200).send(response)
136
- } catch (error) {
137
- await utils.responseError(res,error)
138
- }
139
- }
140
-
141
- self.createCustomer = async (req, res) => {
142
- try {
143
- let token = await Auth.createCustomer(req, res)
144
- res.status(201).send(token)
145
- } catch (error) {
146
- await utils.responseError(res,error)
147
- }
1
+ const Auth = require('../services/auth')
2
+ const utils = require('../config/utils')
3
+
4
+ const self = module.exports
5
+
6
+ self.email = async (req, res) => {
7
+ try {
8
+ const response = await Auth.searchEmail(req.body.email)
9
+ res.status(200).send({ email: response })
10
+ } catch (error) {
11
+ await utils.responseError(res,error)
12
+ }
13
+ }
14
+
15
+ self.login = async (req, res) => {
16
+ try {
17
+ const response = await Auth.login(req.body, res)
18
+ res.status(200).send(response)
19
+ } catch (error) {
20
+ await utils.responseError(res,error)
21
+ }
22
+ }
23
+
24
+ self.logout = async (req, res) => {
25
+ try {
26
+ await Auth.logout(req, res)
27
+ res.status(200).send()
28
+ } catch (error) {
29
+ await utils.responseError(res,error)
30
+ }
31
+ }
32
+
33
+ self.logoutAll = async (req, res) => {
34
+ try {
35
+ await Auth.logoutAll(req.body)
36
+ res.status(200).send()
37
+ } catch (error) {
38
+ await utils.responseError(res,error)
39
+ }
40
+ }
41
+
42
+ self.me = async (req, res) => {
43
+ try {
44
+ const response = await Auth.me(req, res)
45
+ res.status(200).send(response)
46
+ } catch (error) {
47
+ await utils.responseError(res,error)
48
+ }
49
+ }
50
+
51
+ self.resetPass = async (req, res) => {
52
+ try {
53
+ const response = await Auth.resetPass(req, res)
54
+ res.status(200).send(response)
55
+ } catch (error) {
56
+ await utils.responseError(res,error)
57
+ }
58
+ }
59
+
60
+ self.updateAny = async (req, res) => {
61
+ try {
62
+ const response = await Auth.updateAny(req, res)
63
+ res.status(200).send(response)
64
+ } catch (error) {
65
+ await utils.responseError(res,error)
66
+ }
67
+ }
68
+
69
+ self.recoverpassword = async (req, res) => {
70
+ try {
71
+ await Auth.recoverpassword(req, res)
72
+ res.status(200).send()
73
+ } catch (error) {
74
+ await utils.responseError(res,error)
75
+ }
76
+ }
77
+
78
+ self.verifyCode = async (req, res) => {
79
+ try {
80
+ await Auth.verifyCode(req, res)
81
+ res.status(200).send()
82
+ } catch (error) {
83
+ await utils.responseError(res,error)
84
+ }
85
+ }
86
+
87
+ self.resetPassword = async (req, res) => {
88
+ try {
89
+ const response = await Auth.resetPassword(req, res)
90
+ res.status(200).send(response)
91
+ } catch (error) {
92
+ await utils.responseError(res,error)
93
+ }
94
+ }
95
+
96
+ self.sendVerifyMailAccount = async (req, res) => {
97
+ try {
98
+ await Auth.sendVerifyMailAccountJob(req, true)
99
+ res.status(200).send()
100
+ }catch(error) {
101
+ await utils.responseError(res,error)
102
+ }
103
+ }
104
+
105
+ self.verifyMailTokenAccount = async (req, res) => {
106
+ try {
107
+ const response = await Auth.verifyMailTokenAccount(req, res)
108
+ res.status(200).send(response)
109
+ } catch (error) {
110
+ await utils.responseError(res,error)
111
+ }
112
+ }
113
+
114
+ self.updatePicture = async (req, res) => {
115
+ try {
116
+ const response = await Auth.updatePicture(req, res)
117
+ res.status(202).send(response)
118
+ } catch (error) {
119
+ await utils.responseError(res,error)
120
+ }
121
+ }
122
+
123
+ self.verifyPhone = async (req, res) => {
124
+ try {
125
+ await Auth.verifyPhone(req, res)
126
+ res.status(200).send()
127
+ } catch (error) {
128
+ await utils.responseError(res,error)
129
+ }
130
+ }
131
+
132
+ self.validatePhone = async (req, res) => {
133
+ try {
134
+ const response = await Auth.validatePhone(req, res)
135
+ res.status(200).send(response)
136
+ } catch (error) {
137
+ await utils.responseError(res,error)
138
+ }
139
+ }
140
+
141
+ self.createCustomer = async (req, res) => {
142
+ try {
143
+ let token = await Auth.createCustomer(req, res)
144
+ res.status(201).send(token)
145
+ } catch (error) {
146
+ await utils.responseError(res,error)
147
+ }
148
148
  }
@@ -1,87 +1,87 @@
1
- const Functions = require('../models/Functions')
2
- const user = require('./user')
3
- const self = module.exports
4
-
5
- self.create = async (req, res) => {
6
- try {
7
- const functions = new Functions(req.body)
8
- functions.createdAt = (new Date()).getTime()
9
- functions.status = 'Activo'
10
- await functions.save()
11
- res.status(201).send(functions)
12
- } catch (error) {
13
- console.log(error)
14
- res.status(400).send({error:error.message})
15
- }
16
- }
17
-
18
- self.update = async (req, resp) => {
19
- try {
20
- await (new Functions(req.body)).validate()
21
- const count = await Functions.findOne({_id:req.params.FUNCTION_ID}).countDocuments()
22
- if(!count)
23
- throw new Error('Upss! No se encontró el registro')
24
- req.body.lastUpdate = (new Date()).getTime()
25
- const result = await Functions.updateOne({_id:req.params.FUNCTION_ID}, req.body)
26
- resp.status(200).send(req.body)
27
- } catch (error) {
28
- resp.status(400).send({error:error.message})
29
- }
30
- }
31
- self.status = async (req, resp) => {
32
- try {
33
- const _id = req.params.FUNCTION_ID
34
- const functions = await Functions.findOne({ _id })
35
- if(!functions)
36
- throw new Error('Upss! No se encontró el Elemento')
37
- functions.status = req.body.status
38
- functions.lastUpdate = (new Date()).getTime()
39
- const result = await functions.save()
40
- resp.status(200).send(result)
41
- } catch (error) {
42
- resp.status(400).send({error:error.message})
43
- }
44
- }
45
- self.retrieve = async(req, res) => {
46
- try {
47
- const response = await Functions.find({}).sort({ createdAt:-1 })
48
- res.status(200).send(response)
49
- } catch (error) {
50
- res.status(400).send(error)
51
- }
52
- }
53
-
54
- self.get = async(req, res) => {
55
- try {
56
- const _id = req.params.FUNCTION_ID
57
- const functions = await Functions.findOne({ _id }).populate([{ path: "_permissions" },{ path: "_menus" }]).lean()
58
- if(!functions)
59
- res.status(404).send()
60
- res.status(200).send(functions)
61
- } catch (error) {
62
- res.status(400).send(error)
63
- }
64
- }
65
-
66
- self.delete = async(req, res) => {
67
- try {
68
-
69
- const _id = req.params.FUNCTION_ID
70
- const response = await Functions.deleteOne({ _id })
71
- if(!response.deletedCount)
72
- res.status(404).send({ error : "El registro no existe"})
73
- else
74
- res.status(200).send({})
75
- } catch (error) {
76
- res.status(400).send({error:error.message})
77
- }
78
- }
79
-
80
- self.count = async(req, res) => {
81
- try {
82
- let result = await Functions.find({}).countDocuments()
83
- res.status(200).send({ count: result })
84
- } catch (error) {
85
- res.status(400).send({error:error.message})
86
- }
1
+ const Functions = require('../models/Functions')
2
+ const user = require('./user')
3
+ const self = module.exports
4
+
5
+ self.create = async (req, res) => {
6
+ try {
7
+ const functions = new Functions(req.body)
8
+ functions.createdAt = (new Date()).getTime()
9
+ functions.status = 'Activo'
10
+ await functions.save()
11
+ res.status(201).send(functions)
12
+ } catch (error) {
13
+ console.log(error)
14
+ res.status(400).send({error:error.message})
15
+ }
16
+ }
17
+
18
+ self.update = async (req, resp) => {
19
+ try {
20
+ await (new Functions(req.body)).validate()
21
+ const count = await Functions.findOne({_id:req.params.FUNCTION_ID}).countDocuments()
22
+ if(!count)
23
+ throw new Error('Upss! No se encontró el registro')
24
+ req.body.lastUpdate = (new Date()).getTime()
25
+ const result = await Functions.updateOne({_id:req.params.FUNCTION_ID}, req.body)
26
+ resp.status(200).send(req.body)
27
+ } catch (error) {
28
+ resp.status(400).send({error:error.message})
29
+ }
30
+ }
31
+ self.status = async (req, resp) => {
32
+ try {
33
+ const _id = req.params.FUNCTION_ID
34
+ const functions = await Functions.findOne({ _id })
35
+ if(!functions)
36
+ throw new Error('Upss! No se encontró el Elemento')
37
+ functions.status = req.body.status
38
+ functions.lastUpdate = (new Date()).getTime()
39
+ const result = await functions.save()
40
+ resp.status(200).send(result)
41
+ } catch (error) {
42
+ resp.status(400).send({error:error.message})
43
+ }
44
+ }
45
+ self.retrieve = async(req, res) => {
46
+ try {
47
+ const response = await Functions.find({}).sort({ createdAt:-1 })
48
+ res.status(200).send(response)
49
+ } catch (error) {
50
+ res.status(400).send(error)
51
+ }
52
+ }
53
+
54
+ self.get = async(req, res) => {
55
+ try {
56
+ const _id = req.params.FUNCTION_ID
57
+ const functions = await Functions.findOne({ _id }).populate([{ path: "_permissions" },{ path: "_menus" }]).lean()
58
+ if(!functions)
59
+ res.status(404).send()
60
+ res.status(200).send(functions)
61
+ } catch (error) {
62
+ res.status(400).send(error)
63
+ }
64
+ }
65
+
66
+ self.delete = async(req, res) => {
67
+ try {
68
+
69
+ const _id = req.params.FUNCTION_ID
70
+ const response = await Functions.deleteOne({ _id })
71
+ if(!response.deletedCount)
72
+ res.status(404).send({ error : "El registro no existe"})
73
+ else
74
+ res.status(200).send({})
75
+ } catch (error) {
76
+ res.status(400).send({error:error.message})
77
+ }
78
+ }
79
+
80
+ self.count = async(req, res) => {
81
+ try {
82
+ let result = await Functions.find({}).countDocuments()
83
+ res.status(200).send({ count: result })
84
+ } catch (error) {
85
+ res.status(400).send({error:error.message})
86
+ }
87
87
  }