Skip to content

Commit f97d373

Browse files
committed
add back OAuth1 authentication support
1 parent c3f3356 commit f97d373

File tree

6 files changed

+67
-10
lines changed

6 files changed

+67
-10
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ matrix:
2121
- php: 5.4
2222
env: $COMPOSER_OPTIONS="--prefer-lowest --ignore-platform-reqs"
2323
- php: 5.6
24-
env: PACKAGES="php-http/discovery:^1.0 php-http/guzzle6-adapter:^1.0 php-http/message:^1.0"
24+
env: PACKAGES="php-http/discovery:^1.0 php-http/guzzle6-adapter:^1.0 php-http/message:^1.0 xabbuh/oauth1-authentication:~0.1"
2525
- php: 7.0
2626
env: xdebug="yes"
2727

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ CHANGELOG
4343
You can avoid calling `setHttpClient()` and `setRequestFactory` by installing
4444
the [HTTP discovery](http://php-http.org/en/latest/discovery.html) package.
4545

46+
* The `xabbuh/oauth1-authentication` package now must be installed if you want
47+
to use OAuth1 authentication.
48+
4649
* Bumped the required versions of all `php-xapi` packages to the `1.x` release
4750
series.
4851

UPGRADE.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ Upgrading from 0.4 to 0.5
4343
You can avoid calling `setHttpClient()` and `setRequestFactory` by installing
4444
the [HTTP discovery](http://php-http.org/en/latest/discovery.html) package.
4545

46+
* The `xabbuh/oauth1-authentication` package now must be installed if you want
47+
to use OAuth1 authentication.
48+
4649
* A second optional `$attachments` argument (defaulting to `true`) has been added
4750
to the `getStatement()`, `getVoidedStatement()`, and `getStatements()` methods
4851
of the `StatementsApiClient` class and the `StatementsApiClientInterface`.

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
},
3333
"minimum-stability": "dev",
3434
"suggest": {
35-
"php-http/discovery": "For automatic discovery of HTTP clients and request factories"
35+
"php-http/discovery": "For automatic discovery of HTTP clients and request factories",
36+
"xabbuh/oauth1-authentication": "For OAuth1 authentication support"
3637
},
3738
"conflict": {
3839
"xabbuh/xapi-client": "*"

spec/XApiClientBuilderSpec.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,33 @@ function it_throws_an_exception_if_the_base_uri_is_not_configured(HttpClient $ht
8585
$this->shouldThrow('\LogicException')->during('build');
8686
}
8787

88+
function it_throws_an_exception_when_oauth_credentials_are_configured_but_the_auth_package_is_missing(HttpClient $httpClient, RequestFactory $requestFactory)
89+
{
90+
if (class_exists('Xabbuh\Http\Authentication\OAuth1')) {
91+
throw new SkippingException('OAuth1 credentials can be used when the "xabbuh/oauth1-authentication" package is present.');
92+
}
93+
94+
$this->setHttpClient($httpClient);
95+
$this->setRequestFactory($requestFactory);
96+
$this->setBaseUrl('http://example.com/xapi/');
97+
$this->setOAuthCredentials('consumer_key', 'consumer_secret', 'access_token', 'token_secret');
98+
99+
$this->shouldThrow(new \LogicException('The "xabbuh/oauth1-authentication package is needed to use OAuth1 authorization.'))->during('build');
100+
}
101+
102+
function it_accepts_oauth_credentials_when_the_auth_package_is_present(HttpClient $httpClient, RequestFactory $requestFactory)
103+
{
104+
if (!class_exists('Xabbuh\Http\Authentication\OAuth1')) {
105+
throw new SkippingException('OAuth1 credentials cannot be used when the "xabbuh/oauth1-authentication" package is missing.');
106+
}
107+
108+
$this->setHttpClient($httpClient);
109+
$this->setRequestFactory($requestFactory);
110+
$this->setBaseUrl('http://example.com/xapi/');
111+
$this->setOAuthCredentials('consumer_key', 'consumer_secret', 'access_token', 'token_secret');
112+
$this->build();
113+
}
114+
88115
private function isAbleToDiscoverHttpClient()
89116
{
90117
try {

src/XApiClientBuilder.php

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,19 @@
1111

1212
namespace Xabbuh\XApi\Client;
1313

14+
use ApiClients\Tools\Psr7\Oauth1\Definition\AccessToken;
15+
use ApiClients\Tools\Psr7\Oauth1\Definition\ConsumerKey;
16+
use ApiClients\Tools\Psr7\Oauth1\Definition\ConsumerSecret;
17+
use ApiClients\Tools\Psr7\Oauth1\Definition\TokenSecret;
18+
use ApiClients\Tools\Psr7\Oauth1\RequestSigning\RequestSigner;
1419
use Http\Client\Common\Plugin\AuthenticationPlugin;
1520
use Http\Client\Common\PluginClient;
1621
use Http\Client\HttpClient;
1722
use Http\Discovery\HttpClientDiscovery;
1823
use Http\Discovery\MessageFactoryDiscovery;
1924
use Http\Message\Authentication\BasicAuth;
2025
use Http\Message\RequestFactory;
26+
use Xabbuh\Http\Authentication\OAuth1;
2127
use Xabbuh\XApi\Client\Request\Handler;
2228
use Xabbuh\XApi\Serializer\SerializerFactoryInterface;
2329
use Xabbuh\XApi\Serializer\SerializerRegistry;
@@ -46,7 +52,10 @@ final class XApiClientBuilder implements XApiClientBuilderInterface
4652
private $version;
4753
private $username;
4854
private $password;
49-
private $oAuthCredentials;
55+
private $consumerKey;
56+
private $consumerSecret;
57+
private $accessToken;
58+
private $tokenSecret;
5059

5160
public function __construct(SerializerFactoryInterface $serializerFactory = null)
5261
{
@@ -109,12 +118,10 @@ public function setAuth($username, $password)
109118
*/
110119
public function setOAuthCredentials($consumerKey, $consumerSecret, $token, $tokenSecret)
111120
{
112-
$this->oAuthCredentials = array(
113-
'consumer_key' => $consumerKey,
114-
'consumer_secret' => $consumerSecret,
115-
'token' => $token,
116-
'token_secret' => $tokenSecret,
117-
);
121+
$this->consumerKey = $consumerKey;
122+
$this->consumerSecret = $consumerSecret;
123+
$this->accessToken = $token;
124+
$this->tokenSecret = $tokenSecret;
118125

119126
return $this;
120127
}
@@ -156,8 +163,24 @@ public function build()
156163
$serializerRegistry->setActorSerializer($this->serializerFactory->createActorSerializer());
157164
$serializerRegistry->setDocumentDataSerializer($this->serializerFactory->createDocumentDataSerializer());
158165

166+
$plugins = array();
167+
159168
if (null !== $this->username && null !== $this->password) {
160-
$httpClient = new PluginClient($httpClient, array(new AuthenticationPlugin(new BasicAuth($this->username, $this->password))));
169+
$plugins[] = new AuthenticationPlugin(new BasicAuth($this->username, $this->password));
170+
}
171+
172+
if (null !== $this->consumerKey && null !== $this->consumerSecret && null !== $this->accessToken && null !== $this->tokenSecret) {
173+
if (!class_exists('Xabbuh\Http\Authentication\OAuth1')) {
174+
throw new \LogicException('The "xabbuh/oauth1-authentication package is needed to use OAuth1 authorization.');
175+
}
176+
177+
$requestSigner = new RequestSigner(new ConsumerKey($this->consumerKey), new ConsumerSecret($this->consumerSecret));
178+
$oauth = new OAuth1($requestSigner, new AccessToken($this->accessToken), new TokenSecret($this->tokenSecret));
179+
$plugins[] = new AuthenticationPlugin($oauth);
180+
}
181+
182+
if (!empty($plugins)) {
183+
$httpClient = new PluginClient($httpClient, $plugins);
161184
}
162185

163186
$version = null === $this->version ? '1.0.1' : $this->version;

0 commit comments

Comments
 (0)