DefaultTokenMapper.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@owncloud.com>
  6. *
  7. * @license AGPL-3.0
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. */
  22. namespace OC\Authentication\Token;
  23. use OCP\AppFramework\Db\DoesNotExistException;
  24. use OCP\AppFramework\Db\Mapper;
  25. use OCP\DB\QueryBuilder\IQueryBuilder;
  26. use OCP\IDBConnection;
  27. use OCP\IUser;
  28. class DefaultTokenMapper extends Mapper {
  29. public function __construct(IDBConnection $db) {
  30. parent::__construct($db, 'authtoken');
  31. }
  32. /**
  33. * Invalidate (delete) a given token
  34. *
  35. * @param string $token
  36. */
  37. public function invalidate($token) {
  38. /* @var $qb IQueryBuilder */
  39. $qb = $this->db->getQueryBuilder();
  40. $qb->delete('authtoken')
  41. ->where($qb->expr()->eq('token', $qb->createParameter('token')))
  42. ->setParameter('token', $token)
  43. ->execute();
  44. }
  45. /**
  46. * @param int $olderThan
  47. * @param int $remember
  48. */
  49. public function invalidateOld($olderThan, $remember = IToken::DO_NOT_REMEMBER) {
  50. /* @var $qb IQueryBuilder */
  51. $qb = $this->db->getQueryBuilder();
  52. $qb->delete('authtoken')
  53. ->where($qb->expr()->lt('last_activity', $qb->createNamedParameter($olderThan, IQueryBuilder::PARAM_INT)))
  54. ->andWhere($qb->expr()->eq('type', $qb->createNamedParameter(IToken::TEMPORARY_TOKEN, IQueryBuilder::PARAM_INT)))
  55. ->andWhere($qb->expr()->eq('remember', $qb->createNamedParameter($remember, IQueryBuilder::PARAM_INT)))
  56. ->execute();
  57. }
  58. /**
  59. * Get the user UID for the given token
  60. *
  61. * @param string $token
  62. * @throws DoesNotExistException
  63. * @return DefaultToken
  64. */
  65. public function getToken($token) {
  66. /* @var $qb IQueryBuilder */
  67. $qb = $this->db->getQueryBuilder();
  68. $result = $qb->select('id', 'uid', 'login_name', 'password', 'name', 'type', 'remember', 'token', 'last_activity', 'last_check', 'scope')
  69. ->from('authtoken')
  70. ->where($qb->expr()->eq('token', $qb->createNamedParameter($token)))
  71. ->execute();
  72. $data = $result->fetch();
  73. $result->closeCursor();
  74. if ($data === false) {
  75. throw new DoesNotExistException('token does not exist');
  76. }
  77. ;
  78. return DefaultToken::fromRow($data);
  79. }
  80. /**
  81. * Get the token for $id
  82. *
  83. * @param string $id
  84. * @throws DoesNotExistException
  85. * @return DefaultToken
  86. */
  87. public function getTokenById($id) {
  88. /* @var $qb IQueryBuilder */
  89. $qb = $this->db->getQueryBuilder();
  90. $result = $qb->select('id', 'uid', 'login_name', 'password', 'name', 'type', 'token', 'last_activity', 'last_check', 'scope')
  91. ->from('authtoken')
  92. ->where($qb->expr()->eq('id', $qb->createNamedParameter($id)))
  93. ->execute();
  94. $data = $result->fetch();
  95. $result->closeCursor();
  96. if ($data === false) {
  97. throw new DoesNotExistException('token does not exist');
  98. };
  99. return DefaultToken::fromRow($data);
  100. }
  101. /**
  102. * Get all token 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 IUser $user
  108. * @return DefaultToken[]
  109. */
  110. public function getTokenByUser(IUser $user) {
  111. /* @var $qb IQueryBuilder */
  112. $qb = $this->db->getQueryBuilder();
  113. $qb->select('id', 'uid', 'login_name', 'password', 'name', 'type', 'remember', 'token', 'last_activity', 'last_check', 'scope')
  114. ->from('authtoken')
  115. ->where($qb->expr()->eq('uid', $qb->createNamedParameter($user->getUID())))
  116. ->setMaxResults(1000);
  117. $result = $qb->execute();
  118. $data = $result->fetchAll();
  119. $result->closeCursor();
  120. $entities = array_map(function ($row) {
  121. return DefaultToken::fromRow($row);
  122. }, $data);
  123. return $entities;
  124. }
  125. /**
  126. * @param IUser $user
  127. * @param int $id
  128. */
  129. public function deleteById(IUser $user, $id) {
  130. /* @var $qb IQueryBuilder */
  131. $qb = $this->db->getQueryBuilder();
  132. $qb->delete('authtoken')
  133. ->where($qb->expr()->eq('id', $qb->createNamedParameter($id)))
  134. ->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($user->getUID())));
  135. $qb->execute();
  136. }
  137. }