LoginFlowV2Mapper.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2019, Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OC\Core\Db;
  25. use OCP\AppFramework\Db\DoesNotExistException;
  26. use OCP\AppFramework\Db\QBMapper;
  27. use OCP\AppFramework\Utility\ITimeFactory;
  28. use OCP\IDBConnection;
  29. /**
  30. * @template-extends QBMapper<LoginFlowV2>
  31. */
  32. class LoginFlowV2Mapper extends QBMapper {
  33. private const lifetime = 1200;
  34. public function __construct(
  35. IDBConnection $db,
  36. private ITimeFactory $timeFactory,
  37. ) {
  38. parent::__construct(
  39. $db,
  40. 'login_flow_v2',
  41. LoginFlowV2::class,
  42. );
  43. }
  44. /**
  45. * @param string $pollToken
  46. * @return LoginFlowV2
  47. * @throws DoesNotExistException
  48. */
  49. public function getByPollToken(string $pollToken): LoginFlowV2 {
  50. $qb = $this->db->getQueryBuilder();
  51. $qb->select('*')
  52. ->from($this->getTableName())
  53. ->where(
  54. $qb->expr()->eq('poll_token', $qb->createNamedParameter($pollToken))
  55. );
  56. $entity = $this->findEntity($qb);
  57. return $this->validateTimestamp($entity);
  58. }
  59. /**
  60. * @param string $loginToken
  61. * @return LoginFlowV2
  62. * @throws DoesNotExistException
  63. */
  64. public function getByLoginToken(string $loginToken): LoginFlowV2 {
  65. $qb = $this->db->getQueryBuilder();
  66. $qb->select('*')
  67. ->from($this->getTableName())
  68. ->where(
  69. $qb->expr()->eq('login_token', $qb->createNamedParameter($loginToken))
  70. );
  71. $entity = $this->findEntity($qb);
  72. return $this->validateTimestamp($entity);
  73. }
  74. public function cleanup(): void {
  75. $qb = $this->db->getQueryBuilder();
  76. $qb->delete($this->getTableName())
  77. ->where(
  78. $qb->expr()->lt('timestamp', $qb->createNamedParameter($this->timeFactory->getTime() - self::lifetime))
  79. );
  80. $qb->execute();
  81. }
  82. /**
  83. * @param LoginFlowV2 $flowV2
  84. * @return LoginFlowV2
  85. * @throws DoesNotExistException
  86. */
  87. private function validateTimestamp(LoginFlowV2 $flowV2): LoginFlowV2 {
  88. if ($flowV2->getTimestamp() < ($this->timeFactory->getTime() - self::lifetime)) {
  89. $this->delete($flowV2);
  90. throw new DoesNotExistException('Token expired');
  91. }
  92. return $flowV2;
  93. }
  94. }