PageRenderTime 77ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Sabre/DAV/Auth/Backend/AbstractDigest.php

https://github.com/KOLANICH/SabreDAV
PHP | 101 lines | 37 code | 19 blank | 45 comment | 5 complexity | 5046461909f3427e3c8983bab9966e10 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. namespace Sabre\DAV\Auth\Backend;
  3. use Sabre\HTTP;
  4. use Sabre\DAV;
  5. /**
  6. * HTTP Digest authentication backend class
  7. *
  8. * This class can be used by authentication objects wishing to use HTTP Digest
  9. * Most of the digest logic is handled, implementors just need to worry about
  10. * the getDigestHash method
  11. *
  12. * @copyright Copyright (C) 2007-2013 fruux GmbH (https://fruux.com/).
  13. * @author Evert Pot (http://evertpot.com/)
  14. * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
  15. */
  16. abstract class AbstractDigest implements BackendInterface {
  17. /**
  18. * This variable holds the currently logged in username.
  19. *
  20. * @var array|null
  21. */
  22. protected $currentUser;
  23. /**
  24. * Returns a users digest hash based on the username and realm.
  25. *
  26. * If the user was not known, null must be returned.
  27. *
  28. * @param string $realm
  29. * @param string $username
  30. * @return string|null
  31. */
  32. abstract public function getDigestHash($realm, $username);
  33. /**
  34. * Authenticates the user based on the current request.
  35. *
  36. * If authentication is successful, true must be returned.
  37. * If authentication fails, an exception must be thrown.
  38. *
  39. * @param DAV\Server $server
  40. * @param string $realm
  41. * @throws DAV\Exception\NotAuthenticated
  42. * @return bool
  43. */
  44. public function authenticate(DAV\Server $server, $realm) {
  45. $digest = new HTTP\DigestAuth();
  46. // Hooking up request and response objects
  47. $digest->setHTTPRequest($server->httpRequest);
  48. $digest->setHTTPResponse($server->httpResponse);
  49. $digest->setRealm($realm);
  50. $digest->init();
  51. $username = $digest->getUsername();
  52. // No username was given
  53. if (!$username) {
  54. $digest->requireLogin();
  55. throw new DAV\Exception\NotAuthenticated('No digest authentication headers were found');
  56. }
  57. $hash = $this->getDigestHash($realm, $username);
  58. // If this was false, the user account didn't exist
  59. if ($hash===false || is_null($hash)) {
  60. $digest->requireLogin();
  61. throw new DAV\Exception\NotAuthenticated('The supplied username was not on file');
  62. }
  63. if (!is_string($hash)) {
  64. throw new DAV\Exception('The returned value from getDigestHash must be a string or null');
  65. }
  66. // If this was false, the password or part of the hash was incorrect.
  67. if (!$digest->validateA1($hash)) {
  68. $digest->requireLogin();
  69. throw new DAV\Exception\NotAuthenticated('Incorrect username');
  70. }
  71. $this->currentUser = $username;
  72. return true;
  73. }
  74. /**
  75. * Returns the currently logged in username.
  76. *
  77. * @return string|null
  78. */
  79. public function getCurrentUser() {
  80. return $this->currentUser;
  81. }
  82. }