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.

LoginFlowV2Mapper.php 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Core\Db;
  8. use OCP\AppFramework\Db\DoesNotExistException;
  9. use OCP\AppFramework\Db\QBMapper;
  10. use OCP\AppFramework\Utility\ITimeFactory;
  11. use OCP\IDBConnection;
  12. /**
  13. * @template-extends QBMapper<LoginFlowV2>
  14. */
  15. class LoginFlowV2Mapper extends QBMapper {
  16. private const lifetime = 1200;
  17. public function __construct(
  18. IDBConnection $db,
  19. private ITimeFactory $timeFactory,
  20. ) {
  21. parent::__construct(
  22. $db,
  23. 'login_flow_v2',
  24. LoginFlowV2::class,
  25. );
  26. }
  27. /**
  28. * @param string $pollToken
  29. * @return LoginFlowV2
  30. * @throws DoesNotExistException
  31. */
  32. public function getByPollToken(string $pollToken): LoginFlowV2 {
  33. $qb = $this->db->getQueryBuilder();
  34. $qb->select('*')
  35. ->from($this->getTableName())
  36. ->where(
  37. $qb->expr()->eq('poll_token', $qb->createNamedParameter($pollToken))
  38. );
  39. $entity = $this->findEntity($qb);
  40. return $this->validateTimestamp($entity);
  41. }
  42. /**
  43. * @param string $loginToken
  44. * @return LoginFlowV2
  45. * @throws DoesNotExistException
  46. */
  47. public function getByLoginToken(string $loginToken): LoginFlowV2 {
  48. $qb = $this->db->getQueryBuilder();
  49. $qb->select('*')
  50. ->from($this->getTableName())
  51. ->where(
  52. $qb->expr()->eq('login_token', $qb->createNamedParameter($loginToken))
  53. );
  54. $entity = $this->findEntity($qb);
  55. return $this->validateTimestamp($entity);
  56. }
  57. public function cleanup(): void {
  58. $qb = $this->db->getQueryBuilder();
  59. $qb->delete($this->getTableName())
  60. ->where(
  61. $qb->expr()->lt('timestamp', $qb->createNamedParameter($this->timeFactory->getTime() - self::lifetime))
  62. );
  63. $qb->execute();
  64. }
  65. /**
  66. * @param LoginFlowV2 $flowV2
  67. * @return LoginFlowV2
  68. * @throws DoesNotExistException
  69. */
  70. private function validateTimestamp(LoginFlowV2 $flowV2): LoginFlowV2 {
  71. if ($flowV2->getTimestamp() < ($this->timeFactory->getTime() - self::lifetime)) {
  72. $this->delete($flowV2);
  73. throw new DoesNotExistException('Token expired');
  74. }
  75. return $flowV2;
  76. }
  77. }