You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

170 lines
5.9 KiB

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