Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Fixed basic auth headers for curl adapter #1048

Merged
merged 5 commits into from

2 participants

@darh

Auth info needs to be pushed into curl in a special way

@akrabat
Collaborator

Can you supply a unit test that shows that this code works please?

@darh

Well this was kinda useless. I have no idea why auth headers didn't got set so I've added curl_setopt's as I've always did for http-auth. f406054 removes this "fix" + there is also a test for auth-headers if you wish to include it.

@akrabat akrabat commented on the diff
tests/Zend/Http/Client/CurlTest.php
@@ -313,4 +313,26 @@ public function testHeadRequest()
$this->client->send();
$this->assertEquals('', $this->client->getResponse()->getBody());
}
+
+ public function testAuthorizeHeader()
+ {
+ // We just need someone to talk to
+ $this->client->setUri($this->baseuri. 'testHttpAuth.php');
+ $adapter = new Adapter\Curl();
+ $this->client->setAdapter($adapter);
+
+ $uid = 'alice';
+ $pwd = 'secret';
+
+ $hash = base64_encode($uid . ':' . $pwd);
+ $header = 'Authorization: Basic ' . $hash;
@akrabat Collaborator
akrabat added a note

What are $hash and $header for?

@darh
darh added a note

$hash is (last) part of $header and $header is for assertion in the last line of the test. Good enough?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@akrabat akrabat was assigned
@akrabat akrabat merged commit f406054 into zendframework:master
@akrabat
Collaborator

Thank you.

In general, you'll find it easier if you do each pull request within it's own branch:

git checkout -b hotfix/my-branch-name
... do the work ...
git commit
git push origin hotfix/my-branch-name

and then issue the pull request from github from your branch to the ZF2 master

See http://framework.zend.com/wiki/display/ZFDEV2/Zend+Framework+Git+Guide for full details

@darh

Thanks for info. Will try and follow the guide nex time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 14, 2012
  1. Fixed basic auth in Curl adapter for HTTP client

    Denis Arh authored
Commits on Apr 20, 2012
Commits on Apr 21, 2012
  1. Undo basic auth workaround for Curl adapter for HTTP client + added t…

    Denis Arh authored
    …ests for authentication
This page is out of date. Refresh to see the latest.
View
9 library/Zend/Http/Client/Adapter/Curl.php
@@ -324,7 +324,7 @@ public function write($method, $uri, $httpVersion = 1.1, $headers = array(), $bo
$curlMethod = CURLOPT_CUSTOMREQUEST;
$curlValue = "TRACE";
break;
-
+
case 'HEAD' :
$curlMethod = CURLOPT_CUSTOMREQUEST;
$curlValue = "HEAD";
@@ -360,6 +360,13 @@ public function write($method, $uri, $httpVersion = 1.1, $headers = array(), $bo
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
}
+ // Treating basic auth headers in a special way
+ if (array_key_exists('Authorization', $headers) && 'Basic' == substr($headers['Authorization'], 0, 5)) {
+ curl_setopt($this->curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
+ curl_setopt($this->curl, CURLOPT_USERPWD, base64_decode(substr($headers['Authorization'], 6)));
+ unset($headers['Authorization']);
+ }
+
// set additional headers
$headers['Accept'] = '';
$curlHeaders = array();
View
24 tests/Zend/Http/Client/CurlTest.php
@@ -300,7 +300,7 @@ public function testGetCurlHandle()
$this->assertTrue(is_resource($adapter->getHandle()));
}
-
+
/**
* @group ZF-9857
*/
@@ -313,4 +313,26 @@ public function testHeadRequest()
$this->client->send();
$this->assertEquals('', $this->client->getResponse()->getBody());
}
+
+ public function testAuthorizeHeader()
+ {
+ // We just need someone to talk to
+ $this->client->setUri($this->baseuri. 'testHttpAuth.php');
+ $adapter = new Adapter\Curl();
+ $this->client->setAdapter($adapter);
+
+ $uid = 'alice';
+ $pwd = 'secret';
+
+ $hash = base64_encode($uid . ':' . $pwd);
+ $header = 'Authorization: Basic ' . $hash;
@akrabat Collaborator
akrabat added a note

What are $hash and $header for?

@darh
darh added a note

$hash is (last) part of $header and $header is for assertion in the last line of the test. Good enough?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+
+ $this->client->setAuth($uid, $pwd);
+ $res = $this->client->send();
+
+ $curlInfo = curl_getinfo($adapter->getHandle());
+ $this->assertArrayHasKey('request_header', $curlInfo, 'Expecting request_header in curl_getinfo() return value');
+
+ $this->assertContains($header, $curlInfo['request_header'], 'Expecting valid basic authorization header');
+ }
}
Something went wrong with that request. Please try again.