@@ -143,7 +143,7 @@ public function __construct($auth_key, $secret, $app_id, $options = array(), $ho
143
143
144
144
// ensure host doesn't have a scheme prefix
145
145
$ this ->settings ['host ' ] =
146
- preg_replace ('/http[s]?\:\/\// ' , '' , $ this ->settings ['host ' ], 1 );
146
+ preg_replace ('/http[s]?\:\/\// ' , '' , $ this ->settings ['host ' ], 1 );
147
147
}
148
148
149
149
/**
@@ -398,9 +398,15 @@ private function ddn_domain()
398
398
*
399
399
* @return string
400
400
*/
401
- public static function build_auth_query_string ($ auth_key , $ auth_secret , $ request_method , $ request_path ,
402
- $ query_params = array (), $ auth_version = '1.0 ' , $ auth_timestamp = null )
403
- {
401
+ public static function build_auth_query_string (
402
+ $ auth_key ,
403
+ $ auth_secret ,
404
+ $ request_method ,
405
+ $ request_path ,
406
+ $ query_params = array (),
407
+ $ auth_version = '1.0 ' ,
408
+ $ auth_timestamp = null
409
+ ) {
404
410
$ params = array ();
405
411
$ params ['auth_key ' ] = $ auth_key ;
406
412
$ params ['auth_timestamp ' ] = (is_null ($ auth_timestamp ) ? time () : $ auth_timestamp );
@@ -745,4 +751,43 @@ public function notify($interests, $data = array(), $debug = false)
745
751
746
752
return false ;
747
753
}
754
+
755
+ /**
756
+ * Verify that a webhook actually came from Pusher, and marshals them into a Webhook object.
757
+ *
758
+ * @param array $headers an array of headers from the request (for example, from getallheaders())
759
+ * @param string $body the body of the request (for example, from file_get_contents('php://input'))
760
+ *
761
+ * @return Webhook object with the properties time_ms (an int) and events (an array of event objects)
762
+ */
763
+ public function webhook ($ headers , $ body )
764
+ {
765
+ $ this ->ensure_valid_signature ($ headers , $ body );
766
+ $ decoded_json = json_decode ($ body );
767
+ $ webhookobj = new Webhook ($ decoded_json ->time_ms , $ decoded_json ->events );
768
+
769
+ return $ webhookobj ;
770
+ }
771
+
772
+ /**
773
+ * Verify that a given Pusher Signature is valid.
774
+ *
775
+ * @param array $headers an array of headers from the request (for example, from getallheaders())
776
+ * @param string $body the body of the request (for example, from file_get_contents('php://input'))
777
+ *
778
+ * @throws PusherException if signature is inccorrect.
779
+ */
780
+ public function ensure_valid_signature ($ headers , $ body )
781
+ {
782
+ $ x_pusher_key = $ headers ['X-Pusher-Key ' ];
783
+ $ x_pusher_signature = $ headers ['X-Pusher-Signature ' ];
784
+ if ($ x_pusher_key == $ this ->settings ['auth_key ' ]) {
785
+ $ expected = hash_hmac ('sha256 ' , $ body , $ this ->settings ['secret ' ]);
786
+ if ($ expected === $ x_pusher_signature ) {
787
+ return ;
788
+ }
789
+ }
790
+
791
+ throw new PusherException (sprintf ('Received WebHook with invalid signature: got %s, expected %s. ' , $ x_pusher_signature , $ expected ));
792
+ }
748
793
}
0 commit comments