@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 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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/authorize",
3
- "version": "1.6.3",
3
+ "version": "1.6.4",
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",
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 permissions = new Map()
18
+ const authorizations = new Map()
19
19
 
20
20
  if (isBrowser) {
21
21
  crud.listen('update.object', function (data) {
22
- updatePermission(data)
22
+ updateAuthorization(data)
23
23
  });
24
24
 
25
25
  crud.listen('delete.object', function (data) {
26
- deletePermission(data)
26
+ deleteAuthorization(data)
27
27
  });
28
28
  } else {
29
29
  process.on('changed-object', async (data) => {
30
- updatePermission(data)
30
+ updateAuthorization(data)
31
31
  })
32
32
  }
33
33
 
34
- async function updatePermission(data) {
34
+ async function updateAuthorization(data) {
35
35
  const { array, object, organization_id } = data
36
36
 
37
37
  if (array === 'keys' && object) {
38
- let permission = object[0]
39
- if (permission && permission.key && hasPermission(permission.key)) {
40
- let newPermission = await readPermisson(permission.key, organization_id)
41
- setPermission(permission.key, newPermission)
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 deletePermission(data) {
46
+ async function deleteAuthorization(data) {
47
47
  const { array, object, organization_id } = data
48
48
 
49
49
  if (array === 'keys' && object) {
50
- let permission = object[0]
51
- if (permission && permission.key && hasPermission(permission.key)) {
52
- permissions.delete(permission.key)
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 setPermission(key, permission) {
58
- permissions.set(key, permission)
57
+ function setAuthorization(key, authorization) {
58
+ authorizations.set(key, authorization)
59
59
  }
60
60
 
61
- function hasPermission(key) {
62
- return permissions.has(key)
61
+ function hasAuthorization(key) {
62
+ return authorizations.has(key)
63
63
  }
64
64
 
65
- async function getPermission(key, organization_id) {
66
- if (permissions.get(key)) {
67
- return permissions.get(key)
65
+ async function getAuthorization(key, organization_id) {
66
+ if (authorizations.get(key)) {
67
+ return authorizations.get(key)
68
68
  } else {
69
- let permission = await readPermisson(key, organization_id);
70
- permissions.set(key, permission)
71
- return permission
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 readPermisson(key, organization_id) {
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 permission = await crud.send(request)
96
- if (permission && permission.object && permission.object[0]) {
97
- permission = permission.object[0]
95
+ let authorization = await crud.send(request)
96
+ if (authorization && authorization.object && authorization.object[0]) {
97
+ authorization = authorization.object[0]
98
98
 
99
- if (!permission.arrays) {
100
- permission.arrays = {};
99
+ if (!authorization.arrays) {
100
+ authorization.arrays = {};
101
101
  }
102
102
 
103
- if (permission && permission.roles) {
103
+ if (authorization && authorization.roles) {
104
104
  const role_ids = []
105
- permission.roles.forEach((_id) => {
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
- permission = createPermissionObject(permission, roles)
117
+ authorization = createAuthorization(authorization, roles)
118
118
  }
119
119
 
120
120
  }
121
121
 
122
- return permission;
122
+ return authorization;
123
123
 
124
124
  } catch (error) {
125
- console.log("Permission Error", error)
125
+ console.log("authorization Error", error)
126
126
  return null;
127
127
  }
128
128
 
129
129
  }
130
130
 
131
- async function createPermissionObject(permission, roles) {
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 (!permission[roleKey]) {
136
- permission[roleKey] = role[roleKey]
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 (!permission[roleKey].includes(item))
141
- permission[roleKey].push(item)
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 (!permission[roleKey][c]) {
147
- permission[roleKey][c] = role[roleKey][c]
146
+ if (!authorization[roleKey][c]) {
147
+ authorization[roleKey][c] = role[roleKey][c]
148
148
  } else {
149
149
  if (typeof role[roleKey][c] == 'object') {
150
- permission[roleKey][c] = { ...permission[roleKey][c], ...role[roleKey][c] }
150
+ authorization[roleKey][c] = { ...authorization[roleKey][c], ...role[roleKey][c] }
151
151
  } else {
152
- permission[roleKey][c] = role[roleKey][c]
152
+ authorization[roleKey][c] = role[roleKey][c]
153
153
  }
154
154
  }
155
155
  }
156
156
  } else {
157
- permission[roleKey] = role[roleKey]
157
+ authorization[roleKey] = role[roleKey]
158
158
  }
159
159
  }
160
160
  }
161
161
  }
162
162
  })
163
- return permission;
163
+ return authorization;
164
164
  }
165
165
 
166
166
  async function check(data, user_id) {
167
- let permission = false
167
+ let authorization = false
168
168
  if (user_id) {
169
- permission = await checkPermissionObject({
169
+ authorization = await checkAuthorization({
170
170
  key: user_id,
171
171
  data
172
172
  })
173
173
  }
174
- if (!permission || permission.error) {
175
- permission = await checkPermissionObject({
174
+ if (!authorization || authorization.error) {
175
+ authorization = await checkAuthorization({
176
176
  key: data.apikey,
177
177
  data
178
178
  })
179
179
  }
180
- return permission;
180
+ return authorization;
181
181
  }
182
182
 
183
- async function checkPermissionObject({ key, data }) {
184
- let action = data.method
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 permission = await getPermission(key, organization_id)
189
- if (!permission || permission.error)
190
- return permission
191
- if (permission.organization_id !== organization_id)
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 (permission.host && permission.host.length) {
194
- if (!permission.host || (!permission.host.includes(data.host) && !permission.host.includes("*")))
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 (permission.admin == 'true' || permission.admin === true)
198
+ if (autorized.admin == 'true' || autorized.admin === true)
199
199
  return true;
200
200
 
201
- let status = await checkAction(permission.actions, action, endPoint, data, filter)
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 checkAction(permissions, action, endPoint, data) {
210
- if (!permissions || !action || !permissions[action] || permissions[action] == 'false') return false;
211
- if (permissions[action] === true || permissions[action] == 'true' || permissions[action] == '*') return true;
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 = permissions[action].authorize
213
+ let authorized = autorized[method].authorize
214
214
  if (authorized) {
215
- let status = await checkAthorized(authorized, action, endPoint, data)
215
+ let status = await checkAthorized(authorized, method, endPoint, data)
216
216
  if (!status)
217
217
  return false
218
218
  else {
219
- let unauthorized = permissions[action].unauthorize
219
+ let unauthorized = autorized[method].unauthorize
220
220
  if (unauthorized) {
221
- let status = await checkAthorized(unauthorized, action, endPoint, data, true)
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, action, endPoint, data, unauthorize) {
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], action, endPoint, data, key, unauthorize)
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, action, endPoint, data, key, unauthorize) {
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[action])
301
- data.unauthorized = { [action]: { [key]: [data[key]] } }
302
- else if (!data.unauthorized[action][key])
303
- data.unauthorized[action][key] = [data[key]]
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[action][key].push(data[key])
305
+ data.unauthorized[method][key].push(data[key])
306
306
  } else
307
307
  status = true
308
308
  return status