Skip to content

Signedrequest notices #88

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion examples/signedrequest-verification.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
$requestValidator = new \MessageBird\RequestValidator('YOUR_SIGNING_KEY');

// Set up the signed request from the PHP global variables.
$request = \MessageBird\Objects\SignedRequest::createFromGlobals();
try {
$request = \MessageBird\Objects\SignedRequest::createFromGlobals();
} catch (\MessageBird\Exceptions\ValidationException $e) {
// The request was missing expected values, so respond accordingly.
http_response_code(412);
}

if (!$requestValidator->verify($request)) {
// The request was invalid, so respond accordingly.
Expand Down
6 changes: 4 additions & 2 deletions src/MessageBird/Objects/SignedRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@ public static function createFromGlobals()
{
$body = file_get_contents('php://input');
$queryParameters = $_GET;
$requestTimestamp = $_SERVER['HTTP_MESSAGEBIRD_REQUEST_TIMESTAMP'];
$signature = $_SERVER['HTTP_MESSAGEBIRD_SIGNATURE'];
$requestTimestamp = isset($_SERVER['HTTP_MESSAGEBIRD_REQUEST_TIMESTAMP']) ?
$_SERVER['HTTP_MESSAGEBIRD_REQUEST_TIMESTAMP'] : null;
$signature = isset($_SERVER['HTTP_MESSAGEBIRD_SIGNATURE']) ?
$_SERVER['HTTP_MESSAGEBIRD_SIGNATURE'] : null;

$signedRequest = new SignedRequest();
$signedRequest->loadFromArray(compact('body', 'queryParameters', 'requestTimestamp', 'signature'));
Expand Down
162 changes: 162 additions & 0 deletions tests/unit/SignedRequestTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
<?php

use MessageBird\Objects\SignedRequest;

class SignedRequestTest extends PHPUnit_Framework_TestCase
{
public function testCreate()
{
$query = array(
'recipient' => '31612345678',
'reference' => 'FOO',
'statusDatetime' => '2019-01-11T09:17:11+00:00',
'id' => 'eef0ab57a9e049be946f3821568c2b2e',
'status' => 'delivered',
'mccmnc' => '20408',
'ported' => '1',
);
$signature = '2bl+38H4oHVg03pC3bk2LvCB0IHFgfC4cL5HPQ0LdmI=';
$requestTimestamp = 1547198231;
$body = '{"foo":"bar"}';

$request = SignedRequest::create($query, $signature, $requestTimestamp, $body);

self::assertEquals($requestTimestamp, $request->requestTimestamp);
self::assertEquals($body, $request->body);
self::assertEquals($query, $request->queryParameters);
self::assertEquals($signature, $request->signature);
}

public function testLoadFromArray()
{
$query = array(
'recipient' => '31612345678',
'reference' => 'FOO',
'statusDatetime' => '2019-01-11T09:17:11+00:00',
'id' => 'eef0ab57a9e049be946f3821568c2b2e',
'status' => 'delivered',
'mccmnc' => '20408',
'ported' => '1',
);
$signature = '2bl+38H4oHVg03pC3bk2LvCB0IHFgfC4cL5HPQ0LdmI=';
$requestTimestamp = 1547198231;
$body = '{"foo":"bar"}';

$request = new SignedRequest();
$request->loadFromArray(array(
'queryParameters' => $query,
'signature' => $signature,
'requestTimestamp' => $requestTimestamp,
'body' => $body
));

self::assertEquals($requestTimestamp, $request->requestTimestamp);
self::assertEquals($body, $request->body);
self::assertEquals($query, $request->queryParameters);
self::assertEquals($signature, $request->signature);
}

/**
* @expectedException \MessageBird\Exceptions\ValidationException
* @expectedExceptionMessage query
*/
public function testLoadInvalidQuery()
{
$query = null;
$signature = '2bl+38H4oHVg03pC3bk2LvCB0IHFgfC4cL5HPQ0LdmI=';
$requestTimestamp = 1547198231;
$body = '{"foo":"bar"}';

$request = new SignedRequest();
$request->loadFromArray(array(
'queryParameters' => $query,
'signature' => $signature,
'requestTimestamp' => $requestTimestamp,
'body' => $body
));
}

/**
* @expectedException \MessageBird\Exceptions\ValidationException
* @expectedExceptionMessage signature
*/
public function testLoadInvalidSignature()
{
$query = array(
'recipient' => '31612345678',
'reference' => 'FOO',
'statusDatetime' => '2019-01-11T09:17:11+00:00',
'id' => 'eef0ab57a9e049be946f3821568c2b2e',
'status' => 'delivered',
'mccmnc' => '20408',
'ported' => '1',
);
$signature = null;
$requestTimestamp = 1547198231;
$body = '{"foo":"bar"}';

$request = new SignedRequest();
$request->loadFromArray(array(
'queryParameters' => $query,
'signature' => $signature,
'requestTimestamp' => $requestTimestamp,
'body' => $body
));
}

/**
* @expectedException \MessageBird\Exceptions\ValidationException
* @expectedExceptionMessage requestTimestamp
*/
public function testLoadInvalidTimestamp()
{
$query = array(
'recipient' => '31612345678',
'reference' => 'FOO',
'statusDatetime' => '2019-01-11T09:17:11+00:00',
'id' => 'eef0ab57a9e049be946f3821568c2b2e',
'status' => 'delivered',
'mccmnc' => '20408',
'ported' => '1',
);
$signature = '2bl+38H4oHVg03pC3bk2LvCB0IHFgfC4cL5HPQ0LdmI=';
$requestTimestamp = null;
$body = '{"foo":"bar"}';

$request = new SignedRequest();
$request->loadFromArray(array(
'queryParameters' => $query,
'signature' => $signature,
'requestTimestamp' => $requestTimestamp,
'body' => $body
));
}

/**
* @expectedException \MessageBird\Exceptions\ValidationException
* @expectedExceptionMessage body
*/
public function testLoadInvalidBody()
{
$query = array(
'recipient' => '31612345678',
'reference' => 'FOO',
'statusDatetime' => '2019-01-11T09:17:11+00:00',
'id' => 'eef0ab57a9e049be946f3821568c2b2e',
'status' => 'delivered',
'mccmnc' => '20408',
'ported' => '1',
);
$signature = '2bl+38H4oHVg03pC3bk2LvCB0IHFgfC4cL5HPQ0LdmI=';
$requestTimestamp = 1547198231;
$body = null;

$request = new SignedRequest();
$request->loadFromArray(array(
'queryParameters' => $query,
'signature' => $signature,
'requestTimestamp' => $requestTimestamp,
'body' => $body
));
}
}