LoginFlowV2Mapper.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. /** @var ITimeFactory */
  35. private $timeFactory;
  36. public function __construct(IDBConnection $db, ITimeFactory $timeFactory) {
  37. parent::__construct($db, 'login_flow_v2', LoginFlowV2::class);
  38. $this->timeFactory = $timeFactory;
  39. }
  40. /**
  41. * @param string $pollToken
  42. * @return LoginFlowV2
  43. * @throws DoesNotExistException
  44. */
  45. public function getByPollToken(string $pollToken): LoginFlowV2 {
  46. $qb = $this->db->getQueryBuilder();
  47. $qb->select('*')
  48. ->from($this->getTableName())
  49. ->where(
  50. $qb->expr()->eq('poll_token', $qb->createNamedParameter($pollToken))
  51. );
  52. $entity = $this->findEntity($qb);
  53. return $this->validateTimestamp($entity);
  54. }
  55. /**
  56. * @param string $loginToken
  57. * @return LoginFlowV2
  58. * @throws DoesNotExistException
  59. */
  60. public function getByLoginToken(string $loginToken): LoginFlowV2 {
  61. $qb = $this->db->getQueryBuilder();
  62. $qb->select('*')
  63. ->from($this->getTableName())
  64. ->where(
  65. $qb->expr()->eq('login_token', $qb->createNamedParameter($loginToken))
  66. );
  67. $entity = $this->findEntity($qb);
  68. return $this->validateTimestamp($entity);
  69. }
  70. public function cleanup(): void {
  71. $qb = $this->db->getQueryBuilder();
  72. $qb->delete($this->getTableName())
  73. ->where(
  74. $qb->expr()->lt('timestamp', $qb->createNamedParameter($this->timeFactory->getTime() - self::lifetime))
  75. );
  76. $qb->execute();
  77. }
  78. /**
  79. * @param LoginFlowV2 $flowV2
  80. * @return LoginFlowV2
  81. * @throws DoesNotExistException
  82. */
  83. private function validateTimestamp(LoginFlowV2 $flowV2): LoginFlowV2 {
  84. if ($flowV2->getTimestamp() < ($this->timeFactory->getTime() - self::lifetime)) {
  85. $this->delete($flowV2);
  86. throw new DoesNotExistException('Token expired');
  87. }
  88. return $flowV2;
  89. }
  90. }