Files
smartsocket/ts/smartsocket.classes.socketrole.ts

54 lines
1.7 KiB
TypeScript
Raw Normal View History

2018-03-15 02:29:40 +01:00
import * as plugins from './smartsocket.plugins';
2016-08-07 14:58:20 +02:00
2016-08-07 18:59:39 +02:00
// import classes
import { Objectmap } from '@pushrocks/lik';
2018-03-15 02:29:40 +01:00
import { SocketFunction } from './smartsocket.classes.socketfunction';
2019-08-12 22:31:40 +02:00
import { Smartsocket } from './smartsocket.classes.smartsocket';
import { SmartsocketClient } from './smartsocket.classes.smartsocketclient';
import { ISocketConnectionAuthenticationObject } from './smartsocket.classes.socketconnection';
2016-08-08 18:20:00 +02:00
2016-08-07 14:58:20 +02:00
/**
2016-08-07 15:37:52 +02:00
* interface for class SocketRole
2016-08-07 14:58:20 +02:00
*/
2016-08-07 15:37:52 +02:00
export interface SocketRoleOptions {
2018-03-15 02:29:40 +01:00
name: string;
passwordHash: string;
2016-08-07 15:37:52 +02:00
}
/**
* A socketrole defines access to certain routines.
*/
export class SocketRole {
2019-08-12 22:31:40 +02:00
// STATIC
public static getSocketRoleByName(
referenceSmartsocket: Smartsocket | SmartsocketClient,
2019-08-12 22:46:57 +02:00
socketRoleNameArg: string
): SocketRole {
2019-08-12 22:31:40 +02:00
return referenceSmartsocket.socketRoles.find(socketRoleArg => {
return socketRoleArg.name === socketRoleNameArg;
});
}
2019-08-12 22:46:57 +02:00
public static checkPasswordForRole(
2019-08-12 22:31:40 +02:00
dataArg: ISocketConnectionAuthenticationObject,
referenceSmartsocket: Smartsocket | SmartsocketClient
): boolean {
2019-08-12 22:46:57 +02:00
const targetPasswordHash = SocketRole.getSocketRoleByName(referenceSmartsocket, dataArg.role)
.passwordHash;
2019-08-12 22:31:40 +02:00
const computedCompareHash = plugins.smarthash.sha256FromStringSync(dataArg.password);
return targetPasswordHash === computedCompareHash;
}
// INSTANCE
public name: string;
public passwordHash: string;
public allowedFunctions = new Objectmap<SocketFunction>();
2018-03-15 02:29:40 +01:00
constructor(optionsArg: SocketRoleOptions) {
this.name = optionsArg.name;
this.passwordHash = optionsArg.passwordHash;
}
2019-08-12 22:31:40 +02:00
public addSocketFunction(socketFunctionArg: SocketFunction) {
2018-03-15 02:29:40 +01:00
this.allowedFunctions.add(socketFunctionArg);
}
}