1
0

PublicAuth.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bjoern Schiessle <bjoern@schiessle.org>
  6. * @author Björn Schießle <bjoern@schiessle.org>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Lukas Reschke <lukas@statuscode.ch>
  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 Sabre\DAV\Auth\Backend\AbstractBasic;
  36. /**
  37. * Class PublicAuth
  38. *
  39. * @package OCA\DAV\Connector
  40. */
  41. class PublicAuth extends AbstractBasic {
  42. /** @var \OCP\Share\IShare */
  43. private $share;
  44. /** @var IManager */
  45. private $shareManager;
  46. /** @var ISession */
  47. private $session;
  48. /** @var IRequest */
  49. private $request;
  50. /**
  51. * @param IRequest $request
  52. * @param IManager $shareManager
  53. * @param ISession $session
  54. */
  55. public function __construct(IRequest $request,
  56. IManager $shareManager,
  57. ISession $session) {
  58. $this->request = $request;
  59. $this->shareManager = $shareManager;
  60. $this->session = $session;
  61. // setup realm
  62. $defaults = new \OCP\Defaults();
  63. $this->realm = $defaults->getName();
  64. }
  65. /**
  66. * Validates a username and password
  67. *
  68. * This method should return true or false depending on if login
  69. * succeeded.
  70. *
  71. * @param string $username
  72. * @param string $password
  73. *
  74. * @return bool
  75. * @throws \Sabre\DAV\Exception\NotAuthenticated
  76. */
  77. protected function validateUserPass($username, $password) {
  78. try {
  79. $share = $this->shareManager->getShareByToken($username);
  80. } catch (ShareNotFound $e) {
  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() === \OCP\Share::SHARE_TYPE_LINK || $share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL) {
  88. if ($this->shareManager->checkPassword($share, $password)) {
  89. return true;
  90. } else if ($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. return false;
  101. }
  102. } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_REMOTE) {
  103. return true;
  104. } else {
  105. return false;
  106. }
  107. } else {
  108. return true;
  109. }
  110. }
  111. /**
  112. * @return \OCP\Share\IShare
  113. */
  114. public function getShare() {
  115. return $this->share;
  116. }
  117. }