2018-03-15 01:29:40 +00:00
|
|
|
import * as plugins from './smartsocket.plugins';
|
2016-08-07 12:58:20 +00:00
|
|
|
|
2016-08-07 16:59:39 +00:00
|
|
|
// import classes
|
2018-03-15 01:29:40 +00:00
|
|
|
import { SocketFunction } from './smartsocket.classes.socketfunction';
|
2019-08-12 20:31:40 +00:00
|
|
|
import { Smartsocket } from './smartsocket.classes.smartsocket';
|
|
|
|
import { SmartsocketClient } from './smartsocket.classes.smartsocketclient';
|
|
|
|
import { ISocketConnectionAuthenticationObject } from './smartsocket.classes.socketconnection';
|
2016-08-08 16:20:00 +00:00
|
|
|
|
2016-08-07 12:58:20 +00:00
|
|
|
/**
|
2016-08-07 13:37:52 +00:00
|
|
|
* interface for class SocketRole
|
2016-08-07 12:58:20 +00:00
|
|
|
*/
|
2019-08-13 09:36:31 +00:00
|
|
|
export interface ISocketRoleOptions {
|
2018-03-15 01:29:40 +00:00
|
|
|
name: string;
|
|
|
|
passwordHash: string;
|
2016-08-07 13:37:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A socketrole defines access to certain routines.
|
|
|
|
*/
|
|
|
|
export class SocketRole {
|
2019-08-12 20:31:40 +00:00
|
|
|
// STATIC
|
|
|
|
public static getSocketRoleByName(
|
|
|
|
referenceSmartsocket: Smartsocket | SmartsocketClient,
|
2019-08-12 20:46:57 +00:00
|
|
|
socketRoleNameArg: string
|
|
|
|
): SocketRole {
|
2022-01-18 16:10:46 +00:00
|
|
|
return referenceSmartsocket.socketRoles.findSync((socketRoleArg) => {
|
2019-08-12 20:31:40 +00:00
|
|
|
return socketRoleArg.name === socketRoleNameArg;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-09-29 17:21:08 +00:00
|
|
|
public static async checkPasswordForRole(
|
2019-08-12 20:31:40 +00:00
|
|
|
dataArg: ISocketConnectionAuthenticationObject,
|
|
|
|
referenceSmartsocket: Smartsocket | SmartsocketClient
|
2020-09-29 17:21:08 +00:00
|
|
|
): Promise<boolean> {
|
2019-08-12 20:46:57 +00:00
|
|
|
const targetPasswordHash = SocketRole.getSocketRoleByName(referenceSmartsocket, dataArg.role)
|
|
|
|
.passwordHash;
|
2020-09-29 17:21:08 +00:00
|
|
|
const computedCompareHash = await plugins.isohash.sha256FromString(dataArg.password);
|
2019-08-12 20:31:40 +00:00
|
|
|
return targetPasswordHash === computedCompareHash;
|
|
|
|
}
|
|
|
|
|
|
|
|
// INSTANCE
|
|
|
|
public name: string;
|
|
|
|
public passwordHash: string;
|
2020-09-24 18:03:01 +00:00
|
|
|
public allowedFunctions = new plugins.lik.ObjectMap<SocketFunction<any>>();
|
2019-08-13 09:36:31 +00:00
|
|
|
constructor(optionsArg: ISocketRoleOptions) {
|
2018-03-15 01:29:40 +00:00
|
|
|
this.name = optionsArg.name;
|
|
|
|
this.passwordHash = optionsArg.passwordHash;
|
|
|
|
}
|
2019-08-13 09:36:31 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* adds the socketfunction to the socketrole
|
|
|
|
* @param socketFunctionArg
|
|
|
|
*/
|
2019-09-09 21:58:32 +00:00
|
|
|
public addSocketFunction(socketFunctionArg: SocketFunction<any>) {
|
2018-03-15 01:29:40 +00:00
|
|
|
this.allowedFunctions.add(socketFunctionArg);
|
|
|
|
}
|
|
|
|
}
|