Auth.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\DAV\Tests\unit\Connector\Sabre\RequestTest;
  8. use Sabre\DAV\Auth\Backend\BackendInterface;
  9. use Sabre\HTTP\RequestInterface;
  10. use Sabre\HTTP\ResponseInterface;
  11. class Auth implements BackendInterface {
  12. /**
  13. * Auth constructor.
  14. *
  15. * @param string $user
  16. * @param string $password
  17. */
  18. public function __construct(
  19. private $user,
  20. private $password,
  21. ) {
  22. }
  23. /**
  24. * When this method is called, the backend must check if authentication was
  25. * successful.
  26. *
  27. * The returned value must be one of the following
  28. *
  29. * [true, "principals/username"]
  30. * [false, "reason for failure"]
  31. *
  32. * If authentication was successful, it's expected that the authentication
  33. * backend returns a so-called principal url.
  34. *
  35. * Examples of a principal url:
  36. *
  37. * principals/admin
  38. * principals/user1
  39. * principals/users/joe
  40. * principals/uid/123457
  41. *
  42. * If you don't use WebDAV ACL (RFC3744) we recommend that you simply
  43. * return a string such as:
  44. *
  45. * principals/users/[username]
  46. *
  47. * @param RequestInterface $request
  48. * @param ResponseInterface $response
  49. * @return array
  50. */
  51. public function check(RequestInterface $request, ResponseInterface $response) {
  52. $userSession = \OC::$server->getUserSession();
  53. $result = $userSession->login($this->user, $this->password);
  54. if ($result) {
  55. //we need to pass the user name, which may differ from login name
  56. $user = $userSession->getUser()->getUID();
  57. \OC_Util::setupFS($user);
  58. //trigger creation of user home and /files folder
  59. \OC::$server->getUserFolder($user);
  60. return [true, "principals/$user"];
  61. }
  62. return [false, 'login failed'];
  63. }
  64. /**
  65. * This method is called when a user could not be authenticated, and
  66. * authentication was required for the current request.
  67. *
  68. * This gives you the opportunity to set authentication headers. The 401
  69. * status code will already be set.
  70. *
  71. * In this case of Basic Auth, this would for example mean that the
  72. * following header needs to be set:
  73. *
  74. * $response->addHeader('WWW-Authenticate', 'Basic realm=SabreDAV');
  75. *
  76. * Keep in mind that in the case of multiple authentication backends, other
  77. * WWW-Authenticate headers may already have been set, and you'll want to
  78. * append your own WWW-Authenticate header instead of overwriting the
  79. * existing one.
  80. *
  81. * @param RequestInterface $request
  82. * @param ResponseInterface $response
  83. * @return void
  84. */
  85. public function challenge(RequestInterface $request, ResponseInterface $response): void {
  86. // TODO: Implement challenge() method.
  87. }
  88. }