You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/authentication.md
+22-46Lines changed: 22 additions & 46 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,64 +5,40 @@ Although you can access any public data without authentication, you need to auth
5
5
Bitbucket provides Basic and OAuth authentication.
6
6
7
7
### Basic authentication
8
-
To use basic authentication, you need to instantiate `Basic` class from `Bitbucket\API\Authentication` namespace and pass it to `setCredentials()` method, before making first request.
9
-
10
-
```php
11
-
$auth = new Bitbucket\API\Authentication\Basic($bb_user, $bb_pass);
8
+
To use basic authentication, you need to attach `BasicAuthListener` to http client with your username and password.
12
9
10
+
```php
13
11
$user = new Bitbucket\API\User();
14
-
$user->setCredentials($auth);
15
-
16
-
// some code
12
+
$user->getClient()->addListener(
13
+
new Bitbucket\API\Http\Listener\BasicAuthListener($bb_user, $bb_pass)
14
+
);
17
15
18
-
$invitation = new Bitbucket\API\Invitations();
19
-
$invitation->setCredentials($auth);
20
-
```
21
-
22
-
If you need to change credentials, two methods are available for this:
23
-
```php
24
-
$auth->setUsername('username');
25
-
$auth->setPassword('password');
16
+
// now you can access protected endpoints as $bb_user
17
+
$response = $user->get();
26
18
```
27
19
28
20
----
29
21
30
22
### 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.
23
+
This library comes with a `OAuthListener` which will sign all requests for you. All you need to do is to attach the listener to http client with oauth credentials before making a request.
35
24
36
25
```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
26
+
// OAuth 1-legged example
27
+
// You can create a new consumer at: https://bitbucket.org/account/user/<usernameorteam>/api
28
+
$oauth_params = array(
29
+
'oauth_consumer_key' => 'aaa',
30
+
'oauth_consumer_secret' => 'bbb'
31
+
);
32
+
33
+
$user = new Bitbucket\API\User;
34
+
$user->getClient()->addListener(
35
+
new Bitbucket\API\Http\Listener\OAuthListener($oauth_params)
36
+
);
37
+
38
+
// now you can access protected endpoints as consumer owner
39
+
$response = $user->get();
59
40
```
60
41
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_ )
0 commit comments