@@ -23,10 +23,11 @@ To use basic authentication, you need to attach `BasicAuthListener` to http clie
2323 $response = $user->get();
2424 ```
2525
26- ### OAuth authorization
26+ ### OAuth1 authorization
2727This library comes with a ` OAuthListener ` which will sign all requests for you. All you need to do is to attach the listener to
2828http client with oauth credentials before making a request.
2929
30+ #### OAuth1 1-legged
3031 ``` php
3132 // OAuth 1-legged example
3233 // You can create a new consumer at: https://bitbucket.org/account/user/<username or team >/api
@@ -44,8 +45,174 @@ http client with oauth credentials before making a request.
4445 $response = $user->get();
4546 ```
4647
48+ #### OAuth1 3-legged
49+
50+ You can use any 3rd party library to complete this [ flow] [ 3 ] and set OAuth credentials when you instantiate ` OAuthListener ` .
51+
52+ In the following example [ PHP League's OAuth 1.0 Client] [ 4 ] is used.
53+
54+ ``` php
55+ session_start();
56+
57+ // @see: https://bitbucket.org/account/user/<username >/api
58+ $oauth_params = array(
59+ 'identifier' => 'aaa',
60+ 'secret' => 'bbb',
61+ 'callback_uri' => 'http://example.com/oauth1_3legged.php'
62+ );
63+
64+ $server = new League\OAuth1\Client\Server\Bitbucket($oauth_params);
65+
66+ if (array_key_exists('profile', $_GET)) {
67+ if (false === array_key_exists('bb_credentials', $_SESSION)) {
68+ header('Location: ' . $oauth_params['callback_uri']);
69+ exit;
70+ }
71+
72+ $oauth_params = array_merge(unserialize($_SESSION['bb_credentials']), array(
73+ 'oauth_consumer_key' => $oauth_params['identifier'],
74+ 'oauth_consumer_secret' => $oauth_params['secret'],
75+ 'oauth_callback' => $oauth_params['callback_uri'],
76+ ));
77+
78+
79+ $bitbucket = new \Bitbucket\API\Api();
80+ $bitbucket->getClient()->addListener(
81+ new \Bitbucket\API\Http\Listener\OAuthListener($oauth_params)
82+ );
83+
84+ /** @var \Bitbucket\API\User $user */
85+ $user = $bitbucket->api('User');
86+
87+ $profile = json_decode($user->get()->getContent(), true);
88+ echo sprintf('<a href =" ?logout" >Logout %s</a >', $profile['user']['username']);
89+
90+ // show all user repositories
91+ echo '<h3 >My repositories:</h3 ><ul >';
92+ array_walk($profile['repositories'], function($repository) {
93+ $repositoryUrl = str_replace('/1.0/repositories/', '', $repository['resource_uri']);
94+ echo sprintf(
95+ '<li ><a href =" http://bitbucket.org/%s" >%s</a ></li >', $repositoryUrl, $repository['name']
96+ );
97+ });
98+ echo '</ul >';
99+ exit;
100+ } elseif (array_key_exists('login', $_GET)) {
101+ // Retrieve temporary credentials
102+ $temporaryCredentials = $server->getTemporaryCredentials();
103+
104+ // Store credentials in the session, we'll need them later
105+ $_SESSION['temporary_credentials'] = serialize($temporaryCredentials);
106+ session_write_close();
107+
108+ // Second part of OAuth 1.0 authentication is to redirect the
109+ // resource owner to the login screen on the server.
110+ $server->authorize($temporaryCredentials);
111+ exit;
112+ } elseif (array_key_exists('oauth_token', $_GET) && array_key_exists('oauth_verifier', $_GET)) {
113+ // Retrieve the temporary credentials we saved before
114+ $temporaryCredentials = unserialize($_SESSION['temporary_credentials']);
115+
116+ // We will now retrieve token credentials from the server
117+ $tokenCredentials = $server->getTokenCredentials(
118+ $temporaryCredentials, $_GET['oauth_token'], $_GET['oauth_verifier']
119+ );
120+
121+ $oauth_params = array(
122+ 'oauth_token' => $tokenCredentials->getIdentifier(),
123+ 'oauth_token_secret' => $tokenCredentials->getSecret()
124+ );
125+
126+ unset($_SESSION['temporary_credentials'], $_SESSION['token_credentials']);
127+ $_SESSION['bb_credentials'] = serialize($oauth_params);
128+ session_write_close();
129+
130+ // redirect the user to the profile page, in order to fetch his/her information.
131+ header('Location: '.$oauth_params['callback_uri'].'?profile');
132+ exit;
133+ } elseif (array_key_exists('logout', $_GET)) {
134+ unset($_SESSION['bb_credentials']);
135+ session_write_close();
136+ }
137+
138+ echo '<a href =" ?login" >Login with BitBucket!</a >';
139+ ```
140+
141+ ### OAuth2 authorization
142+
143+ You can use ` OAuth2Listener ` in order to make authorized requests using version 2 of OAuth protocol.
144+
145+ #### OAuth2 client credentials (_ 2-legged flow_ )
146+
147+ ``` php
148+ // @see: https://bitbucket.org/account/user/<username or team >/api
149+ $oauth_params = array(
150+ 'client_id' => 'aaa',
151+ 'client_secret' => 'bbb'
152+ );
153+
154+ $bitbucket = new \Bitbucket\API\Api();
155+ $bitbucket->getClient()->addListener(
156+ new \Bitbucket\API\Http\Listener\OAuth2Listener($oauth_params)
157+ );
158+
159+ $repositories = $bitbucket->api('Repositories');
160+ $response = $repositories->all('my_account'); // should include private repositories
161+ ```
162+
163+ #### OAuth2 Authorization code (_ 3-legged flow_ )
164+
165+ You can use any 3rd party library to complete this [ flow] [ 3 ] and set ` access_token ` option when you instantiate ` OAuth2Listener ` .
166+
167+ In the following example [ PHP League's OAuth 2.0 Client] [ 1 ] is used with [ Bitbucket Provider] [ 2 ] .
168+
169+ ``` php
170+ session_start();
171+
172+ $provider = new Stevenmaguire\OAuth2\Client\Provider\Bitbucket([
173+ 'clientId' => $_ENV['bitbucket_consumer_key'],
174+ 'clientSecret' => $_ENV['bitbucket_consumer_secret'],
175+ 'redirectUri' => 'http://example.com/bitbucket_login.php'
176+ ]);
177+ if (!isset($_GET['code'])) {
178+
179+ // If we don't have an authorization code then get one
180+ $authUrl = $provider->getAuthorizationUrl();
181+ $_SESSION['oauth2state'] = $provider->getState();
182+ header('Location: '.$authUrl);
183+ exit;
184+
185+ // Check given state against previously stored one to mitigate CSRF attack
186+ } elseif (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) {
187+
188+ unset($_SESSION['oauth2state']);
189+ exit('Invalid state');
190+
191+ } else {
192+
193+ // Try to get an access token (using the authorization code grant)
194+ $token = $provider->getAccessToken('authorization_code', [
195+ 'code' => $_GET['code']
196+ ]);
197+
198+ $bitbucket = new Bitbucket\API\Repositories();
199+ $bitbucket->getClient()->addListener(
200+ new \Bitbucket\API\Http\Listener\OAuth2Listener(
201+ array('access_token' => $token->getToken())
202+ )
203+ );
204+
205+ echo $bitbucket->all('my_account')->getContent(); // should include private repositories
206+ }
207+ ```
208+
47209----
48210
49211#### Related:
50212 * [ Authentication @ BB Wiki] ( https://confluence.atlassian.com/display/BITBUCKET/Use+the+Bitbucket+REST+APIs#UsetheBitbucketRESTAPIs-Authentication )
51213 * [ OAuth on Bitbucket @ BB Wiki] ( https://confluence.atlassian.com/display/BITBUCKET/OAuth+on+Bitbucket )
214+
215+ [ 1 ] : http://oauth2-client.thephpleague.com/
216+ [ 2 ] : https://github.com/stevenmaguire/oauth2-bitbucket
217+ [ 3 ] : http://oauthbible.com/#oauth-2-three-legged
218+ [ 4 ] : https://github.com/thephpleague/oauth1-client
0 commit comments