auth.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. /**
  3. * @author Arthur Schiwon <blizzz@owncloud.com>
  4. * @author Bart Visscher <bartv@thisnet.nl>
  5. * @author Christian Seiler <christian@iwakd.de>
  6. * @author Jakob Sack <mail@jakobsack.de>
  7. * @author Lukas Reschke <lukas@owncloud.com>
  8. * @author Markus Goetz <markus@woboq.com>
  9. * @author Michael Gapczynski <GapczynskiM@gmail.com>
  10. * @author Morris Jobke <hey@morrisjobke.de>
  11. * @author Robin McCorkell <rmccorkell@karoshi.org.uk>
  12. * @author Thomas Müller <thomas.mueller@tmit.eu>
  13. * @author Vincent Petry <pvince81@owncloud.com>
  14. *
  15. * @copyright Copyright (c) 2015, ownCloud, Inc.
  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 OC\Connector\Sabre;
  32. use Exception;
  33. use Sabre\DAV\Auth\Backend\AbstractBasic;
  34. use Sabre\DAV\Exception\NotAuthenticated;
  35. use Sabre\DAV\Exception\ServiceUnavailable;
  36. class Auth extends AbstractBasic {
  37. const DAV_AUTHENTICATED = 'AUTHENTICATED_TO_DAV_BACKEND';
  38. /**
  39. * Whether the user has initially authenticated via DAV
  40. *
  41. * This is required for WebDAV clients that resent the cookies even when the
  42. * account was changed.
  43. *
  44. * @see https://github.com/owncloud/core/issues/13245
  45. *
  46. * @param string $username
  47. * @return bool
  48. */
  49. protected function isDavAuthenticated($username) {
  50. return !is_null(\OC::$server->getSession()->get(self::DAV_AUTHENTICATED)) &&
  51. \OC::$server->getSession()->get(self::DAV_AUTHENTICATED) === $username;
  52. }
  53. /**
  54. * Validates a username and password
  55. *
  56. * This method should return true or false depending on if login
  57. * succeeded.
  58. *
  59. * @param string $username
  60. * @param string $password
  61. * @return bool
  62. */
  63. protected function validateUserPass($username, $password) {
  64. if (\OC_User::isLoggedIn() &&
  65. $this->isDavAuthenticated(\OC_User::getUser())
  66. ) {
  67. \OC_Util::setupFS(\OC_User::getUser());
  68. \OC::$server->getSession()->close();
  69. return true;
  70. } else {
  71. \OC_Util::setUpFS(); //login hooks may need early access to the filesystem
  72. if(\OC_User::login($username, $password)) {
  73. // make sure we use ownCloud's internal username here
  74. // and not the HTTP auth supplied one, see issue #14048
  75. $ocUser = \OC_User::getUser();
  76. \OC_Util::setUpFS($ocUser);
  77. \OC::$server->getSession()->set(self::DAV_AUTHENTICATED, $ocUser);
  78. \OC::$server->getSession()->close();
  79. return true;
  80. } else {
  81. \OC::$server->getSession()->close();
  82. return false;
  83. }
  84. }
  85. }
  86. /**
  87. * Returns information about the currently logged in username.
  88. *
  89. * If nobody is currently logged in, this method should return null.
  90. *
  91. * @return string|null
  92. */
  93. public function getCurrentUser() {
  94. $user = \OC_User::getUser();
  95. if($user && $this->isDavAuthenticated($user)) {
  96. return $user;
  97. }
  98. return null;
  99. }
  100. /**
  101. * Override function here. We want to cache authentication cookies
  102. * in the syncing client to avoid HTTP-401 roundtrips.
  103. * If the sync client supplies the cookies, then OC_User::isLoggedIn()
  104. * will return true and we can see this WebDAV request as already authenticated,
  105. * even if there are no HTTP Basic Auth headers.
  106. * In other case, just fallback to the parent implementation.
  107. *
  108. * @param \Sabre\DAV\Server $server
  109. * @param string $realm
  110. * @return bool
  111. * @throws ServiceUnavailable
  112. */
  113. public function authenticate(\Sabre\DAV\Server $server, $realm) {
  114. try {
  115. $result = $this->auth($server, $realm);
  116. return $result;
  117. } catch (NotAuthenticated $e) {
  118. throw $e;
  119. } catch (Exception $e) {
  120. $class = get_class($e);
  121. $msg = $e->getMessage();
  122. throw new ServiceUnavailable("$class: $msg");
  123. }
  124. }
  125. /**
  126. * @param \Sabre\DAV\Server $server
  127. * @param $realm
  128. * @return bool
  129. */
  130. private function auth(\Sabre\DAV\Server $server, $realm) {
  131. if (\OC_User::handleApacheAuth() ||
  132. (\OC_User::isLoggedIn() && is_null(\OC::$server->getSession()->get(self::DAV_AUTHENTICATED)))
  133. ) {
  134. $user = \OC_User::getUser();
  135. \OC_Util::setupFS($user);
  136. $this->currentUser = $user;
  137. \OC::$server->getSession()->close();
  138. return true;
  139. }
  140. return parent::authenticate($server, $realm);
  141. }
  142. }