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.

190 lines
6.3 KiB

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