@cocreate/authorize 1.6.3 → 1.6.4
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/CHANGELOG.md +7 -0
- package/package.json +1 -1
- package/src/index.js +76 -76
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
## [1.6.4](https://github.com/CoCreate-app/CoCreate-authorize/compare/v1.6.3...v1.6.4) (2023-08-21)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* update permission functions and variable to authorize for improved clarity and readability ([05f6006](https://github.com/CoCreate-app/CoCreate-authorize/commit/05f6006058e5dc49f81395f6860c2586936727ea))
|
|
7
|
+
|
|
1
8
|
## [1.6.3](https://github.com/CoCreate-app/CoCreate-authorize/compare/v1.6.2...v1.6.3) (2023-08-21)
|
|
2
9
|
|
|
3
10
|
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -15,64 +15,64 @@
|
|
|
15
15
|
}
|
|
16
16
|
}(typeof self !== 'undefined' ? self : this, function (isBrowser, crud, { getValueFromObject, dotNotationToObject }) {
|
|
17
17
|
|
|
18
|
-
const
|
|
18
|
+
const authorizations = new Map()
|
|
19
19
|
|
|
20
20
|
if (isBrowser) {
|
|
21
21
|
crud.listen('update.object', function (data) {
|
|
22
|
-
|
|
22
|
+
updateAuthorization(data)
|
|
23
23
|
});
|
|
24
24
|
|
|
25
25
|
crud.listen('delete.object', function (data) {
|
|
26
|
-
|
|
26
|
+
deleteAuthorization(data)
|
|
27
27
|
});
|
|
28
28
|
} else {
|
|
29
29
|
process.on('changed-object', async (data) => {
|
|
30
|
-
|
|
30
|
+
updateAuthorization(data)
|
|
31
31
|
})
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
-
async function
|
|
34
|
+
async function updateAuthorization(data) {
|
|
35
35
|
const { array, object, organization_id } = data
|
|
36
36
|
|
|
37
37
|
if (array === 'keys' && object) {
|
|
38
|
-
let
|
|
39
|
-
if (
|
|
40
|
-
let
|
|
41
|
-
|
|
38
|
+
let authorization = object[0]
|
|
39
|
+
if (authorization && authorization.key && hasAuthorization(authorization.key)) {
|
|
40
|
+
let newAuthorization = await readAuthorization(authorization.key, organization_id)
|
|
41
|
+
setAuthorization(authorization.key, newAuthorization)
|
|
42
42
|
}
|
|
43
43
|
}
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
-
async function
|
|
46
|
+
async function deleteAuthorization(data) {
|
|
47
47
|
const { array, object, organization_id } = data
|
|
48
48
|
|
|
49
49
|
if (array === 'keys' && object) {
|
|
50
|
-
let
|
|
51
|
-
if (
|
|
52
|
-
|
|
50
|
+
let authorization = object[0]
|
|
51
|
+
if (authorization && authorization.key && hasAuthorization(authorization.key)) {
|
|
52
|
+
authorizations.delete(authorization.key)
|
|
53
53
|
}
|
|
54
54
|
}
|
|
55
55
|
}
|
|
56
56
|
|
|
57
|
-
function
|
|
58
|
-
|
|
57
|
+
function setAuthorization(key, authorization) {
|
|
58
|
+
authorizations.set(key, authorization)
|
|
59
59
|
}
|
|
60
60
|
|
|
61
|
-
function
|
|
62
|
-
return
|
|
61
|
+
function hasAuthorization(key) {
|
|
62
|
+
return authorizations.has(key)
|
|
63
63
|
}
|
|
64
64
|
|
|
65
|
-
async function
|
|
66
|
-
if (
|
|
67
|
-
return
|
|
65
|
+
async function getAuthorization(key, organization_id) {
|
|
66
|
+
if (authorizations.get(key)) {
|
|
67
|
+
return authorizations.get(key)
|
|
68
68
|
} else {
|
|
69
|
-
let
|
|
70
|
-
|
|
71
|
-
return
|
|
69
|
+
let authorization = await readAuthorization(key, organization_id);
|
|
70
|
+
authorizations.set(key, authorization)
|
|
71
|
+
return authorization
|
|
72
72
|
}
|
|
73
73
|
}
|
|
74
74
|
|
|
75
|
-
async function
|
|
75
|
+
async function readAuthorization(key, organization_id) {
|
|
76
76
|
try {
|
|
77
77
|
if (!organization_id)
|
|
78
78
|
return null;
|
|
@@ -92,17 +92,17 @@
|
|
|
92
92
|
request.filter.query.push({ key: 'default', value: true, operator: '$eq' })
|
|
93
93
|
|
|
94
94
|
|
|
95
|
-
let
|
|
96
|
-
if (
|
|
97
|
-
|
|
95
|
+
let authorization = await crud.send(request)
|
|
96
|
+
if (authorization && authorization.object && authorization.object[0]) {
|
|
97
|
+
authorization = authorization.object[0]
|
|
98
98
|
|
|
99
|
-
if (!
|
|
100
|
-
|
|
99
|
+
if (!authorization.arrays) {
|
|
100
|
+
authorization.arrays = {};
|
|
101
101
|
}
|
|
102
102
|
|
|
103
|
-
if (
|
|
103
|
+
if (authorization && authorization.roles) {
|
|
104
104
|
const role_ids = []
|
|
105
|
-
|
|
105
|
+
authorization.roles.forEach((_id) => {
|
|
106
106
|
if (_id)
|
|
107
107
|
role_ids.push({ _id })
|
|
108
108
|
})
|
|
@@ -114,91 +114,91 @@
|
|
|
114
114
|
let roles = await crud.send(request)
|
|
115
115
|
roles = roles.object
|
|
116
116
|
|
|
117
|
-
|
|
117
|
+
authorization = createAuthorization(authorization, roles)
|
|
118
118
|
}
|
|
119
119
|
|
|
120
120
|
}
|
|
121
121
|
|
|
122
|
-
return
|
|
122
|
+
return authorization;
|
|
123
123
|
|
|
124
124
|
} catch (error) {
|
|
125
|
-
console.log("
|
|
125
|
+
console.log("authorization Error", error)
|
|
126
126
|
return null;
|
|
127
127
|
}
|
|
128
128
|
|
|
129
129
|
}
|
|
130
130
|
|
|
131
|
-
async function
|
|
131
|
+
async function createAuthorization(authorization, roles) {
|
|
132
132
|
roles.map(role => {
|
|
133
133
|
for (const roleKey in role) {
|
|
134
134
|
if (!["_id", "type", "name", "organization_id"].includes(roleKey)) {
|
|
135
|
-
if (!
|
|
136
|
-
|
|
135
|
+
if (!authorization[roleKey]) {
|
|
136
|
+
authorization[roleKey] = role[roleKey]
|
|
137
137
|
} else {
|
|
138
138
|
if (Array.isArray(role[roleKey])) {
|
|
139
139
|
for (let item of role[roleKey]) {
|
|
140
|
-
if (!
|
|
141
|
-
|
|
140
|
+
if (!authorization[roleKey].includes(item))
|
|
141
|
+
authorization[roleKey].push(item)
|
|
142
142
|
}
|
|
143
143
|
}
|
|
144
144
|
else if (typeof role[roleKey] == 'object') {
|
|
145
145
|
for (const c of Object.keys(role[roleKey])) {
|
|
146
|
-
if (!
|
|
147
|
-
|
|
146
|
+
if (!authorization[roleKey][c]) {
|
|
147
|
+
authorization[roleKey][c] = role[roleKey][c]
|
|
148
148
|
} else {
|
|
149
149
|
if (typeof role[roleKey][c] == 'object') {
|
|
150
|
-
|
|
150
|
+
authorization[roleKey][c] = { ...authorization[roleKey][c], ...role[roleKey][c] }
|
|
151
151
|
} else {
|
|
152
|
-
|
|
152
|
+
authorization[roleKey][c] = role[roleKey][c]
|
|
153
153
|
}
|
|
154
154
|
}
|
|
155
155
|
}
|
|
156
156
|
} else {
|
|
157
|
-
|
|
157
|
+
authorization[roleKey] = role[roleKey]
|
|
158
158
|
}
|
|
159
159
|
}
|
|
160
160
|
}
|
|
161
161
|
}
|
|
162
162
|
})
|
|
163
|
-
return
|
|
163
|
+
return authorization;
|
|
164
164
|
}
|
|
165
165
|
|
|
166
166
|
async function check(data, user_id) {
|
|
167
|
-
let
|
|
167
|
+
let authorization = false
|
|
168
168
|
if (user_id) {
|
|
169
|
-
|
|
169
|
+
authorization = await checkAuthorization({
|
|
170
170
|
key: user_id,
|
|
171
171
|
data
|
|
172
172
|
})
|
|
173
173
|
}
|
|
174
|
-
if (!
|
|
175
|
-
|
|
174
|
+
if (!authorization || authorization.error) {
|
|
175
|
+
authorization = await checkAuthorization({
|
|
176
176
|
key: data.apikey,
|
|
177
177
|
data
|
|
178
178
|
})
|
|
179
179
|
}
|
|
180
|
-
return
|
|
180
|
+
return authorization;
|
|
181
181
|
}
|
|
182
182
|
|
|
183
|
-
async function
|
|
184
|
-
let
|
|
185
|
-
let { organization_id, filter, endPoint } = data
|
|
183
|
+
async function checkAuthorization({ key, data }) {
|
|
184
|
+
// let method = data.method
|
|
185
|
+
let { method, organization_id, filter, endPoint } = data
|
|
186
186
|
if (!key || !organization_id) return false;
|
|
187
187
|
|
|
188
|
-
let
|
|
189
|
-
if (!
|
|
190
|
-
return
|
|
191
|
-
if (
|
|
188
|
+
let autorized = await getAuthorization(key, organization_id)
|
|
189
|
+
if (!autorized || autorized.error)
|
|
190
|
+
return autorized
|
|
191
|
+
if (autorized.organization_id !== organization_id)
|
|
192
192
|
return false;
|
|
193
|
-
if (
|
|
194
|
-
if (!
|
|
193
|
+
if (autorized.host && autorized.host.length) {
|
|
194
|
+
if (!autorized.host || (!autorized.host.includes(data.host) && !autorized.host.includes("*")))
|
|
195
195
|
return false;
|
|
196
196
|
|
|
197
197
|
}
|
|
198
|
-
if (
|
|
198
|
+
if (autorized.admin == 'true' || autorized.admin === true)
|
|
199
199
|
return true;
|
|
200
200
|
|
|
201
|
-
let status = await
|
|
201
|
+
let status = await checkMethod(autorized.actions, method, endPoint, data, filter)
|
|
202
202
|
|
|
203
203
|
if (!status)
|
|
204
204
|
return false
|
|
@@ -206,19 +206,19 @@
|
|
|
206
206
|
return { authorized: data };
|
|
207
207
|
}
|
|
208
208
|
|
|
209
|
-
async function
|
|
210
|
-
if (!
|
|
211
|
-
if (
|
|
209
|
+
async function checkMethod(autorized, method, endPoint, data) {
|
|
210
|
+
if (!autorized || !method || !autorized[method] || autorized[method] == 'false') return false;
|
|
211
|
+
if (autorized[method] === true || autorized[method] == 'true' || autorized[method] == '*') return true;
|
|
212
212
|
|
|
213
|
-
let authorized =
|
|
213
|
+
let authorized = autorized[method].authorize
|
|
214
214
|
if (authorized) {
|
|
215
|
-
let status = await checkAthorized(authorized,
|
|
215
|
+
let status = await checkAthorized(authorized, method, endPoint, data)
|
|
216
216
|
if (!status)
|
|
217
217
|
return false
|
|
218
218
|
else {
|
|
219
|
-
let unauthorized =
|
|
219
|
+
let unauthorized = autorized[method].unauthorize
|
|
220
220
|
if (unauthorized) {
|
|
221
|
-
let status = await checkAthorized(unauthorized,
|
|
221
|
+
let status = await checkAthorized(unauthorized, method, endPoint, data, true)
|
|
222
222
|
if (status)
|
|
223
223
|
return false
|
|
224
224
|
}
|
|
@@ -228,7 +228,7 @@
|
|
|
228
228
|
return false
|
|
229
229
|
}
|
|
230
230
|
|
|
231
|
-
async function checkAthorized(authorized,
|
|
231
|
+
async function checkAthorized(authorized, method, endPoint, data, unauthorize) {
|
|
232
232
|
if (!Array.isArray(authorized))
|
|
233
233
|
authorized = [authorized]
|
|
234
234
|
|
|
@@ -250,7 +250,7 @@
|
|
|
250
250
|
|
|
251
251
|
// if authorized[i] is an object
|
|
252
252
|
for (const key of Object.keys(authorized[i])) {
|
|
253
|
-
status = await checkAthorizedKey(authorized[i],
|
|
253
|
+
status = await checkAthorizedKey(authorized[i], method, endPoint, data, key, unauthorize)
|
|
254
254
|
}
|
|
255
255
|
|
|
256
256
|
}
|
|
@@ -259,7 +259,7 @@
|
|
|
259
259
|
|
|
260
260
|
}
|
|
261
261
|
|
|
262
|
-
async function checkAthorizedKey(authorized,
|
|
262
|
+
async function checkAthorizedKey(authorized, method, endPoint, data, key, unauthorize) {
|
|
263
263
|
let status = false;
|
|
264
264
|
let keyStatus = false;
|
|
265
265
|
|
|
@@ -297,12 +297,12 @@
|
|
|
297
297
|
|
|
298
298
|
// if key status is false for unauthorized case
|
|
299
299
|
if (!keyStatus || keyStatus && unauthorize) {
|
|
300
|
-
if (!data.unauthorized || !data.unauthorized[
|
|
301
|
-
data.unauthorized = { [
|
|
302
|
-
else if (!data.unauthorized[
|
|
303
|
-
data.unauthorized[
|
|
300
|
+
if (!data.unauthorized || !data.unauthorized[method])
|
|
301
|
+
data.unauthorized = { [method]: { [key]: [data[key]] } }
|
|
302
|
+
else if (!data.unauthorized[method][key])
|
|
303
|
+
data.unauthorized[method][key] = [data[key]]
|
|
304
304
|
else
|
|
305
|
-
data.unauthorized[
|
|
305
|
+
data.unauthorized[method][key].push(data[key])
|
|
306
306
|
} else
|
|
307
307
|
status = true
|
|
308
308
|
return status
|