AbstractDigest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. declare(strict_types=1);
  3. namespace Sabre\DAV\Auth\Backend;
  4. use Sabre\DAV;
  5. use Sabre\HTTP;
  6. use Sabre\HTTP\RequestInterface;
  7. use Sabre\HTTP\ResponseInterface;
  8. /**
  9. * HTTP Digest authentication backend class.
  10. *
  11. * This class can be used by authentication objects wishing to use HTTP Digest
  12. * Most of the digest logic is handled, implementors just need to worry about
  13. * the getDigestHash method
  14. *
  15. * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
  16. * @author Evert Pot (http://evertpot.com/)
  17. * @license http://sabre.io/license/ Modified BSD License
  18. */
  19. abstract class AbstractDigest 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 = 'SabreDAV';
  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. * Sets the authentication realm for this backend.
  38. *
  39. * Be aware that for Digest authentication, the realm influences the digest
  40. * hash. Choose the realm wisely, because if you change it later, all the
  41. * existing hashes will break and nobody can authenticate.
  42. *
  43. * @param string $realm
  44. */
  45. public function setRealm($realm)
  46. {
  47. $this->realm = $realm;
  48. }
  49. /**
  50. * Returns a users digest hash based on the username and realm.
  51. *
  52. * If the user was not known, null must be returned.
  53. *
  54. * @param string $realm
  55. * @param string $username
  56. *
  57. * @return string|null
  58. */
  59. abstract public function getDigestHash($realm, $username);
  60. /**
  61. * When this method is called, the backend must check if authentication was
  62. * successful.
  63. *
  64. * The returned value must be one of the following
  65. *
  66. * [true, "principals/username"]
  67. * [false, "reason for failure"]
  68. *
  69. * If authentication was successful, it's expected that the authentication
  70. * backend returns a so-called principal url.
  71. *
  72. * Examples of a principal url:
  73. *
  74. * principals/admin
  75. * principals/user1
  76. * principals/users/joe
  77. * principals/uid/123457
  78. *
  79. * If you don't use WebDAV ACL (RFC3744) we recommend that you simply
  80. * return a string such as:
  81. *
  82. * principals/users/[username]
  83. *
  84. * @return array
  85. */
  86. public function check(RequestInterface $request, ResponseInterface $response)
  87. {
  88. $digest = new HTTP\Auth\Digest(
  89. $this->realm,
  90. $request,
  91. $response
  92. );
  93. $digest->init();
  94. $username = $digest->getUsername();
  95. // No username was given
  96. if (!$username) {
  97. return [false, "No 'Authorization: Digest' header found. Either the client didn't send one, or the server is misconfigured"];
  98. }
  99. $hash = $this->getDigestHash($this->realm, $username);
  100. // If this was false, the user account didn't exist
  101. if (false === $hash || is_null($hash)) {
  102. return [false, 'Username or password was incorrect'];
  103. }
  104. if (!is_string($hash)) {
  105. throw new DAV\Exception('The returned value from getDigestHash must be a string or null');
  106. }
  107. // If this was false, the password or part of the hash was incorrect.
  108. if (!$digest->validateA1($hash)) {
  109. return [false, 'Username or password was incorrect'];
  110. }
  111. return [true, $this->principalPrefix.$username];
  112. }
  113. /**
  114. * This method is called when a user could not be authenticated, and
  115. * authentication was required for the current request.
  116. *
  117. * This gives you the opportunity to set authentication headers. The 401
  118. * status code will already be set.
  119. *
  120. * In this case of Basic Auth, this would for example mean that the
  121. * following header needs to be set:
  122. *
  123. * $response->addHeader('WWW-Authenticate', 'Basic realm=SabreDAV');
  124. *
  125. * Keep in mind that in the case of multiple authentication backends, other
  126. * WWW-Authenticate headers may already have been set, and you'll want to
  127. * append your own WWW-Authenticate header instead of overwriting the
  128. * existing one.
  129. */
  130. public function challenge(RequestInterface $request, ResponseInterface $response)
  131. {
  132. $auth = new HTTP\Auth\Digest(
  133. $this->realm,
  134. $request,
  135. $response
  136. );
  137. $auth->init();
  138. $oldStatus = $response->getStatus() ?: 200;
  139. $auth->requireLogin();
  140. // Preventing the digest utility from modifying the http status code,
  141. // this should be handled by the main plugin.
  142. $response->setStatus($oldStatus);
  143. }
  144. }