1
0

PublicKeyTokenMapperTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test\Authentication\Token;
  8. use OC;
  9. use OC\Authentication\Token\PublicKeyToken;
  10. use OC\Authentication\Token\PublicKeyTokenMapper;
  11. use OCP\Authentication\Token\IToken;
  12. use OCP\DB\QueryBuilder\IQueryBuilder;
  13. use OCP\IDBConnection;
  14. use OCP\IUser;
  15. use Test\TestCase;
  16. /**
  17. * @group DB
  18. */
  19. class PublicKeyTokenMapperTest extends TestCase {
  20. /** @var PublicKeyTokenMapper */
  21. private $mapper;
  22. /** @var IDBConnection */
  23. private $dbConnection;
  24. /** @var int */
  25. private $time;
  26. protected function setUp(): void {
  27. parent::setUp();
  28. $this->dbConnection = OC::$server->getDatabaseConnection();
  29. $this->time = time();
  30. $this->resetDatabase();
  31. $this->mapper = new PublicKeyTokenMapper($this->dbConnection);
  32. }
  33. private function resetDatabase() {
  34. $qb = $this->dbConnection->getQueryBuilder();
  35. $qb->delete('authtoken')->execute();
  36. $qb->insert('authtoken')->values([
  37. 'uid' => $qb->createNamedParameter('user1'),
  38. 'login_name' => $qb->createNamedParameter('User1'),
  39. 'password' => $qb->createNamedParameter('a75c7116460c082912d8f6860a850904|3nz5qbG1nNSLLi6V|c55365a0e54cfdfac4a175bcf11a7612aea74492277bba6e5d96a24497fa9272488787cb2f3ad34d8b9b8060934fce02f008d371df3ff3848f4aa61944851ff0'),
  40. 'name' => $qb->createNamedParameter('Firefox on Linux'),
  41. 'token' => $qb->createNamedParameter('9c5a2e661482b65597408a6bb6c4a3d1af36337381872ac56e445a06cdb7fea2b1039db707545c11027a4966919918b19d875a8b774840b18c6cbb7ae56fe206'),
  42. 'type' => $qb->createNamedParameter(IToken::TEMPORARY_TOKEN),
  43. 'last_activity' => $qb->createNamedParameter($this->time - 120, IQueryBuilder::PARAM_INT), // Two minutes ago
  44. 'last_check' => $this->time - 60 * 10, // 10mins ago
  45. 'public_key' => $qb->createNamedParameter('public key'),
  46. 'private_key' => $qb->createNamedParameter('private key'),
  47. 'version' => $qb->createNamedParameter(2),
  48. ])->execute();
  49. $qb->insert('authtoken')->values([
  50. 'uid' => $qb->createNamedParameter('user2'),
  51. 'login_name' => $qb->createNamedParameter('User2'),
  52. 'password' => $qb->createNamedParameter('971a337057853344700bbeccf836519f|UwOQwyb34sJHtqPV|036d4890f8c21d17bbc7b88072d8ef049a5c832a38e97f3e3d5f9186e896c2593aee16883f617322fa242728d0236ff32d163caeb4bd45e14ca002c57a88665f'),
  53. 'name' => $qb->createNamedParameter('Firefox on Android'),
  54. 'token' => $qb->createNamedParameter('1504445f1524fc801035448a95681a9378ba2e83930c814546c56e5d6ebde221198792fd900c88ed5ead0555780dad1ebce3370d7e154941cd5de87eb419899b'),
  55. 'type' => $qb->createNamedParameter(IToken::TEMPORARY_TOKEN),
  56. 'last_activity' => $qb->createNamedParameter($this->time - 60 * 60 * 24 * 3, IQueryBuilder::PARAM_INT), // Three days ago
  57. 'last_check' => $this->time - 10, // 10secs ago
  58. 'public_key' => $qb->createNamedParameter('public key'),
  59. 'private_key' => $qb->createNamedParameter('private key'),
  60. 'version' => $qb->createNamedParameter(2),
  61. ])->execute();
  62. $qb->insert('authtoken')->values([
  63. 'uid' => $qb->createNamedParameter('user1'),
  64. 'login_name' => $qb->createNamedParameter('User1'),
  65. 'password' => $qb->createNamedParameter('063de945d6f6b26862d9b6f40652f2d5|DZ/z520tfdXPtd0T|395f6b89be8d9d605e409e20b9d9abe477fde1be38a3223f9e508f979bf906e50d9eaa4dca983ca4fb22a241eb696c3f98654e7775f78c4caf13108f98642b53'),
  66. 'name' => $qb->createNamedParameter('Iceweasel on Linux'),
  67. 'token' => $qb->createNamedParameter('47af8697ba590fb82579b5f1b3b6e8066773a62100abbe0db09a289a62f5d980dc300fa3d98b01d7228468d1ab05c1aa14c8d14bd5b6eee9cdf1ac14864680c3'),
  68. 'type' => $qb->createNamedParameter(IToken::TEMPORARY_TOKEN),
  69. 'last_activity' => $qb->createNamedParameter($this->time - 120, IQueryBuilder::PARAM_INT), // Two minutes ago
  70. 'last_check' => $this->time - 60 * 10, // 10mins ago
  71. 'public_key' => $qb->createNamedParameter('public key'),
  72. 'private_key' => $qb->createNamedParameter('private key'),
  73. 'version' => $qb->createNamedParameter(2),
  74. ])->execute();
  75. $qb->insert('authtoken')->values([
  76. 'uid' => $qb->createNamedParameter('user3'),
  77. 'login_name' => $qb->createNamedParameter('User3'),
  78. 'password' => $qb->createNamedParameter('063de945d6f6b26862d9b6f40652f2d5|DZ/z520tfdXPtd0T|395f6b89be8d9d605e409e20b9d9abe477fde1be38a3223f9e508f979bf906e50d9eaa4dca983ca4fb22a241eb696c3f98654e7775f78c4caf13108f98642b53'),
  79. 'name' => $qb->createNamedParameter('Iceweasel on Linux'),
  80. 'token' => $qb->createNamedParameter('6d9a290d239d09f2cc33a03cc54cccd46f7dc71630dcc27d39214824bd3e093f1feb4e2b55eb159d204caa15dee9556c202a5aa0b9d67806c3f4ec2cde11af67'),
  81. 'type' => $qb->createNamedParameter(IToken::TEMPORARY_TOKEN),
  82. 'last_activity' => $qb->createNamedParameter($this->time - 120, IQueryBuilder::PARAM_INT), // Two minutes ago
  83. 'last_check' => $this->time - 60 * 10, // 10mins ago
  84. 'public_key' => $qb->createNamedParameter('public key'),
  85. 'private_key' => $qb->createNamedParameter('private key'),
  86. 'version' => $qb->createNamedParameter(2),
  87. 'password_invalid' => $qb->createNamedParameter(1),
  88. ])->execute();
  89. $qb->insert('authtoken')->values([
  90. 'uid' => $qb->createNamedParameter('user3'),
  91. 'login_name' => $qb->createNamedParameter('User3'),
  92. 'password' => $qb->createNamedParameter('063de945d6f6b26862d9b6f40652f2d5|DZ/z520tfdXPtd0T|395f6b89be8d9d605e409e20b9d9abe477fde1be38a3223f9e508f979bf906e50d9eaa4dca983ca4fb22a241eb696c3f98654e7775f78c4caf13108f98642b53'),
  93. 'name' => $qb->createNamedParameter('Iceweasel on Linux'),
  94. 'token' => $qb->createNamedParameter('84c5808c6445b6d65b8aa5b03840f09b27de603f0fb970906fb14ea4b115b7bf5ec53fada5c093fe46afdcd7bbc9617253a4d105f7dfb32719f9973d72412f31'),
  95. 'type' => $qb->createNamedParameter(IToken::PERMANENT_TOKEN),
  96. 'last_activity' => $qb->createNamedParameter($this->time - 60 * 3, IQueryBuilder::PARAM_INT), // Three minutes ago
  97. 'last_check' => $this->time - 60 * 10, // 10mins ago
  98. 'public_key' => $qb->createNamedParameter('public key'),
  99. 'private_key' => $qb->createNamedParameter('private key'),
  100. 'version' => $qb->createNamedParameter(2),
  101. 'password_invalid' => $qb->createNamedParameter(1),
  102. ])->execute();
  103. }
  104. private function getNumberOfTokens() {
  105. $qb = $this->dbConnection->getQueryBuilder();
  106. $result = $qb->select($qb->func()->count('*', 'count'))
  107. ->from('authtoken')
  108. ->execute()
  109. ->fetch();
  110. return (int)$result['count'];
  111. }
  112. public function testInvalidate(): void {
  113. $token = '9c5a2e661482b65597408a6bb6c4a3d1af36337381872ac56e445a06cdb7fea2b1039db707545c11027a4966919918b19d875a8b774840b18c6cbb7ae56fe206';
  114. $this->mapper->invalidate($token);
  115. $this->assertSame(4, $this->getNumberOfTokens());
  116. }
  117. public function testInvalidateInvalid(): void {
  118. $token = 'youwontfindthisoneinthedatabase';
  119. $this->mapper->invalidate($token);
  120. $this->assertSame(5, $this->getNumberOfTokens());
  121. }
  122. public function testInvalidateOld(): void {
  123. $olderThan = $this->time - 60 * 60; // One hour
  124. $this->mapper->invalidateOld($olderThan);
  125. $this->assertSame(4, $this->getNumberOfTokens());
  126. }
  127. public function testInvalidateLastUsedBefore(): void {
  128. $before = $this->time - 60 * 2; // Two minutes
  129. $this->mapper->invalidateLastUsedBefore('user3', $before);
  130. $this->assertSame(4, $this->getNumberOfTokens());
  131. }
  132. public function testGetToken(): void {
  133. $token = new PublicKeyToken();
  134. $token->setUid('user2');
  135. $token->setLoginName('User2');
  136. $token->setPassword('971a337057853344700bbeccf836519f|UwOQwyb34sJHtqPV|036d4890f8c21d17bbc7b88072d8ef049a5c832a38e97f3e3d5f9186e896c2593aee16883f617322fa242728d0236ff32d163caeb4bd45e14ca002c57a88665f');
  137. $token->setName('Firefox on Android');
  138. $token->setToken('1504445f1524fc801035448a95681a9378ba2e83930c814546c56e5d6ebde221198792fd900c88ed5ead0555780dad1ebce3370d7e154941cd5de87eb419899b');
  139. $token->setType(IToken::TEMPORARY_TOKEN);
  140. $token->setRemember(IToken::DO_NOT_REMEMBER);
  141. $token->setLastActivity($this->time - 60 * 60 * 24 * 3);
  142. $token->setLastCheck($this->time - 10);
  143. $token->setPublicKey('public key');
  144. $token->setPrivateKey('private key');
  145. $token->setVersion(PublicKeyToken::VERSION);
  146. $dbToken = $this->mapper->getToken($token->getToken());
  147. $token->setId($dbToken->getId()); // We don't know the ID
  148. $token->resetUpdatedFields();
  149. $this->assertEquals($token, $dbToken);
  150. }
  151. public function testGetInvalidToken(): void {
  152. $this->expectException(\OCP\AppFramework\Db\DoesNotExistException::class);
  153. $token = 'thisisaninvalidtokenthatisnotinthedatabase';
  154. $this->mapper->getToken($token);
  155. }
  156. public function testGetTokenById(): void {
  157. $token = new PublicKeyToken();
  158. $token->setUid('user2');
  159. $token->setLoginName('User2');
  160. $token->setPassword('971a337057853344700bbeccf836519f|UwOQwyb34sJHtqPV|036d4890f8c21d17bbc7b88072d8ef049a5c832a38e97f3e3d5f9186e896c2593aee16883f617322fa242728d0236ff32d163caeb4bd45e14ca002c57a88665f');
  161. $token->setName('Firefox on Android');
  162. $token->setToken('1504445f1524fc801035448a95681a9378ba2e83930c814546c56e5d6ebde221198792fd900c88ed5ead0555780dad1ebce3370d7e154941cd5de87eb419899b');
  163. $token->setType(IToken::TEMPORARY_TOKEN);
  164. $token->setRemember(IToken::DO_NOT_REMEMBER);
  165. $token->setLastActivity($this->time - 60 * 60 * 24 * 3);
  166. $token->setLastCheck($this->time - 10);
  167. $token->setPublicKey('public key');
  168. $token->setPrivateKey('private key');
  169. $token->setVersion(PublicKeyToken::VERSION);
  170. $dbToken = $this->mapper->getToken($token->getToken());
  171. $token->setId($dbToken->getId()); // We don't know the ID
  172. $token->resetUpdatedFields();
  173. $dbToken = $this->mapper->getTokenById($token->getId());
  174. $this->assertEquals($token, $dbToken);
  175. }
  176. public function testGetTokenByIdNotFound(): void {
  177. $this->expectException(\OCP\AppFramework\Db\DoesNotExistException::class);
  178. $this->mapper->getTokenById(-1);
  179. }
  180. public function testGetInvalidTokenById(): void {
  181. $this->expectException(\OCP\AppFramework\Db\DoesNotExistException::class);
  182. $id = '42';
  183. $this->mapper->getToken($id);
  184. }
  185. public function testGetTokenByUser(): void {
  186. $this->assertCount(2, $this->mapper->getTokenByUser('user1'));
  187. }
  188. public function testGetTokenByUserNotFound(): void {
  189. $this->assertCount(0, $this->mapper->getTokenByUser('user1000'));
  190. }
  191. public function testGetById(): void {
  192. /** @var IUser|\PHPUnit\Framework\MockObject\MockObject $user */
  193. $user = $this->createMock(IUser::class);
  194. $qb = $this->dbConnection->getQueryBuilder();
  195. $qb->select('id')
  196. ->from('authtoken')
  197. ->where($qb->expr()->eq('token', $qb->createNamedParameter('9c5a2e661482b65597408a6bb6c4a3d1af36337381872ac56e445a06cdb7fea2b1039db707545c11027a4966919918b19d875a8b774840b18c6cbb7ae56fe206')));
  198. $result = $qb->execute();
  199. $id = $result->fetch()['id'];
  200. $token = $this->mapper->getTokenById((int)$id);
  201. $this->assertEquals('user1', $token->getUID());
  202. }
  203. public function testDeleteByName(): void {
  204. $qb = $this->dbConnection->getQueryBuilder();
  205. $qb->select('name')
  206. ->from('authtoken')
  207. ->where($qb->expr()->eq('token', $qb->createNamedParameter('9c5a2e661482b65597408a6bb6c4a3d1af36337381872ac56e445a06cdb7fea2b1039db707545c11027a4966919918b19d875a8b774840b18c6cbb7ae56fe206')));
  208. $result = $qb->execute();
  209. $name = $result->fetch()['name'];
  210. $this->mapper->deleteByName($name);
  211. $this->assertEquals(4, $this->getNumberOfTokens());
  212. }
  213. public function testHasExpiredTokens(): void {
  214. $this->assertFalse($this->mapper->hasExpiredTokens('user1'));
  215. $this->assertTrue($this->mapper->hasExpiredTokens('user3'));
  216. }
  217. }