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