Guest User

Untitled

a guest
Jun 21st, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. <?php
  2. private function _prepareDigestAuth()
  3. {
  4. $digest = NULL;
  5. $nonce = uniqid();
  6.  
  7. // We need to test which server authentication variable to use
  8. if(isset($_SERVER['PHP_AUTH_DIGEST']))
  9. {
  10. $digest = $_SERVER['PHP_AUTH_DIGEST'];
  11. }
  12.  
  13. elseif(isset($_SERVER['HTTP_AUTHORIZATION']))
  14. {
  15. $digest = $_SERVER['HTTP_AUTHORIZATION'];
  16. }
  17.  
  18. else
  19. {
  20. $digest = "";
  21. }
  22.  
  23. // If there was no digest, show login
  24. if (empty($digest))
  25. {
  26. $this->_forceLogin($nonce);
  27. }
  28.  
  29. preg_match_all('@(username|nonce|uri|nc|cnonce|qop|response)'.
  30. '=[\'"]?([^\'",]+)@', $digest, $t);
  31. $digest_parts = array_combine($t[1], $t[2]);
  32.  
  33. if ( !$this->_checkLogin($digest_parts['username']) )
  34. {
  35. $this->_forceLogin($nonce);
  36. }
  37.  
  38. $valid_logins =& $this->config->item('rest_valid_logins');
  39. $valid_pass = $valid_logins[$digest_parts['username']];
  40.  
  41. // Based on all the info we gathered we can figure out what the response should be
  42. $A1 = md5($digest['username'] . ':' . $this->config->item('rest_realm') . ':' . $valid_pass);
  43. $A2 = md5($_SERVER['REQUEST_METHOD'].':'.$digest['uri']);
  44.  
  45. $valid_response = md5($A1.':'.$digest['nonce'].':'.$digest['nc'].':'.$digest['cnonce'].':'.$digest['qop'].':'.$A2);
  46.  
  47. if ($digest['response'] != $valid_response)
  48. {
  49. $this->_forceLogin($nonce);
  50. }
  51.  
  52. }
  53.  
  54.  
  55. private function _forceLogin($nonce = '')
  56. {
  57. header('HTTP/1.0 401 Unauthorized');
  58.  
  59. if($this->config->item('rest_auth') == 'basic')
  60. {
  61. header('WWW-Authenticate: Basic realm="'.$this->config->item('rest_realm').'"');
  62. }
  63.  
  64. elseif($this->config->item('rest_auth') == 'digest')
  65. {
  66. header('WWW-Authenticate: Digest realm="'.$this->config->item('rest_realm'). '" qop="auth" nonce="'.$nonce.'" opaque="'.md5($this->config->item('rest_realm')).'"');
  67. }
  68.  
  69. echo 'Text to send if user hits Cancel button';
  70. die();
  71. }
  72. /php?>
Add Comment
Please, Sign In to add comment