Strava.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. /**
  3. * Strava service.
  4. *
  5. * @author Pedro Amorim <contact@pamorim.fr>
  6. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  7. * @link http://strava.github.io/
  8. * @link http://strava.github.io/api/v3/oauth/
  9. */
  10. namespace OAuth\OAuth2\Service;
  11. use OAuth\OAuth2\Token\StdOAuth2Token;
  12. use OAuth\Common\Http\Exception\TokenResponseException;
  13. use OAuth\Common\Http\Uri\Uri;
  14. use OAuth\Common\Consumer\CredentialsInterface;
  15. use OAuth\Common\Http\Client\ClientInterface;
  16. use OAuth\Common\Storage\TokenStorageInterface;
  17. use OAuth\Common\Http\Uri\UriInterface;
  18. use OAuth\OAuth2\Service\Exception\InvalidAccessTypeException;
  19. /**
  20. * Strava service.
  21. *
  22. * @author Pedro Amorim <contact@pamorim.fr>
  23. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  24. * @link http://strava.github.io/
  25. * @link http://strava.github.io/api/v3/oauth/
  26. */
  27. class Strava extends AbstractService
  28. {
  29. /**
  30. * Scopes
  31. */
  32. // default
  33. const SCOPE_PUBLIC = 'public';
  34. // Modify activities, upload on the user’s behalf
  35. const SCOPE_WRITE = 'write';
  36. // View private activities and data within privacy zones
  37. const SCOPE_VIEW_PRIVATE = 'view_private';
  38. protected $approvalPrompt = 'auto';
  39. public function __construct(
  40. CredentialsInterface $credentials,
  41. ClientInterface $httpClient,
  42. TokenStorageInterface $storage,
  43. $scopes = array(),
  44. UriInterface $baseApiUri = null
  45. ) {
  46. if (empty($scopes)) {
  47. $scopes = array(self::SCOPE_PUBLIC);
  48. }
  49. parent::__construct(
  50. $credentials,
  51. $httpClient,
  52. $storage,
  53. $scopes,
  54. $baseApiUri,
  55. true
  56. );
  57. if (null === $baseApiUri) {
  58. $this->baseApiUri = new Uri('https://www.strava.com/api/v3/');
  59. }
  60. }
  61. /**
  62. * {@inheritdoc}
  63. */
  64. public function getAuthorizationEndpoint()
  65. {
  66. return new Uri('https://www.strava.com/oauth/authorize?approval_prompt=' . $this->approvalPrompt);
  67. }
  68. /**
  69. * {@inheritdoc}
  70. */
  71. public function getAccessTokenEndpoint()
  72. {
  73. return new Uri('https://www.strava.com/oauth/token');
  74. }
  75. /**
  76. * {@inheritdoc}
  77. */
  78. protected function getAuthorizationMethod()
  79. {
  80. return static::AUTHORIZATION_METHOD_HEADER_BEARER;
  81. }
  82. /**
  83. * {@inheritdoc}
  84. */
  85. protected function parseAccessTokenResponse($responseBody)
  86. {
  87. $data = json_decode($responseBody, true);
  88. if (null === $data || !is_array($data)) {
  89. throw new TokenResponseException('Unable to parse response.');
  90. } elseif (isset($data['error_description'])) {
  91. throw new TokenResponseException(
  92. 'Error in retrieving token: "' . $data['error_description'] . '"'
  93. );
  94. } elseif (isset($data['error'])) {
  95. throw new TokenResponseException(
  96. 'Error in retrieving token: "' . $data['error'] . '"'
  97. );
  98. }
  99. $token = new StdOAuth2Token();
  100. $token->setAccessToken($data['access_token']);
  101. if (isset($data['expires_in'])) {
  102. $token->setLifeTime($data['expires_in']);
  103. unset($data['expires_in']);
  104. }
  105. if (isset($data['refresh_token'])) {
  106. $token->setRefreshToken($data['refresh_token']);
  107. unset($data['refresh_token']);
  108. }
  109. unset($data['access_token']);
  110. $token->setExtraParams($data);
  111. return $token;
  112. }
  113. public function setApprouvalPrompt($prompt)
  114. {
  115. if (!in_array($prompt, array('auto', 'force'), true)) {
  116. // @todo Maybe could we rename this exception
  117. throw new InvalidAccessTypeException('Invalid approuvalPrompt, expected either auto or force.');
  118. }
  119. $this->approvalPrompt = $prompt;
  120. }
  121. /**
  122. * {@inheritdoc}
  123. */
  124. protected function getScopesDelimiter()
  125. {
  126. return ',';
  127. }
  128. }