2014-10-06 08:21:26 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace HttpSignatures\Test;
|
|
|
|
|
2015-10-19 04:02:53 +00:00
|
|
|
use GuzzleHttp\Client;
|
2015-11-09 17:11:11 +00:00
|
|
|
use GuzzleHttp\Handler\CurlHandler;
|
|
|
|
use GuzzleHttp\Handler\MockHandler;
|
|
|
|
use GuzzleHttp\HandlerStack;
|
|
|
|
use GuzzleHttp\Middleware;
|
|
|
|
use GuzzleHttp\Psr7\Request;
|
|
|
|
use GuzzleHttp\Psr7\Response;
|
2014-10-06 08:21:26 +00:00
|
|
|
use HttpSignatures\Context;
|
2015-11-09 17:11:11 +00:00
|
|
|
use HttpSignatures\GuzzleHttpSignatures;
|
2014-10-06 08:21:26 +00:00
|
|
|
|
|
|
|
class GuzzleHttpSignerTest extends \PHPUnit_Framework_TestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var Context
|
|
|
|
*/
|
|
|
|
private $context;
|
|
|
|
|
|
|
|
/**
|
2015-10-19 04:02:53 +00:00
|
|
|
* @var Client
|
2014-10-06 08:21:26 +00:00
|
|
|
*/
|
|
|
|
private $client;
|
|
|
|
|
2015-11-09 17:11:11 +00:00
|
|
|
/**
|
|
|
|
* @var
|
|
|
|
*/
|
|
|
|
private $history = [];
|
|
|
|
|
2014-10-06 08:21:26 +00:00
|
|
|
public function setUp()
|
|
|
|
{
|
2015-11-09 17:11:11 +00:00
|
|
|
$this->context = new Context([
|
|
|
|
'keys' => ['pda' => 'secret'],
|
2014-10-06 08:21:26 +00:00
|
|
|
'algorithm' => 'hmac-sha256',
|
2015-11-09 17:11:11 +00:00
|
|
|
'headers' => ['(request-target)', 'date'],
|
2014-10-06 08:21:26 +00:00
|
|
|
]);
|
2015-10-19 04:02:53 +00:00
|
|
|
|
2015-11-09 17:11:11 +00:00
|
|
|
$stack = new HandlerStack();
|
|
|
|
$stack->setHandler(new MockHandler([
|
|
|
|
new Response(200, ['Content-Length' => 0]),
|
|
|
|
]));
|
|
|
|
$stack->push(GuzzleHttpSignatures::middlewareFromContext($this->context));
|
|
|
|
$stack->push(Middleware::history($this->history));
|
|
|
|
$this->client = new Client(['handler' => $stack]);
|
2014-10-06 08:21:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test signing a message
|
|
|
|
*/
|
|
|
|
public function testGuzzleRequestHasExpectedHeaders()
|
|
|
|
{
|
2015-11-09 17:11:11 +00:00
|
|
|
$this->client->get('/path?query=123', [
|
|
|
|
'headers' => ['date' => 'today', 'accept' => 'llamas']
|
|
|
|
]);
|
|
|
|
|
|
|
|
// get last request
|
|
|
|
$message = end($this->history);
|
|
|
|
/** @var Request $request */
|
|
|
|
$request = $message['request'];
|
|
|
|
/** @var Response $response */
|
|
|
|
$response = $message['request'];
|
2014-10-06 08:21:26 +00:00
|
|
|
|
|
|
|
$expectedString = implode(
|
|
|
|
',',
|
2015-11-09 17:11:11 +00:00
|
|
|
[
|
2014-10-06 08:21:26 +00:00
|
|
|
'keyId="pda"',
|
|
|
|
'algorithm="hmac-sha256"',
|
|
|
|
'headers="(request-target) date"',
|
|
|
|
'signature="SFlytCGpsqb/9qYaKCQklGDvwgmrwfIERFnwt+yqPJw="',
|
2015-11-09 17:11:11 +00:00
|
|
|
]
|
2014-10-06 08:21:26 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
2015-11-09 17:11:11 +00:00
|
|
|
[$expectedString],
|
|
|
|
$request->getHeader('Signature')
|
2014-10-06 08:21:26 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
2015-11-09 17:11:11 +00:00
|
|
|
['Signature ' . $expectedString],
|
|
|
|
$request->getHeader('Authorization')
|
2014-10-06 08:21:26 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test signing a message with a URL that doesn't contain a ?query
|
|
|
|
*/
|
|
|
|
public function testGuzzleRequestHasExpectedHeaders2()
|
|
|
|
{
|
2015-11-09 17:11:11 +00:00
|
|
|
$this->client->get('/path', [
|
|
|
|
'headers' => ['date' => 'today', 'accept' => 'llamas']
|
|
|
|
]);
|
|
|
|
|
|
|
|
// get last request
|
|
|
|
$message = end($this->history);
|
|
|
|
/** @var Request $request */
|
|
|
|
$request = $message['request'];
|
|
|
|
/** @var Response $response */
|
|
|
|
$response = $message['request'];
|
2014-10-06 08:21:26 +00:00
|
|
|
|
|
|
|
$expectedString = implode(
|
|
|
|
',',
|
2015-11-09 17:11:11 +00:00
|
|
|
[
|
2014-10-06 08:21:26 +00:00
|
|
|
'keyId="pda"',
|
|
|
|
'algorithm="hmac-sha256"',
|
|
|
|
'headers="(request-target) date"',
|
|
|
|
'signature="DAtF133khP05pS5Gh8f+zF/UF7mVUojMj7iJZO3Xk4o="',
|
2015-11-09 17:11:11 +00:00
|
|
|
]
|
2014-10-06 08:21:26 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
2015-11-09 17:11:11 +00:00
|
|
|
[$expectedString],
|
|
|
|
$request->getHeader('Signature')
|
2014-10-06 08:21:26 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
2015-11-09 17:11:11 +00:00
|
|
|
['Signature ' . $expectedString],
|
|
|
|
$request->getHeader('Authorization')
|
2014-10-06 08:21:26 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-11-09 17:11:11 +00:00
|
|
|
public function getVerifyGuzzleRequestVectors() {
|
|
|
|
return [
|
|
|
|
/* path, headers */
|
|
|
|
['/path?query=123', ['date' => 'today', 'accept' => 'llamas']],
|
|
|
|
['/path?z=zebra&a=antelope', ['date' => 'today']],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider getVerifyGuzzleRequestVectors
|
|
|
|
* @param string $path
|
|
|
|
* @param array $headers
|
|
|
|
*/
|
|
|
|
public function testVerifyGuzzleRequest($path, $headers)
|
2014-10-06 08:21:26 +00:00
|
|
|
{
|
2015-11-09 17:11:11 +00:00
|
|
|
$this->client->get($path, ['headers' => $headers]);
|
|
|
|
|
|
|
|
// get last request
|
|
|
|
$message = end($this->history);
|
|
|
|
/** @var Request $request */
|
|
|
|
$request = $message['request'];
|
|
|
|
/** @var Response $response */
|
|
|
|
$response = $message['request'];
|
2014-10-06 08:21:26 +00:00
|
|
|
|
2015-11-09 17:11:11 +00:00
|
|
|
$this->assertTrue($this->context->verifier()->isValid($request));
|
2014-10-06 08:21:26 +00:00
|
|
|
}
|
|
|
|
}
|