BearerAuth.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. /**
  3. * HTTP Bearer Authentication handler
  4. *
  5. * Use this class for easy http authentication setup
  6. *
  7. * @package Sabre
  8. * @subpackage HTTP
  9. * @copyright Copyright (C) 2007-2011 Rooftop Solutions. All rights reserved.
  10. * @author Evert Pot (http://www.rooftopsolutions.nl/)
  11. * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
  12. */
  13. class Sabre_HTTP_BearerAuth extends Sabre_HTTP_AbstractAuth {
  14. /**
  15. * Returns the supplied username and password.
  16. *
  17. * The returned array has two values:
  18. * * 0 - username
  19. * * 1 - password
  20. *
  21. * If nothing was supplied, 'false' will be returned
  22. *
  23. * @return mixed
  24. */
  25. public function getUserPass() {
  26. // Apache and mod_php
  27. if (($user = $this->httpRequest->getRawServerValue('PHP_AUTH_USER')) && ($pass = $this->httpRequest->getRawServerValue('PHP_AUTH_PW'))) {
  28. return array($user,$pass);
  29. }
  30. // Most other webservers
  31. $auth = $this->httpRequest->getHeader('Authorization');
  32. if (!$auth) return false;
  33. if (strpos(strtolower($auth),'bearer')!==0) return false;
  34. return explode(':', base64_decode(substr($auth, 7)));
  35. }
  36. /**
  37. * Returns an HTTP 401 header, forcing login
  38. *
  39. * This should be called when username and password are incorrect, or not supplied at all
  40. *
  41. * @return void
  42. */
  43. public function requireLogin() {
  44. $this->httpResponse->setHeader('WWW-Authenticate','Basic realm="' . $this->realm . '"');
  45. $this->httpResponse->sendStatus(401);
  46. }
  47. }