LegacyPublicAuth.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\DAV\Connector;
  8. use OCA\DAV\Connector\Sabre\PublicAuth;
  9. use OCP\IRequest;
  10. use OCP\ISession;
  11. use OCP\Security\Bruteforce\IThrottler;
  12. use OCP\Share\Exceptions\ShareNotFound;
  13. use OCP\Share\IManager;
  14. use OCP\Share\IShare;
  15. use Sabre\DAV\Auth\Backend\AbstractBasic;
  16. /**
  17. * Class PublicAuth
  18. *
  19. * @package OCA\DAV\Connector
  20. */
  21. class LegacyPublicAuth extends AbstractBasic {
  22. private const BRUTEFORCE_ACTION = 'legacy_public_webdav_auth';
  23. private ?IShare $share = null;
  24. private IManager $shareManager;
  25. private ISession $session;
  26. private IRequest $request;
  27. private IThrottler $throttler;
  28. public function __construct(IRequest $request,
  29. IManager $shareManager,
  30. ISession $session,
  31. IThrottler $throttler) {
  32. $this->request = $request;
  33. $this->shareManager = $shareManager;
  34. $this->session = $session;
  35. $this->throttler = $throttler;
  36. // setup realm
  37. $defaults = new \OCP\Defaults();
  38. $this->realm = $defaults->getName() ?: 'Nextcloud';
  39. }
  40. /**
  41. * Validates a username and password
  42. *
  43. * This method should return true or false depending on if login
  44. * succeeded.
  45. *
  46. * @param string $username
  47. * @param string $password
  48. *
  49. * @return bool
  50. * @throws \Sabre\DAV\Exception\NotAuthenticated
  51. */
  52. protected function validateUserPass($username, $password) {
  53. $this->throttler->sleepDelayOrThrowOnMax($this->request->getRemoteAddress(), self::BRUTEFORCE_ACTION);
  54. try {
  55. $share = $this->shareManager->getShareByToken($username);
  56. } catch (ShareNotFound $e) {
  57. $this->throttler->registerAttempt(self::BRUTEFORCE_ACTION, $this->request->getRemoteAddress());
  58. return false;
  59. }
  60. $this->share = $share;
  61. \OC_User::setIncognitoMode(true);
  62. // check if the share is password protected
  63. if ($share->getPassword() !== null) {
  64. if ($share->getShareType() === IShare::TYPE_LINK
  65. || $share->getShareType() === IShare::TYPE_EMAIL
  66. || $share->getShareType() === IShare::TYPE_CIRCLE) {
  67. if ($this->shareManager->checkPassword($share, $password)) {
  68. return true;
  69. } elseif ($this->session->exists(PublicAuth::DAV_AUTHENTICATED)
  70. && $this->session->get(PublicAuth::DAV_AUTHENTICATED) === $share->getId()) {
  71. return true;
  72. } else {
  73. if (in_array('XMLHttpRequest', explode(',', $this->request->getHeader('X-Requested-With')))) {
  74. // do not re-authenticate over ajax, use dummy auth name to prevent browser popup
  75. http_response_code(401);
  76. header('WWW-Authenticate: DummyBasic realm="' . $this->realm . '"');
  77. throw new \Sabre\DAV\Exception\NotAuthenticated('Cannot authenticate over ajax calls');
  78. }
  79. $this->throttler->registerAttempt(self::BRUTEFORCE_ACTION, $this->request->getRemoteAddress());
  80. return false;
  81. }
  82. } elseif ($share->getShareType() === IShare::TYPE_REMOTE) {
  83. return true;
  84. } else {
  85. $this->throttler->registerAttempt(self::BRUTEFORCE_ACTION, $this->request->getRemoteAddress());
  86. return false;
  87. }
  88. }
  89. return true;
  90. }
  91. public function getShare(): IShare {
  92. assert($this->share !== null);
  93. return $this->share;
  94. }
  95. }