GuzzleHttpSignerTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. namespace HttpSignatures\Test;
  3. use GuzzleHttp\Client;
  4. use GuzzleHttp\Handler\CurlHandler;
  5. use GuzzleHttp\Handler\MockHandler;
  6. use GuzzleHttp\HandlerStack;
  7. use GuzzleHttp\Middleware;
  8. use GuzzleHttp\Psr7\Request;
  9. use GuzzleHttp\Psr7\Response;
  10. use HttpSignatures\Context;
  11. use HttpSignatures\GuzzleHttpSignatures;
  12. class GuzzleHttpSignerTest extends \PHPUnit_Framework_TestCase
  13. {
  14. /**
  15. * @var Context
  16. */
  17. private $context;
  18. /**
  19. * @var Client
  20. */
  21. private $client;
  22. /**
  23. * @var
  24. */
  25. private $history = [];
  26. public function setUp()
  27. {
  28. $this->context = new Context([
  29. 'keys' => ['pda' => 'secret'],
  30. 'algorithm' => 'hmac-sha256',
  31. 'headers' => ['(request-target)', 'date'],
  32. ]);
  33. $stack = new HandlerStack();
  34. $stack->setHandler(new MockHandler([
  35. new Response(200, ['Content-Length' => 0]),
  36. ]));
  37. $stack->push(GuzzleHttpSignatures::middlewareFromContext($this->context));
  38. $stack->push(Middleware::history($this->history));
  39. $this->client = new Client(['handler' => $stack]);
  40. }
  41. /**
  42. * test signing a message
  43. */
  44. public function testGuzzleRequestHasExpectedHeaders()
  45. {
  46. $this->client->get('/path?query=123', [
  47. 'headers' => ['date' => 'today', 'accept' => 'llamas']
  48. ]);
  49. // get last request
  50. $message = end($this->history);
  51. /** @var Request $request */
  52. $request = $message['request'];
  53. /** @var Response $response */
  54. $response = $message['request'];
  55. $expectedString = implode(
  56. ',',
  57. [
  58. 'keyId="pda"',
  59. 'algorithm="hmac-sha256"',
  60. 'headers="(request-target) date"',
  61. 'signature="SFlytCGpsqb/9qYaKCQklGDvwgmrwfIERFnwt+yqPJw="',
  62. ]
  63. );
  64. $this->assertEquals(
  65. [$expectedString],
  66. $request->getHeader('Signature')
  67. );
  68. $this->assertEquals(
  69. ['Signature ' . $expectedString],
  70. $request->getHeader('Authorization')
  71. );
  72. }
  73. /**
  74. * test signing a message with a URL that doesn't contain a ?query
  75. */
  76. public function testGuzzleRequestHasExpectedHeaders2()
  77. {
  78. $this->client->get('/path', [
  79. 'headers' => ['date' => 'today', 'accept' => 'llamas']
  80. ]);
  81. // get last request
  82. $message = end($this->history);
  83. /** @var Request $request */
  84. $request = $message['request'];
  85. /** @var Response $response */
  86. $response = $message['request'];
  87. $expectedString = implode(
  88. ',',
  89. [
  90. 'keyId="pda"',
  91. 'algorithm="hmac-sha256"',
  92. 'headers="(request-target) date"',
  93. 'signature="DAtF133khP05pS5Gh8f+zF/UF7mVUojMj7iJZO3Xk4o="',
  94. ]
  95. );
  96. $this->assertEquals(
  97. [$expectedString],
  98. $request->getHeader('Signature')
  99. );
  100. $this->assertEquals(
  101. ['Signature ' . $expectedString],
  102. $request->getHeader('Authorization')
  103. );
  104. }
  105. public function getVerifyGuzzleRequestVectors() {
  106. return [
  107. /* path, headers */
  108. ['/path?query=123', ['date' => 'today', 'accept' => 'llamas']],
  109. ['/path?z=zebra&a=antelope', ['date' => 'today']],
  110. ];
  111. }
  112. /**
  113. * @dataProvider getVerifyGuzzleRequestVectors
  114. * @param string $path
  115. * @param array $headers
  116. */
  117. public function testVerifyGuzzleRequest($path, $headers)
  118. {
  119. $this->client->get($path, ['headers' => $headers]);
  120. // get last request
  121. $message = end($this->history);
  122. /** @var Request $request */
  123. $request = $message['request'];
  124. /** @var Response $response */
  125. $response = $message['request'];
  126. $this->assertTrue($this->context->verifier()->isValid($request));
  127. }
  128. }