Skip to content

Commit f951f6e

Browse files
committed
[DOCS] OAuth1 3-legged example
Added example on how to use this library in combination with a 3rd party OAuth1 client in a 3-legged flow. Ref: #33
1 parent a352e9f commit f951f6e

File tree

1 file changed

+96
-1
lines changed

1 file changed

+96
-1
lines changed

docs/examples/authentication.md

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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
2727
This library comes with a `OAuthListener` which will sign all requests for you. All you need to do is to attach the listener to
2828
http 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,6 +45,99 @@ 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+
47141
### OAuth2 authorization
48142

49143
You can use `OAuth2Listener` in order to make authorized requests using version 2 of OAuth protocol.
@@ -121,3 +215,4 @@ In the following example [PHP League's OAuth 2.0 Client][1] is used with [Bitbuc
121215
[1]: http://oauth2-client.thephpleague.com/
122216
[2]: https://github.com/stevenmaguire/oauth2-bitbucket
123217
[3]: http://oauthbible.com/#oauth-2-three-legged
218+
[4]: https://github.com/thephpleague/oauth1-client

0 commit comments

Comments
 (0)