VerifierTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace HttpSignatures\tests;
  3. use GuzzleHttp\Psr7\Request;
  4. use HttpSignatures\KeyStore;
  5. use HttpSignatures\Verifier;
  6. class VerifierTest extends \PHPUnit_Framework_TestCase
  7. {
  8. const DATE = "Fri, 01 Aug 2014 13:44:32 -0700";
  9. const DATE_DIFFERENT = "Fri, 01 Aug 2014 13:44:33 -0700";
  10. /**
  11. * @var Verifier
  12. */
  13. private $verifier;
  14. /**
  15. * @var Request
  16. */
  17. private $message;
  18. public function setUp()
  19. {
  20. $this->setUpVerifier();
  21. $this->setUpValidMessage();
  22. }
  23. private function setUpVerifier()
  24. {
  25. $keyStore = new KeyStore(["pda" => "secret"]);
  26. $this->verifier = new Verifier($keyStore);
  27. }
  28. private function setUpValidMessage()
  29. {
  30. $signatureHeader = sprintf(
  31. 'keyId="%s",algorithm="%s",headers="%s",signature="%s"',
  32. "pda",
  33. "hmac-sha256",
  34. "(request-target) date",
  35. "cS2VvndvReuTLy52Ggi4j6UaDqGm9hMb4z0xJZ6adqU="
  36. );
  37. $this->message = new Request('GET', '/path?query=123', [
  38. "Date" => self::DATE,
  39. "Signature" => $signatureHeader
  40. ]);
  41. }
  42. public function testVerifyValidMessage()
  43. {
  44. $this->assertTrue($this->verifier->isValid($this->message));
  45. }
  46. public function testVerifyValidMessageAuthorizationHeader()
  47. {
  48. $message = $this->message->withHeader('Authorization', "Signature {$this->message->getHeader('Signature')[0]}");
  49. $message = $message->withoutHeader('Signature');
  50. $this->assertTrue($this->verifier->isValid($this->message));
  51. }
  52. public function testRejectTamperedRequestMethod()
  53. {
  54. $message = $this->message->withMethod('POST');
  55. $this->assertFalse($this->verifier->isValid($message));
  56. }
  57. public function testRejectTamperedDate()
  58. {
  59. $message = $this->message->withHeader('Date', self::DATE_DIFFERENT);
  60. $this->assertFalse($this->verifier->isValid($message));
  61. }
  62. public function testRejectTamperedSignature()
  63. {
  64. $message = $this->message->withHeader(
  65. 'Signature',
  66. preg_replace('/signature="/', 'signature="x', $this->message->getHeader('Signature')[0])
  67. );
  68. $this->assertFalse($this->verifier->isValid($message));
  69. }
  70. public function testRejectMessageWithoutSignatureHeader()
  71. {
  72. $message = $this->message->withoutHeader('Signature');
  73. $this->assertFalse($this->verifier->isValid($message));
  74. }
  75. public function testRejectMessageWithGarbageSignatureHeader()
  76. {
  77. $message = $this->message->withHeader('Signature', 'not="a",valid="signature"');
  78. $this->assertFalse($this->verifier->isValid($message));
  79. }
  80. public function testRejectMessageWithPartialSignatureHeader()
  81. {
  82. $message = $this->message->withHeader('Signature', 'keyId="aa",algorithm="bb"');
  83. $this->assertFalse($this->verifier->isValid($message));
  84. }
  85. public function testRejectsMessageWithUnknownKeyId()
  86. {
  87. $keyStore = new KeyStore(["nope" => "secret"]);
  88. $verifier = new Verifier($keyStore);
  89. $this->assertFalse($verifier->isValid($this->message));
  90. }
  91. public function testRejectsMessageMissingSignedHeaders()
  92. {
  93. $message = $this->message->withoutHeader('Date');
  94. $this->assertFalse($this->verifier->isValid($message));
  95. }
  96. }