DefaultTokenMapperTest.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. /**
  3. * @author Christoph Wurst <christoph@owncloud.com>
  4. *
  5. * @copyright Copyright (c) 2016, ownCloud, Inc.
  6. * @license AGPL-3.0
  7. *
  8. * This code is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License, version 3,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License, version 3,
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>
  19. *
  20. */
  21. namespace Test\Authentication\Token;
  22. use OC;
  23. use OC\Authentication\Token\DefaultToken;
  24. use OC\Authentication\Token\DefaultTokenMapper;
  25. use OC\Authentication\Token\IToken;
  26. use OCP\DB\QueryBuilder\IQueryBuilder;
  27. use OCP\IDBConnection;
  28. use OCP\IUser;
  29. use Test\TestCase;
  30. /**
  31. * Class DefaultTokenMapperTest
  32. *
  33. * @group DB
  34. * @package Test\Authentication
  35. */
  36. class DefaultTokenMapperTest extends TestCase {
  37. /** @var DefaultTokenMapper */
  38. private $mapper;
  39. /** @var IDBConnection */
  40. private $dbConnection;
  41. private $time;
  42. protected function setUp() {
  43. parent::setUp();
  44. $this->dbConnection = OC::$server->getDatabaseConnection();
  45. $this->time = time();
  46. $this->resetDatabase();
  47. $this->mapper = new DefaultTokenMapper($this->dbConnection);
  48. }
  49. private function resetDatabase() {
  50. $qb = $this->dbConnection->getQueryBuilder();
  51. $qb->delete('authtoken')->execute();
  52. $qb->insert('authtoken')->values([
  53. 'uid' => $qb->createNamedParameter('user1'),
  54. 'login_name' => $qb->createNamedParameter('User1'),
  55. 'password' => $qb->createNamedParameter('a75c7116460c082912d8f6860a850904|3nz5qbG1nNSLLi6V|c55365a0e54cfdfac4a175bcf11a7612aea74492277bba6e5d96a24497fa9272488787cb2f3ad34d8b9b8060934fce02f008d371df3ff3848f4aa61944851ff0'),
  56. 'name' => $qb->createNamedParameter('Firefox on Linux'),
  57. 'token' => $qb->createNamedParameter('9c5a2e661482b65597408a6bb6c4a3d1af36337381872ac56e445a06cdb7fea2b1039db707545c11027a4966919918b19d875a8b774840b18c6cbb7ae56fe206'),
  58. 'type' => $qb->createNamedParameter(IToken::TEMPORARY_TOKEN),
  59. 'last_activity' => $qb->createNamedParameter($this->time - 120, IQueryBuilder::PARAM_INT), // Two minutes ago
  60. 'last_check' => $this->time - 60 * 10, // 10mins ago
  61. ])->execute();
  62. $qb->insert('authtoken')->values([
  63. 'uid' => $qb->createNamedParameter('user2'),
  64. 'login_name' => $qb->createNamedParameter('User2'),
  65. 'password' => $qb->createNamedParameter('971a337057853344700bbeccf836519f|UwOQwyb34sJHtqPV|036d4890f8c21d17bbc7b88072d8ef049a5c832a38e97f3e3d5f9186e896c2593aee16883f617322fa242728d0236ff32d163caeb4bd45e14ca002c57a88665f'),
  66. 'name' => $qb->createNamedParameter('Firefox on Android'),
  67. 'token' => $qb->createNamedParameter('1504445f1524fc801035448a95681a9378ba2e83930c814546c56e5d6ebde221198792fd900c88ed5ead0555780dad1ebce3370d7e154941cd5de87eb419899b'),
  68. 'type' => $qb->createNamedParameter(IToken::TEMPORARY_TOKEN),
  69. 'last_activity' => $qb->createNamedParameter($this->time - 60 * 60 * 24 * 3, IQueryBuilder::PARAM_INT), // Three days ago
  70. 'last_check' => $this->time - 10, // 10secs ago
  71. ])->execute();
  72. $qb->insert('authtoken')->values([
  73. 'uid' => $qb->createNamedParameter('user1'),
  74. 'login_name' => $qb->createNamedParameter('User1'),
  75. 'password' => $qb->createNamedParameter('063de945d6f6b26862d9b6f40652f2d5|DZ/z520tfdXPtd0T|395f6b89be8d9d605e409e20b9d9abe477fde1be38a3223f9e508f979bf906e50d9eaa4dca983ca4fb22a241eb696c3f98654e7775f78c4caf13108f98642b53'),
  76. 'name' => $qb->createNamedParameter('Iceweasel on Linux'),
  77. 'token' => $qb->createNamedParameter('47af8697ba590fb82579b5f1b3b6e8066773a62100abbe0db09a289a62f5d980dc300fa3d98b01d7228468d1ab05c1aa14c8d14bd5b6eee9cdf1ac14864680c3'),
  78. 'type' => $qb->createNamedParameter(IToken::TEMPORARY_TOKEN),
  79. 'last_activity' => $qb->createNamedParameter($this->time - 120, IQueryBuilder::PARAM_INT), // Two minutes ago
  80. 'last_check' => $this->time - 60 * 10, // 10mins ago
  81. ])->execute();
  82. }
  83. private function getNumberOfTokens() {
  84. $qb = $this->dbConnection->getQueryBuilder();
  85. $result = $qb->select($qb->func()->count('*', 'count'))
  86. ->from('authtoken')
  87. ->execute()
  88. ->fetch();
  89. return (int) $result['count'];
  90. }
  91. public function testInvalidate() {
  92. $token = '9c5a2e661482b65597408a6bb6c4a3d1af36337381872ac56e445a06cdb7fea2b1039db707545c11027a4966919918b19d875a8b774840b18c6cbb7ae56fe206';
  93. $this->mapper->invalidate($token);
  94. $this->assertSame(2, $this->getNumberOfTokens());
  95. }
  96. public function testInvalidateInvalid() {
  97. $token = 'youwontfindthisoneinthedatabase';
  98. $this->mapper->invalidate($token);
  99. $this->assertSame(3, $this->getNumberOfTokens());
  100. }
  101. public function testInvalidateOld() {
  102. $olderThan = $this->time - 60 * 60; // One hour
  103. $this->mapper->invalidateOld($olderThan);
  104. $this->assertSame(2, $this->getNumberOfTokens());
  105. }
  106. public function testGetToken() {
  107. $token = new DefaultToken();
  108. $token->setUid('user2');
  109. $token->setLoginName('User2');
  110. $token->setPassword('971a337057853344700bbeccf836519f|UwOQwyb34sJHtqPV|036d4890f8c21d17bbc7b88072d8ef049a5c832a38e97f3e3d5f9186e896c2593aee16883f617322fa242728d0236ff32d163caeb4bd45e14ca002c57a88665f');
  111. $token->setName('Firefox on Android');
  112. $token->setToken('1504445f1524fc801035448a95681a9378ba2e83930c814546c56e5d6ebde221198792fd900c88ed5ead0555780dad1ebce3370d7e154941cd5de87eb419899b');
  113. $token->setType(IToken::TEMPORARY_TOKEN);
  114. $token->setRemember(IToken::DO_NOT_REMEMBER);
  115. $token->setLastActivity($this->time - 60 * 60 * 24 * 3);
  116. $token->setLastCheck($this->time - 10);
  117. $token->setVersion(DefaultToken::VERSION);
  118. $dbToken = $this->mapper->getToken($token->getToken());
  119. $token->setId($dbToken->getId()); // We don't know the ID
  120. $token->resetUpdatedFields();
  121. $this->assertEquals($token, $dbToken);
  122. }
  123. /**
  124. * @expectedException \OCP\AppFramework\Db\DoesNotExistException
  125. */
  126. public function testGetInvalidToken() {
  127. $token = 'thisisaninvalidtokenthatisnotinthedatabase';
  128. $this->mapper->getToken($token);
  129. }
  130. public function testGetTokenById() {
  131. $token = new DefaultToken();
  132. $token->setUid('user2');
  133. $token->setLoginName('User2');
  134. $token->setPassword('971a337057853344700bbeccf836519f|UwOQwyb34sJHtqPV|036d4890f8c21d17bbc7b88072d8ef049a5c832a38e97f3e3d5f9186e896c2593aee16883f617322fa242728d0236ff32d163caeb4bd45e14ca002c57a88665f');
  135. $token->setName('Firefox on Android');
  136. $token->setToken('1504445f1524fc801035448a95681a9378ba2e83930c814546c56e5d6ebde221198792fd900c88ed5ead0555780dad1ebce3370d7e154941cd5de87eb419899b');
  137. $token->setType(IToken::TEMPORARY_TOKEN);
  138. $token->setRemember(IToken::DO_NOT_REMEMBER);
  139. $token->setLastActivity($this->time - 60 * 60 * 24 * 3);
  140. $token->setLastCheck($this->time - 10);
  141. $token->setVersion(DefaultToken::VERSION);
  142. $dbToken = $this->mapper->getToken($token->getToken());
  143. $token->setId($dbToken->getId()); // We don't know the ID
  144. $token->resetUpdatedFields();
  145. $dbToken = $this->mapper->getTokenById($token->getId());
  146. $this->assertEquals($token, $dbToken);
  147. }
  148. /**
  149. * @expectedException \OCP\AppFramework\Db\DoesNotExistException
  150. */
  151. public function testGetTokenByIdNotFound() {
  152. $this->mapper->getTokenById(-1);
  153. }
  154. /**
  155. * @expectedException \OCP\AppFramework\Db\DoesNotExistException
  156. */
  157. public function testGetInvalidTokenById() {
  158. $id = 42;
  159. $this->mapper->getToken($id);
  160. }
  161. public function testGetTokenByUser() {
  162. $this->assertCount(2, $this->mapper->getTokenByUser('user1'));
  163. }
  164. public function testGetTokenByUserNotFound() {
  165. $this->assertCount(0, $this->mapper->getTokenByUser('user1000'));
  166. }
  167. public function testDeleteById() {
  168. /** @var IUser|\PHPUnit_Framework_MockObject_MockObject $user */
  169. $user = $this->createMock(IUser::class);
  170. $qb = $this->dbConnection->getQueryBuilder();
  171. $qb->select('id')
  172. ->from('authtoken')
  173. ->where($qb->expr()->eq('token', $qb->createNamedParameter('9c5a2e661482b65597408a6bb6c4a3d1af36337381872ac56e445a06cdb7fea2b1039db707545c11027a4966919918b19d875a8b774840b18c6cbb7ae56fe206')));
  174. $result = $qb->execute();
  175. $id = $result->fetch()['id'];
  176. $this->mapper->deleteById('user1', $id);
  177. $this->assertEquals(2, $this->getNumberOfTokens());
  178. }
  179. public function testDeleteByIdWrongUser() {
  180. $id = 33;
  181. $this->mapper->deleteById('user1000', $id);
  182. $this->assertEquals(3, $this->getNumberOfTokens());
  183. }
  184. public function testDeleteByName() {
  185. $qb = $this->dbConnection->getQueryBuilder();
  186. $qb->select('name')
  187. ->from('authtoken')
  188. ->where($qb->expr()->eq('token', $qb->createNamedParameter('9c5a2e661482b65597408a6bb6c4a3d1af36337381872ac56e445a06cdb7fea2b1039db707545c11027a4966919918b19d875a8b774840b18c6cbb7ae56fe206')));
  189. $result = $qb->execute();
  190. $name = $result->fetch()['name'];
  191. $this->mapper->deleteByName($name);
  192. $this->assertEquals(2, $this->getNumberOfTokens());
  193. }
  194. }