LegacyPublicAuth.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Björn Schießle <bjoern@schiessle.org>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Lukas Reschke <lukas@statuscode.ch>
  9. * @author Maxence Lange <maxence@artificial-owl.com>
  10. * @author Robin Appelman <robin@icewind.nl>
  11. * @author Roeland Jago Douma <roeland@famdouma.nl>
  12. * @author Thomas Müller <thomas.mueller@tmit.eu>
  13. * @author Vincent Petry <vincent@nextcloud.com>
  14. *
  15. * @license AGPL-3.0
  16. *
  17. * This code is free software: you can redistribute it and/or modify
  18. * it under the terms of the GNU Affero General Public License, version 3,
  19. * as published by the Free Software Foundation.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU Affero General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Affero General Public License, version 3,
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>
  28. *
  29. */
  30. namespace OCA\DAV\Connector;
  31. use OCA\DAV\Connector\Sabre\PublicAuth;
  32. use OCP\IRequest;
  33. use OCP\ISession;
  34. use OCP\Security\Bruteforce\IThrottler;
  35. use OCP\Share\Exceptions\ShareNotFound;
  36. use OCP\Share\IManager;
  37. use OCP\Share\IShare;
  38. use Sabre\DAV\Auth\Backend\AbstractBasic;
  39. /**
  40. * Class PublicAuth
  41. *
  42. * @package OCA\DAV\Connector
  43. */
  44. class LegacyPublicAuth extends AbstractBasic {
  45. private const BRUTEFORCE_ACTION = 'legacy_public_webdav_auth';
  46. private ?IShare $share = null;
  47. private IManager $shareManager;
  48. private ISession $session;
  49. private IRequest $request;
  50. private IThrottler $throttler;
  51. public function __construct(IRequest $request,
  52. IManager $shareManager,
  53. ISession $session,
  54. IThrottler $throttler) {
  55. $this->request = $request;
  56. $this->shareManager = $shareManager;
  57. $this->session = $session;
  58. $this->throttler = $throttler;
  59. // setup realm
  60. $defaults = new \OCP\Defaults();
  61. $this->realm = $defaults->getName() ?: 'Nextcloud';
  62. }
  63. /**
  64. * Validates a username and password
  65. *
  66. * This method should return true or false depending on if login
  67. * succeeded.
  68. *
  69. * @param string $username
  70. * @param string $password
  71. *
  72. * @return bool
  73. * @throws \Sabre\DAV\Exception\NotAuthenticated
  74. */
  75. protected function validateUserPass($username, $password) {
  76. $this->throttler->sleepDelayOrThrowOnMax($this->request->getRemoteAddress(), self::BRUTEFORCE_ACTION);
  77. try {
  78. $share = $this->shareManager->getShareByToken($username);
  79. } catch (ShareNotFound $e) {
  80. $this->throttler->registerAttempt(self::BRUTEFORCE_ACTION, $this->request->getRemoteAddress());
  81. return false;
  82. }
  83. $this->share = $share;
  84. \OC_User::setIncognitoMode(true);
  85. // check if the share is password protected
  86. if ($share->getPassword() !== null) {
  87. if ($share->getShareType() === IShare::TYPE_LINK
  88. || $share->getShareType() === IShare::TYPE_EMAIL
  89. || $share->getShareType() === IShare::TYPE_CIRCLE) {
  90. if ($this->shareManager->checkPassword($share, $password)) {
  91. return true;
  92. } elseif ($this->session->exists(PublicAuth::DAV_AUTHENTICATED)
  93. && $this->session->get(PublicAuth::DAV_AUTHENTICATED) === $share->getId()) {
  94. return true;
  95. } else {
  96. if (in_array('XMLHttpRequest', explode(',', $this->request->getHeader('X-Requested-With')))) {
  97. // do not re-authenticate over ajax, use dummy auth name to prevent browser popup
  98. http_response_code(401);
  99. header('WWW-Authenticate: DummyBasic realm="' . $this->realm . '"');
  100. throw new \Sabre\DAV\Exception\NotAuthenticated('Cannot authenticate over ajax calls');
  101. }
  102. $this->throttler->registerAttempt(self::BRUTEFORCE_ACTION, $this->request->getRemoteAddress());
  103. return false;
  104. }
  105. } elseif ($share->getShareType() === IShare::TYPE_REMOTE) {
  106. return true;
  107. } else {
  108. $this->throttler->registerAttempt(self::BRUTEFORCE_ACTION, $this->request->getRemoteAddress());
  109. return false;
  110. }
  111. }
  112. return true;
  113. }
  114. public function getShare(): IShare {
  115. assert($this->share !== null);
  116. return $this->share;
  117. }
  118. }