Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- private function _prepareDigestAuth()
- {
- $digest = NULL;
- $nonce = uniqid();
- // We need to test which server authentication variable to use
- if(isset($_SERVER['PHP_AUTH_DIGEST']))
- {
- $digest = $_SERVER['PHP_AUTH_DIGEST'];
- }
- elseif(isset($_SERVER['HTTP_AUTHORIZATION']))
- {
- $digest = $_SERVER['HTTP_AUTHORIZATION'];
- }
- else
- {
- $digest = "";
- }
- // If there was no digest, show login
- if (empty($digest))
- {
- $this->_forceLogin($nonce);
- }
- preg_match_all('@(username|nonce|uri|nc|cnonce|qop|response)'.
- '=[\'"]?([^\'",]+)@', $digest, $t);
- $digest_parts = array_combine($t[1], $t[2]);
- if ( !$this->_checkLogin($digest_parts['username']) )
- {
- $this->_forceLogin($nonce);
- }
- $valid_logins =& $this->config->item('rest_valid_logins');
- $valid_pass = $valid_logins[$digest_parts['username']];
- // Based on all the info we gathered we can figure out what the response should be
- $A1 = md5($digest['username'] . ':' . $this->config->item('rest_realm') . ':' . $valid_pass);
- $A2 = md5($_SERVER['REQUEST_METHOD'].':'.$digest['uri']);
- $valid_response = md5($A1.':'.$digest['nonce'].':'.$digest['nc'].':'.$digest['cnonce'].':'.$digest['qop'].':'.$A2);
- if ($digest['response'] != $valid_response)
- {
- $this->_forceLogin($nonce);
- }
- }
- private function _forceLogin($nonce = '')
- {
- header('HTTP/1.0 401 Unauthorized');
- if($this->config->item('rest_auth') == 'basic')
- {
- header('WWW-Authenticate: Basic realm="'.$this->config->item('rest_realm').'"');
- }
- elseif($this->config->item('rest_auth') == 'digest')
- {
- header('WWW-Authenticate: Digest realm="'.$this->config->item('rest_realm'). '" qop="auth" nonce="'.$nonce.'" opaque="'.md5($this->config->item('rest_realm')).'"');
- }
- echo 'Text to send if user hits Cancel button';
- die();
- }
- /php?>
Add Comment
Please, Sign In to add comment