-
Notifications
You must be signed in to change notification settings - Fork 308
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #168 from pusher/wh-validation
Add pusher signature validation
- Loading branch information
Showing
4 changed files
with
132 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
namespace Pusher; | ||
|
||
class Webhook | ||
{ | ||
private $time_ms; | ||
private $events = array(); | ||
|
||
public function __construct($time_ms, $events) | ||
{ | ||
$this->time_ms = $time_ms; | ||
$this->events = $events; | ||
} | ||
|
||
public function get_events() | ||
{ | ||
return $this->events; | ||
} | ||
|
||
public function get_time_ms() | ||
{ | ||
return $this->time_ms; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
class webhookTest extends PHPUnit_Framework_TestCase | ||
{ | ||
protected function setUp() | ||
{ | ||
$this->pusher = new Pusher\Pusher('thisisaauthkey', 'thisisasecret', 1, true); | ||
$this->auth_key = 'thisisaauthkey'; | ||
} | ||
|
||
public function testValidWebhookSignature() | ||
{ | ||
$signature = '40e0ad3b9aa49529322879e84de1aaaf18bde1efe839ca263d540cc865510d25'; | ||
$body = '{"hello":"world"}'; | ||
$headers = array( | ||
'X-Pusher-Key' => $this->auth_key, | ||
'X-Pusher-Signature' => $signature, | ||
); | ||
|
||
$this->pusher->ensure_valid_signature($headers, $body); | ||
} | ||
|
||
/** | ||
* @expectedException \Pusher\PusherException | ||
*/ | ||
public function testInvalidWebhookSignature() | ||
{ | ||
$signature = 'potato'; | ||
$body = '{"hello":"world"}'; | ||
$headers = array( | ||
'X-Pusher-Key' => $this->auth_key, | ||
'X-Pusher-Signature' => $signature, | ||
); | ||
$wrong_signature = $this->pusher->ensure_valid_signature($headers, $body); | ||
} | ||
|
||
public function testDecodeWebhook() | ||
{ | ||
$headers_json = '{"X-Pusher-Key":"'.$this->auth_key.'","X-Pusher-Signature":"a19cab2af3ca1029257570395e78d5d675e9e700ca676d18a375a7083178df1c"}'; | ||
$body = '{"time_ms":1530710011901,"events":[{"name":"client_event","channel":"private-my-channel","event":"client-event","data":"Unencrypted","socket_id":"240621.35780774"}]}'; | ||
$headers = json_decode($headers_json, true); | ||
|
||
$decodedWebhook = $this->pusher->webhook($headers, $body); | ||
$this->assertEquals($decodedWebhook->get_time_ms(), 1530710011901); | ||
$this->assertEquals(count($decodedWebhook->get_events()), 1); | ||
} | ||
} |