SignerTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. /**
  3. * @copyright 2016, Roeland Jago Douma <roeland@famdouma.nl>
  4. *
  5. * @author Roeland Jago Douma <roeland@famdouma.nl>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace Test\Security\IdentityProof;
  24. use OC\Security\IdentityProof\Key;
  25. use OC\Security\IdentityProof\Manager;
  26. use OC\Security\IdentityProof\Signer;
  27. use OCP\AppFramework\Utility\ITimeFactory;
  28. use OCP\IUser;
  29. use OCP\IUserManager;
  30. use Test\TestCase;
  31. class SignerTest extends TestCase {
  32. /** @var string */
  33. private $private = '-----BEGIN PRIVATE KEY-----
  34. MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDImc6QvHBjBIoo
  35. w9nnywiPNESleUuFJ2yQ3Gd/3w2BkblojlUAJAsUd/bQokjOH9d+7nqyzgSiXjRl
  36. iwKagY6NjcNEEq0X5KOMNwx6uEbtq3+7pA7H2JefNrnAuD+Fhp3Hyo3h1cse6hAq
  37. 6Zr8haCiSdFBfelLnx/X3gPgCzgl6GnvSmiqPEPFGng822d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W9Guz3Am8EhaB1zEsie773O+0QKBgFb/
  54. W3fqNufcQIRTMt5j2ACnwD95A2HiEVotXYl6KnbXN4god0VR4zaadNhqNRHNAAL2
  55. pNjJ9j7BWYNF2cngq1+NSOlzc51fVyjCAhqX5cDXkXGVjPJRDWAIh0clBvcOTwnN
  56. tAKgJhP9AS3rdvHR1szGEA2VnocWwMqfu//AowhdAoGACYwuBjfUWc21jcT5yeLZ
  57. ahLp+ImQsKDE0swhmk4uesbLLPRfyvpLca98XbBMuS1iLrVUY3mEfIV7ltaBajE0
  58. l3eB7suqch3WUzH1RMWzwpuUMWV/A8qjPbIrd2QYUFYxJsU88lBqRg92rPnri6Ec
  59. kC6HCb+CXsMRD7yp8KrrYnw=
  60. -----END PRIVATE KEY-----';
  61. /** @var string */
  62. private $public = '-----BEGIN PUBLIC KEY-----
  63. MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAyJnOkLxwYwSKKMPZ58sI
  64. jzREpXlLhSdskNxnf98NgZG5aI5VACQLFHf20KJIzh/Xfu56ss4Eol40ZYsCmoGO
  65. jY3DRBKtF+SjjDcMerhG7at/u6QOx9iXnza5wLg/hYadx8qN4dXLHuoQKuma/IWg
  66. oknRQX3pS58f194D4As4Jehp70poqjxDxRp4PNtnZVtkVviCFL+Mti6CB9jymaca
  67. LbcbRRiKXCkkhxlHzVsmuyIeBOkQYDq6cruIa5IpJeF3YeEVYAH/xh+eBYEIvwBX
  68. kUupZVyYXqd5WTrS5xXWDTyhc9Yv1LwaMlL/Ja7LzWu+hyIEjJhkCjjeIkDtD/zm
  69. gQIDAQAB
  70. -----END PUBLIC KEY-----';
  71. /** @var Key */
  72. private $key;
  73. /** @var Manager|\PHPUnit_Framework_MockObject_MockObject */
  74. private $keyManager;
  75. /** @var ITimeFactory|\PHPUnit_Framework_MockObject_MockObject */
  76. private $timeFactory;
  77. /** @var IUserManager|\PHPUnit_Framework_MockObject_MockObject */
  78. private $userManager;
  79. /** @var Signer */
  80. private $signer;
  81. public function setUp() {
  82. parent::setUp();
  83. $this->key = new Key($this->public, $this->private);
  84. $this->keyManager = $this->createMock(Manager::class);
  85. $this->timeFactory = $this->createMock(ITimeFactory::class);
  86. $this->userManager = $this->createMock(IUserManager::class);
  87. $this->signer = new Signer(
  88. $this->keyManager,
  89. $this->timeFactory,
  90. $this->userManager
  91. );
  92. }
  93. public function testSign() {
  94. $user = $this->createMock(IUser::class);
  95. $user->method('getCloudId')
  96. ->willReturn('foo@example.com');
  97. $this->timeFactory->method('getTime')
  98. ->willReturn(42);
  99. $this->keyManager->method('getKey')
  100. ->with($this->equalTo($user))
  101. ->willReturn($this->key);
  102. $data = [
  103. 'foo' => 'bar',
  104. 'abc' => 'def',
  105. 'xyz' => 123,
  106. ];
  107. $expects = [
  108. 'message' => [
  109. 'data' => $data,
  110. 'type' => 'myType',
  111. 'signer' => 'foo@example.com',
  112. 'timestamp' => 42,
  113. ],
  114. 'signature' => 'E1fNdoWMX1QmSyKv+S3FtOgLe9niYGQFWOKGaMLxc2h7s3V++EIqJvw/NCLBfrUowzWkTzhkjfbHaf88Hz34WAn4sAwXYAO8cnboQs6SClKRzQ/nvbtLgd2wm9RQ8UTOM7wR6C7HpIn4qqJ4BTQ1bAwYAiJ2GoK+W8wC0o0Gpub2906j3JJ4cbc9lufd5ohWKCev8Ubem/EEKaRIZA7qHh+Q1MKXTaJQJlCjTMe5PyGy0fsmtVxsPls3/Fkd9sVeHEHSYHzOiF6ttlxWou4TdRbq3WSEVpt1DOOvkKI9w2+zBJ7IPH8CnVpXcdIzWDctUygZKzNMUQnweDOOziEdUw=='
  115. ];
  116. $result = $this->signer->sign('myType', $data, $user);
  117. $this->assertEquals($expects, $result);
  118. }
  119. public function testVerifyValid() {
  120. $data = [
  121. 'message' => [
  122. 'data' => [
  123. 'foo' => 'bar',
  124. 'abc' => 'def',
  125. 'xyz' => 123,
  126. ],
  127. 'type' => 'myType',
  128. 'signer' => 'foo@example.com',
  129. 'timestamp' => 42,
  130. ],
  131. 'signature' => 'E1fNdoWMX1QmSyKv+S3FtOgLe9niYGQFWOKGaMLxc2h7s3V++EIqJvw/NCLBfrUowzWkTzhkjfbHaf88Hz34WAn4sAwXYAO8cnboQs6SClKRzQ/nvbtLgd2wm9RQ8UTOM7wR6C7HpIn4qqJ4BTQ1bAwYAiJ2GoK+W8wC0o0Gpub2906j3JJ4cbc9lufd5ohWKCev8Ubem/EEKaRIZA7qHh+Q1MKXTaJQJlCjTMe5PyGy0fsmtVxsPls3/Fkd9sVeHEHSYHzOiF6ttlxWou4TdRbq3WSEVpt1DOOvkKI9w2+zBJ7IPH8CnVpXcdIzWDctUygZKzNMUQnweDOOziEdUw=='
  132. ];
  133. $user = $this->createMock(IUser::class);
  134. $this->keyManager->method('getKey')
  135. ->with($this->equalTo($user))
  136. ->willReturn($this->key);
  137. $this->userManager->method('get')
  138. ->with('foo')
  139. ->willReturn($user);
  140. $this->assertTrue($this->signer->verify($data));
  141. }
  142. public function testVerifyInvalid() {
  143. $data = [
  144. 'message' => [
  145. 'data' => [
  146. 'foo' => 'bar',
  147. 'abc' => 'def',
  148. 'xyz' => 123,
  149. ],
  150. 'type' => 'myType',
  151. 'signer' => 'foo@example.com',
  152. 'timestamp' => 42,
  153. ],
  154. 'signature' => 'Invalid sig'
  155. ];
  156. $user = $this->createMock(IUser::class);
  157. $this->keyManager->method('getKey')
  158. ->with($this->equalTo($user))
  159. ->willReturn($this->key);
  160. $this->userManager->method('get')
  161. ->with('foo')
  162. ->willReturn($user);
  163. $this->assertFalse($this->signer->verify($data));
  164. }
  165. public function testVerifyInvalidData() {
  166. $data = [
  167. ];
  168. $this->assertFalse($this->signer->verify($data));
  169. }
  170. }