@cocreate/authorize 1.6.3 → 1.6.5
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 +14 -0
- package/LICENSE +656 -547
- package/package.json +2 -2
- package/src/index.js +88 -86
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cocreate/authorize",
|
|
3
|
-
"version": "1.6.
|
|
3
|
+
"version": "1.6.5",
|
|
4
4
|
"description": "A simple authorize component in vanilla javascript. Easily configured using HTML5 data-attributes and/or JavaScript API.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"authorize",
|
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
"url": "git+https://github.com/CoCreate-app/CoCreate-authorize.git"
|
|
35
35
|
},
|
|
36
36
|
"author": "CoCreate LLC",
|
|
37
|
-
"license": "
|
|
37
|
+
"license": "AGPL-3.0",
|
|
38
38
|
"bugs": {
|
|
39
39
|
"url": "https://github.com/CoCreate-app/CoCreate-authorize/issues"
|
|
40
40
|
},
|
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;
|
|
@@ -81,124 +81,126 @@
|
|
|
81
81
|
method: 'read.object',
|
|
82
82
|
array: 'keys',
|
|
83
83
|
organization_id,
|
|
84
|
-
|
|
85
|
-
|
|
84
|
+
object: {
|
|
85
|
+
$filter: {
|
|
86
|
+
query: []
|
|
87
|
+
}
|
|
86
88
|
}
|
|
87
89
|
}
|
|
88
90
|
|
|
89
91
|
if (key)
|
|
90
|
-
request.filter.query.push({ key: 'key', value: key, operator: '$eq' })
|
|
92
|
+
request.object.$filter.query.push({ key: 'key', value: key, operator: '$eq' })
|
|
91
93
|
else
|
|
92
|
-
request.filter.query.push({ key: 'default', value: true, operator: '$eq' })
|
|
94
|
+
request.object.$filter.query.push({ key: 'default', value: true, operator: '$eq' })
|
|
93
95
|
|
|
94
96
|
|
|
95
|
-
let
|
|
96
|
-
if (
|
|
97
|
-
|
|
97
|
+
let authorization = await crud.send(request)
|
|
98
|
+
if (authorization && authorization.object && authorization.object[0]) {
|
|
99
|
+
authorization = authorization.object[0]
|
|
98
100
|
|
|
99
|
-
if (!
|
|
100
|
-
|
|
101
|
+
if (!authorization.arrays) {
|
|
102
|
+
authorization.arrays = {};
|
|
101
103
|
}
|
|
102
104
|
|
|
103
|
-
if (
|
|
105
|
+
if (authorization && authorization.roles) {
|
|
104
106
|
const role_ids = []
|
|
105
|
-
|
|
107
|
+
authorization.roles.forEach((_id) => {
|
|
106
108
|
if (_id)
|
|
107
109
|
role_ids.push({ _id })
|
|
108
110
|
})
|
|
109
111
|
|
|
110
|
-
delete request.filter
|
|
112
|
+
delete request.object.$filter
|
|
111
113
|
delete request.request
|
|
112
114
|
request.object = role_ids
|
|
113
115
|
|
|
114
116
|
let roles = await crud.send(request)
|
|
115
117
|
roles = roles.object
|
|
116
118
|
|
|
117
|
-
|
|
119
|
+
authorization = createAuthorization(authorization, roles)
|
|
118
120
|
}
|
|
119
121
|
|
|
120
122
|
}
|
|
121
123
|
|
|
122
|
-
return
|
|
124
|
+
return authorization;
|
|
123
125
|
|
|
124
126
|
} catch (error) {
|
|
125
|
-
console.log("
|
|
127
|
+
console.log("authorization Error", error)
|
|
126
128
|
return null;
|
|
127
129
|
}
|
|
128
130
|
|
|
129
131
|
}
|
|
130
132
|
|
|
131
|
-
async function
|
|
133
|
+
async function createAuthorization(authorization, roles) {
|
|
132
134
|
roles.map(role => {
|
|
133
135
|
for (const roleKey in role) {
|
|
134
136
|
if (!["_id", "type", "name", "organization_id"].includes(roleKey)) {
|
|
135
|
-
if (!
|
|
136
|
-
|
|
137
|
+
if (!authorization[roleKey]) {
|
|
138
|
+
authorization[roleKey] = role[roleKey]
|
|
137
139
|
} else {
|
|
138
140
|
if (Array.isArray(role[roleKey])) {
|
|
139
141
|
for (let item of role[roleKey]) {
|
|
140
|
-
if (!
|
|
141
|
-
|
|
142
|
+
if (!authorization[roleKey].includes(item))
|
|
143
|
+
authorization[roleKey].push(item)
|
|
142
144
|
}
|
|
143
145
|
}
|
|
144
146
|
else if (typeof role[roleKey] == 'object') {
|
|
145
147
|
for (const c of Object.keys(role[roleKey])) {
|
|
146
|
-
if (!
|
|
147
|
-
|
|
148
|
+
if (!authorization[roleKey][c]) {
|
|
149
|
+
authorization[roleKey][c] = role[roleKey][c]
|
|
148
150
|
} else {
|
|
149
151
|
if (typeof role[roleKey][c] == 'object') {
|
|
150
|
-
|
|
152
|
+
authorization[roleKey][c] = { ...authorization[roleKey][c], ...role[roleKey][c] }
|
|
151
153
|
} else {
|
|
152
|
-
|
|
154
|
+
authorization[roleKey][c] = role[roleKey][c]
|
|
153
155
|
}
|
|
154
156
|
}
|
|
155
157
|
}
|
|
156
158
|
} else {
|
|
157
|
-
|
|
159
|
+
authorization[roleKey] = role[roleKey]
|
|
158
160
|
}
|
|
159
161
|
}
|
|
160
162
|
}
|
|
161
163
|
}
|
|
162
164
|
})
|
|
163
|
-
return
|
|
165
|
+
return authorization;
|
|
164
166
|
}
|
|
165
167
|
|
|
166
168
|
async function check(data, user_id) {
|
|
167
|
-
let
|
|
169
|
+
let authorization = false
|
|
168
170
|
if (user_id) {
|
|
169
|
-
|
|
171
|
+
authorization = await checkAuthorization({
|
|
170
172
|
key: user_id,
|
|
171
173
|
data
|
|
172
174
|
})
|
|
173
175
|
}
|
|
174
|
-
if (!
|
|
175
|
-
|
|
176
|
+
if (!authorization || authorization.error) {
|
|
177
|
+
authorization = await checkAuthorization({
|
|
176
178
|
key: data.apikey,
|
|
177
179
|
data
|
|
178
180
|
})
|
|
179
181
|
}
|
|
180
|
-
return
|
|
182
|
+
return authorization;
|
|
181
183
|
}
|
|
182
184
|
|
|
183
|
-
async function
|
|
184
|
-
let
|
|
185
|
-
let {
|
|
185
|
+
async function checkAuthorization({ key, data }) {
|
|
186
|
+
// let method = data.method
|
|
187
|
+
let { method, organization_id, endPoint } = data
|
|
186
188
|
if (!key || !organization_id) return false;
|
|
187
189
|
|
|
188
|
-
let
|
|
189
|
-
if (!
|
|
190
|
-
return
|
|
191
|
-
if (
|
|
190
|
+
let autorized = await getAuthorization(key, organization_id)
|
|
191
|
+
if (!autorized || autorized.error)
|
|
192
|
+
return autorized
|
|
193
|
+
if (autorized.organization_id !== organization_id)
|
|
192
194
|
return false;
|
|
193
|
-
if (
|
|
194
|
-
if (!
|
|
195
|
+
if (autorized.host && autorized.host.length) {
|
|
196
|
+
if (!autorized.host || (!autorized.host.includes(data.host) && !autorized.host.includes("*")))
|
|
195
197
|
return false;
|
|
196
198
|
|
|
197
199
|
}
|
|
198
|
-
if (
|
|
200
|
+
if (autorized.admin == 'true' || autorized.admin === true)
|
|
199
201
|
return true;
|
|
200
202
|
|
|
201
|
-
let status = await
|
|
203
|
+
let status = await checkMethod(autorized.actions, method, endPoint, data)
|
|
202
204
|
|
|
203
205
|
if (!status)
|
|
204
206
|
return false
|
|
@@ -206,19 +208,19 @@
|
|
|
206
208
|
return { authorized: data };
|
|
207
209
|
}
|
|
208
210
|
|
|
209
|
-
async function
|
|
210
|
-
if (!
|
|
211
|
-
if (
|
|
211
|
+
async function checkMethod(autorized, method, endPoint, data) {
|
|
212
|
+
if (!autorized || !method || !autorized[method] || autorized[method] == 'false') return false;
|
|
213
|
+
if (autorized[method] === true || autorized[method] == 'true' || autorized[method] == '*') return true;
|
|
212
214
|
|
|
213
|
-
let authorized =
|
|
215
|
+
let authorized = autorized[method].authorize
|
|
214
216
|
if (authorized) {
|
|
215
|
-
let status = await checkAthorized(authorized,
|
|
217
|
+
let status = await checkAthorized(authorized, method, endPoint, data)
|
|
216
218
|
if (!status)
|
|
217
219
|
return false
|
|
218
220
|
else {
|
|
219
|
-
let unauthorized =
|
|
221
|
+
let unauthorized = autorized[method].unauthorize
|
|
220
222
|
if (unauthorized) {
|
|
221
|
-
let status = await checkAthorized(unauthorized,
|
|
223
|
+
let status = await checkAthorized(unauthorized, method, endPoint, data, true)
|
|
222
224
|
if (status)
|
|
223
225
|
return false
|
|
224
226
|
}
|
|
@@ -228,7 +230,7 @@
|
|
|
228
230
|
return false
|
|
229
231
|
}
|
|
230
232
|
|
|
231
|
-
async function checkAthorized(authorized,
|
|
233
|
+
async function checkAthorized(authorized, method, endPoint, data, unauthorize) {
|
|
232
234
|
if (!Array.isArray(authorized))
|
|
233
235
|
authorized = [authorized]
|
|
234
236
|
|
|
@@ -250,7 +252,7 @@
|
|
|
250
252
|
|
|
251
253
|
// if authorized[i] is an object
|
|
252
254
|
for (const key of Object.keys(authorized[i])) {
|
|
253
|
-
status = await checkAthorizedKey(authorized[i],
|
|
255
|
+
status = await checkAthorizedKey(authorized[i], method, endPoint, data, key, unauthorize)
|
|
254
256
|
}
|
|
255
257
|
|
|
256
258
|
}
|
|
@@ -259,7 +261,7 @@
|
|
|
259
261
|
|
|
260
262
|
}
|
|
261
263
|
|
|
262
|
-
async function checkAthorizedKey(authorized,
|
|
264
|
+
async function checkAthorizedKey(authorized, method, endPoint, data, key, unauthorize) {
|
|
263
265
|
let status = false;
|
|
264
266
|
let keyStatus = false;
|
|
265
267
|
|
|
@@ -297,12 +299,12 @@
|
|
|
297
299
|
|
|
298
300
|
// if key status is false for unauthorized case
|
|
299
301
|
if (!keyStatus || keyStatus && unauthorize) {
|
|
300
|
-
if (!data.unauthorized || !data.unauthorized[
|
|
301
|
-
data.unauthorized = { [
|
|
302
|
-
else if (!data.unauthorized[
|
|
303
|
-
data.unauthorized[
|
|
302
|
+
if (!data.unauthorized || !data.unauthorized[method])
|
|
303
|
+
data.unauthorized = { [method]: { [key]: [data[key]] } }
|
|
304
|
+
else if (!data.unauthorized[method][key])
|
|
305
|
+
data.unauthorized[method][key] = [data[key]]
|
|
304
306
|
else
|
|
305
|
-
data.unauthorized[
|
|
307
|
+
data.unauthorized[method][key].push(data[key])
|
|
306
308
|
} else
|
|
307
309
|
status = true
|
|
308
310
|
return status
|
|
@@ -348,20 +350,20 @@
|
|
|
348
350
|
}
|
|
349
351
|
|
|
350
352
|
async function checkFilter(authorized, data, apikey, unauthorize) {
|
|
351
|
-
if (data.filter && data.filter.query) {
|
|
353
|
+
if (data.object.$filter && data.object.$filter.query) {
|
|
352
354
|
let key
|
|
353
|
-
if (data.filter.type == 'object')
|
|
355
|
+
if (data.object.$filter.type == 'object')
|
|
354
356
|
key = '_id'
|
|
355
|
-
else if (data.filter.type == 'array')
|
|
357
|
+
else if (data.object.$filter.type == 'array')
|
|
356
358
|
key = 'name'
|
|
357
359
|
if (key) {
|
|
358
360
|
for (let value of authorized[apikey]) {
|
|
359
361
|
if (value[key])
|
|
360
362
|
value = value[key]
|
|
361
363
|
if (unauthorize)
|
|
362
|
-
data.filter.query.push({ key, value, operator: '$ne', logicalOperator: 'or' })
|
|
364
|
+
data.object.$filter.query.push({ key, value, operator: '$ne', logicalOperator: 'or' })
|
|
363
365
|
else
|
|
364
|
-
data.filter.query.push({ key, value, operator: '$eq', logicalOperator: 'or' })
|
|
366
|
+
data.object.$filter.query.push({ key, value, operator: '$eq', logicalOperator: 'or' })
|
|
365
367
|
}
|
|
366
368
|
if (!unauthorize)
|
|
367
369
|
return true
|