1
0

PublicAuth.php 3.5 KB

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