Skip to content

Commit

Permalink
feat: Simplify WebSocketServer Class, Replace Permission and Auth Ins…
Browse files Browse the repository at this point in the history
…tance with Single functions.

The SocketServer class has been updated with modifications to simplify the class and replace the setPermission and setAuth methods with authorize and authenticate functions. The code changes involve renaming the existing Instance functions, updating the handleUpgrade method to better handle permission and authorization checks.
  • Loading branch information
frankpagan committed May 25, 2023
1 parent 4509209 commit 4ec6eb3
Showing 1 changed file with 6 additions and 16 deletions.
22 changes: 6 additions & 16 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,6 @@ class SocketServer extends EventEmitter {

//. websocket server
this.wss = new WebSocket.Server({ noServer: true });
this.permissionInstance = null;
this.authInstance = null;
}

setPermission(instance) {
this.permissionInstance = instance;
}

setAuth(instance) {
this.authInstance = instance
}

handleUpgrade(req, socket, head) {
Expand Down Expand Up @@ -138,17 +128,17 @@ class SocketServer extends EventEmitter {
organization_id
});
let user_id = null;
if (this.authInstance)
user_id = await this.authInstance.getUserId(req);
if (this.authenticate)
user_id = await this.authenticate.getUserId(req);

if (this.permissionInstance) {
if (this.authorize) {
data.host = this.getHost(req)
const permission = await this.permissionInstance.check(action, data, user_id)
const permission = await this.authorize.check(action, data, user_id)
if (permission.dbUrl === false) {
data.database = process.env.organization_id
data.organization_id = process.env.organization_id

const permission2 = await this.permissionInstance.check(action, data, req, user_id)
const permission2 = await this.authorize.check(action, data, req, user_id)
if (!permission2 || permission2.error) {
return this.send(socket, 'Access Denied', { action, permission2, ...data })
}
Expand Down Expand Up @@ -258,7 +248,7 @@ class SocketServer extends EventEmitter {
if (isAsync) {
asyncData.push({ socket, message: JSON.stringify({ action, data }) })
} else {
const permission = await this.permissionInstance.check(action, data, socket.config.user_id)
const permission = await this.authorize.check(action, data, socket.config.user_id)
if (permission && permission.authorized)
data = permission.authorized

Expand Down

0 comments on commit 4ec6eb3

Please sign in to comment.