2010-11-13 22:51:09 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Digest authentication
|
|
|
|
*
|
|
|
|
* PHP 5
|
|
|
|
*
|
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2013-02-08 11:59:49 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2010-11-13 22:51:09 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2013-02-08 11:59:49 +00:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2010-11-13 22:51:09 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Network.Http
|
2010-11-13 22:51:09 +00:00
|
|
|
* @since CakePHP(tm) v 2.0.0
|
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Digest authentication
|
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Network.Http
|
2010-11-13 22:51:09 +00:00
|
|
|
*/
|
2010-11-20 19:47:35 +00:00
|
|
|
class DigestAuthentication {
|
2010-11-13 22:51:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Authentication
|
|
|
|
*
|
|
|
|
* @param HttpSocket $http
|
2010-12-03 23:57:15 +00:00
|
|
|
* @param array $authInfo
|
2010-11-13 22:51:09 +00:00
|
|
|
* @return void
|
|
|
|
* @link http://www.ietf.org/rfc/rfc2617.txt
|
|
|
|
*/
|
2010-12-03 23:57:15 +00:00
|
|
|
public static function authentication(HttpSocket $http, &$authInfo) {
|
|
|
|
if (isset($authInfo['user'], $authInfo['pass'])) {
|
|
|
|
if (!isset($authInfo['realm']) && !self::_getServerInformation($http, $authInfo)) {
|
2010-11-13 22:51:09 +00:00
|
|
|
return;
|
|
|
|
}
|
2010-12-03 23:57:15 +00:00
|
|
|
$http->request['header']['Authorization'] = self::_generateHeader($http, $authInfo);
|
2010-11-13 22:51:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-12-02 05:58:09 +00:00
|
|
|
* Retrieve information about the authentication
|
2010-11-13 22:51:09 +00:00
|
|
|
*
|
|
|
|
* @param HttpSocket $http
|
2011-07-29 03:56:10 +00:00
|
|
|
* @param array $authInfo
|
2010-11-13 22:51:09 +00:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2010-12-03 23:57:15 +00:00
|
|
|
protected static function _getServerInformation(HttpSocket $http, &$authInfo) {
|
2010-11-13 22:51:09 +00:00
|
|
|
$originalRequest = $http->request;
|
2010-12-15 03:51:00 +00:00
|
|
|
$http->configAuth(false);
|
2010-11-13 22:51:09 +00:00
|
|
|
$http->request($http->request);
|
|
|
|
$http->request = $originalRequest;
|
2010-12-15 03:51:00 +00:00
|
|
|
$http->configAuth('Digest', $authInfo);
|
2010-11-13 22:51:09 +00:00
|
|
|
|
2010-11-13 23:47:25 +00:00
|
|
|
if (empty($http->response['header']['WWW-Authenticate'])) {
|
2010-11-13 22:51:09 +00:00
|
|
|
return false;
|
|
|
|
}
|
2010-11-13 23:47:25 +00:00
|
|
|
preg_match_all('@(\w+)=(?:(?:")([^"]+)"|([^\s,$]+))@', $http->response['header']['WWW-Authenticate'], $matches, PREG_SET_ORDER);
|
2010-11-13 22:51:09 +00:00
|
|
|
foreach ($matches as $match) {
|
2010-12-03 23:57:15 +00:00
|
|
|
$authInfo[$match[1]] = $match[2];
|
2010-11-13 22:51:09 +00:00
|
|
|
}
|
2010-12-03 23:57:15 +00:00
|
|
|
if (!empty($authInfo['qop']) && empty($authInfo['nc'])) {
|
|
|
|
$authInfo['nc'] = 1;
|
2010-11-13 22:51:09 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate the header Authorization
|
|
|
|
*
|
|
|
|
* @param HttpSocket $http
|
2010-12-03 23:57:15 +00:00
|
|
|
* @param array $authInfo
|
2010-11-13 22:51:09 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2010-12-03 23:57:15 +00:00
|
|
|
protected static function _generateHeader(HttpSocket $http, &$authInfo) {
|
|
|
|
$a1 = md5($authInfo['user'] . ':' . $authInfo['realm'] . ':' . $authInfo['pass']);
|
2010-11-13 22:51:09 +00:00
|
|
|
$a2 = md5($http->request['method'] . ':' . $http->request['uri']['path']);
|
|
|
|
|
2010-12-03 23:57:15 +00:00
|
|
|
if (empty($authInfo['qop'])) {
|
|
|
|
$response = md5($a1 . ':' . $authInfo['nonce'] . ':' . $a2);
|
2010-11-13 22:51:09 +00:00
|
|
|
} else {
|
2010-12-03 23:57:15 +00:00
|
|
|
$authInfo['cnonce'] = uniqid();
|
|
|
|
$nc = sprintf('%08x', $authInfo['nc']++);
|
|
|
|
$response = md5($a1 . ':' . $authInfo['nonce'] . ':' . $nc . ':' . $authInfo['cnonce'] . ':auth:' . $a2);
|
2010-11-13 22:51:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$authHeader = 'Digest ';
|
2010-12-03 23:57:15 +00:00
|
|
|
$authHeader .= 'username="' . str_replace(array('\\', '"'), array('\\\\', '\\"'), $authInfo['user']) . '", ';
|
|
|
|
$authHeader .= 'realm="' . $authInfo['realm'] . '", ';
|
|
|
|
$authHeader .= 'nonce="' . $authInfo['nonce'] . '", ';
|
2010-11-13 22:51:09 +00:00
|
|
|
$authHeader .= 'uri="' . $http->request['uri']['path'] . '", ';
|
|
|
|
$authHeader .= 'response="' . $response . '"';
|
2010-12-03 23:57:15 +00:00
|
|
|
if (!empty($authInfo['opaque'])) {
|
|
|
|
$authHeader .= ', opaque="' . $authInfo['opaque'] . '"';
|
2010-11-13 22:51:09 +00:00
|
|
|
}
|
2010-12-03 23:57:15 +00:00
|
|
|
if (!empty($authInfo['qop'])) {
|
|
|
|
$authHeader .= ', qop="auth", nc=' . $nc . ', cnonce="' . $authInfo['cnonce'] . '"';
|
2010-11-13 22:51:09 +00:00
|
|
|
}
|
|
|
|
return $authHeader;
|
|
|
|
}
|
2012-03-04 16:18:20 +00:00
|
|
|
|
2010-11-13 22:51:09 +00:00
|
|
|
}
|