@@ -73,7 +73,7 @@ export class Server extends EventEmitter {
7373 public services : IServices ;
7474 public log : ( type : string , data : any ) => any ;
7575 public authorizeConnection : ( req : Request , res ?: Response ) => boolean ;
76- public authenticate : ( security : IServerAuthenticate , processAuthResult ?: ( result : boolean ) => void , req ?: Request , obj ?: any ) => boolean | void | Promise < void > ;
76+ public authenticate : ( security : IServerAuthenticate , processAuthResult ?: ( result : boolean ) => void , req ?: Request , obj ?: any ) => boolean | void | Promise < boolean > ;
7777
7878 private wsdl : WSDL ;
7979 private suppressStack : boolean ;
@@ -385,10 +385,27 @@ export class Server extends EventEmitter {
385385 // Authentication
386386 if ( typeof authenticate === 'function' ) {
387387 let authResultProcessed = false ;
388- const processAuthResult = ( authResult ) => {
389- if ( ! authResultProcessed && ( authResult || authResult === false ) ) {
390- authResultProcessed = true ;
391- if ( authResult ) {
388+ const processAuthResult = ( authResult : boolean | Error ) => {
389+ if ( authResultProcessed ) {
390+ return ;
391+ }
392+
393+ authResultProcessed = true ;
394+ // Handle errors
395+ if ( authResult instanceof Error ) {
396+ return this . _sendError ( {
397+ Code : {
398+ Value : 'SOAP-ENV:Server' ,
399+ Subcode : { value : 'InternalServerError' } ,
400+ } ,
401+ Reason : { Text : authResult . toString ( ) } ,
402+ statusCode : 500 ,
403+ } , callback , includeTimestamp ) ;
404+ }
405+
406+ // Handle actual results
407+ if ( typeof authResult === 'boolean' ) {
408+ if ( authResult === true ) {
392409 try {
393410 process ( ) ;
394411 } catch ( error ) {
@@ -417,7 +434,17 @@ export class Server extends EventEmitter {
417434 }
418435 } ;
419436
420- processAuthResult ( authenticate ( obj . Header && obj . Header . Security , processAuthResult , req , obj ) ) ;
437+ const functionResult = authenticate ( obj . Header && obj . Header . Security , processAuthResult , req , obj ) ;
438+ if ( isPromiseLike < boolean > ( functionResult ) ) {
439+ functionResult . then ( ( result : boolean ) => {
440+ processAuthResult ( result ) ;
441+ } , ( err : any ) => {
442+ processAuthResult ( err ) ;
443+ } ) ;
444+ }
445+ if ( typeof functionResult === 'boolean' ) {
446+ processAuthResult ( functionResult ) ;
447+ }
421448 } else {
422449 throw new Error ( 'Invalid authenticate function (not a function)' ) ;
423450 }
0 commit comments