Add http digest auth tests

This commit is contained in:
Zach Borboa
2015-02-22 02:30:35 +07:00
parent 06b0cb62a0
commit ecd0831953
2 changed files with 69 additions and 0 deletions
+24
View File
@@ -382,6 +382,30 @@ class CurlTest extends PHPUnit_Framework_TestCase
$this->assertEquals($password, $json->password);
}
public function testDigestHttpAuth()
{
$username = 'myusername';
$password = 'mypassword';
$invalid_password = 'anotherpassword';
$test = new Test();
$test->server('http_digest_auth', 'GET');
$this->assertEquals('canceled', $test->curl->response);
$this->assertEquals(401, $test->curl->http_status_code);
$test = new Test();
$test->curl->setDigestAuthentication($username, $invalid_password);
$test->server('http_digest_auth', 'GET');
$this->assertEquals('invalid', $test->curl->response);
$this->assertEquals(401, $test->curl->http_status_code);
$test = new Test();
$test->curl->setDigestAuthentication($username, $password);
$test->server('http_digest_auth', 'GET');
$this->assertEquals('valid', $test->curl->response);
$this->assertEquals(200, $test->curl->http_status_code);
}
public function testReferrer()
{
$test = new Test();
+45
View File
@@ -44,6 +44,51 @@ if ($test == 'http_basic_auth') {
'password' => $_SERVER['PHP_AUTH_PW'],
));
exit;
} elseif ($test == 'http_digest_auth') {
$users = array(
'myusername' => 'mypassword',
);
$realm = 'Restricted area';
$qop = 'auth';
$nonce = md5(uniqid());
$opaque = md5(uniqid());
if (empty($_SERVER['PHP_AUTH_DIGEST'])) {
header('HTTP/1.1 401 Unauthorized');
header(sprintf(
'WWW-Authenticate: Digest realm="%s", qop="%s", nonce="%s", opaque="%s"', $realm, $qop, $nonce, $opaque));
echo 'canceled';
exit;
}
$data = array(
'nonce' => '',
'nc' => '',
'cnonce' => '',
'qop' => '',
'username' => '',
'uri' => '',
'response' => '',
);
preg_match_all('@(' . implode('|', array_keys($data)) . ')=(?:([\'"])([^\2]+?)\2|([^\s,]+))@',
$_SERVER['PHP_AUTH_DIGEST'], $matches, PREG_SET_ORDER);
foreach ($matches as $match) {
$data[$match['1']] = $match['3'] ? $match['3'] : $match['4'];
}
$A1 = md5($data['username'] . ':' . $realm . ':' . $users[$data['username']]);
$A2 = md5($_SERVER['REQUEST_METHOD'] . ':' . $data['uri']);
$valid_response = md5(
$A1 . ':' . $data['nonce'] . ':' . $data['nc'] . ':' . $data['cnonce'] . ':' . $data['qop'] . ':' . $A2);
if (!($data['response'] === $valid_response)) {
header('HTTP/1.1 401 Unauthorized');
echo 'invalid';
exit;
}
echo 'valid';
exit;
} elseif ($test === 'get') {
echo http_build_query($_GET);
exit;