@cocreate/socket-server 1.8.2 → 1.9.0

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.9.0](https://github.com/CoCreate-app/CoCreate-socket-server/compare/v1.8.2...v1.9.0) (2023-05-24)
2
+
3
+
4
+ ### Features
5
+
6
+ * Refactored SocketServer permission checking and added a new function getHost. ([c6a51d1](https://github.com/CoCreate-app/CoCreate-socket-server/commit/c6a51d143fda143aca3d059958bc668f9fac6d04))
7
+
1
8
  ## [1.8.2](https://github.com/CoCreate-app/CoCreate-socket-server/compare/v1.8.1...v1.8.2) (2023-05-21)
2
9
 
3
10
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/socket-server",
3
- "version": "1.8.2",
3
+ "version": "1.9.0",
4
4
  "description": "CoCreate-socket-server",
5
5
  "keywords": [
6
6
  "cocreate-socket",
package/src/index.js CHANGED
@@ -129,7 +129,7 @@ class SocketServer extends EventEmitter {
129
129
  async onMessage(req, socket, message) {
130
130
  try {
131
131
  const organization_id = socket.config.organization_id
132
- const { action, data } = JSON.parse(message)
132
+ let { action, data } = JSON.parse(message)
133
133
 
134
134
  if (action) {
135
135
  this.emit("setBandwidth", {
@@ -142,25 +142,31 @@ class SocketServer extends EventEmitter {
142
142
  user_id = await this.authInstance.getUserId(req);
143
143
 
144
144
  if (this.permissionInstance) {
145
- const permission = await this.permissionInstance.check(action, data, req, user_id)
146
- if (!permission || permission.error) {
147
- if (permission.dbUrl === false) {
148
- data.database = process.env.organization_id
149
- data.organization_id = process.env.organization_id
150
-
151
- const permission2 = await this.permissionInstance.check(action, data, req, user_id)
152
- if (!permission2 || permission2.error) {
153
- return this.send(socket, 'Access Denied', { action, permission2, ...data })
154
- }
155
- } else if (!user_id) {
156
- return this.send(socket, 'Access Denied', { action, permission, ...data })
145
+ data.host = this.getHost(req)
146
+ const permission = await this.permissionInstance.check(action, data, user_id)
147
+ if (permission.dbUrl === false) {
148
+ data.database = process.env.organization_id
149
+ data.organization_id = process.env.organization_id
150
+
151
+ const permission2 = await this.permissionInstance.check(action, data, req, user_id)
152
+ if (!permission2 || permission2.error) {
153
+ return this.send(socket, 'Access Denied', { action, permission2, ...data })
157
154
  }
158
- // dburl is true and db does not have 'keys' collection
159
- // action: syncCollection data{collection: 'keys', document[]}
160
- // actions: add keys as once keys are added admin user can do anything
161
-
155
+ } else if (!permission || permission.error) {
156
+ if (!user_id)
157
+ this.send(socket, 'updateUserStatus', { userStatus: 'off', clientId: data.clientId, organization_id })
162
158
 
159
+ return this.send(socket, 'Access Denied', { action, permission, ...data })
163
160
  }
161
+ // dburl is true and db does not have 'keys' collection
162
+ // action: syncCollection data{collection: 'keys', document[]}
163
+ // actions: add keys as once keys are added admin user can do anything
164
+
165
+
166
+ // }
167
+
168
+ if (permission.authorized)
169
+ data = permission.authorized
164
170
 
165
171
  if (user_id) {
166
172
  if (!socket.config.user_id) {
@@ -191,13 +197,8 @@ class SocketServer extends EventEmitter {
191
197
  }
192
198
 
193
199
  broadcast(socket, action, data) {
194
- const self = this;
195
200
  if (!data.uid)
196
201
  data.uid = uid.generate()
197
- const responseData = JSON.stringify({
198
- action,
199
- data
200
- });
201
202
 
202
203
  const asyncId = socket.config.asyncId
203
204
  let isAsync = false;
@@ -214,6 +215,7 @@ class SocketServer extends EventEmitter {
214
215
  url += `/${namespace}`
215
216
  }
216
217
 
218
+
217
219
  let rooms = data.room;
218
220
  if (rooms) {
219
221
  if (!rooms.isArray())
@@ -222,76 +224,63 @@ class SocketServer extends EventEmitter {
222
224
  let url = url;
223
225
  url += `/${room}`;
224
226
 
225
- const clients = this.clients.get(url);
226
- if (clients) {
227
- clients.forEach((client) => {
228
- if (socket != client && data.broadcast != false || socket == client && data.broadcastSender != false) {
229
- if (isAsync) {
230
- asyncData.push({ socket: client, message: responseData })
231
- } else {
232
- // TODO: check permission and remove any items client does not have a permission
233
- client.send(responseData);
234
- }
235
- this.emit("setBandwidth", {
236
- type: 'out',
237
- data: responseData,
238
- organization_id
239
- })
240
- }
241
- })
242
- }
227
+ this.send(socket, action, data, url, isAsync, asyncData)
243
228
  }
244
-
245
229
  } else {
246
- this.clients.forEach((value, key) => {
247
- if (key.includes(url)) {
248
- value.forEach(client => {
249
- if (socket != client && data.broadcast != false || socket == client && data.broadcastSender != false) {
250
- if (isAsync) {
251
- asyncData.push({ socket: client, message: responseData })
252
- } else {
253
- // TODO: check permission and remove any items client does not have a permission
254
- client.send(responseData);
255
- }
256
- this.emit("setBandwidth", {
257
- type: 'out',
258
- data: responseData,
259
- organization_id
260
- })
261
-
262
- }
263
- })
264
- }
265
- })
230
+ this.send(socket, action, data, url, isAsync, asyncData)
266
231
  }
267
232
 
268
- //. set async processing
269
233
  if (isAsync) {
270
234
  this.asyncMessages.get(socket.config.key).setMessage(asyncId, asyncData)
271
235
  }
272
236
 
273
237
  }
274
238
 
275
- send(socket, action, data) {
239
+ async send(socket, action, data, url, isAsync, asyncData) {
276
240
  const asyncId = socket.config.asyncId
277
- let responseData = JSON.stringify({
278
- action,
279
- data
280
- });
281
241
 
282
- if (asyncId && socket.config && socket.config.key) {
242
+ if (!url && asyncId && socket.config && socket.config.key) {
243
+ let responseData = JSON.stringify({
244
+ action,
245
+ data
246
+ });
283
247
  this.asyncMessages.get(socket.config.key).setMessage(asyncId, [{ socket, message: responseData }]);
284
248
  } else {
285
- // TODO: check permission and remove any items client does not have a permission
286
- socket.send(responseData);
249
+ let clients
250
+ if (!url)
251
+ clients = [socket]
252
+ else
253
+ clients = this.clients.get(url);
254
+
255
+ if (clients) {
256
+ for (let client of clients) {
257
+ if (socket != client && data.broadcast != false || socket == client && data.broadcastSender != false) {
258
+ if (isAsync) {
259
+ asyncData.push({ socket, message: JSON.stringify({ action, data }) })
260
+ } else {
261
+ const permission = await this.permissionInstance.check(action, data, socket.config.user_id)
262
+ if (permission && permission.authorized)
263
+ data = permission.authorized
264
+
265
+ let responseData = JSON.stringify({
266
+ action,
267
+ data
268
+ });
269
+
270
+ socket.send(responseData);
271
+
272
+ if (socket.config && socket.config.organization_id)
273
+ this.emit("setBandwidth", {
274
+ type: 'out',
275
+ data: responseData,
276
+ organization_id: socket.config.organization_id
277
+ })
278
+ }
279
+ }
280
+ }
281
+ }
287
282
  }
288
283
 
289
- if (socket.config && socket.config.organization_id)
290
- this.emit("setBandwidth", {
291
- type: 'out',
292
- data: responseData,
293
- organization_id: socket.config.organization_id
294
- })
295
284
  }
296
285
 
297
286
  // addAsyncMessage(key) {
@@ -313,6 +302,14 @@ class SocketServer extends EventEmitter {
313
302
  }
314
303
  return params
315
304
  }
305
+
306
+ getHost(req) {
307
+ const headers = req['headers']
308
+ let origin = headers['origin'];
309
+ if (origin && origin !== 'null')
310
+ return url.parse(origin).hostname;
311
+ }
312
+
316
313
  }
317
314
 
318
315