1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19:
20:
21: 22: 23: 24: 25:
26: class DigestAuthentication {
27:
28: 29: 30: 31: 32: 33: 34: 35:
36: public static function authentication(HttpSocket $http, &$authInfo) {
37: if (isset($authInfo['user'], $authInfo['pass'])) {
38: if (!isset($authInfo['realm']) && !self::_getServerInformation($http, $authInfo)) {
39: return;
40: }
41: $http->request['header']['Authorization'] = self::_generateHeader($http, $authInfo);
42: }
43: }
44:
45: 46: 47: 48: 49: 50: 51:
52: protected static function _getServerInformation(HttpSocket $http, &$authInfo) {
53: $originalRequest = $http->request;
54: $http->configAuth(false);
55: $http->request($http->request);
56: $http->request = $originalRequest;
57: $http->configAuth('Digest', $authInfo);
58:
59: if (empty($http->response['header']['WWW-Authenticate'])) {
60: return false;
61: }
62: preg_match_all('@(\w+)=(?:(?:")([^"]+)"|([^\s,$]+))@', $http->response['header']['WWW-Authenticate'], $matches, PREG_SET_ORDER);
63: foreach ($matches as $match) {
64: $authInfo[$match[1]] = $match[2];
65: }
66: if (!empty($authInfo['qop']) && empty($authInfo['nc'])) {
67: $authInfo['nc'] = 1;
68: }
69: return true;
70: }
71:
72: 73: 74: 75: 76: 77: 78:
79: protected static function _generateHeader(HttpSocket $http, &$authInfo) {
80: $a1 = md5($authInfo['user'] . ':' . $authInfo['realm'] . ':' . $authInfo['pass']);
81: $a2 = md5($http->request['method'] . ':' . $http->request['uri']['path']);
82:
83: if (empty($authInfo['qop'])) {
84: $response = md5($a1 . ':' . $authInfo['nonce'] . ':' . $a2);
85: } else {
86: $authInfo['cnonce'] = uniqid();
87: $nc = sprintf('%08x', $authInfo['nc']++);
88: $response = md5($a1 . ':' . $authInfo['nonce'] . ':' . $nc . ':' . $authInfo['cnonce'] . ':auth:' . $a2);
89: }
90:
91: $authHeader = 'Digest ';
92: $authHeader .= 'username="' . str_replace(array('\\', '"'), array('\\\\', '\\"'), $authInfo['user']) . '", ';
93: $authHeader .= 'realm="' . $authInfo['realm'] . '", ';
94: $authHeader .= 'nonce="' . $authInfo['nonce'] . '", ';
95: $authHeader .= 'uri="' . $http->request['uri']['path'] . '", ';
96: $authHeader .= 'response="' . $response . '"';
97: if (!empty($authInfo['opaque'])) {
98: $authHeader .= ', opaque="' . $authInfo['opaque'] . '"';
99: }
100: if (!empty($authInfo['qop'])) {
101: $authHeader .= ', qop="auth", nc=' . $nc . ', cnonce="' . $authInfo['cnonce'] . '"';
102: }
103: return $authHeader;
104: }
105:
106: }
107: