fix scoping of socket roles
This commit is contained in:
@ -16,6 +16,7 @@ export class Smartsocket {
|
||||
options: ISmartsocketConstructorOptions
|
||||
io: SocketIO.Server;
|
||||
openSockets = new Objectmap<SocketConnection>();
|
||||
socketRoles = new Objectmap<SocketRole>();
|
||||
constructor(optionsArg: ISmartsocketConstructorOptions) {
|
||||
this.options = optionsArg;
|
||||
};
|
||||
@ -29,6 +30,7 @@ export class Smartsocket {
|
||||
authenticated:false,
|
||||
role:undefined,
|
||||
side:"server",
|
||||
smartsocketHost: this,
|
||||
socket:socketArg
|
||||
});
|
||||
plugins.beautylog.log("Socket connected. Trying to authenticate...")
|
||||
@ -81,5 +83,16 @@ export class Smartsocket {
|
||||
done.resolve(dataArg.funcDataArg);
|
||||
});
|
||||
return done.promise;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* adds socketRoles
|
||||
*/
|
||||
addSocketRoles(socketRolesArray:SocketRole[]):void{
|
||||
for(let socketRole of socketRolesArray){
|
||||
this.socketRoles.add(socketRole);
|
||||
};
|
||||
return;
|
||||
};
|
||||
|
||||
}
|
@ -47,6 +47,7 @@ export class SmartsocketClient {
|
||||
authenticated:false,
|
||||
role:undefined,
|
||||
side:"client",
|
||||
smartsocketHost: null,
|
||||
socket: plugins.socketIoClient(socketUrl,{multiplex:false})
|
||||
});
|
||||
this.socketConnection.socket.on("requestAuth", () => {
|
||||
|
@ -4,6 +4,7 @@ import * as helpers from "./smartsocket.helpers";
|
||||
import {Objectmap} from "lik";
|
||||
|
||||
// import classes
|
||||
import { Smartsocket } from "./smartsocket.classes.smartsocket"
|
||||
import { SocketFunction } from "./smartsocket.classes.socketfunction";
|
||||
import { SocketRequest, ISocketRequestDataObject, allSocketRequests } from "./smartsocket.classes.socketrequest";
|
||||
import { SocketRole } from "./smartsocket.classes.socketrole";
|
||||
@ -23,6 +24,7 @@ export interface ISocketConnectionConstructorOptions {
|
||||
authenticated: boolean;
|
||||
role: SocketRole;
|
||||
side: TSocketConnectionSide;
|
||||
smartsocketHost: Smartsocket;
|
||||
socket: SocketIO.Socket | SocketIOClient.Socket;
|
||||
};
|
||||
|
||||
@ -46,12 +48,14 @@ export class SocketConnection {
|
||||
side:TSocketConnectionSide;
|
||||
authenticated: boolean = false;
|
||||
role: SocketRole;
|
||||
smartsocketHost:Smartsocket;
|
||||
socket: SocketIO.Socket | SocketIOClient.Socket;
|
||||
constructor(optionsArg: ISocketConnectionConstructorOptions) {
|
||||
this.alias = optionsArg.alias;
|
||||
this.authenticated = optionsArg.authenticated;
|
||||
this.role = optionsArg.role;
|
||||
this.side = optionsArg.side;
|
||||
this.smartsocketHost = optionsArg.smartsocketHost;
|
||||
this.socket = optionsArg.socket;
|
||||
|
||||
// standard behaviour that is always true
|
||||
@ -72,10 +76,10 @@ export class SocketConnection {
|
||||
this.socket.on("dataAuth", (dataArg:ISocketConnectionAuthenticationObject) => {
|
||||
plugins.beautylog.log("received authentication data. now hashing and comparing...");
|
||||
this.socket.removeListener("dataAuth", () => { });
|
||||
if (helpers.checkPasswordForRole(dataArg)) { // TODO: authenticate password
|
||||
if (helpers.checkPasswordForRole(dataArg,this.smartsocketHost)) { // TODO: authenticate password
|
||||
this.alias = dataArg.alias
|
||||
this.authenticated = true;
|
||||
this.role = helpers.getSocketRoleByName(dataArg.role);
|
||||
this.role = helpers.getSocketRoleByName(dataArg.role,this.smartsocketHost);
|
||||
this.socket.emit("authenticated");
|
||||
plugins.beautylog.ok(`socket with >>alias ${this.alias} >>role ${this.role} is authenticated!`);
|
||||
done.resolve(this);
|
||||
|
@ -5,9 +5,6 @@ import { Objectmap } from "lik";
|
||||
import { SocketFunction } from "./smartsocket.classes.socketfunction";
|
||||
|
||||
|
||||
export let allSocketRoles = new Objectmap<SocketRole>();
|
||||
|
||||
|
||||
/**
|
||||
* interface for class SocketRole
|
||||
*/
|
||||
@ -26,7 +23,6 @@ export class SocketRole {
|
||||
constructor(optionsArg: SocketRoleOptions) {
|
||||
this.name = optionsArg.name;
|
||||
this.passwordHash = optionsArg.passwordHash;
|
||||
allSocketRoles.add(this);
|
||||
};
|
||||
addSocketFunction(socketFunctionArg:SocketFunction){
|
||||
this.allowedFunctions.add(socketFunctionArg);
|
||||
|
@ -5,11 +5,11 @@ import { Smartsocket } from "./smartsocket.classes.smartsocket";
|
||||
import { SocketFunction, allSocketFunctions } from "./smartsocket.classes.socketfunction";
|
||||
import { SocketConnection, ISocketConnectionAuthenticationObject } from "./smartsocket.classes.socketconnection";
|
||||
import { SocketRequest, allSocketRequests, TSocketRequestSide } from "./smartsocket.classes.socketrequest";
|
||||
import { SocketRole, allSocketRoles } from "./smartsocket.classes.socketrole";
|
||||
import { SocketRole } from "./smartsocket.classes.socketrole";
|
||||
|
||||
// SocketConnection helpers
|
||||
export let checkPasswordForRole = (dataArg: ISocketConnectionAuthenticationObject): boolean => {
|
||||
let targetPasswordHash = getSocketRoleByName(dataArg.role).passwordHash;
|
||||
export let checkPasswordForRole = (dataArg: ISocketConnectionAuthenticationObject, referenceSmartsocket:Smartsocket): boolean => {
|
||||
let targetPasswordHash = getSocketRoleByName(dataArg.role,referenceSmartsocket).passwordHash;
|
||||
let computedCompareHash = plugins.nodehash.sha256FromStringSync(dataArg.password);
|
||||
return targetPasswordHash === computedCompareHash;
|
||||
}
|
||||
@ -34,7 +34,7 @@ export let getSocketRequestById = (shortIdArg: string, requestSide?: TSocketRequ
|
||||
/**
|
||||
* get corresponding SocketRole instance by name
|
||||
*/
|
||||
export let getSocketRoleByName = (socketRoleNameArg: string): SocketRole => {
|
||||
return allSocketRoles.find((socketRoleArg) => { return socketRoleArg.name === socketRoleNameArg })
|
||||
export let getSocketRoleByName = (socketRoleNameArg: string,referenceSmartsocket:Smartsocket): SocketRole => {
|
||||
return referenceSmartsocket.socketRoles.find((socketRoleArg) => { return socketRoleArg.name === socketRoleNameArg })
|
||||
};
|
||||
|
||||
|
Reference in New Issue
Block a user