VerificationTokenTest.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2021 Arthur Schiwon <blizzz@arthur-schiwon.de>
  5. *
  6. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace Test\Security\VerificationToken;
  25. use OC\Security\VerificationToken\VerificationToken;
  26. use OCP\AppFramework\Utility\ITimeFactory;
  27. use OCP\BackgroundJob\IJobList;
  28. use OCP\IConfig;
  29. use OCP\IUser;
  30. use OCP\Security\ICrypto;
  31. use OCP\Security\ISecureRandom;
  32. use OCP\Security\VerificationToken\InvalidTokenException;
  33. use Test\TestCase;
  34. class VerificationTokenTest extends TestCase {
  35. /** @var VerificationToken */
  36. protected $token;
  37. /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
  38. protected $config;
  39. /** @var ISecureRandom|\PHPUnit\Framework\MockObject\MockObject */
  40. protected $secureRandom;
  41. /** @var ICrypto|\PHPUnit\Framework\MockObject\MockObject */
  42. protected $crypto;
  43. /** @var ITimeFactory|\PHPUnit\Framework\MockObject\MockObject */
  44. protected $timeFactory;
  45. protected function setUp(): void {
  46. parent::setUp();
  47. $this->config = $this->createMock(IConfig::class);
  48. $this->crypto = $this->createMock(ICrypto::class);
  49. $this->timeFactory = $this->createMock(ITimeFactory::class);
  50. $this->secureRandom = $this->createMock(ISecureRandom::class);
  51. $this->jobList = $this->createMock(IJobList::class);
  52. $this->token = new VerificationToken(
  53. $this->config,
  54. $this->crypto,
  55. $this->timeFactory,
  56. $this->secureRandom,
  57. $this->jobList
  58. );
  59. }
  60. public function testTokenUserUnknown() {
  61. $this->expectException(InvalidTokenException::class);
  62. $this->expectExceptionCode(InvalidTokenException::USER_UNKNOWN);
  63. $this->token->check('encryptedToken', null, 'fingerprintToken', 'foobar');
  64. }
  65. public function testTokenUserUnknown2() {
  66. $user = $this->createMock(IUser::class);
  67. $user->expects($this->atLeastOnce())
  68. ->method('isEnabled')
  69. ->willReturn(false);
  70. $this->expectException(InvalidTokenException::class);
  71. $this->expectExceptionCode(InvalidTokenException::USER_UNKNOWN);
  72. $this->token->check('encryptedToken', $user, 'fingerprintToken', 'foobar');
  73. }
  74. public function testTokenNotFound() {
  75. $user = $this->createMock(IUser::class);
  76. $user->expects($this->atLeastOnce())
  77. ->method('isEnabled')
  78. ->willReturn(true);
  79. $user->expects($this->atLeastOnce())
  80. ->method('getUID')
  81. ->willReturn('alice');
  82. // implicit: IConfig::getUserValue returns null by default
  83. $this->expectException(InvalidTokenException::class);
  84. $this->expectExceptionCode(InvalidTokenException::TOKEN_NOT_FOUND);
  85. $this->token->check('encryptedToken', $user, 'fingerprintToken', 'foobar');
  86. }
  87. public function testTokenDecryptionError() {
  88. $user = $this->createMock(IUser::class);
  89. $user->expects($this->atLeastOnce())
  90. ->method('isEnabled')
  91. ->willReturn(true);
  92. $user->expects($this->atLeastOnce())
  93. ->method('getUID')
  94. ->willReturn('alice');
  95. $this->config->expects($this->atLeastOnce())
  96. ->method('getUserValue')
  97. ->with('alice', 'core', 'fingerprintToken', null)
  98. ->willReturn('encryptedToken');
  99. $this->config->expects($this->any())
  100. ->method('getSystemValue')
  101. ->with('secret')
  102. ->willReturn('357111317');
  103. $this->crypto->method('decrypt')
  104. ->with('encryptedToken', 'foobar' . '357111317')
  105. ->willThrowException(new \Exception('decryption failed'));
  106. $this->expectException(InvalidTokenException::class);
  107. $this->expectExceptionCode(InvalidTokenException::TOKEN_DECRYPTION_ERROR);
  108. $this->token->check('encryptedToken', $user, 'fingerprintToken', 'foobar');
  109. }
  110. public function testTokenInvalidFormat() {
  111. $user = $this->createMock(IUser::class);
  112. $user->expects($this->atLeastOnce())
  113. ->method('isEnabled')
  114. ->willReturn(true);
  115. $user->expects($this->atLeastOnce())
  116. ->method('getUID')
  117. ->willReturn('alice');
  118. $this->config->expects($this->atLeastOnce())
  119. ->method('getUserValue')
  120. ->with('alice', 'core', 'fingerprintToken', null)
  121. ->willReturn('encryptedToken');
  122. $this->config->expects($this->any())
  123. ->method('getSystemValue')
  124. ->with('secret')
  125. ->willReturn('357111317');
  126. $this->crypto->method('decrypt')
  127. ->with('encryptedToken', 'foobar' . '357111317')
  128. ->willReturn('decrypted^nonsense');
  129. $this->expectException(InvalidTokenException::class);
  130. $this->expectExceptionCode(InvalidTokenException::TOKEN_INVALID_FORMAT);
  131. $this->token->check('encryptedToken', $user, 'fingerprintToken', 'foobar');
  132. }
  133. public function testTokenExpired() {
  134. $user = $this->createMock(IUser::class);
  135. $user->expects($this->atLeastOnce())
  136. ->method('isEnabled')
  137. ->willReturn(true);
  138. $user->expects($this->atLeastOnce())
  139. ->method('getUID')
  140. ->willReturn('alice');
  141. $user->expects($this->any())
  142. ->method('getLastLogin')
  143. ->willReturn(604803);
  144. $this->config->expects($this->atLeastOnce())
  145. ->method('getUserValue')
  146. ->with('alice', 'core', 'fingerprintToken', null)
  147. ->willReturn('encryptedToken');
  148. $this->config->expects($this->any())
  149. ->method('getSystemValue')
  150. ->with('secret')
  151. ->willReturn('357111317');
  152. $this->crypto->method('decrypt')
  153. ->with('encryptedToken', 'foobar' . '357111317')
  154. ->willReturn('604800:mY70K3n');
  155. $this->timeFactory->expects($this->any())
  156. ->method('getTime')
  157. ->willReturn(604800 * 3);
  158. $this->expectException(InvalidTokenException::class);
  159. $this->expectExceptionCode(InvalidTokenException::TOKEN_EXPIRED);
  160. $this->token->check('encryptedToken', $user, 'fingerprintToken', 'foobar');
  161. }
  162. public function testTokenExpiredByLogin() {
  163. $user = $this->createMock(IUser::class);
  164. $user->expects($this->atLeastOnce())
  165. ->method('isEnabled')
  166. ->willReturn(true);
  167. $user->expects($this->atLeastOnce())
  168. ->method('getUID')
  169. ->willReturn('alice');
  170. $user->expects($this->any())
  171. ->method('getLastLogin')
  172. ->willReturn(604803);
  173. $this->config->expects($this->atLeastOnce())
  174. ->method('getUserValue')
  175. ->with('alice', 'core', 'fingerprintToken', null)
  176. ->willReturn('encryptedToken');
  177. $this->config->expects($this->any())
  178. ->method('getSystemValue')
  179. ->with('secret')
  180. ->willReturn('357111317');
  181. $this->crypto->method('decrypt')
  182. ->with('encryptedToken', 'foobar' . '357111317')
  183. ->willReturn('604800:mY70K3n');
  184. $this->timeFactory->expects($this->any())
  185. ->method('getTime')
  186. ->willReturn(604801);
  187. $this->expectException(InvalidTokenException::class);
  188. $this->expectExceptionCode(InvalidTokenException::TOKEN_EXPIRED);
  189. $this->token->check('encryptedToken', $user, 'fingerprintToken', 'foobar', true);
  190. }
  191. public function testTokenMismatch() {
  192. $user = $this->createMock(IUser::class);
  193. $user->expects($this->atLeastOnce())
  194. ->method('isEnabled')
  195. ->willReturn(true);
  196. $user->expects($this->atLeastOnce())
  197. ->method('getUID')
  198. ->willReturn('alice');
  199. $user->expects($this->any())
  200. ->method('getLastLogin')
  201. ->willReturn(604703);
  202. $this->config->expects($this->atLeastOnce())
  203. ->method('getUserValue')
  204. ->with('alice', 'core', 'fingerprintToken', null)
  205. ->willReturn('encryptedToken');
  206. $this->config->expects($this->any())
  207. ->method('getSystemValue')
  208. ->with('secret')
  209. ->willReturn('357111317');
  210. $this->crypto->method('decrypt')
  211. ->with('encryptedToken', 'foobar' . '357111317')
  212. ->willReturn('604802:mY70K3n');
  213. $this->timeFactory->expects($this->any())
  214. ->method('getTime')
  215. ->willReturn(604801);
  216. $this->expectException(InvalidTokenException::class);
  217. $this->expectExceptionCode(InvalidTokenException::TOKEN_MISMATCH);
  218. $this->token->check('encryptedToken', $user, 'fingerprintToken', 'foobar');
  219. }
  220. public function testTokenSuccess() {
  221. $user = $this->createMock(IUser::class);
  222. $user->expects($this->atLeastOnce())
  223. ->method('isEnabled')
  224. ->willReturn(true);
  225. $user->expects($this->atLeastOnce())
  226. ->method('getUID')
  227. ->willReturn('alice');
  228. $user->expects($this->any())
  229. ->method('getLastLogin')
  230. ->willReturn(604703);
  231. $this->config->expects($this->atLeastOnce())
  232. ->method('getUserValue')
  233. ->with('alice', 'core', 'fingerprintToken', null)
  234. ->willReturn('encryptedToken');
  235. $this->config->expects($this->any())
  236. ->method('getSystemValue')
  237. ->with('secret')
  238. ->willReturn('357111317');
  239. $this->crypto->method('decrypt')
  240. ->with('encryptedToken', 'foobar' . '357111317')
  241. ->willReturn('604802:barfoo');
  242. $this->timeFactory->expects($this->any())
  243. ->method('getTime')
  244. ->willReturn(604801);
  245. $this->token->check('barfoo', $user, 'fingerprintToken', 'foobar');
  246. }
  247. public function testCreate() {
  248. $user = $this->createMock(IUser::class);
  249. $user->expects($this->any())
  250. ->method('getUID')
  251. ->willReturn('alice');
  252. $this->secureRandom->expects($this->atLeastOnce())
  253. ->method('generate')
  254. ->willReturn('barfoo');
  255. $this->crypto->expects($this->atLeastOnce())
  256. ->method('encrypt')
  257. ->willReturn('encryptedToken');
  258. $this->config->expects($this->atLeastOnce())
  259. ->method('setUserValue')
  260. ->with('alice', 'core', 'fingerprintToken', 'encryptedToken');
  261. $vToken = $this->token->create($user, 'fingerprintToken', 'foobar');
  262. $this->assertSame('barfoo', $vToken);
  263. }
  264. }