AbstractBearer.php 3.7 KB

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