PublicAuth.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 OC\Security\Bruteforce\Throttler;
  32. use OCP\IRequest;
  33. use OCP\ISession;
  34. use OCP\Share\Exceptions\ShareNotFound;
  35. use OCP\Share\IManager;
  36. use OCP\Share\IShare;
  37. use Sabre\DAV\Auth\Backend\AbstractBasic;
  38. /**
  39. * Class PublicAuth
  40. *
  41. * @package OCA\DAV\Connector
  42. */
  43. class PublicAuth extends AbstractBasic {
  44. private const BRUTEFORCE_ACTION = 'public_webdav_auth';
  45. private ?IShare $share = null;
  46. private IManager $shareManager;
  47. private ISession $session;
  48. private IRequest $request;
  49. private Throttler $throttler;
  50. public function __construct(IRequest $request,
  51. IManager $shareManager,
  52. ISession $session,
  53. Throttler $throttler) {
  54. $this->request = $request;
  55. $this->shareManager = $shareManager;
  56. $this->session = $session;
  57. $this->throttler = $throttler;
  58. // setup realm
  59. $defaults = new \OCP\Defaults();
  60. $this->realm = $defaults->getName();
  61. }
  62. /**
  63. * Validates a username and password
  64. *
  65. * This method should return true or false depending on if login
  66. * succeeded.
  67. *
  68. * @param string $username
  69. * @param string $password
  70. * @return bool
  71. * @throws \Sabre\DAV\Exception\NotAuthenticated
  72. */
  73. protected function validateUserPass($username, $password) {
  74. $this->throttler->sleepDelayOrThrowOnMax($this->request->getRemoteAddress(), self::BRUTEFORCE_ACTION);
  75. try {
  76. $share = $this->shareManager->getShareByToken($username);
  77. } catch (ShareNotFound $e) {
  78. $this->throttler->registerAttempt(self::BRUTEFORCE_ACTION, $this->request->getRemoteAddress());
  79. return false;
  80. }
  81. $this->share = $share;
  82. \OC_User::setIncognitoMode(true);
  83. // check if the share is password protected
  84. if ($share->getPassword() !== null) {
  85. if ($share->getShareType() === IShare::TYPE_LINK
  86. || $share->getShareType() === IShare::TYPE_EMAIL
  87. || $share->getShareType() === IShare::TYPE_CIRCLE) {
  88. if ($this->shareManager->checkPassword($share, $password)) {
  89. return true;
  90. } elseif ($this->session->exists('public_link_authenticated')
  91. && $this->session->get('public_link_authenticated') === (string)$share->getId()) {
  92. return true;
  93. } else {
  94. if (in_array('XMLHttpRequest', explode(',', $this->request->getHeader('X-Requested-With')))) {
  95. // do not re-authenticate over ajax, use dummy auth name to prevent browser popup
  96. http_response_code(401);
  97. header('WWW-Authenticate: DummyBasic realm="' . $this->realm . '"');
  98. throw new \Sabre\DAV\Exception\NotAuthenticated('Cannot authenticate over ajax calls');
  99. }
  100. $this->throttler->registerAttempt(self::BRUTEFORCE_ACTION, $this->request->getRemoteAddress());
  101. return false;
  102. }
  103. } elseif ($share->getShareType() === IShare::TYPE_REMOTE) {
  104. return true;
  105. } else {
  106. $this->throttler->registerAttempt(self::BRUTEFORCE_ACTION, $this->request->getRemoteAddress());
  107. return false;
  108. }
  109. }
  110. return true;
  111. }
  112. public function getShare(): IShare {
  113. assert($this->share !== null);
  114. return $this->share;
  115. }
  116. }