PublicKeyTokenMapperTest.php 12 KB

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