2022-07-27 10:34:41 +00:00
|
|
|
import * as plugins from './sl.receiver.plugins.js';
|
2018-10-31 16:42:18 +00:00
|
|
|
|
2020-06-07 12:41:25 +00:00
|
|
|
import {
|
|
|
|
ILogPackage,
|
|
|
|
ILogPackageAuthenticated,
|
2022-03-07 21:08:32 +00:00
|
|
|
ILogDestination,
|
2020-06-07 12:41:25 +00:00
|
|
|
} from '@pushrocks/smartlog-interfaces';
|
2018-11-11 00:38:19 +00:00
|
|
|
|
2020-06-10 06:57:47 +00:00
|
|
|
export type TValidatorFunction = (logPackage: ILogPackage) => Promise<boolean>;
|
2018-11-11 00:38:19 +00:00
|
|
|
|
|
|
|
export interface ISmartlogReceiverOptions {
|
|
|
|
smartlogInstance: plugins.smartlog.Smartlog;
|
|
|
|
passphrase: string;
|
|
|
|
validatorFunction: TValidatorFunction;
|
|
|
|
}
|
2018-10-31 16:42:18 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* a class that receives smartlog packages
|
|
|
|
*/
|
|
|
|
export class SmartlogReceiver {
|
2019-10-22 13:38:19 +00:00
|
|
|
public passphrase: string;
|
|
|
|
public validatorFunction: TValidatorFunction;
|
|
|
|
public smartlogInstance: plugins.smartlog.Smartlog;
|
2018-11-11 00:38:19 +00:00
|
|
|
|
|
|
|
constructor(smartlogReceiverOptions: ISmartlogReceiverOptions) {
|
|
|
|
this.passphrase = smartlogReceiverOptions.passphrase;
|
2020-06-10 09:21:59 +00:00
|
|
|
this.validatorFunction =
|
|
|
|
smartlogReceiverOptions.validatorFunction ||
|
2022-03-07 21:08:32 +00:00
|
|
|
(async (logpackageArg) => {
|
2020-06-10 09:21:59 +00:00
|
|
|
return true;
|
|
|
|
});
|
2018-11-11 00:38:19 +00:00
|
|
|
this.smartlogInstance = smartlogReceiverOptions.smartlogInstance;
|
|
|
|
}
|
|
|
|
|
2018-10-31 16:42:18 +00:00
|
|
|
/**
|
|
|
|
* handles a authenticated log
|
|
|
|
*/
|
2019-10-22 13:38:19 +00:00
|
|
|
public async handleAuthenticatedLog(authenticatedLogPackageArg: ILogPackageAuthenticated) {
|
2018-11-11 00:38:19 +00:00
|
|
|
const authString = authenticatedLogPackageArg.auth;
|
|
|
|
const logPackage = authenticatedLogPackageArg.logPackage;
|
|
|
|
|
2020-06-10 06:57:47 +00:00
|
|
|
if (
|
2020-06-10 09:21:59 +00:00
|
|
|
authString === plugins.smarthash.sha256FromStringSync(this.passphrase) &&
|
|
|
|
(await this.validatorFunction(logPackage))
|
2020-06-10 06:57:47 +00:00
|
|
|
) {
|
2020-06-08 08:44:06 +00:00
|
|
|
// Message authenticated lets clean up.
|
|
|
|
logPackage.correlation ? null : (logPackage.correlation = { id: '123', type: 'none' });
|
|
|
|
logPackage.correlation.id ? null : (logPackage.correlation.id = '123');
|
|
|
|
logPackage.correlation.type ? null : (logPackage.correlation.type = 'none');
|
|
|
|
|
2020-06-07 15:04:36 +00:00
|
|
|
this.smartlogInstance.handleLog(logPackage);
|
2018-11-11 18:06:53 +00:00
|
|
|
return { status: 'ok' };
|
2018-11-11 00:38:19 +00:00
|
|
|
} else {
|
|
|
|
this.smartlogInstance.log('error', 'Message rejected because of bad passphrase');
|
2018-11-11 18:06:53 +00:00
|
|
|
return { status: 'error' };
|
2018-11-11 00:38:19 +00:00
|
|
|
// console.log(plugins.smarthash.sha256FromStringSync(this.passphrase));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-22 13:38:19 +00:00
|
|
|
/**
|
|
|
|
* handles an array of authenticated logs
|
|
|
|
* @param authenticatedLogsPackageArrayArg
|
|
|
|
*/
|
2020-06-07 12:41:25 +00:00
|
|
|
public async handleManyAuthenticatedLogs(
|
|
|
|
authenticatedLogsPackageArrayArg: ILogPackageAuthenticated[]
|
|
|
|
) {
|
2019-10-22 13:38:19 +00:00
|
|
|
const promiseArray: Array<Promise<any>> = [];
|
|
|
|
for (const logPackage of authenticatedLogsPackageArrayArg) {
|
|
|
|
promiseArray.push(this.handleAuthenticatedLog(logPackage));
|
|
|
|
}
|
|
|
|
await Promise.all(promiseArray);
|
|
|
|
}
|
2018-10-31 16:42:18 +00:00
|
|
|
}
|