PublicAuth.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Björn Schießle <bjoern@schiessle.org>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Lukas Reschke <lukas@statuscode.ch>
  8. * @author Maxence Lange <maxence@artificial-owl.com>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  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 <pvince81@owncloud.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 OCP\IRequest;
  32. use OCP\ISession;
  33. use OCP\Share\Exceptions\ShareNotFound;
  34. use OCP\Share\IManager;
  35. use OCP\Share\IShare;
  36. use Sabre\DAV\Auth\Backend\AbstractBasic;
  37. /**
  38. * Class PublicAuth
  39. *
  40. * @package OCA\DAV\Connector
  41. */
  42. class PublicAuth extends AbstractBasic {
  43. /** @var \OCP\Share\IShare */
  44. private $share;
  45. /** @var IManager */
  46. private $shareManager;
  47. /** @var ISession */
  48. private $session;
  49. /** @var IRequest */
  50. private $request;
  51. /**
  52. * @param IRequest $request
  53. * @param IManager $shareManager
  54. * @param ISession $session
  55. */
  56. public function __construct(IRequest $request,
  57. IManager $shareManager,
  58. ISession $session) {
  59. $this->request = $request;
  60. $this->shareManager = $shareManager;
  61. $this->session = $session;
  62. // setup realm
  63. $defaults = new \OCP\Defaults();
  64. $this->realm = $defaults->getName();
  65. }
  66. /**
  67. * Validates a username and password
  68. *
  69. * This method should return true or false depending on if login
  70. * succeeded.
  71. *
  72. * @param string $username
  73. * @param string $password
  74. *
  75. * @return bool
  76. * @throws \Sabre\DAV\Exception\NotAuthenticated
  77. */
  78. protected function validateUserPass($username, $password) {
  79. try {
  80. $share = $this->shareManager->getShareByToken($username);
  81. } catch (ShareNotFound $e) {
  82. return false;
  83. }
  84. $this->share = $share;
  85. \OC_User::setIncognitoMode(true);
  86. // check if the share is password protected
  87. if ($share->getPassword() !== null) {
  88. if ($share->getShareType() === IShare::TYPE_LINK
  89. || $share->getShareType() === IShare::TYPE_EMAIL
  90. || $share->getShareType() === IShare::TYPE_CIRCLE) {
  91. if ($this->shareManager->checkPassword($share, $password)) {
  92. return true;
  93. } else if ($this->session->exists('public_link_authenticated')
  94. && $this->session->get('public_link_authenticated') === (string)$share->getId()) {
  95. return true;
  96. } else {
  97. if (in_array('XMLHttpRequest', explode(',', $this->request->getHeader('X-Requested-With')))) {
  98. // do not re-authenticate over ajax, use dummy auth name to prevent browser popup
  99. http_response_code(401);
  100. header('WWW-Authenticate','DummyBasic realm="' . $this->realm . '"');
  101. throw new \Sabre\DAV\Exception\NotAuthenticated('Cannot authenticate over ajax calls');
  102. }
  103. return false;
  104. }
  105. } else if ($share->getShareType() === IShare::TYPE_REMOTE) {
  106. return true;
  107. } else {
  108. return false;
  109. }
  110. } else {
  111. return true;
  112. }
  113. }
  114. /**
  115. * @return \OCP\Share\IShare
  116. */
  117. public function getShare() {
  118. return $this->share;
  119. }
  120. }