From ecd0831953d627c8b14cc5edc2dcd197b59c1acc Mon Sep 17 00:00:00 2001 From: Zach Borboa Date: Sun, 22 Feb 2015 02:30:35 +0700 Subject: [PATCH] Add http digest auth tests --- tests/PHPCurlClass/PHPCurlClassTest.php | 24 +++++++++++++ tests/PHPCurlClass/server.php | 45 +++++++++++++++++++++++++ 2 files changed, 69 insertions(+) diff --git a/tests/PHPCurlClass/PHPCurlClassTest.php b/tests/PHPCurlClass/PHPCurlClassTest.php index 9364da5..3965315 100644 --- a/tests/PHPCurlClass/PHPCurlClassTest.php +++ b/tests/PHPCurlClass/PHPCurlClassTest.php @@ -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(); diff --git a/tests/PHPCurlClass/server.php b/tests/PHPCurlClass/server.php index 4aba6d4..56efabe 100644 --- a/tests/PHPCurlClass/server.php +++ b/tests/PHPCurlClass/server.php @@ -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;