update structure

This commit is contained in:
Philipp Kunz 2016-08-08 18:20:00 +02:00
parent fc530ba37e
commit 8d58e0b2f3
16 changed files with 220 additions and 99 deletions

View File

@ -1,12 +1,6 @@
/// <reference types="socket.io" />
import { Objectmap } from "lik";
import { SocketRole } from "./smartsocket.classes.socketrole";
export interface ISocketObject {
alias?: string;
authenticated: boolean;
role?: string;
socket: SocketIO.Socket;
}
export interface ISmartsocketConstructorOptions {
port: number;
}
@ -14,13 +8,13 @@ export declare class Smartsocket {
options: ISmartsocketConstructorOptions;
io: SocketIO.Server;
openSockets: Objectmap;
registeredRoles: Objectmap;
registeredFunctions: Objectmap;
constructor(optionsArg: ISmartsocketConstructorOptions);
/**
* the standard handler for new socket connections
*/
private _handleSocket(socket);
registerFunctions(socketRoleArg: SocketRole): void;
private _handleSocket(socketArg);
registerFunction(socketRoleArg: SocketRole): void;
/**
* starts listening to incling sockets:
*/

View File

@ -3,12 +3,12 @@ const plugins = require("./smartsocket.plugins");
const helpers = require("./smartsocket.helpers");
// classes
const lik_1 = require("lik");
;
const smartsocket_classes_socketconnection_1 = require("./smartsocket.classes.socketconnection");
;
class Smartsocket {
constructor(optionsArg) {
this.openSockets = new lik_1.Objectmap();
this.registeredRoles = new lik_1.Objectmap();
this.registeredFunctions = new lik_1.Objectmap();
/**
* starts listening to incling sockets:
*/
@ -32,20 +32,20 @@ class Smartsocket {
/**
* the standard handler for new socket connections
*/
_handleSocket(socket) {
let socketObject = {
socket: socket,
authenticated: false
};
_handleSocket(socketArg) {
let socketConnection = new smartsocket_classes_socketconnection_1.SocketConnection({
authenticated: false,
socket: socketArg
});
plugins.beautylog.log("Socket connected. Trying to authenticate...");
this.openSockets.add(socketObject);
helpers.authenticateSocket(socketObject)
this.openSockets.add(socketConnection);
helpers.authenticateSocket(socketConnection)
.then();
}
registerFunctions(socketRoleArg) {
this.registeredRoles.add(socketRoleArg);
registerFunction(socketRoleArg) {
this.registeredFunctions.add(socketRoleArg);
}
;
}
exports.Smartsocket = Smart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0FBQTtRQXZDRyxJQUFJLENBQUMsT0FBTyxHQUFHLFVBQVUsQ0FBQztJQUM5QixDQUFDOztJQUVEOztPQUVHO0lBQ0ssYUFBYSxDQUFDLFNBQVM7UUFDM0IsSUFBSSxnQkFBZ0IsR0FBc0IsSUFBSSx1REFBZ0IsQ0FBQztZQUMzRCxhQUFhLEVBQUMsS0FBSztZQUNuQixNQUFNLEVBQUMsU0FBUztTQUNuQixDQUFDLENBQUM7UUFDSCxPQUFPLENBQUMsU0FBUyxDQUFDLEdBQUcsQ0FBQyw2Q0FBNkMsQ0FBQyxDQUFBO1FBQ3BFLElBQUksQ0FBQyxXQUFXLENBQUMsR0FBRyxDQUFDLGdCQUFnQixDQUFDLENBQUM7UUFDdkMsT0FBTyxDQUFDLGtCQUFrQixDQUFDLGdCQUFnQixDQUFDO2FBQ3ZDLElBQUksRUFBRSxDQUFDO0lBQ2hCLENBQUM7SUFFRCxnQkFBZ0IsQ0FBQyxhQUF5QjtRQUN0QyxJQUFJLENBQUMsbUJBQW1CLENBQUMsR0FBRyxDQUFDLGFBQWEsQ0FBQyxDQUFDO0lBRWhELENBQUM7O0FBb0JMLENBQUM7QUE5Q1ksbUJBQVcsY0E4Q3ZCLENBQUEifQ==

View File

@ -0,0 +1,14 @@
/// <reference types="socket.io" />
export interface ISocketConnectionOptions {
alias?: string;
authenticated: boolean;
role?: string;
socket: SocketIO.Socket;
}
export declare class SocketConnection {
alias?: string;
authenticated: boolean;
role?: string;
socket: SocketIO.Socket;
constructor(optionsArg: ISocketConnectionOptions);
}

View File

@ -0,0 +1,13 @@
"use strict";
;
class SocketConnection {
constructor(optionsArg) {
this.alias = optionsArg.alias;
this.authenticated = optionsArg.authenticated;
this.role = optionsArg.role;
this.socket = optionsArg.socket;
}
}
exports.SocketConnection = SocketConnection;
;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRzb2NrZXQuY2xhc3Nlcy5zb2NrZXRjb25uZWN0aW9uLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvc21hcnRzb2NrZXQuY2xhc3Nlcy5zb2NrZXRjb25uZWN0aW9uLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7QUFPQyxDQUFDO0FBRUY7SUFLSSxZQUFZLFVBQW1DO1FBQzNDLElBQUksQ0FBQyxLQUFLLEdBQUcsVUFBVSxDQUFDLEtBQUssQ0FBQztRQUM5QixJQUFJLENBQUMsYUFBYSxHQUFHLFVBQVUsQ0FBQyxhQUFhLENBQUM7UUFDOUMsSUFBSSxDQUFDLElBQUksR0FBRyxVQUFVLENBQUMsSUFBSSxDQUFDO1FBQzVCLElBQUksQ0FBQyxNQUFNLEdBQUcsVUFBVSxDQUFDLE1BQU0sQ0FBQztJQUNwQyxDQUFDO0FBQ0wsQ0FBQztBQVhZLHdCQUFnQixtQkFXNUIsQ0FBQTtBQUFBLENBQUMifQ==

View File

@ -1,4 +1,9 @@
import { SocketRole } from "./smartsocket.classes.socketrole";
export interface ISocketFunctionData {
functionName: string;
functionData: any;
responseTimeout?: number;
}
export interface SocketFunctionOptions {
name: string;
func: any;
@ -9,4 +14,6 @@ export declare class SocketFunction {
func: any;
roles: SocketRole[];
constructor(optionsArg: SocketFunctionOptions);
private _notifyRole(socketRoleArg);
functionRequest(dataArg: ISocketFunctionData): void;
}

View File

@ -1,12 +1,21 @@
"use strict";
;
;
class SocketFunction {
constructor(optionsArg) {
this.name = optionsArg.name;
this.func = optionsArg.func;
this.roles = optionsArg.roles;
for (let socketRoleArg of this.roles) {
this._notifyRole(socketRoleArg);
}
}
;
_notifyRole(socketRoleArg) {
socketRoleArg.addSocketFunction(this);
}
functionRequest(dataArg) {
}
}
exports.SocketFunction = SocketFunction;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRzb2NrZXQuY2xhc3Nlcy5zb2NrZXRmdW5jdGlvbi5qcyIsInNvdXJjZVJ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08sV0FBVyxDQUFDLGFBQXdCO1FBQ3hDLGFBQWEsQ0FBQyxpQkFBaUIsQ0FBQyxJQUFJLENBQUMsQ0FBQztJQUMxQyxDQUFDO0lBQ0QsZUFBZSxDQUFDLE9BQTJCO0lBRTNDLENBQUM7QUFDTCxDQUFDO0FBbEJZLHNCQUFjLGlCQWtCMUIsQ0FBQSJ9

View File

@ -1,3 +1,4 @@
import { SocketFunction } from "./smartsocket.classes.socketfunction";
/**
* interface for class SocketRole
*/
@ -11,5 +12,7 @@ export interface SocketRoleOptions {
export declare class SocketRole {
name: string;
passwordHash: string;
allowedFunctions: SocketFunction[];
constructor(optionsArg: SocketRoleOptions);
addSocketFunction(socketFunctionArg: SocketFunction): void;
}

View File

@ -7,6 +7,10 @@ class SocketRole {
this.name = optionsArg.name;
this.passwordHash = optionsArg.passwordHash;
}
;
addSocketFunction(socketFunctionArg) {
this.allowedFunctions.push(socketFunctionArg);
}
}
exports.SocketRole = SocketRole;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRzb2NrZXQuY2xhc3Nlcy5zb2NrZXRyb2xlLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvc21hcnRzb2NrZXQuY2xhc3Nlcy5zb2NrZXRyb2xlLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7QUFjQTs7R0FFRztBQUNIO0lBR0ksWUFBWSxVQUE0QjtRQUNwQyxJQUFJLENBQUMsSUFBSSxHQUFHLFVBQVUsQ0FBQyxJQUFJLENBQUM7UUFDNUIsSUFBSSxDQUFDLFlBQVksR0FBRyxVQUFVLENBQUMsWUFBWSxDQUFDO0lBQ2hELENBQUM7QUFDTCxDQUFDO0FBUFksa0JBQVUsYUFPdEIsQ0FBQSJ9
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRzb2NrZXQuY2xhc3Nlcy5zb2NrZXRyb2xlLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvc21hcnRzb2NrZXQuY2xhc3Nlcy5zb2NrZXRyb2xlLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7QUFjQTs7R0FFRztBQUNIO0lBSUksWUFBWSxVQUE2QjtRQUNyQyxJQUFJLENBQUMsSUFBSSxHQUFHLFVBQVUsQ0FBQyxJQUFJLENBQUM7UUFDNUIsSUFBSSxDQUFDLFlBQVksR0FBRyxVQUFVLENBQUMsWUFBWSxDQUFDO0lBQ2hELENBQUM7O0lBQ0QsaUJBQWlCLENBQUMsaUJBQWdDO1FBQzlDLElBQUksQ0FBQyxnQkFBZ0IsQ0FBQyxJQUFJLENBQUMsaUJBQWlCLENBQUMsQ0FBQztJQUNsRCxDQUFDO0FBQ0wsQ0FBQztBQVhZLGtCQUFVLGFBV3RCLENBQUEifQ==

View File

@ -1,7 +1,8 @@
/// <reference types="q" />
import * as plugins from "./smartsocket.plugins";
import { ISocketObject } from "./smartsocket.classes.smartsocket";
import { SocketConnection } from "./smartsocket.classes.socketconnection";
/**
* authenticate a socket
*/
export declare let authenticateSocket: (socketObjectArg: ISocketObject) => plugins.q.Promise<{}>;
export declare let authenticateSocket: (socketConnectionArg: SocketConnection) => plugins.q.Promise<{}>;
export declare let listenToReadySocket: (socketConnectionArg: SocketConnection) => plugins.q.Promise<{}>;

View File

@ -3,26 +3,31 @@ const plugins = require("./smartsocket.plugins");
/**
* authenticate a socket
*/
exports.authenticateSocket = (socketObjectArg) => {
exports.authenticateSocket = (socketConnectionArg) => {
let done = plugins.q.defer();
socketObjectArg.socket.on("dataAuth", dataArg => {
socketConnectionArg.socket.on("dataAuth", dataArg => {
plugins.beautylog.log("received authentication data. now hashing and comparing...");
socketObjectArg.socket.removeListener("dataAuth", () => { });
socketConnectionArg.socket.removeListener("dataAuth", () => { });
if ((true)) {
socketObjectArg.alias = dataArg.alias;
socketObjectArg.authenticated = true;
socketObjectArg.role = dataArg.role;
socketObjectArg.socket.emit("authenticated");
plugins.beautylog.ok(`socket with >>alias ${socketObjectArg.alias} >>role ${socketObjectArg.role} is authenticated!`);
done.resolve(socketObjectArg);
socketConnectionArg.alias = dataArg.alias;
socketConnectionArg.authenticated = true;
socketConnectionArg.role = dataArg.role;
socketConnectionArg.socket.emit("authenticated");
plugins.beautylog.ok(`socket with >>alias ${socketConnectionArg.alias} >>role ${socketConnectionArg.role} is authenticated!`);
done.resolve(socketConnectionArg);
}
else {
socketObjectArg.socket.disconnect();
socketConnectionArg.socket.disconnect();
done.reject("not authenticated");
}
;
});
socketObjectArg.socket.emit("requestAuth");
socketConnectionArg.socket.emit("requestAuth");
return done.promise;
};
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRzb2NrZXQuaGVscGVycy5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3NtYXJ0c29ja2V0LmhlbHBlcnMudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLE1BQVksT0FBTyxXQUFNLHVCQUF1QixDQUFDLENBQUE7QUFRakQ7O0dBRUc7QUFDUSwwQkFBa0IsR0FBRyxDQUFDLGVBQThCO0lBQzNELElBQUksSUFBSSxHQUFHLE9BQU8sQ0FBQyxDQUFDLENBQUMsS0FBSyxFQUFFLENBQUM7SUFDN0IsZUFBZSxDQUFDLE1BQU0sQ0FBQyxFQUFFLENBQUMsVUFBVSxFQUFFLE9BQU87UUFDekMsT0FBTyxDQUFDLFNBQVMsQ0FBQyxHQUFHLENBQUMsNERBQTRELENBQUMsQ0FBQztRQUNwRixlQUFlLENBQUMsTUFBTSxDQUFDLGNBQWMsQ0FBQyxVQUFVLEVBQUUsUUFBUSxDQUFDLENBQUMsQ0FBQztRQUM3RCxFQUFFLENBQUEsQ0FBQyxDQUFDLElBQUksQ0FBQyxDQUFDLENBQUEsQ0FBQztZQUNQLGVBQWUsQ0FBQyxLQUFLLEdBQUcsT0FBTyxDQUFDLEtBQUssQ0FBQTtZQUNyQyxlQUFlLENBQUMsYUFBYSxHQUFHLElBQUksQ0FBQztZQUNyQyxlQUFlLENBQUMsSUFBSSxHQUFHLE9BQU8sQ0FBQyxJQUFJLENBQUM7WUFDcEMsZUFBZSxDQUFDLE1BQU0sQ0FBQyxJQUFJLENBQUMsZUFBZSxDQUFDLENBQUM7WUFDN0MsT0FBTyxDQUFDLFNBQVMsQ0FBQyxFQUFFLENBQUMsdUJBQXVCLGVBQWUsQ0FBQyxLQUFLLFdBQVcsZUFBZSxDQUFDLElBQUksb0JBQW9CLENBQUMsQ0FBQTtZQUNySCxJQUFJLENBQUMsT0FBTyxDQUFDLGVBQWUsQ0FBQyxDQUFDO1FBQ2xDLENBQUM7UUFBQyxJQUFJLENBQUMsQ0FBQztZQUNKLGVBQWUsQ0FBQyxNQUFNLENBQUMsVUFBVSxFQUFFLENBQUM7WUFDcEMsSUFBSSxDQUFDLE1BQU0sQ0FBQyxtQkFBbUIsQ0FBQyxDQUFDO1FBQ3JDLENBQUM7UUFBQSxDQUFDO0lBQ04sQ0FBQyxDQUFDLENBQUM7SUFDSCxlQUFlLENBQUMsTUFBTSxDQUFDLElBQUksQ0FBQyxhQUFhLENBQUMsQ0FBQztJQUMzQyxNQUFNLENBQUMsSUFBSSxDQUFDLE9BQU8sQ0FBQztBQUN4QixDQUFDLENBQUMifQ==
exports.listenToReadySocket = (socketConnectionArg) => {
let done = plugins.q.defer();
// socketConnectionArg.socket.listen("function", )
return done.promise;
};
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRzb2NrZXQuaGVscGVycy5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3NtYXJ0c29ja2V0LmhlbHBlcnMudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLE1BQVksT0FBTyxXQUFNLHVCQUF1QixDQUFDLEN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UFBQSxDQUFDO0lBQ04sQ0FBQyxDQUFDLENBQUM7SUFDSCxtQkFBbUIsQ0FBQyxNQUFNLENBQUMsSUFBSSxDQUFDLGFBQWEsQ0FBQyxDQUFDO0lBQy9DLE1BQU0sQ0FBQyxJQUFJLENBQUMsT0FBTyxDQUFDO0FBQ3hCLENBQUMsQ0FBQztBQUVTLDJCQUFtQixHQUFHLENBQUMsbUJBQXFDO0lBQ25FLElBQUksSUFBSSxHQUFHLE9BQU8sQ0FBQyxDQUFDLENBQUMsS0FBSyxFQUFFLENBQUM7SUFDN0Isa0RBQWtEO0lBQ2xELE1BQU0sQ0FBQyxJQUFJLENBQUMsT0FBTyxDQUFDO0FBQ3hCLENBQUMsQ0FBQSJ9

View File

@ -19,11 +19,13 @@
"homepage": "https://gitlab.com/pushrocks/smartsocket#README",
"dependencies": {
"@types/q": "0.x.x",
"@types/shortid": "0.0.27",
"@types/socket.io": "^1.4.26",
"@types/socket.io-client": "^1.4.26",
"beautylog": "^5.0.20",
"lik": "^1.0.10",
"lik": "^1.0.15",
"q": "^1.4.1",
"shortid": "^2.2.6",
"socket.io": "^1.4.8",
"socket.io-client": "^1.4.8",
"taskbuffer": "^1.0.7"

View File

@ -3,16 +3,9 @@ import * as helpers from "./smartsocket.helpers";
// classes
import { Objectmap } from "lik";
import {SocketRole} from "./smartsocket.classes.socketrole";
import {SocketFunction} from "./smartsocket.classes.socketfunction";
export interface ISocketObject {
alias?:string;
authenticated: boolean
role?:string,
socket: SocketIO.Socket,
};
import { SocketRole } from "./smartsocket.classes.socketrole";
import { SocketFunction } from "./smartsocket.classes.socketfunction";
import { SocketConnection } from "./smartsocket.classes.socketconnection";
export interface ISmartsocketConstructorOptions {
port: number;
@ -20,10 +13,9 @@ export interface ISmartsocketConstructorOptions {
};
export class Smartsocket {
options:ISmartsocketConstructorOptions
options: ISmartsocketConstructorOptions
io: SocketIO.Server;
openSockets = new Objectmap();
registeredRoles = new Objectmap();
openSockets = new Objectmap<SocketConnection>();
constructor(optionsArg: ISmartsocketConstructorOptions) {
this.options = optionsArg;
};
@ -31,22 +23,17 @@ export class Smartsocket {
/**
* the standard handler for new socket connections
*/
private _handleSocket(socket) {
let socketObject: ISocketObject = {
socket: socket,
authenticated: false
};
private _handleSocket(socketArg) {
let socketConnection: SocketConnection = new SocketConnection({
authenticated:false,
socket:socketArg
});
plugins.beautylog.log("Socket connected. Trying to authenticate...")
this.openSockets.add(socketObject);
helpers.authenticateSocket(socketObject)
.then();
}
registerFunctions(socketRoleArg:SocketRole){
this.registeredRoles.add(socketRoleArg);
this.openSockets.add(socketConnection);
socketConnection.authenticate()
.then(socketConnection.listenToFunctionRequests);
};
/**
* starts listening to incling sockets:
*/
@ -58,7 +45,7 @@ export class Smartsocket {
});
}
closeServer = () => {
this.openSockets.forEach((socketObjectArg: ISocketObject) => {
this.openSockets.forEach((socketObjectArg: SocketConnection) => {
plugins.beautylog.log(`disconnect socket with >>alias ${socketObjectArg.alias}`);
socketObjectArg.socket.disconnect();
});

View File

@ -0,0 +1,64 @@
import * as plugins from "./smartsocket.plugins";
import * as helpers from "./smartsocket.helpers";
// import classes
import { SocketRole } from "./smartsocket.classes.socketrole";
import { SocketFunction, ISocketFunctionData } from "./smartsocket.classes.socketfunction";
export interface ISocketConnectionOptions {
alias?: string;
authenticated: boolean;
role?: SocketRole;
socket: SocketIO.Socket;
};
export class SocketConnection {
alias?: string;
authenticated: boolean;
role?: SocketRole;
socket: SocketIO.Socket;
constructor(optionsArg: ISocketConnectionOptions) {
this.alias = optionsArg.alias;
this.authenticated = optionsArg.authenticated;
this.role = optionsArg.role;
this.socket = optionsArg.socket;
}
/**
* authenticate the socket
*/
authenticate() {
let done = plugins.q.defer();
this.socket.on("dataAuth", dataArg => {
plugins.beautylog.log("received authentication data. now hashing and comparing...");
this.socket.removeListener("dataAuth", () => { });
if ((true)) { // TODO: authenticate password
this.alias = dataArg.alias
this.authenticated = true;
this.role = helpers.findSocketRoleByString(dataArg.role);
this.socket.emit("authenticated");
plugins.beautylog.ok(`socket with >>alias ${this.alias} >>role ${this.role} is authenticated!`);
done.resolve(this);
} else {
this.socket.disconnect();
done.reject("not authenticated");
};
});
this.socket.emit("requestAuth");
return done.promise;
};
/**
* listen to function requests
*/
listenToFunctionRequests() {
let done = plugins.q.defer();
if(this.authenticated){
this.socket.on("function", (dataArg:ISocketFunctionData) => {
this.role.allowedFunctions
})
} else {
done.reject("socket needs to be authenticated first");
};
return done.promise;
}
};

View File

@ -4,7 +4,11 @@ import * as plugins from "./smartsocket.plugins";
import { Stringmap } from "lik";
import { SocketRole } from "./smartsocket.classes.socketrole";
export interface ISocketFunctionData {
functionName:string,
functionData:any,
responseTimeout?:number
};
export interface SocketFunctionOptions {
name: string;
@ -16,9 +20,31 @@ export class SocketFunction {
name: string;
func: any;
roles: SocketRole[];
/**
* the constructor for SocketFunction
*/
constructor(optionsArg: SocketFunctionOptions) {
this.name = optionsArg.name;
this.func = optionsArg.func;
this.roles = optionsArg.roles;
for (let socketRoleArg of this.roles){
this._notifyRole(socketRoleArg);
}
};
/**
* notifies a role about access to this SocketFunction
*/
private _notifyRole(socketRoleArg:SocketRole){
socketRoleArg.addSocketFunction(this);
}
/**
* handles a function request to this SocketFunction
*/
functionRequest(dataArg:ISocketFunctionData){
};
}

View File

@ -1,25 +1,34 @@
import * as plugins from "./smartsocket.plugins";
// import classes
import { Stringmap } from "lik";
import { Objectmap } from "lik";
import { SocketFunction } from "./smartsocket.classes.socketfunction";
export let allSocketRoles = new Objectmap<SocketRole>();
/**
* interface for class SocketRole
*/
export interface SocketRoleOptions {
name:string;
passwordHash:string;
name: string;
passwordHash: string;
}
/**
* A socketrole defines access to certain routines.
*/
export class SocketRole {
name:string;
passwordHash:string;
constructor(optionsArg:SocketRoleOptions){
name: string;
passwordHash: string;
allowedFunctions = new Objectmap<SocketFunction>();
constructor(optionsArg: SocketRoleOptions) {
this.name = optionsArg.name;
this.passwordHash = optionsArg.passwordHash;
allSocketRoles.add(this);
};
addSocketFunction(socketFunctionArg:SocketFunction){
this.allowedFunctions.add(socketFunctionArg);
}
}

View File

@ -1,31 +1,14 @@
import * as plugins from "./smartsocket.plugins";
// interfaces
import {ISocketObject} from "./smartsocket.classes.smartsocket";
// classes
import { Smartsocket } from "./smartsocket.classes.smartsocket";
import { SocketFunction } from "./smartsocket.classes.socketfunction";
import { SocketConnection } from "./smartsocket.classes.socketconnection";
import { SocketRole, allSocketRoles } from "./smartsocket.classes.socketrole";
// SocketRole helpers
export let findSocketRoleByString = (socketRoleNameArg: string): SocketRole => {
return allSocketRoles.find((socketRoleArg) => { return socketRoleArg.name === socketRoleNameArg })
};
/**
* authenticate a socket
*/
export let authenticateSocket = (socketObjectArg: ISocketObject) => {
let done = plugins.q.defer();
socketObjectArg.socket.on("dataAuth", dataArg => {
plugins.beautylog.log("received authentication data. now hashing and comparing...");
socketObjectArg.socket.removeListener("dataAuth", () => { });
if((true)){ // TODO: authenticate password
socketObjectArg.alias = dataArg.alias
socketObjectArg.authenticated = true;
socketObjectArg.role = dataArg.role;
socketObjectArg.socket.emit("authenticated");
plugins.beautylog.ok(`socket with >>alias ${socketObjectArg.alias} >>role ${socketObjectArg.role} is authenticated!`)
done.resolve(socketObjectArg);
} else {
socketObjectArg.socket.disconnect();
done.reject("not authenticated");
};
});
socketObjectArg.socket.emit("requestAuth");
return done.promise;
};