AbstractBasic.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. declare(strict_types=1);
  3. namespace Sabre\DAV\Auth\Backend;
  4. use Sabre\HTTP;
  5. use Sabre\HTTP\RequestInterface;
  6. use Sabre\HTTP\ResponseInterface;
  7. /**
  8. * HTTP Basic authentication backend class.
  9. *
  10. * This class can be used by authentication objects wishing to use HTTP Basic
  11. * Most of the digest logic is handled, implementors just need to worry about
  12. * the validateUserPass method.
  13. *
  14. * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
  15. * @author James David Low (http://jameslow.com/)
  16. * @author Evert Pot (http://evertpot.com/)
  17. * @license http://sabre.io/license/ Modified BSD License
  18. */
  19. abstract class AbstractBasic implements BackendInterface
  20. {
  21. /**
  22. * Authentication Realm.
  23. *
  24. * The realm is often displayed by browser clients when showing the
  25. * authentication dialog.
  26. *
  27. * @var string
  28. */
  29. protected $realm = 'sabre/dav';
  30. /**
  31. * This is the prefix that will be used to generate principal urls.
  32. *
  33. * @var string
  34. */
  35. protected $principalPrefix = 'principals/';
  36. /**
  37. * Validates a username and password.
  38. *
  39. * This method should return true or false depending on if login
  40. * succeeded.
  41. *
  42. * @param string $username
  43. * @param string $password
  44. *
  45. * @return bool
  46. */
  47. abstract protected function validateUserPass($username, $password);
  48. /**
  49. * Sets the authentication realm for this backend.
  50. *
  51. * @param string $realm
  52. */
  53. public function setRealm($realm)
  54. {
  55. $this->realm = $realm;
  56. }
  57. /**
  58. * When this method is called, the backend must check if authentication was
  59. * successful.
  60. *
  61. * The returned value must be one of the following
  62. *
  63. * [true, "principals/username"]
  64. * [false, "reason for failure"]
  65. *
  66. * If authentication was successful, it's expected that the authentication
  67. * backend returns a so-called principal url.
  68. *
  69. * Examples of a principal url:
  70. *
  71. * principals/admin
  72. * principals/user1
  73. * principals/users/joe
  74. * principals/uid/123457
  75. *
  76. * If you don't use WebDAV ACL (RFC3744) we recommend that you simply
  77. * return a string such as:
  78. *
  79. * principals/users/[username]
  80. *
  81. * @return array
  82. */
  83. public function check(RequestInterface $request, ResponseInterface $response)
  84. {
  85. $auth = new HTTP\Auth\Basic(
  86. $this->realm,
  87. $request,
  88. $response
  89. );
  90. $userpass = $auth->getCredentials();
  91. if (!$userpass) {
  92. return [false, "No 'Authorization: Basic' header found. Either the client didn't send one, or the server is misconfigured"];
  93. }
  94. if (!$this->validateUserPass($userpass[0], $userpass[1])) {
  95. return [false, 'Username or password was incorrect'];
  96. }
  97. return [true, $this->principalPrefix.$userpass[0]];
  98. }
  99. /**
  100. * This method is called when a user could not be authenticated, and
  101. * authentication was required for the current request.
  102. *
  103. * This gives you the opportunity to set authentication headers. The 401
  104. * status code will already be set.
  105. *
  106. * In this case of Basic Auth, this would for example mean that the
  107. * following header needs to be set:
  108. *
  109. * $response->addHeader('WWW-Authenticate', 'Basic realm=SabreDAV');
  110. *
  111. * Keep in mind that in the case of multiple authentication backends, other
  112. * WWW-Authenticate headers may already have been set, and you'll want to
  113. * append your own WWW-Authenticate header instead of overwriting the
  114. * existing one.
  115. */
  116. public function challenge(RequestInterface $request, ResponseInterface $response)
  117. {
  118. $auth = new HTTP\Auth\Basic(
  119. $this->realm,
  120. $request,
  121. $response
  122. );
  123. $auth->requireLogin();
  124. }
  125. }