diff --git a/test/test.ts b/test/test.ts index 0b127ff..defddf2 100644 --- a/test/test.ts +++ b/test/test.ts @@ -12,7 +12,7 @@ tap.test('should create a valid SmartlogReceiver', async () => { testReceiver = new smartlogReceiver.SmartlogReceiver({ passphrase: 'hi', smartlogInstance: testSmartlog, - validatorFunction: () => { + validatorFunction: async () => { return true; } }); diff --git a/ts/sl.classes.smartlogreceiver.ts b/ts/sl.classes.smartlogreceiver.ts index 22cbefd..c8e4371 100644 --- a/ts/sl.classes.smartlogreceiver.ts +++ b/ts/sl.classes.smartlogreceiver.ts @@ -6,7 +6,7 @@ import { ILogDestination, } from '@pushrocks/smartlog-interfaces'; -export type TValidatorFunction = (logPackage: ILogPackage) => boolean; +export type TValidatorFunction = (logPackage: ILogPackage) => Promise; export interface ISmartlogReceiverOptions { smartlogInstance: plugins.smartlog.Smartlog; @@ -24,7 +24,7 @@ export class SmartlogReceiver { constructor(smartlogReceiverOptions: ISmartlogReceiverOptions) { this.passphrase = smartlogReceiverOptions.passphrase; - this.validatorFunction = smartlogReceiverOptions.validatorFunction; + this.validatorFunction = smartlogReceiverOptions.validatorFunction || (async (logpackageArg) => {return true}); this.smartlogInstance = smartlogReceiverOptions.smartlogInstance; } @@ -35,7 +35,10 @@ export class SmartlogReceiver { const authString = authenticatedLogPackageArg.auth; const logPackage = authenticatedLogPackageArg.logPackage; - if (authString === plugins.smarthash.sha256FromStringSync(this.passphrase)) { + if ( + authString === plugins.smarthash.sha256FromStringSync(this.passphrase) + && await this.validatorFunction(logPackage) + ) { // Message authenticated lets clean up. logPackage.correlation ? null : (logPackage.correlation = { id: '123', type: 'none' }); logPackage.correlation.id ? null : (logPackage.correlation.id = '123');