Apache.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. declare(strict_types=1);
  3. namespace Sabre\DAV\Auth\Backend;
  4. use Sabre\HTTP\RequestInterface;
  5. use Sabre\HTTP\ResponseInterface;
  6. /**
  7. * Apache (or NGINX) authenticator.
  8. *
  9. * This authentication backend assumes that authentication has been
  10. * configured in apache (or NGINX), rather than within SabreDAV.
  11. *
  12. * Make sure apache (or NGINX) is properly configured for this to work.
  13. *
  14. * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
  15. * @author Evert Pot (http://evertpot.com/)
  16. * @license http://sabre.io/license/ Modified BSD License
  17. */
  18. class Apache implements BackendInterface
  19. {
  20. /**
  21. * This is the prefix that will be used to generate principal urls.
  22. *
  23. * @var string
  24. */
  25. protected $principalPrefix = 'principals/';
  26. /**
  27. * When this method is called, the backend must check if authentication was
  28. * successful.
  29. *
  30. * The returned value must be one of the following
  31. *
  32. * [true, "principals/username"]
  33. * [false, "reason for failure"]
  34. *
  35. * If authentication was successful, it's expected that the authentication
  36. * backend returns a so-called principal url.
  37. *
  38. * Examples of a principal url:
  39. *
  40. * principals/admin
  41. * principals/user1
  42. * principals/users/joe
  43. * principals/uid/123457
  44. *
  45. * If you don't use WebDAV ACL (RFC3744) we recommend that you simply
  46. * return a string such as:
  47. *
  48. * principals/users/[username]
  49. *
  50. * @return array
  51. */
  52. public function check(RequestInterface $request, ResponseInterface $response)
  53. {
  54. $remoteUser = $request->getRawServerValue('REMOTE_USER');
  55. if (is_null($remoteUser)) {
  56. $remoteUser = $request->getRawServerValue('REDIRECT_REMOTE_USER');
  57. }
  58. if (is_null($remoteUser)) {
  59. $remoteUser = $request->getRawServerValue('PHP_AUTH_USER');
  60. }
  61. if (is_null($remoteUser)) {
  62. return [false, 'No REMOTE_USER, REDIRECT_REMOTE_USER, or PHP_AUTH_USER property was found in the PHP $_SERVER super-global. This likely means your server is not configured correctly'];
  63. }
  64. return [true, $this->principalPrefix.$remoteUser];
  65. }
  66. /**
  67. * This method is called when a user could not be authenticated, and
  68. * authentication was required for the current request.
  69. *
  70. * This gives you the opportunity to set authentication headers. The 401
  71. * status code will already be set.
  72. *
  73. * In this case of Basic Auth, this would for example mean that the
  74. * following header needs to be set:
  75. *
  76. * $response->addHeader('WWW-Authenticate', 'Basic realm=SabreDAV');
  77. *
  78. * Keep in mind that in the case of multiple authentication backends, other
  79. * WWW-Authenticate headers may already have been set, and you'll want to
  80. * append your own WWW-Authenticate header instead of overwriting the
  81. * existing one.
  82. */
  83. public function challenge(RequestInterface $request, ResponseInterface $response)
  84. {
  85. }
  86. }