PublicAuth.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 Morris Jobke <hey@morrisjobke.de>
  11. * @author Robin Appelman <robin@icewind.nl>
  12. * @author Roeland Jago Douma <roeland@famdouma.nl>
  13. * @author Thomas Müller <thomas.mueller@tmit.eu>
  14. * @author Vincent Petry <pvince81@owncloud.com>
  15. *
  16. * @license AGPL-3.0
  17. *
  18. * This code is free software: you can redistribute it and/or modify
  19. * it under the terms of the GNU Affero General Public License, version 3,
  20. * as published by the Free Software Foundation.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU Affero General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU Affero General Public License, version 3,
  28. * along with this program. If not, see <http://www.gnu.org/licenses/>
  29. *
  30. */
  31. namespace OCA\DAV\Connector;
  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. /** @var \OCP\Share\IShare */
  45. private $share;
  46. /** @var IManager */
  47. private $shareManager;
  48. /** @var ISession */
  49. private $session;
  50. /** @var IRequest */
  51. private $request;
  52. /**
  53. * @param IRequest $request
  54. * @param IManager $shareManager
  55. * @param ISession $session
  56. */
  57. public function __construct(IRequest $request,
  58. IManager $shareManager,
  59. ISession $session) {
  60. $this->request = $request;
  61. $this->shareManager = $shareManager;
  62. $this->session = $session;
  63. // setup realm
  64. $defaults = new \OCP\Defaults();
  65. $this->realm = $defaults->getName();
  66. }
  67. /**
  68. * Validates a username and password
  69. *
  70. * This method should return true or false depending on if login
  71. * succeeded.
  72. *
  73. * @param string $username
  74. * @param string $password
  75. *
  76. * @return bool
  77. * @throws \Sabre\DAV\Exception\NotAuthenticated
  78. */
  79. protected function validateUserPass($username, $password) {
  80. try {
  81. $share = $this->shareManager->getShareByToken($username);
  82. } catch (ShareNotFound $e) {
  83. return false;
  84. }
  85. $this->share = $share;
  86. \OC_User::setIncognitoMode(true);
  87. // check if the share is password protected
  88. if ($share->getPassword() !== null) {
  89. if ($share->getShareType() === IShare::TYPE_LINK
  90. || $share->getShareType() === IShare::TYPE_EMAIL
  91. || $share->getShareType() === IShare::TYPE_CIRCLE) {
  92. if ($this->shareManager->checkPassword($share, $password)) {
  93. return true;
  94. } elseif ($this->session->exists('public_link_authenticated')
  95. && $this->session->get('public_link_authenticated') === (string)$share->getId()) {
  96. return true;
  97. } else {
  98. if (in_array('XMLHttpRequest', explode(',', $this->request->getHeader('X-Requested-With')))) {
  99. // do not re-authenticate over ajax, use dummy auth name to prevent browser popup
  100. http_response_code(401);
  101. header('WWW-Authenticate','DummyBasic realm="' . $this->realm . '"');
  102. throw new \Sabre\DAV\Exception\NotAuthenticated('Cannot authenticate over ajax calls');
  103. }
  104. return false;
  105. }
  106. } elseif ($share->getShareType() === IShare::TYPE_REMOTE) {
  107. return true;
  108. } else {
  109. return false;
  110. }
  111. } else {
  112. return true;
  113. }
  114. }
  115. /**
  116. * @return \OCP\Share\IShare
  117. */
  118. public function getShare() {
  119. return $this->share;
  120. }
  121. }