From c27b9b7e779d8552e933a8f5108acd9b01f71eb8 Mon Sep 17 00:00:00 2001 From: Brad Stinson Date: Tue, 25 Sep 2012 16:16:08 -0500 Subject: [PATCH] Added digest auth support support for Added support for digest authentication and included required unit tests. --- src/Httpful/Request.php | 26 ++++++++++++++++++++++++++ tests/Httpful/HttpfulTest.php | 13 +++++++++++++ 2 files changed, 39 insertions(+) diff --git a/src/Httpful/Request.php b/src/Httpful/Request.php index 3b5b341..92c783a 100644 --- a/src/Httpful/Request.php +++ b/src/Httpful/Request.php @@ -131,6 +131,14 @@ public function hasBasicAuth() return isset($this->password) && isset($this->username); } + /** + * @return bool Is this request setup for digest auth? + */ + public function hasDigestAuth() + { + return isset($this->password) && isset($this->username) && $this->additional_curl_opts['CURLOPT_HTTPAUTH'] = CURLAUTH_DIGEST; + } + /** * If the response is a 301 or 302 redirect, automatically * send off another request to that location @@ -219,6 +227,24 @@ public function authenticateWithBasic($username, $password) return $this->basicAuth($username, $password); } + /** + * User Digest Auth. + * @return Request this + * @param string $username + * @param string $password + */ + public function digestAuth($username, $password) + { + $this->addOnCurlOption(CURLOPT_HTTPAUTH, CURLAUTH_DIGEST); + return $this->basicAuth($username, $password); + } + + // @alias of digestAuth + public function authenticateWithDigest($username, $password) + { + return $this->digestAuth($username, $password); + } + /** * @return is this request setup for client side cert? */ diff --git a/tests/Httpful/HttpfulTest.php b/tests/Httpful/HttpfulTest.php index 5efb6ba..ca36afb 100644 --- a/tests/Httpful/HttpfulTest.php +++ b/tests/Httpful/HttpfulTest.php @@ -216,6 +216,19 @@ function testAuthSetup() $this->assertTrue($r->hasBasicAuth()); } + function testDigestAuthSetup() + { + $username = 'nathan'; + $password = 'opensesame'; + + $r = Request::get('http://example.com/') + ->authenticateWithDigest($username, $password); + + $this->assertEquals($username, $r->username); + $this->assertEquals($password, $r->password); + $this->assertTrue($r->hasDigestAuth()); + } + function testJsonResponseParse() { $req = Request::init()->sendsAndExpects(Mime::JSON);