1 | <?php |
||
18 | abstract class AbstractService extends BaseAbstractService implements ServiceInterface |
||
19 | { |
||
20 | /** @const OAUTH_VERSION */ |
||
21 | const OAUTH_VERSION = 2; |
||
22 | |||
23 | /** @var array */ |
||
24 | protected $scopes; |
||
25 | |||
26 | /** @var UriInterface|null */ |
||
27 | protected $baseApiUri; |
||
28 | |||
29 | /** @var bool */ |
||
30 | protected $stateParameterInAuthUrl; |
||
31 | |||
32 | /** @var string */ |
||
33 | protected $apiVersion; |
||
34 | |||
35 | /** |
||
36 | * @param CredentialsInterface $credentials |
||
37 | * @param ClientInterface $httpClient |
||
38 | * @param TokenStorageInterface $storage |
||
39 | * @param array $scopes |
||
40 | * @param UriInterface|null $baseApiUri |
||
41 | * @param bool $stateParameterInAutUrl |
||
42 | * @param string $apiVersion |
||
43 | * |
||
44 | * @throws InvalidScopeException |
||
45 | */ |
||
46 | public function __construct( |
||
47 | CredentialsInterface $credentials, |
||
48 | ClientInterface $httpClient, |
||
49 | TokenStorageInterface $storage, |
||
50 | $scopes = array(), |
||
51 | UriInterface $baseApiUri = null, |
||
52 | $stateParameterInAutUrl = false, |
||
53 | $apiVersion = "" |
||
54 | ) { |
||
55 | parent::__construct($credentials, $httpClient, $storage); |
||
56 | $this->stateParameterInAuthUrl = $stateParameterInAutUrl; |
||
57 | |||
58 | foreach ($scopes as $scope) { |
||
59 | if (!$this->isValidScope($scope)) { |
||
60 | throw new InvalidScopeException('Scope ' . $scope . ' is not valid for service ' . get_class($this)); |
||
61 | } |
||
62 | } |
||
63 | |||
64 | $this->scopes = $scopes; |
||
65 | |||
66 | $this->baseApiUri = $baseApiUri; |
||
67 | |||
68 | $this->apiVersion = $apiVersion; |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * {@inheritdoc} |
||
73 | */ |
||
74 | public function getAuthorizationUri(array $additionalParameters = array()) |
||
103 | |||
104 | /** |
||
105 | * {@inheritdoc} |
||
106 | */ |
||
107 | public function requestAccessToken($code, $state = null) |
||
132 | |||
133 | /** |
||
134 | * Sends an authenticated API request to the path provided. |
||
135 | * If the path provided is not an absolute URI, the base API Uri (must be passed into constructor) will be used. |
||
136 | * |
||
137 | * @param string|UriInterface $path |
||
138 | * @param string $method HTTP method |
||
139 | * @param array $body Request body if applicable. |
||
140 | * @param array $extraHeaders Extra headers if applicable. These will override service-specific |
||
141 | * any defaults. |
||
142 | * |
||
143 | * @return string |
||
144 | * |
||
145 | * @throws ExpiredTokenException |
||
146 | * @throws Exception |
||
147 | */ |
||
148 | public function request($path, $method = 'GET', $body = null, array $extraHeaders = array()) |
||
187 | |||
188 | /** |
||
189 | * Accessor to the storage adapter to be able to retrieve tokens |
||
190 | * |
||
191 | * @return TokenStorageInterface |
||
192 | */ |
||
193 | public function getStorage() |
||
197 | |||
198 | /** |
||
199 | * Refreshes an OAuth2 access token. |
||
200 | * |
||
201 | * @param TokenInterface $token |
||
202 | * |
||
203 | * @return TokenInterface $token |
||
204 | * |
||
205 | * @throws MissingRefreshTokenException |
||
206 | */ |
||
207 | public function refreshAccessToken(TokenInterface $token) |
||
233 | |||
234 | /** |
||
235 | * Return whether or not the passed scope value is valid. |
||
236 | * |
||
237 | * @param string $scope |
||
238 | * |
||
239 | * @return bool |
||
240 | */ |
||
241 | public function isValidScope($scope) |
||
247 | |||
248 | /** |
||
249 | * Check if the given service need to generate a unique state token to build the authorization url |
||
250 | * |
||
251 | * @return bool |
||
252 | */ |
||
253 | public function needsStateParameterInAuthUrl() |
||
257 | |||
258 | /** |
||
259 | * Validates the authorization state against a given one |
||
260 | * |
||
261 | * @param string $state |
||
262 | * @throws InvalidAuthorizationStateException |
||
263 | */ |
||
264 | protected function validateAuthorizationState($state) |
||
270 | |||
271 | /** |
||
272 | * Generates a random string to be used as state |
||
273 | * |
||
274 | * @return string |
||
275 | */ |
||
276 | protected function generateAuthorizationState() |
||
280 | |||
281 | /** |
||
282 | * Retrieves the authorization state for the current service |
||
283 | * |
||
284 | * @return string |
||
285 | */ |
||
286 | protected function retrieveAuthorizationState() |
||
290 | |||
291 | /** |
||
292 | * Stores a given authorization state into the storage |
||
293 | * |
||
294 | * @param string $state |
||
295 | */ |
||
296 | protected function storeAuthorizationState($state) |
||
300 | |||
301 | /** |
||
302 | * Return any additional headers always needed for this service implementation's OAuth calls. |
||
303 | * |
||
304 | * @return array |
||
305 | */ |
||
306 | protected function getExtraOAuthHeaders() |
||
310 | |||
311 | /** |
||
312 | * Return any additional headers always needed for this service implementation's API calls. |
||
313 | * |
||
314 | * @return array |
||
315 | */ |
||
316 | protected function getExtraApiHeaders() |
||
320 | |||
321 | /** |
||
322 | * Parses the access token response and returns a TokenInterface. |
||
323 | * |
||
324 | * @abstract |
||
325 | * |
||
326 | * @param string $responseBody |
||
327 | * |
||
328 | * @return TokenInterface |
||
329 | * |
||
330 | * @throws TokenResponseException |
||
331 | */ |
||
332 | abstract protected function parseAccessTokenResponse($responseBody); |
||
333 | |||
334 | /** |
||
335 | * Returns a class constant from ServiceInterface defining the authorization method used for the API |
||
336 | * Header is the sane default. |
||
337 | * |
||
338 | * @return int |
||
339 | */ |
||
340 | protected function getAuthorizationMethod() |
||
344 | |||
345 | /** |
||
346 | * Returns api version string if is set else retrun empty string |
||
347 | * |
||
348 | * @return string |
||
349 | */ |
||
350 | protected function getApiVersionString() |
||
354 | |||
355 | /** |
||
356 | * Returns delimiter to scopes in getAuthorizationUri |
||
357 | * For services that do not fully respect the Oauth's RFC, |
||
358 | * and use scopes with commas as delimiter |
||
359 | * |
||
360 | * @return string |
||
361 | */ |
||
362 | protected function getScopesDelimiter() |
||
366 | } |
||
367 |