| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* Helper files for HTTP_Request2 unit tests. Should be accessible via HTTP.
|
|
|
4 |
*
|
|
|
5 |
* PHP version 5
|
|
|
6 |
*
|
|
|
7 |
* LICENSE:
|
|
|
8 |
*
|
|
|
9 |
* Copyright (c) 2008-2011, Alexey Borzov <avb@php.net>
|
|
|
10 |
* All rights reserved.
|
|
|
11 |
*
|
|
|
12 |
* Redistribution and use in source and binary forms, with or without
|
|
|
13 |
* modification, are permitted provided that the following conditions
|
|
|
14 |
* are met:
|
|
|
15 |
*
|
|
|
16 |
* * Redistributions of source code must retain the above copyright
|
|
|
17 |
* notice, this list of conditions and the following disclaimer.
|
|
|
18 |
* * Redistributions in binary form must reproduce the above copyright
|
|
|
19 |
* notice, this list of conditions and the following disclaimer in the
|
|
|
20 |
* documentation and/or other materials provided with the distribution.
|
|
|
21 |
* * The names of the authors may not be used to endorse or promote products
|
|
|
22 |
* derived from this software without specific prior written permission.
|
|
|
23 |
*
|
|
|
24 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
|
|
|
25 |
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
|
|
26 |
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
27 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
|
|
28 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
|
29 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
30 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
|
31 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
|
|
32 |
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
|
33 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
34 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
35 |
*
|
|
|
36 |
* @category HTTP
|
|
|
37 |
* @package HTTP_Request2
|
|
|
38 |
* @author Alexey Borzov <avb@php.net>
|
|
|
39 |
* @license http://opensource.org/licenses/bsd-license.php New BSD License
|
|
|
40 |
* @version SVN: $Id: digestauth.php 308300 2011-02-13 12:24:18Z avb $
|
|
|
41 |
* @link http://pear.php.net/package/HTTP_Request2
|
|
|
42 |
*/
|
|
|
43 |
|
|
|
44 |
/**
|
|
|
45 |
* Mostly borrowed from PHP manual and Socket Adapter implementation
|
|
|
46 |
*
|
|
|
47 |
* @link http://php.net/manual/en/features.http-auth.php
|
|
|
48 |
*/
|
|
|
49 |
|
|
|
50 |
/**
|
|
|
51 |
* Parses the Digest auth header
|
|
|
52 |
*
|
|
|
53 |
* @param string $txt
|
|
|
54 |
*/
|
|
|
55 |
function http_digest_parse($txt)
|
|
|
56 |
{
|
|
|
57 |
$token = '[^\x00-\x1f\x7f-\xff()<>@,;:\\\\"/\[\]?={}\s]+';
|
|
|
58 |
$quoted = '"(?:\\\\.|[^\\\\"])*"';
|
|
|
59 |
|
|
|
60 |
// protect against missing data
|
|
|
61 |
$needed_parts = array_flip(array('nonce', 'nc', 'cnonce', 'qop', 'username', 'uri', 'response'));
|
|
|
62 |
$data = array();
|
|
|
63 |
|
|
|
64 |
preg_match_all("!({$token})\\s*=\\s*({$token}|{$quoted})!", $txt, $matches);
|
|
|
65 |
for ($i = 0; $i < count($matches[0]); $i++) {
|
|
|
66 |
// ignore unneeded parameters
|
|
|
67 |
if (isset($needed_parts[$matches[1][$i]])) {
|
|
|
68 |
unset($needed_parts[$matches[1][$i]]);
|
|
|
69 |
if ('"' == substr($matches[2][$i], 0, 1)) {
|
|
|
70 |
$data[$matches[1][$i]] = substr($matches[2][$i], 1, -1);
|
|
|
71 |
} else {
|
|
|
72 |
$data[$matches[1][$i]] = $matches[2][$i];
|
|
|
73 |
}
|
|
|
74 |
}
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
return !empty($needed_parts) ? false : $data;
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
$realm = 'HTTP_Request2 tests';
|
|
|
81 |
$wantedUser = isset($_GET['user']) ? $_GET['user'] : null;
|
|
|
82 |
$wantedPass = isset($_GET['pass']) ? $_GET['pass'] : null;
|
|
|
83 |
$validAuth = false;
|
|
|
84 |
|
|
|
85 |
if (!empty($_SERVER['PHP_AUTH_DIGEST'])
|
|
|
86 |
&& ($data = http_digest_parse($_SERVER['PHP_AUTH_DIGEST']))
|
|
|
87 |
&& $wantedUser == $data['username']
|
|
|
88 |
) {
|
|
|
89 |
// generate the valid response
|
|
|
90 |
$a1 = md5($data['username'] . ':' . $realm . ':' . $wantedPass);
|
|
|
91 |
$a2 = md5($_SERVER['REQUEST_METHOD'] . ':' . $data['uri']);
|
|
|
92 |
$response = md5($a1. ':' . $data['nonce'] . ':' . $data['nc'] . ':'
|
|
|
93 |
. $data['cnonce'] . ':' . $data['qop'] . ':' . $a2);
|
|
|
94 |
|
|
|
95 |
// check valid response against existing one
|
|
|
96 |
$validAuth = ($data['response'] == $response);
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
if (!$validAuth || empty($_SERVER['PHP_AUTH_DIGEST'])) {
|
|
|
100 |
header('WWW-Authenticate: Digest realm="' . $realm .
|
|
|
101 |
'",qop="auth",nonce="' . uniqid() . '"', true, 401);
|
|
|
102 |
echo "Login required";
|
|
|
103 |
} else {
|
|
|
104 |
echo "Username={$user}";
|
|
|
105 |
}
|
|
|
106 |
?>
|