Yahoo.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace OAuth\OAuth2\Service;
  3. use OAuth\OAuth2\Token\StdOAuth2Token;
  4. use OAuth\Common\Http\Exception\TokenResponseException;
  5. use OAuth\Common\Http\Uri\Uri;
  6. class Yahoo extends AbstractService
  7. {
  8. /**
  9. * {@inheritdoc}
  10. */
  11. public function getAuthorizationEndpoint()
  12. {
  13. return new Uri('https://api.login.yahoo.com/oauth2/request_auth');
  14. }
  15. /**
  16. * {@inheritdoc}
  17. */
  18. public function getAccessTokenEndpoint()
  19. {
  20. return new Uri('https://api.login.yahoo.com/oauth2/get_token');
  21. }
  22. /**
  23. * {@inheritdoc}
  24. */
  25. protected function getAuthorizationMethod()
  26. {
  27. return static::AUTHORIZATION_METHOD_HEADER_BEARER;
  28. }
  29. /**
  30. * {@inheritdoc}
  31. */
  32. protected function parseAccessTokenResponse($responseBody)
  33. {
  34. $data = json_decode($responseBody, true);
  35. if (null === $data || !is_array($data)) {
  36. throw new TokenResponseException('Unable to parse response.');
  37. } elseif (isset($data['error'])) {
  38. throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
  39. }
  40. $token = new StdOAuth2Token();
  41. $token->setAccessToken($data['access_token']);
  42. $token->setLifetime($data['expires_in']);
  43. if (isset($data['refresh_token'])) {
  44. $token->setRefreshToken($data['refresh_token']);
  45. unset($data['refresh_token']);
  46. }
  47. unset($data['access_token']);
  48. unset($data['expires_in']);
  49. $token->setExtraParams($data);
  50. return $token;
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. protected function getExtraOAuthHeaders()
  56. {
  57. $encodedCredentials = base64_encode(
  58. $this->credentials->getConsumerId() . ':' . $this->credentials->getConsumerSecret()
  59. );
  60. return array('Authorization' => 'Basic ' . $encodedCredentials);
  61. }
  62. }