99namespace Bluem \BluemPHP \Validators ;
1010
1111use Bluem \BluemPHP \Helpers \Now ;
12- use Selective \XmlDSig \PublicKeyStore ;
13- use Selective \XmlDSig \CryptoVerifier ;
14- use Selective \XmlDSig \XmlSignatureVerifier ;
15-
12+ use DOMDocument ;
1613use Exception ;
14+ use RobRichards \XMLSecLibs \XMLSecurityDSig ;
15+ use RobRichards \XMLSecLibs \XMLSecurityKey ;
1716
1817class WebhookSignatureValidation extends WebhookValidator
1918{
@@ -28,26 +27,36 @@ public function __construct(
2827 * Validate webhook signature based on a key file
2928 * available in the `keys` folder.
3029 */
30+
3131 public function validate (string $ data ): self
3232 {
3333 $ temp_file = tmpfile ();
3434 fwrite ($ temp_file , $ data );
3535 $ temp_file_path = stream_get_meta_data ($ temp_file )['uri ' ];
3636
37- $ publicKeyStore = new PublicKeyStore ();
38-
3937 $ public_key_file_path = dirname (__DIR__ , 2 ) . self ::KEY_FOLDER . $ this ->getKeyFileName ();
4038
4139 try {
42- $ publicKeyStore ->loadFromPem (file_get_contents ($ public_key_file_path ));
43- $ cryptoVerifier = new CryptoVerifier ($ publicKeyStore );
40+ $ xml = new DOMDocument ();
41+ $ xml ->load ($ temp_file_path );
42+
43+ $ objDSig = new XMLSecurityDSig ();
44+
45+ $ objDSig ->locateSignature ($ xml );
46+
47+ $ objDSig ->canonicalizeSignedInfo ();
48+
49+ // Validate the reference
50+ if (! $ objDSig ->validateReference ()) {
51+ $ this ->addError ("Reference validation failed " );
52+ }
4453
45- // Create a verifier instance and pass the crypto decoder
46- $ xmlSignatureVerifier = new XmlSignatureVerifier ($ cryptoVerifier );
54+ // Load the public key
55+ $ key = new XMLSecurityKey (XMLSecurityKey::RSA_SHA256 , ['type ' => 'public ' ]);
56+ $ key ->loadKey ($ public_key_file_path , true , true );
4757
48- // Verify a XML file
49- $ xmlVerified = $ xmlSignatureVerifier ->verifyXml (file_get_contents ($ temp_file_path ));
50- if (! $ xmlVerified ) {
58+ // Verify the signature
59+ if (! $ objDSig ->verify ($ key )) {
5160 $ this ->addError ("Invalid signature " );
5261 }
5362 } catch (Exception $ e ) {
0 commit comments