|
11 | 11 |
|
12 | 12 | namespace Xabbuh\XApi\Client; |
13 | 13 |
|
| 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; |
14 | 19 | use Http\Client\Common\Plugin\AuthenticationPlugin; |
15 | 20 | use Http\Client\Common\PluginClient; |
16 | 21 | use Http\Client\HttpClient; |
17 | 22 | use Http\Discovery\HttpClientDiscovery; |
18 | 23 | use Http\Discovery\MessageFactoryDiscovery; |
19 | 24 | use Http\Message\Authentication\BasicAuth; |
20 | 25 | use Http\Message\RequestFactory; |
| 26 | +use Xabbuh\Http\Authentication\OAuth1; |
21 | 27 | use Xabbuh\XApi\Client\Request\Handler; |
22 | 28 | use Xabbuh\XApi\Serializer\SerializerFactoryInterface; |
23 | 29 | use Xabbuh\XApi\Serializer\SerializerRegistry; |
@@ -46,7 +52,10 @@ final class XApiClientBuilder implements XApiClientBuilderInterface |
46 | 52 | private $version; |
47 | 53 | private $username; |
48 | 54 | private $password; |
49 | | - private $oAuthCredentials; |
| 55 | + private $consumerKey; |
| 56 | + private $consumerSecret; |
| 57 | + private $accessToken; |
| 58 | + private $tokenSecret; |
50 | 59 |
|
51 | 60 | public function __construct(SerializerFactoryInterface $serializerFactory = null) |
52 | 61 | { |
@@ -109,12 +118,10 @@ public function setAuth($username, $password) |
109 | 118 | */ |
110 | 119 | public function setOAuthCredentials($consumerKey, $consumerSecret, $token, $tokenSecret) |
111 | 120 | { |
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; |
118 | 125 |
|
119 | 126 | return $this; |
120 | 127 | } |
@@ -156,8 +163,24 @@ public function build() |
156 | 163 | $serializerRegistry->setActorSerializer($this->serializerFactory->createActorSerializer()); |
157 | 164 | $serializerRegistry->setDocumentDataSerializer($this->serializerFactory->createDocumentDataSerializer()); |
158 | 165 |
|
| 166 | + $plugins = array(); |
| 167 | + |
159 | 168 | 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); |
161 | 184 | } |
162 | 185 |
|
163 | 186 | $version = null === $this->version ? '1.0.1' : $this->version; |
|
0 commit comments