Auth.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Robin Appelman <robin@icewind.nl>
  8. * @author Thomas Müller <thomas.mueller@tmit.eu>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OCA\DAV\Tests\unit\Connector\Sabre\RequestTest;
  26. use Sabre\DAV\Auth\Backend\BackendInterface;
  27. use Sabre\HTTP\RequestInterface;
  28. use Sabre\HTTP\ResponseInterface;
  29. class Auth implements BackendInterface {
  30. /**
  31. * @var string
  32. */
  33. private $user;
  34. /**
  35. * @var string
  36. */
  37. private $password;
  38. /**
  39. * Auth constructor.
  40. *
  41. * @param string $user
  42. * @param string $password
  43. */
  44. public function __construct($user, $password) {
  45. $this->user = $user;
  46. $this->password = $password;
  47. }
  48. /**
  49. * When this method is called, the backend must check if authentication was
  50. * successful.
  51. *
  52. * The returned value must be one of the following
  53. *
  54. * [true, "principals/username"]
  55. * [false, "reason for failure"]
  56. *
  57. * If authentication was successful, it's expected that the authentication
  58. * backend returns a so-called principal url.
  59. *
  60. * Examples of a principal url:
  61. *
  62. * principals/admin
  63. * principals/user1
  64. * principals/users/joe
  65. * principals/uid/123457
  66. *
  67. * If you don't use WebDAV ACL (RFC3744) we recommend that you simply
  68. * return a string such as:
  69. *
  70. * principals/users/[username]
  71. *
  72. * @param RequestInterface $request
  73. * @param ResponseInterface $response
  74. * @return array
  75. */
  76. public function check(RequestInterface $request, ResponseInterface $response) {
  77. $userSession = \OC::$server->getUserSession();
  78. $result = $userSession->login($this->user, $this->password);
  79. if ($result) {
  80. //we need to pass the user name, which may differ from login name
  81. $user = $userSession->getUser()->getUID();
  82. \OC_Util::setupFS($user);
  83. //trigger creation of user home and /files folder
  84. \OC::$server->getUserFolder($user);
  85. return [true, "principals/$user"];
  86. }
  87. return [false, "login failed"];
  88. }
  89. /**
  90. * This method is called when a user could not be authenticated, and
  91. * authentication was required for the current request.
  92. *
  93. * This gives you the opportunity to set authentication headers. The 401
  94. * status code will already be set.
  95. *
  96. * In this case of Basic Auth, this would for example mean that the
  97. * following header needs to be set:
  98. *
  99. * $response->addHeader('WWW-Authenticate', 'Basic realm=SabreDAV');
  100. *
  101. * Keep in mind that in the case of multiple authentication backends, other
  102. * WWW-Authenticate headers may already have been set, and you'll want to
  103. * append your own WWW-Authenticate header instead of overwriting the
  104. * existing one.
  105. *
  106. * @param RequestInterface $request
  107. * @param ResponseInterface $response
  108. * @return void
  109. */
  110. public function challenge(RequestInterface $request, ResponseInterface $response) {
  111. // TODO: Implement challenge() method.
  112. }
  113. }