@@ -333,28 +333,56 @@ SignedXml.defaultNsForPrefix = {
333
333
334
334
SignedXml . findAncestorNs = findAncestorNs ;
335
335
336
- SignedXml . prototype . checkSignature = function ( xml ) {
336
+ SignedXml . prototype . checkSignature = function ( xml , callback ) {
337
+ if ( callback != null && typeof callback !== 'function' ) {
338
+ throw new Error ( "Last paramater must be a callback function" )
339
+ }
340
+
337
341
this . validationErrors = [ ]
338
342
this . signedXml = xml
339
343
340
344
if ( ! this . keyInfoProvider ) {
341
- throw new Error ( "cannot validate signature since no key info resolver was provided" )
345
+ var err = new Error ( "cannot validate signature since no key info resolver was provided" )
346
+ if ( ! callback ) {
347
+ throw err
348
+ } else {
349
+ callback ( err )
350
+ return
351
+ }
342
352
}
343
353
344
354
this . signingKey = this . keyInfoProvider . getKey ( this . keyInfo )
345
- if ( ! this . signingKey ) throw new Error ( "key info provider could not resolve key info " + this . keyInfo )
355
+ if ( ! this . signingKey ) {
356
+ var err = new Error ( "key info provider could not resolve key info " + this . keyInfo )
357
+ if ( ! callback ) {
358
+ throw err
359
+ } else {
360
+ callback ( err )
361
+ return
362
+ }
363
+ }
346
364
347
365
var doc = new Dom ( ) . parseFromString ( xml )
348
366
349
367
if ( ! this . validateReferences ( doc ) ) {
350
- return false ;
351
- }
352
-
353
- if ( ! this . validateSignatureValue ( doc ) ) {
354
- return false ;
368
+ if ( ! callback ) {
369
+ return false ;
370
+ } else {
371
+ callback ( new Error ( 'Could not validate references' ) )
372
+ }
355
373
}
356
374
357
- return true
375
+ var ret = this . validateSignatureValue ( doc )
376
+ if ( ret instanceof Promise ) {
377
+ ret . then ( ( asyncValidateSig ) => {
378
+ callback ( null , asyncValidateSig )
379
+ } )
380
+ } else {
381
+ if ( ! ret ) {
382
+ return false ;
383
+ }
384
+ return true
385
+ }
358
386
}
359
387
360
388
SignedXml . prototype . getCanonSignedInfoXml = function ( doc ) {
@@ -411,7 +439,7 @@ SignedXml.prototype.validateSignatureValue = function(doc) {
411
439
SignedXml . prototype . calculateSignatureValue = function ( doc ) {
412
440
var signedInfoCanon = this . getCanonSignedInfoXml ( doc )
413
441
var signer = this . findSignatureAlgorithm ( this . signatureAlgorithm )
414
- this . signatureValue = signer . getSignature ( signedInfoCanon , this . signingKey )
442
+ return this . signatureValue = signer . getSignature ( signedInfoCanon , this . signingKey )
415
443
}
416
444
417
445
SignedXml . prototype . findSignatureAlgorithm = function ( name ) {
@@ -654,7 +682,15 @@ SignedXml.prototype.addReference = function(xpath, transforms, digestAlgorithm,
654
682
* `append`, `prepend`, `before`, `after`
655
683
*
656
684
*/
657
- SignedXml . prototype . computeSignature = function ( xml , opts ) {
685
+ SignedXml . prototype . computeSignature = function ( xml , opts , callback ) {
686
+ if ( typeof opts === 'function' && callback == null ) {
687
+ callback = opts
688
+ }
689
+
690
+ if ( callback != null && typeof callback !== 'function' ) {
691
+ throw new Error ( "Last paramater must be a callback function" )
692
+ }
693
+
658
694
var doc = new Dom ( ) . parseFromString ( xml ) ,
659
695
xmlNsAttr = "xmlns" ,
660
696
signatureAttrs = [ ] ,
@@ -676,8 +712,14 @@ SignedXml.prototype.computeSignature = function(xml, opts) {
676
712
location . action = location . action || "append" ;
677
713
678
714
if ( validActions . indexOf ( location . action ) === - 1 ) {
679
- throw new Error ( "location.action option has an invalid action: " + location . action +
680
- ", must be any of the following values: " + validActions . join ( ", " ) ) ;
715
+ var err = new Error ( "location.action option has an invalid action: " + location . action +
716
+ ", must be any of the following values: " + validActions . join ( ", " ) ) ;
717
+ if ( ! callback ) {
718
+ throw err ;
719
+ } else {
720
+ callback ( err , null )
721
+ return
722
+ }
681
723
}
682
724
683
725
// automatic insertion of `:`
@@ -719,7 +761,13 @@ SignedXml.prototype.computeSignature = function(xml, opts) {
719
761
var referenceNode = xpath . select ( location . reference , doc ) ;
720
762
721
763
if ( ! referenceNode || referenceNode . length === 0 ) {
722
- throw new Error ( "the following xpath cannot be used because it was not found: " + location . reference ) ;
764
+ var err = new Error ( "the following xpath cannot be used because it was not found: " + location . reference ) ;
765
+ if ( ! callback ) {
766
+ throw err
767
+ } else {
768
+ callback ( err , null )
769
+ return
770
+ }
723
771
}
724
772
725
773
referenceNode = referenceNode [ 0 ] ;
@@ -735,16 +783,36 @@ SignedXml.prototype.computeSignature = function(xml, opts) {
735
783
}
736
784
737
785
this . signatureNode = signatureDoc
738
- this . calculateSignatureValue ( doc )
739
-
740
786
var signedInfoNode = utils . findChilds ( this . signatureNode , "SignedInfo" )
741
- if ( signedInfoNode . length == 0 ) throw new Error ( "could not find SignedInfo element in the message" )
742
-
787
+ if ( signedInfoNode . length == 0 ) {
788
+ var err = new Error ( "could not find SignedInfo element in the message" )
789
+ if ( ! callback ) {
790
+ throw err
791
+ } else {
792
+ callback ( err )
793
+ return
794
+ }
795
+ }
743
796
signedInfoNode = signedInfoNode [ 0 ] ;
744
- signatureDoc . insertBefore ( this . createSignature ( prefix ) , signedInfoNode . nextSibling )
745
797
746
- this . signatureXml = signatureDoc . toString ( )
747
- this . signedXml = doc . toString ( )
798
+ var ret = this . calculateSignatureValue ( doc )
799
+ if ( ret instanceof Promise ) {
800
+ ret . then ( ( asyncSig ) => {
801
+ this . signatureValue = asyncSig
802
+
803
+ signatureDoc . insertBefore ( this . createSignature ( prefix ) , signedInfoNode . nextSibling )
804
+ this . signatureXml = signatureDoc . toString ( )
805
+ this . signedXml = doc . toString ( )
806
+
807
+ if ( callback ) callback ( )
808
+ } ) . catch ( function ( err ) {
809
+ if ( callback ) callback ( err )
810
+ } )
811
+ } else {
812
+ signatureDoc . insertBefore ( this . createSignature ( prefix ) , signedInfoNode . nextSibling )
813
+ this . signatureXml = signatureDoc . toString ( )
814
+ this . signedXml = doc . toString ( )
815
+ }
748
816
}
749
817
750
818
SignedXml . prototype . getKeyInfo = function ( prefix ) {
0 commit comments