1
0

LoginFlowV2Mapper.php 2.8 KB

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