DefaultTokenMapper.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, ownCloud, Inc.
  5. *
  6. * @author Bjoern Schiessle <bjoern@schiessle.org>
  7. * @author Christoph Wurst <christoph@owncloud.com>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author Lukas Reschke <lukas@statuscode.ch>
  10. * @author Marcel Waldvogel <marcel.waldvogel@uni-konstanz.de>
  11. * @author Robin Appelman <robin@icewind.nl>
  12. * @author Roeland Jago Douma <roeland@famdouma.nl>
  13. *
  14. * @license AGPL-3.0
  15. *
  16. * This code is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License, version 3,
  18. * as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License, version 3,
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>
  27. *
  28. */
  29. namespace OC\Authentication\Token;
  30. use OCP\AppFramework\Db\DoesNotExistException;
  31. use OCP\AppFramework\Db\QBMapper;
  32. use OCP\DB\QueryBuilder\IQueryBuilder;
  33. use OCP\IDBConnection;
  34. class DefaultTokenMapper extends QBMapper {
  35. public function __construct(IDBConnection $db) {
  36. parent::__construct($db, 'authtoken');
  37. }
  38. /**
  39. * Invalidate (delete) a given token
  40. *
  41. * @param string $token
  42. */
  43. public function invalidate(string $token) {
  44. /* @var $qb IQueryBuilder */
  45. $qb = $this->db->getQueryBuilder();
  46. $qb->delete('authtoken')
  47. ->where($qb->expr()->eq('token', $qb->createNamedParameter($token, IQueryBuilder::PARAM_STR)))
  48. ->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(DefaultToken::VERSION, IQueryBuilder::PARAM_INT)))
  49. ->execute();
  50. }
  51. /**
  52. * @param int $olderThan
  53. * @param int $remember
  54. */
  55. public function invalidateOld(int $olderThan, int $remember = IToken::DO_NOT_REMEMBER) {
  56. /* @var $qb IQueryBuilder */
  57. $qb = $this->db->getQueryBuilder();
  58. $qb->delete('authtoken')
  59. ->where($qb->expr()->lt('last_activity', $qb->createNamedParameter($olderThan, IQueryBuilder::PARAM_INT)))
  60. ->andWhere($qb->expr()->eq('type', $qb->createNamedParameter(IToken::TEMPORARY_TOKEN, IQueryBuilder::PARAM_INT)))
  61. ->andWhere($qb->expr()->eq('remember', $qb->createNamedParameter($remember, IQueryBuilder::PARAM_INT)))
  62. ->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(DefaultToken::VERSION, IQueryBuilder::PARAM_INT)))
  63. ->execute();
  64. }
  65. /**
  66. * Get the user UID for the given token
  67. *
  68. * @param string $token
  69. * @throws DoesNotExistException
  70. * @return DefaultToken
  71. */
  72. public function getToken(string $token): DefaultToken {
  73. /* @var $qb IQueryBuilder */
  74. $qb = $this->db->getQueryBuilder();
  75. $result = $qb->select('id', 'uid', 'login_name', 'password', 'name', 'token', 'type', 'remember', 'last_activity', 'last_check', 'scope', 'expires', 'version')
  76. ->from('authtoken')
  77. ->where($qb->expr()->eq('token', $qb->createNamedParameter($token)))
  78. ->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(DefaultToken::VERSION, IQueryBuilder::PARAM_INT)))
  79. ->execute();
  80. $data = $result->fetch();
  81. $result->closeCursor();
  82. if ($data === false) {
  83. throw new DoesNotExistException('token does not exist');
  84. }
  85. return DefaultToken::fromRow($data);
  86. }
  87. /**
  88. * Get the token for $id
  89. *
  90. * @param int $id
  91. * @throws DoesNotExistException
  92. * @return DefaultToken
  93. */
  94. public function getTokenById(int $id): DefaultToken {
  95. /* @var $qb IQueryBuilder */
  96. $qb = $this->db->getQueryBuilder();
  97. $result = $qb->select('id', 'uid', 'login_name', 'password', 'name', 'token', 'type', 'remember', 'last_activity', 'last_check', 'scope', 'expires', 'version')
  98. ->from('authtoken')
  99. ->where($qb->expr()->eq('id', $qb->createNamedParameter($id)))
  100. ->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(DefaultToken::VERSION, IQueryBuilder::PARAM_INT)))
  101. ->execute();
  102. $data = $result->fetch();
  103. $result->closeCursor();
  104. if ($data === false) {
  105. throw new DoesNotExistException('token does not exist');
  106. }
  107. return DefaultToken::fromRow($data);
  108. }
  109. /**
  110. * Get all tokens of a user
  111. *
  112. * The provider may limit the number of result rows in case of an abuse
  113. * where a high number of (session) tokens is generated
  114. *
  115. * @param string $uid
  116. * @return DefaultToken[]
  117. */
  118. public function getTokenByUser(string $uid): array {
  119. /* @var $qb IQueryBuilder */
  120. $qb = $this->db->getQueryBuilder();
  121. $qb->select('id', 'uid', 'login_name', 'password', 'name', 'token', 'type', 'remember', 'last_activity', 'last_check', 'scope', 'expires', 'version')
  122. ->from('authtoken')
  123. ->where($qb->expr()->eq('uid', $qb->createNamedParameter($uid)))
  124. ->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(DefaultToken::VERSION, IQueryBuilder::PARAM_INT)))
  125. ->setMaxResults(1000);
  126. $result = $qb->execute();
  127. $data = $result->fetchAll();
  128. $result->closeCursor();
  129. $entities = array_map(function ($row) {
  130. return DefaultToken::fromRow($row);
  131. }, $data);
  132. return $entities;
  133. }
  134. public function deleteById(string $uid, int $id) {
  135. /* @var $qb IQueryBuilder */
  136. $qb = $this->db->getQueryBuilder();
  137. $qb->delete('authtoken')
  138. ->where($qb->expr()->eq('id', $qb->createNamedParameter($id)))
  139. ->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($uid)))
  140. ->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(DefaultToken::VERSION, IQueryBuilder::PARAM_INT)));
  141. $qb->execute();
  142. }
  143. /**
  144. * delete all auth token which belong to a specific client if the client was deleted
  145. *
  146. * @param string $name
  147. */
  148. public function deleteByName(string $name) {
  149. $qb = $this->db->getQueryBuilder();
  150. $qb->delete('authtoken')
  151. ->where($qb->expr()->eq('name', $qb->createNamedParameter($name), IQueryBuilder::PARAM_STR))
  152. ->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(DefaultToken::VERSION, IQueryBuilder::PARAM_INT)));
  153. $qb->execute();
  154. }
  155. }