@cocreate/socket-server 1.8.2 → 1.10.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,17 @@
1
+ # [1.10.0](https://github.com/CoCreate-app/CoCreate-socket-server/compare/v1.9.0...v1.10.0) (2023-05-25)
2
+
3
+
4
+ ### Features
5
+
6
+ * Simplify WebSocketServer Class, Replace Permission and Auth Instance with Single functions. ([4ec6eb3](https://github.com/CoCreate-app/CoCreate-socket-server/commit/4ec6eb35e034a119c3f4e24a054a7708f04e8ac4))
7
+
8
+ # [1.9.0](https://github.com/CoCreate-app/CoCreate-socket-server/compare/v1.8.2...v1.9.0) (2023-05-24)
9
+
10
+
11
+ ### Features
12
+
13
+ * Refactored SocketServer permission checking and added a new function getHost. ([c6a51d1](https://github.com/CoCreate-app/CoCreate-socket-server/commit/c6a51d143fda143aca3d059958bc668f9fac6d04))
14
+
1
15
  ## [1.8.2](https://github.com/CoCreate-app/CoCreate-socket-server/compare/v1.8.1...v1.8.2) (2023-05-21)
2
16
 
3
17
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/socket-server",
3
- "version": "1.8.2",
3
+ "version": "1.10.0",
4
4
  "description": "CoCreate-socket-server",
5
5
  "keywords": [
6
6
  "cocreate-socket",
package/src/index.js CHANGED
@@ -15,16 +15,6 @@ class SocketServer extends EventEmitter {
15
15
 
16
16
  //. websocket server
17
17
  this.wss = new WebSocket.Server({ noServer: true });
18
- this.permissionInstance = null;
19
- this.authInstance = null;
20
- }
21
-
22
- setPermission(instance) {
23
- this.permissionInstance = instance;
24
- }
25
-
26
- setAuth(instance) {
27
- this.authInstance = instance
28
18
  }
29
19
 
30
20
  handleUpgrade(req, socket, head) {
@@ -129,7 +119,7 @@ class SocketServer extends EventEmitter {
129
119
  async onMessage(req, socket, message) {
130
120
  try {
131
121
  const organization_id = socket.config.organization_id
132
- const { action, data } = JSON.parse(message)
122
+ let { action, data } = JSON.parse(message)
133
123
 
134
124
  if (action) {
135
125
  this.emit("setBandwidth", {
@@ -138,29 +128,35 @@ class SocketServer extends EventEmitter {
138
128
  organization_id
139
129
  });
140
130
  let user_id = null;
141
- if (this.authInstance)
142
- user_id = await this.authInstance.getUserId(req);
143
-
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 })
131
+ if (this.authenticate)
132
+ user_id = await this.authenticate.getUserId(req);
133
+
134
+ if (this.authorize) {
135
+ data.host = this.getHost(req)
136
+ const permission = await this.authorize.check(action, data, user_id)
137
+ if (permission.dbUrl === false) {
138
+ data.database = process.env.organization_id
139
+ data.organization_id = process.env.organization_id
140
+
141
+ const permission2 = await this.authorize.check(action, data, req, user_id)
142
+ if (!permission2 || permission2.error) {
143
+ return this.send(socket, 'Access Denied', { action, permission2, ...data })
157
144
  }
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
-
145
+ } else if (!permission || permission.error) {
146
+ if (!user_id)
147
+ this.send(socket, 'updateUserStatus', { userStatus: 'off', clientId: data.clientId, organization_id })
162
148
 
149
+ return this.send(socket, 'Access Denied', { action, permission, ...data })
163
150
  }
151
+ // dburl is true and db does not have 'keys' collection
152
+ // action: syncCollection data{collection: 'keys', document[]}
153
+ // actions: add keys as once keys are added admin user can do anything
154
+
155
+
156
+ // }
157
+
158
+ if (permission.authorized)
159
+ data = permission.authorized
164
160
 
165
161
  if (user_id) {
166
162
  if (!socket.config.user_id) {
@@ -191,13 +187,8 @@ class SocketServer extends EventEmitter {
191
187
  }
192
188
 
193
189
  broadcast(socket, action, data) {
194
- const self = this;
195
190
  if (!data.uid)
196
191
  data.uid = uid.generate()
197
- const responseData = JSON.stringify({
198
- action,
199
- data
200
- });
201
192
 
202
193
  const asyncId = socket.config.asyncId
203
194
  let isAsync = false;
@@ -214,6 +205,7 @@ class SocketServer extends EventEmitter {
214
205
  url += `/${namespace}`
215
206
  }
216
207
 
208
+
217
209
  let rooms = data.room;
218
210
  if (rooms) {
219
211
  if (!rooms.isArray())
@@ -222,76 +214,63 @@ class SocketServer extends EventEmitter {
222
214
  let url = url;
223
215
  url += `/${room}`;
224
216
 
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
- }
217
+ this.send(socket, action, data, url, isAsync, asyncData)
243
218
  }
244
-
245
219
  } 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
- })
220
+ this.send(socket, action, data, url, isAsync, asyncData)
266
221
  }
267
222
 
268
- //. set async processing
269
223
  if (isAsync) {
270
224
  this.asyncMessages.get(socket.config.key).setMessage(asyncId, asyncData)
271
225
  }
272
226
 
273
227
  }
274
228
 
275
- send(socket, action, data) {
229
+ async send(socket, action, data, url, isAsync, asyncData) {
276
230
  const asyncId = socket.config.asyncId
277
- let responseData = JSON.stringify({
278
- action,
279
- data
280
- });
281
231
 
282
- if (asyncId && socket.config && socket.config.key) {
232
+ if (!url && asyncId && socket.config && socket.config.key) {
233
+ let responseData = JSON.stringify({
234
+ action,
235
+ data
236
+ });
283
237
  this.asyncMessages.get(socket.config.key).setMessage(asyncId, [{ socket, message: responseData }]);
284
238
  } else {
285
- // TODO: check permission and remove any items client does not have a permission
286
- socket.send(responseData);
239
+ let clients
240
+ if (!url)
241
+ clients = [socket]
242
+ else
243
+ clients = this.clients.get(url);
244
+
245
+ if (clients) {
246
+ for (let client of clients) {
247
+ if (socket != client && data.broadcast != false || socket == client && data.broadcastSender != false) {
248
+ if (isAsync) {
249
+ asyncData.push({ socket, message: JSON.stringify({ action, data }) })
250
+ } else {
251
+ const permission = await this.authorize.check(action, data, socket.config.user_id)
252
+ if (permission && permission.authorized)
253
+ data = permission.authorized
254
+
255
+ let responseData = JSON.stringify({
256
+ action,
257
+ data
258
+ });
259
+
260
+ socket.send(responseData);
261
+
262
+ if (socket.config && socket.config.organization_id)
263
+ this.emit("setBandwidth", {
264
+ type: 'out',
265
+ data: responseData,
266
+ organization_id: socket.config.organization_id
267
+ })
268
+ }
269
+ }
270
+ }
271
+ }
287
272
  }
288
273
 
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
274
  }
296
275
 
297
276
  // addAsyncMessage(key) {
@@ -313,6 +292,14 @@ class SocketServer extends EventEmitter {
313
292
  }
314
293
  return params
315
294
  }
295
+
296
+ getHost(req) {
297
+ const headers = req['headers']
298
+ let origin = headers['origin'];
299
+ if (origin && origin !== 'null')
300
+ return url.parse(origin).hostname;
301
+ }
302
+
316
303
  }
317
304
 
318
305