1 | <?php |
||
16 | abstract class AbstractService extends BaseAbstractService implements ServiceInterface |
||
17 | { |
||
18 | /** @const OAUTH_VERSION */ |
||
19 | const OAUTH_VERSION = 1; |
||
20 | |||
21 | /** @var SignatureInterface */ |
||
22 | protected $signature; |
||
23 | |||
24 | /** @var UriInterface|null */ |
||
25 | protected $baseApiUri; |
||
26 | |||
27 | /** |
||
28 | * {@inheritDoc} |
||
29 | */ |
||
30 | public function __construct( |
||
31 | CredentialsInterface $credentials, |
||
32 | ClientInterface $httpClient, |
||
33 | TokenStorageInterface $storage, |
||
34 | SignatureInterface $signature, |
||
35 | UriInterface $baseApiUri = null |
||
36 | ) { |
||
37 | parent::__construct($credentials, $httpClient, $storage); |
||
38 | |||
39 | $this->signature = $signature; |
||
40 | $this->baseApiUri = $baseApiUri; |
||
41 | |||
42 | $this->signature->setHashingAlgorithm($this->getSignatureMethod()); |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * {@inheritDoc} |
||
47 | */ |
||
48 | public function requestRequestToken() |
||
49 | { |
||
50 | $authorizationHeader = array('Authorization' => $this->buildAuthorizationHeaderForTokenRequest()); |
||
51 | $headers = array_merge($authorizationHeader, $this->getExtraOAuthHeaders()); |
||
52 | |||
53 | $responseBody = $this->httpClient->retrieveResponse($this->getRequestTokenEndpoint(), array(), $headers); |
||
54 | |||
55 | $token = $this->parseRequestTokenResponse($responseBody); |
||
56 | $this->storage->storeAccessToken($this->service(), $token); |
||
57 | |||
58 | return $token; |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * {@inheritdoc} |
||
63 | */ |
||
64 | public function getAuthorizationUri(array $additionalParameters = array()) |
||
65 | { |
||
66 | // Build the url |
||
67 | $url = clone $this->getAuthorizationEndpoint(); |
||
68 | foreach ($additionalParameters as $key => $val) { |
||
69 | $url->addToQuery($key, $val); |
||
70 | } |
||
71 | |||
72 | return $url; |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * {@inheritDoc} |
||
77 | */ |
||
78 | public function requestAccessToken($token, $verifier, $tokenSecret = null) |
||
79 | { |
||
80 | if (is_null($tokenSecret)) { |
||
81 | $storedRequestToken = $this->storage->retrieveAccessToken($this->service()); |
||
82 | $tokenSecret = $storedRequestToken->getRequestTokenSecret(); |
||
83 | } |
||
84 | $this->signature->setTokenSecret($tokenSecret); |
||
85 | |||
86 | $bodyParams = array( |
||
87 | 'oauth_verifier' => $verifier, |
||
88 | ); |
||
89 | |||
90 | $authorizationHeader = array( |
||
91 | 'Authorization' => $this->buildAuthorizationHeaderForAccessRequest( |
||
92 | 'POST', |
||
93 | $this->getAccessTokenEndpoint(), |
||
94 | $this->storage->retrieveAccessToken($this->service()), |
||
|
|||
95 | $bodyParams |
||
96 | ) |
||
97 | ); |
||
98 | |||
99 | $headers = array_merge($authorizationHeader, $this->getExtraOAuthHeaders()); |
||
100 | |||
101 | $responseBody = $this->httpClient->retrieveResponse($this->getAccessTokenEndpoint(), $bodyParams, $headers); |
||
102 | |||
103 | $token = $this->parseAccessTokenResponse($responseBody); |
||
104 | $this->storage->storeAccessToken($this->service(), $token); |
||
105 | |||
106 | return $token; |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * Refreshes an OAuth1 access token |
||
111 | * @param TokenInterface $token |
||
112 | * @return TokenInterface $token |
||
113 | */ |
||
114 | public function refreshAccessToken(TokenInterface $token) |
||
117 | |||
118 | /** |
||
119 | * Sends an authenticated API request to the path provided. |
||
120 | * If the path provided is not an absolute URI, the base API Uri (must be passed into constructor) will be used. |
||
121 | * |
||
122 | * @param string|UriInterface $path |
||
123 | * @param string $method HTTP method |
||
124 | * @param array $body Request body if applicable (key/value pairs) |
||
125 | * @param array $extraHeaders Extra headers if applicable. |
||
126 | * These will override service-specific any defaults. |
||
127 | * |
||
128 | * @return string |
||
129 | */ |
||
130 | public function request($path, $method = 'GET', $body = null, array $extraHeaders = array()) |
||
131 | { |
||
132 | $uri = $this->determineRequestUriFromPath($path, $this->baseApiUri); |
||
133 | |||
134 | /** @var $token StdOAuth1Token */ |
||
135 | $token = $this->storage->retrieveAccessToken($this->service()); |
||
136 | $extraHeaders = array_merge($this->getExtraApiHeaders(), $extraHeaders); |
||
137 | $authorizationHeader = array( |
||
138 | 'Authorization' => $this->buildAuthorizationHeaderForAPIRequest($method, $uri, $token, $body) |
||
139 | ); |
||
140 | $headers = array_merge($authorizationHeader, $extraHeaders); |
||
141 | |||
142 | return $this->httpClient->retrieveResponse($uri, $body, $headers, $method); |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * Return any additional headers always needed for this service implementation's OAuth calls. |
||
147 | * |
||
148 | * @return array |
||
149 | */ |
||
150 | protected function getExtraOAuthHeaders() |
||
151 | { |
||
152 | return array(); |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * Return any additional headers always needed for this service implementation's API calls. |
||
157 | * |
||
158 | * @return array |
||
159 | */ |
||
160 | protected function getExtraApiHeaders() |
||
161 | { |
||
162 | return array(); |
||
163 | } |
||
164 | |||
165 | /** |
||
166 | * Builds the authorization header for getting an access or request token. |
||
167 | * |
||
168 | * @param array $extraParameters |
||
169 | * |
||
170 | * @return string |
||
171 | */ |
||
172 | protected function buildAuthorizationHeaderForTokenRequest(array $extraParameters = array()) |
||
173 | { |
||
174 | $parameters = $this->getBasicAuthorizationHeaderInfo(); |
||
175 | $parameters = array_merge($parameters, $extraParameters); |
||
176 | $parameters['oauth_signature'] = $this->signature->getSignature( |
||
177 | $this->getRequestTokenEndpoint(), |
||
178 | $parameters, |
||
179 | 'POST' |
||
180 | ); |
||
181 | |||
182 | $authorizationHeader = 'OAuth '; |
||
183 | $delimiter = ''; |
||
184 | foreach ($parameters as $key => $value) { |
||
185 | $authorizationHeader .= $delimiter . rawurlencode($key) . '="' . rawurlencode($value) . '"'; |
||
186 | |||
187 | $delimiter = ', '; |
||
188 | } |
||
189 | |||
190 | return $authorizationHeader; |
||
191 | } |
||
192 | |||
193 | /** |
||
194 | * Builds the authorization header to get an access token. |
||
195 | * |
||
196 | * @param string $method |
||
197 | * @param UriInterface $uri The uri the request is headed |
||
198 | * @param TokenInterface $token |
||
199 | * @param array $bodyParams Request body if applicable (key/value pairs) |
||
200 | * |
||
201 | * @return string |
||
202 | */ |
||
203 | protected function buildAuthorizationHeaderForAccessRequest( |
||
214 | |||
215 | /** |
||
216 | * Builds the authorization header for an authenticated API request |
||
217 | * |
||
218 | * @param string $method |
||
219 | * @param UriInterface $uri The uri the request is headed |
||
220 | * @param TokenInterface $token |
||
221 | * @param array $bodyParams Request body if applicable (key/value pairs) |
||
222 | * |
||
223 | * @return string |
||
224 | */ |
||
225 | protected function buildAuthorizationHeaderForAPIRequest( |
||
266 | |||
267 | /** |
||
268 | * Builds the authorization header array. |
||
269 | * |
||
270 | * @return array |
||
271 | */ |
||
272 | protected function getBasicAuthorizationHeaderInfo() |
||
273 | { |
||
274 | $dateTime = new \DateTime(); |
||
275 | $headerParameters = array( |
||
276 | 'oauth_callback' => $this->credentials->getCallbackUrl(), |
||
277 | 'oauth_consumer_key' => $this->credentials->getConsumerId(), |
||
278 | 'oauth_nonce' => $this->generateNonce(), |
||
279 | 'oauth_signature_method' => $this->getSignatureMethod(), |
||
280 | 'oauth_timestamp' => $dateTime->format('U'), |
||
281 | 'oauth_version' => $this->getVersion(), |
||
282 | ); |
||
283 | |||
284 | return $headerParameters; |
||
285 | } |
||
286 | |||
287 | /** |
||
288 | * Pseudo random string generator used to build a unique string to sign each request |
||
289 | * |
||
290 | * @param int $length |
||
291 | * |
||
292 | * @return string |
||
293 | */ |
||
294 | protected function generateNonce($length = 32) |
||
306 | |||
307 | /** |
||
308 | * @return string |
||
309 | */ |
||
310 | protected function getSignatureMethod() |
||
314 | |||
315 | /** |
||
316 | * This returns the version used in the authorization header of the requests |
||
317 | * |
||
318 | * @return string |
||
319 | */ |
||
320 | protected function getVersion() |
||
324 | |||
325 | /** |
||
326 | * Parses the request token response and returns a TokenInterface. |
||
327 | * |
||
328 | * @param string $responseBody |
||
329 | * |
||
330 | * @return TokenInterface |
||
331 | * |
||
332 | * @throws TokenResponseException |
||
333 | */ |
||
334 | protected function parseRequestTokenResponse($responseBody) |
||
355 | |||
356 | /** |
||
357 | * Parses the access token response and returns a TokenInterface. |
||
358 | * |
||
359 | * @param string $responseBody |
||
360 | * |
||
361 | * @return TokenInterface |
||
362 | * |
||
363 | * @throws TokenResponseException |
||
364 | */ |
||
365 | protected function parseAccessTokenResponse($responseBody) |
||
380 | |||
381 | /** |
||
382 | * General validation of the response body. |
||
383 | * |
||
384 | * @param string $responseBody |
||
385 | * @return array |
||
386 | * @throws TokenResponseException |
||
387 | */ |
||
388 | protected function validateTokenResponse($responseBody) |
||
402 | } |
||
403 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.