Skip to content

Commit 0a441b3

Browse files
committed
closes gentlero#5
1 parent 208b2fd commit 0a441b3

File tree

3 files changed

+218
-2
lines changed

3 files changed

+218
-2
lines changed

docs/authentication.md

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,44 @@ $auth->setPassword('password');
2727

2828
----
2929

30-
### OAuth authentication
31-
Soon
30+
### OAuth authorization
31+
To use OAuth, you will need to instantiate `OAuth` class from `Bitbucket\API\Authentication` namespace and pass it to `setCredentials()` method, before making first request.
32+
`OAuth` accepts a string or array as constructor. Those parameters are actually OAuth parameters, that have been previously signed.
33+
34+
**NOTE:** `OAuth` class will _NOT_ sign the request. It will just build the authorization header from previously signed OAuth parameters.
35+
36+
```php
37+
// use 3rd party OAuth library to sign the request and pass already signed parameters to `OAuth` class.
38+
39+
$auth = new Bitbucket\API\Authentication\OAuth(array(
40+
'oauth_version' => '1.0',
41+
'oauth_nonce' => 'aaaaaaaaaaaaaaa',
42+
'oauth_timestamp' => '1370771799',
43+
'oauth_consumer_key' => 'xxxxxxxxxxxxxxx',
44+
'oauth_signature_method' => 'HMAC-SHA1',
45+
'oauth_signature' => 'yyyyyyyyyyyyyyy'
46+
));
47+
48+
$user = new Bitbucket\API\User();
49+
$user->setCredentials($auth);
50+
```
51+
52+
You can also send the parameters as string, instead of array:
53+
```php
54+
// use 3rd party OAuth library to sign the request and pass already signed parameters to `OAuth` class.
55+
56+
$auth = new Bitbucket\API\Authentication\OAuth('oauth_version="1.0",oauth_nonce="aaaaaaaaaaaaaaa",oauth_timestamp="1370771799",oauth_consumer_key="xxxxxxxxxxxxxxx",oauth_signature_method="HMAC-SHA1",oauth_signature="yyyyyyyyyyyyyyy"');
57+
58+
// rest of the code
59+
```
60+
61+
**NOTES:**
62+
63+
* `OAuth` class will prepend `Authorization: OAuth` to those parameters and will add the result to current request header.
64+
* When choosing an OAuth library, take into consideration the fact that [Bitbucket](https://bitbucket.org) uses OAuth 1.0a ( _3-Legged and 2-Legged_ )
65+
66+
----
67+
68+
#### Related:
69+
* [Authentication @ BB Wiki](https://confluence.atlassian.com/display/BITBUCKET/Use+the+Bitbucket+REST+APIs#UsetheBitbucketRESTAPIs-Authentication)
70+
* [OAuth on Bitbucket @ BB Wiki](https://confluence.atlassian.com/display/BITBUCKET/OAuth+on+Bitbucket)
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the bitbucket_api package.
5+
*
6+
* (c) Alexandru G. <alex@gentle.ro>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Bitbucket\API\Authentication;
13+
14+
use Buzz\Message\RequestInterface;
15+
16+
/**
17+
* OAuth class
18+
*
19+
* Build authorization header from previously signed OAuth parameters
20+
* and pass it to current request.
21+
*
22+
* @author Alexandru G. <alex@gentle.ro>
23+
*/
24+
class OAuth implements AuthenticationInterface
25+
{
26+
/**
27+
* @var string
28+
*/
29+
protected $authHeader;
30+
31+
/**
32+
* @param string|array $params OAuth signed parameters
33+
*/
34+
public function __construct($params)
35+
{
36+
$this->setAuthHeader($params);
37+
}
38+
39+
/**
40+
* Get authorization header parameters
41+
*
42+
* @access public
43+
* @return string
44+
*/
45+
public function getAuthHeader()
46+
{
47+
return $this->authHeader;
48+
}
49+
50+
/**
51+
* Set authorization header parameters
52+
*
53+
* @access public
54+
* @param string|array $params OAuth signed parameters
55+
* @return void
56+
*/
57+
public function setAuthHeader($params)
58+
{
59+
if (is_array($params)) {
60+
$this->authHeader = $this->toHeader($params);
61+
} else {
62+
63+
if (strpos($params, 'Authorization: ') !== false) {
64+
$params = str_replace('Authorization: ', '', $params);
65+
}
66+
67+
$this->authHeader = $params;
68+
}
69+
}
70+
71+
/**
72+
* Build authorization header from array
73+
*
74+
* @access public
75+
* @param array $params
76+
* @return string
77+
*/
78+
protected function toHeader(array $params)
79+
{
80+
$out = '';
81+
$last = end($params);
82+
83+
foreach ($params as $k => $v) {
84+
$out .= $k.'="'.$v.'"';
85+
86+
if ($v != $last) {
87+
$out .= ',';
88+
}
89+
}
90+
91+
return $out;
92+
}
93+
94+
/**
95+
* {@inheritdoc}
96+
*/
97+
public function authenticate(RequestInterface $request)
98+
{
99+
if (strpos($this->authHeader, 'OAuth') === false) {
100+
$this->authHeader = 'OAuth '.$this->authHeader;
101+
}
102+
103+
$request->addHeader('Authorization: '.$this->authHeader);
104+
105+
return $request;
106+
}
107+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
namespace Bitbucket\Tests\API\Authentication;
4+
5+
use Bitbucket\Tests\API as Tests;
6+
use Bitbucket\API\Authentication;
7+
use Buzz\Message\Request;
8+
9+
class OAuthTest extends Tests\TestCase
10+
{
11+
public function testGetSetAuthHeader()
12+
{
13+
// from string
14+
$auth = new Authentication\OAuth('Authorization: dummy');
15+
$this->assertEquals('dummy', $auth->getAuthHeader());
16+
17+
// from array
18+
$auth->setAuthHeader($this->getArrayParams());
19+
$this->assertEquals($this->getStringParams(), $auth->getAuthHeader());
20+
}
21+
22+
public function testAuthenticateFromArraySuccess()
23+
{
24+
$params = $this->getArrayParams();
25+
$auth = new Authentication\OAuth($params);
26+
$request = new Request();
27+
28+
$auth->authenticate($request);
29+
30+
// check if header is set
31+
$header = $request->getHeader('Authorization');
32+
$this->assertNotEmpty($header);
33+
34+
// check if header was built correctly
35+
$this->assertEquals('OAuth '.$this->getStringParams(), $header);
36+
}
37+
38+
public function testAuthenticateFromStringSuccess()
39+
{
40+
$params = $this->getStringParams();
41+
$auth = new Authentication\OAuth($params);
42+
$request = new Request();
43+
44+
$auth->authenticate($request);
45+
46+
// check if header is set
47+
$header = $request->getHeader('Authorization');
48+
$this->assertNotEmpty($header);
49+
50+
// check if header was built correctly
51+
$this->assertEquals('OAuth '.$params, $header);
52+
}
53+
54+
private function getArrayParams()
55+
{
56+
return array(
57+
'oauth_version' => '1.0',
58+
'oauth_nonce' => 'aaaaaaaaaaaaaaa',
59+
'oauth_timestamp' => '1370771799',
60+
'oauth_consumer_key' => 'xxxxxxxxxxxxxxx',
61+
'oauth_signature_method' => 'HMAC-SHA1',
62+
'oauth_signature' => 'yyyyyyyyyyyyyyy'
63+
);
64+
}
65+
66+
private function getStringParams()
67+
{
68+
return 'oauth_version="1.0",oauth_nonce="aaaaaaaaaaaaaaa",oauth_timestamp="1370771799",oauth_consumer_key="xxxxxxxxxxxxxxx",oauth_signature_method="HMAC-SHA1",oauth_signature="yyyyyyyyyyyyyyy"';
69+
}
70+
}

0 commit comments

Comments
 (0)