VerificationTokenTest.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test\Security\VerificationToken;
  8. use OC\Security\VerificationToken\VerificationToken;
  9. use OCP\AppFramework\Utility\ITimeFactory;
  10. use OCP\BackgroundJob\IJobList;
  11. use OCP\IConfig;
  12. use OCP\IUser;
  13. use OCP\Security\ICrypto;
  14. use OCP\Security\ISecureRandom;
  15. use OCP\Security\VerificationToken\InvalidTokenException;
  16. use PHPUnit\Framework\MockObject\MockObject;
  17. use Test\TestCase;
  18. class VerificationTokenTest extends TestCase {
  19. /** @var VerificationToken */
  20. protected $token;
  21. /** @var IConfig|MockObject */
  22. protected $config;
  23. /** @var ISecureRandom|MockObject */
  24. protected $secureRandom;
  25. /** @var ICrypto|MockObject */
  26. protected $crypto;
  27. /** @var ITimeFactory|MockObject */
  28. protected $timeFactory;
  29. /** @var IJobList|MockObject */
  30. protected $jobList;
  31. protected function setUp(): void {
  32. parent::setUp();
  33. $this->config = $this->createMock(IConfig::class);
  34. $this->crypto = $this->createMock(ICrypto::class);
  35. $this->timeFactory = $this->createMock(ITimeFactory::class);
  36. $this->secureRandom = $this->createMock(ISecureRandom::class);
  37. $this->jobList = $this->createMock(IJobList::class);
  38. $this->token = new VerificationToken(
  39. $this->config,
  40. $this->crypto,
  41. $this->timeFactory,
  42. $this->secureRandom,
  43. $this->jobList
  44. );
  45. }
  46. public function testTokenUserUnknown(): void {
  47. $this->expectException(InvalidTokenException::class);
  48. $this->expectExceptionCode(InvalidTokenException::USER_UNKNOWN);
  49. $this->token->check('encryptedToken', null, 'fingerprintToken', 'foobar');
  50. }
  51. public function testTokenUserUnknown2(): void {
  52. $user = $this->createMock(IUser::class);
  53. $user->expects($this->atLeastOnce())
  54. ->method('isEnabled')
  55. ->willReturn(false);
  56. $this->expectException(InvalidTokenException::class);
  57. $this->expectExceptionCode(InvalidTokenException::USER_UNKNOWN);
  58. $this->token->check('encryptedToken', $user, 'fingerprintToken', 'foobar');
  59. }
  60. public function testTokenNotFound(): void {
  61. $user = $this->createMock(IUser::class);
  62. $user->expects($this->atLeastOnce())
  63. ->method('isEnabled')
  64. ->willReturn(true);
  65. $user->expects($this->atLeastOnce())
  66. ->method('getUID')
  67. ->willReturn('alice');
  68. // implicit: IConfig::getUserValue returns null by default
  69. $this->expectException(InvalidTokenException::class);
  70. $this->expectExceptionCode(InvalidTokenException::TOKEN_NOT_FOUND);
  71. $this->token->check('encryptedToken', $user, 'fingerprintToken', 'foobar');
  72. }
  73. public function testTokenDecryptionError(): void {
  74. $user = $this->createMock(IUser::class);
  75. $user->expects($this->atLeastOnce())
  76. ->method('isEnabled')
  77. ->willReturn(true);
  78. $user->expects($this->atLeastOnce())
  79. ->method('getUID')
  80. ->willReturn('alice');
  81. $this->config->expects($this->atLeastOnce())
  82. ->method('getUserValue')
  83. ->with('alice', 'core', 'fingerprintToken', null)
  84. ->willReturn('encryptedToken');
  85. $this->config->expects($this->any())
  86. ->method('getSystemValueString')
  87. ->with('secret')
  88. ->willReturn('357111317');
  89. $this->crypto->method('decrypt')
  90. ->with('encryptedToken', 'foobar' . '357111317')
  91. ->willThrowException(new \Exception('decryption failed'));
  92. $this->expectException(InvalidTokenException::class);
  93. $this->expectExceptionCode(InvalidTokenException::TOKEN_DECRYPTION_ERROR);
  94. $this->token->check('encryptedToken', $user, 'fingerprintToken', 'foobar');
  95. }
  96. public function testTokenInvalidFormat(): void {
  97. $user = $this->createMock(IUser::class);
  98. $user->expects($this->atLeastOnce())
  99. ->method('isEnabled')
  100. ->willReturn(true);
  101. $user->expects($this->atLeastOnce())
  102. ->method('getUID')
  103. ->willReturn('alice');
  104. $this->config->expects($this->atLeastOnce())
  105. ->method('getUserValue')
  106. ->with('alice', 'core', 'fingerprintToken', null)
  107. ->willReturn('encryptedToken');
  108. $this->config->expects($this->any())
  109. ->method('getSystemValueString')
  110. ->with('secret')
  111. ->willReturn('357111317');
  112. $this->crypto->method('decrypt')
  113. ->with('encryptedToken', 'foobar' . '357111317')
  114. ->willReturn('decrypted^nonsense');
  115. $this->expectException(InvalidTokenException::class);
  116. $this->expectExceptionCode(InvalidTokenException::TOKEN_INVALID_FORMAT);
  117. $this->token->check('encryptedToken', $user, 'fingerprintToken', 'foobar');
  118. }
  119. public function testTokenExpired(): void {
  120. $user = $this->createMock(IUser::class);
  121. $user->expects($this->atLeastOnce())
  122. ->method('isEnabled')
  123. ->willReturn(true);
  124. $user->expects($this->atLeastOnce())
  125. ->method('getUID')
  126. ->willReturn('alice');
  127. $user->expects($this->any())
  128. ->method('getLastLogin')
  129. ->willReturn(604803);
  130. $this->config->expects($this->atLeastOnce())
  131. ->method('getUserValue')
  132. ->with('alice', 'core', 'fingerprintToken', null)
  133. ->willReturn('encryptedToken');
  134. $this->config->expects($this->any())
  135. ->method('getSystemValueString')
  136. ->with('secret')
  137. ->willReturn('357111317');
  138. $this->crypto->method('decrypt')
  139. ->with('encryptedToken', 'foobar' . '357111317')
  140. ->willReturn('604800:mY70K3n');
  141. $this->timeFactory->expects($this->any())
  142. ->method('getTime')
  143. ->willReturn(604800 * 3);
  144. $this->expectException(InvalidTokenException::class);
  145. $this->expectExceptionCode(InvalidTokenException::TOKEN_EXPIRED);
  146. $this->token->check('encryptedToken', $user, 'fingerprintToken', 'foobar');
  147. }
  148. public function testTokenExpiredByLogin(): void {
  149. $user = $this->createMock(IUser::class);
  150. $user->expects($this->atLeastOnce())
  151. ->method('isEnabled')
  152. ->willReturn(true);
  153. $user->expects($this->atLeastOnce())
  154. ->method('getUID')
  155. ->willReturn('alice');
  156. $user->expects($this->any())
  157. ->method('getLastLogin')
  158. ->willReturn(604803);
  159. $this->config->expects($this->atLeastOnce())
  160. ->method('getUserValue')
  161. ->with('alice', 'core', 'fingerprintToken', null)
  162. ->willReturn('encryptedToken');
  163. $this->config->expects($this->any())
  164. ->method('getSystemValueString')
  165. ->with('secret')
  166. ->willReturn('357111317');
  167. $this->crypto->method('decrypt')
  168. ->with('encryptedToken', 'foobar' . '357111317')
  169. ->willReturn('604800:mY70K3n');
  170. $this->timeFactory->expects($this->any())
  171. ->method('getTime')
  172. ->willReturn(604801);
  173. $this->expectException(InvalidTokenException::class);
  174. $this->expectExceptionCode(InvalidTokenException::TOKEN_EXPIRED);
  175. $this->token->check('encryptedToken', $user, 'fingerprintToken', 'foobar', true);
  176. }
  177. public function testTokenMismatch(): void {
  178. $user = $this->createMock(IUser::class);
  179. $user->expects($this->atLeastOnce())
  180. ->method('isEnabled')
  181. ->willReturn(true);
  182. $user->expects($this->atLeastOnce())
  183. ->method('getUID')
  184. ->willReturn('alice');
  185. $user->expects($this->any())
  186. ->method('getLastLogin')
  187. ->willReturn(604703);
  188. $this->config->expects($this->atLeastOnce())
  189. ->method('getUserValue')
  190. ->with('alice', 'core', 'fingerprintToken', null)
  191. ->willReturn('encryptedToken');
  192. $this->config->expects($this->any())
  193. ->method('getSystemValueString')
  194. ->with('secret')
  195. ->willReturn('357111317');
  196. $this->crypto->method('decrypt')
  197. ->with('encryptedToken', 'foobar' . '357111317')
  198. ->willReturn('604802:mY70K3n');
  199. $this->timeFactory->expects($this->any())
  200. ->method('getTime')
  201. ->willReturn(604801);
  202. $this->expectException(InvalidTokenException::class);
  203. $this->expectExceptionCode(InvalidTokenException::TOKEN_MISMATCH);
  204. $this->token->check('encryptedToken', $user, 'fingerprintToken', 'foobar');
  205. }
  206. public function testTokenSuccess(): void {
  207. $user = $this->createMock(IUser::class);
  208. $user->expects($this->atLeastOnce())
  209. ->method('isEnabled')
  210. ->willReturn(true);
  211. $user->expects($this->atLeastOnce())
  212. ->method('getUID')
  213. ->willReturn('alice');
  214. $user->expects($this->any())
  215. ->method('getLastLogin')
  216. ->willReturn(604703);
  217. $this->config->expects($this->atLeastOnce())
  218. ->method('getUserValue')
  219. ->with('alice', 'core', 'fingerprintToken', null)
  220. ->willReturn('encryptedToken');
  221. $this->config->expects($this->any())
  222. ->method('getSystemValueString')
  223. ->with('secret')
  224. ->willReturn('357111317');
  225. $this->crypto->method('decrypt')
  226. ->with('encryptedToken', 'foobar' . '357111317')
  227. ->willReturn('604802:barfoo');
  228. $this->timeFactory->expects($this->any())
  229. ->method('getTime')
  230. ->willReturn(604801);
  231. $this->token->check('barfoo', $user, 'fingerprintToken', 'foobar');
  232. }
  233. public function testCreate(): void {
  234. $user = $this->createMock(IUser::class);
  235. $user->expects($this->any())
  236. ->method('getUID')
  237. ->willReturn('alice');
  238. $this->secureRandom->expects($this->atLeastOnce())
  239. ->method('generate')
  240. ->willReturn('barfoo');
  241. $this->crypto->expects($this->atLeastOnce())
  242. ->method('encrypt')
  243. ->willReturn('encryptedToken');
  244. $this->config->expects($this->atLeastOnce())
  245. ->method('setUserValue')
  246. ->with('alice', 'core', 'fingerprintToken', 'encryptedToken');
  247. $vToken = $this->token->create($user, 'fingerprintToken', 'foobar');
  248. $this->assertSame('barfoo', $vToken);
  249. }
  250. }