Пример #1
0
 public function __construct(CredentialsInterface $credentials, ClientInterface $httpClient, TokenStorageInterface $storage, SignatureInterface $signature, UriInterface $baseApiUri = null)
 {
     //This is a bit of a hack; but Not really sure what else todo.
     $signature = new SignatureRsaSha1($credentials);
     parent::__construct($credentials, $httpClient, $storage, $signature, $baseApiUri);
     if (null === $baseApiUri) {
         $this->baseApiUri = new Uri('https://api.xero.com/api.xro/2.0/');
     }
     //Hack like a shit c**t.
     $a = new \OAuth\OAuth1\Token\StdOAuth1Token();
     $a->setAccessToken($this->credentials->getConsumerId());
     $a->setAccessTokenSecret($this->credentials->getConsumerSecret());
     $this->storage->storeAccessToken($this->service(), $a);
 }
Пример #2
0
 /**
  * Returns oauth provider object from saved token.
  *
  * @return \OAuth\Common\Service\AbstractService
  */
 public function providerFromToken()
 {
     if (empty($this->token)) {
         throw new MissingTokenException('Token not Found');
     }
     try {
         $tw = $this->provider();
         $storage = $tw->getStorage();
         $oToken = new StdOAuth1Token($this->token->getToken());
         //$token->setAccessToken();
         $oToken->setAccessTokenSecret($this->token->getSecret());
         $storage->storeAccessToken('Twitter', $oToken);
         return $tw;
     } catch (TokenResponseException $e) {
         throw new InvalidTokenException('Cannot verify saved token. User has either revoked the priveleges or created a new token.');
     }
 }
Пример #3
0
 private function getTwitterService()
 {
     $key = TWITTER_APP_CONSUMER_KEY;
     $secret = TWITTER_APP_CONSUMER_SECRET;
     $access_token = TWITTER_APP_ACCESS_TOKEN;
     $access_secret = TWITTER_APP_ACCESS_SECRET;
     /** @var \OAuth\ServiceFactory $factory */
     $factory = \Core::make('oauth/factory/service');
     // Initialize the token
     $token = new StdOAuth1Token($access_token);
     $token->setAccessTokenSecret($access_secret);
     // Store the token in memory
     $storage = new Memory();
     $storage->storeAccessToken('Twitter', $token);
     // Create the twitter service
     return $factory->createService('twitter', new Credentials($key, $secret, ''), $storage);
 }
 protected function _parseToken($responseBody)
 {
     $data = $this->_parseResponseBody($responseBody);
     $token = new StdOAuth1Token();
     $token->setRequestToken($data['oauth_token']);
     $token->setRequestTokenSecret($data['oauth_token_secret']);
     $token->setAccessToken($data['oauth_token']);
     $token->setAccessTokenSecret($data['oauth_token_secret']);
     $token->setEndOfLife(StdOAuth1Token::EOL_NEVER_EXPIRES);
     unset($data['oauth_token'], $data['oauth_token_secret']);
     $token->setExtraParams($data);
     return $token;
 }
Пример #5
0
 /**
  * {@inheritdoc}
  */
 protected function parseAccessTokenResponse($responseBody)
 {
     parse_str($responseBody, $data);
     if (null === $data || !is_array($data)) {
         throw new TokenResponseException('Unable to parse response.');
     } elseif (isset($data['error'])) {
         throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
     }
     $token = new StdOAuth1Token();
     $token->setRequestToken($data['oauth_token']);
     $token->setRequestTokenSecret($data['oauth_token_secret']);
     $token->setAccessToken($data['oauth_token']);
     $token->setAccessTokenSecret($data['oauth_token_secret']);
     if (isset($data['oauth_expires_in'])) {
         $token->setLifetime($data['oauth_expires_in']);
     } else {
         $token->setEndOfLife(StdOAuth1Token::EOL_NEVER_EXPIRES);
     }
     unset($data['oauth_token'], $data['oauth_token_secret']);
     $token->setExtraParams($data);
     return $token;
 }
Пример #6
0
 public function refreshAccessToken()
 {
     $access_token = $this->access_token;
     $service = Facades\Social::service($this->provider);
     if (2 === Facades\Social::oauthSpec($this->provider)) {
         $token = new StdOAuth2Token();
         $token->setAccessToken(array_get($access_token, 'token'));
         $token->setRefreshToken(array_get($access_token, 'refresh_token'));
     } else {
         $token = new StdOAuth1Token();
         $token->setAccessToken(array_get($access_token, 'token'));
         $token->setAccessTokenSecret(array_get($access_token, 'secret'));
         $token->setRefreshToken(array_get($access_token, 'refresh_token'));
     }
     $service->getStorage()->storeAccessToken(ucfirst($this->provider), $token);
     try {
         $new_token = $service->refreshAccessToken($token);
     } catch (\Exception $e) {
         return false;
     }
     if (!$new_token->getAccessToken()) {
         return false;
     }
     $access_token['token'] = $new_token->getAccessToken();
     if ($new_token->getEndOfLife()) {
         $access_token['end_of_life'] = $new_token->getEndOfLife();
     }
     if ($new_token->getExtraParams()) {
         $access_token['extra_params'] = $new_token->getExtraParams();
     }
     if (2 !== Facades\Social::oauthSpec($this->provider) && $new_token->getAccessTokenSecret()) {
         $access_token['secret'] = $new_token->getAccessTokenSecret();
     }
     $this->access_token = $access_token;
     $this->save();
     return true;
 }
Пример #7
0
 /**
  * {@inheritdoc}
  */
 protected function parseAccessTokenResponse($sResponseBody)
 {
     $aData = array();
     parse_str($sResponseBody, $aData);
     if ($aData === null || !is_array($aData)) {
         throw new TokenResponseException('Unable to parse response.');
     } else {
         if (isset($aData['oauth_err_code']) && isset($aData['oauth_err_message'])) {
             throw new TokenResponseException('Error in retrieving token: "' . $aData['oauth_err_message'] . '"');
         }
     }
     $oToken = new StdOAuth1Token();
     $oToken->setRequestToken($aData['oauth_token']);
     $oToken->setRequestTokenSecret($aData['oauth_token_secret']);
     $oToken->setAccessToken($aData['oauth_token']);
     $oToken->setAccessTokenSecret($aData['oauth_token_secret']);
     $oToken->setEndOfLife(StdOAuth1Token::EOL_NEVER_EXPIRES);
     unset($aData['oauth_token'], $aData['oauth_token_secret']);
     $oToken->setExtraParams($aData);
     return $oToken;
 }
Пример #8
0
 /**
  * Create token object from array.
  *
  * @param array $data
  * @return TokenInterface
  */
 public function arrayToToken(array $data)
 {
     if ($data && array_key_exists('accessToken', $data) && array_key_exists('accessTokenSecret', $data) && array_key_exists('requestToken', $data) && array_key_exists('requestTokenSecret', $data) && array_key_exists('endOfLife', $data) && array_key_exists('extraParams', $data)) {
         $token = new StdOAuth1Token($data['accessToken']);
         $token->setAccessTokenSecret($data['accessTokenSecret']);
         $token->setRequestToken($data['requestToken']);
         $token->setRequestTokenSecret($data['requestTokenSecret']);
         $token->setEndOfLife($data['endOfLife']);
         $token->setExtraParams($data['extraParams']);
     } elseif ($data && array_key_exists('accessToken', $data) && array_key_exists('refreshToken', $data) && array_key_exists('endOfLife', $data) && array_key_exists('extraParams', $data)) {
         $token = new StdOAuth2Token($data['accessToken'], $data['refreshToken'], null, $data['extraParams']);
         $token->setEndOfLife($data['endOfLife']);
     }
     if (!isset($token) || !$token) {
         return null;
     }
     return $token;
 }
Пример #9
0
 /**
  * Get token from storage
  *
  * @param  string $provider
  * @param  int $key
  * @return Token
  */
 public function getToken($provider, $key)
 {
     $provider = ucfirst(strtolower($provider));
     $data = $this['option']->get('oauth:token:' . $provider . ':' . $key);
     if ($data && array_key_exists('accessToken', $data) && array_key_exists('accessTokenSecret', $data) && array_key_exists('requestToken', $data) && array_key_exists('requestTokenSecret', $data) && array_key_exists('endOfLife', $data) && array_key_exists('extraParams', $data)) {
         $token = new StdOAuth1Token($data['accessToken']);
         $token->setAccessTokenSecret($data['accessTokenSecret']);
         $token->setRequestToken($data['requestToken']);
         $token->setRequestTokenSecret($data['requestTokenSecret']);
         $token->setEndOfLife($data['endOfLife']);
         $token->setExtraParams($data['extraParams']);
     } elseif ($data && array_key_exists('accessToken', $data) && array_key_exists('refreshToken', $data) && array_key_exists('endOfLife', $data) && array_key_exists('extraParams', $data)) {
         $token = new StdOAuth2Token($data['accessToken'], $data['refreshToken'], null, $data['extraParams']);
         $token->setEndOfLife($data['endOfLife']);
     }
     if (!isset($token) || !$token) {
         return null;
     }
     return $token;
 }
Пример #10
0
 /**
  * Builds the oAuth authorization header for an authenticated API request
  *
  * @param UriInterface $uri the uri the request is headed
  * @param \OAuth\OAuth1\Token\TokenInterface $token
  * @param string $tokenSecret used to verify the passed token
  * @param array $bodyParams
  * @param string $method HTTP method to use
  * @return array
  */
 public function buildOauthAuthorizationHeader($uri, $token, $tokenSecret, $bodyParams, $method = 'GET')
 {
     $uri = new Uri($uri);
     $tokenObj = new StdOAuth1Token();
     $tokenObj->setAccessToken($token);
     $tokenObj->setAccessTokenSecret($tokenSecret);
     $tokenObj->setEndOfLife(StdOAuth1Token::EOL_NEVER_EXPIRES);
     return ['Authorization: ' . $this->buildAuthorizationHeaderForAPIRequest($method, $uri, $tokenObj, $bodyParams)];
 }
Пример #11
0
 /**
  * @param string|array $responseBody
  * @return StdOAuth1Token
  * @throws TokenResponseException
  */
 protected function parseAccessTokenResponse($responseBody)
 {
     if (!is_array($responseBody)) {
         parse_str($responseBody, $data);
         if (!isset($data) || !is_array($data)) {
             throw new TokenResponseException('Unable to parse response.');
         }
     } else {
         $data = $responseBody;
     }
     $error = $this->service->getAccessTokenResponseError($data);
     if (isset($error)) {
         throw new TokenResponseException('Error in retrieving token: "' . $error . '"');
     }
     $token = new StdOAuth1Token();
     $names = $this->service->getAccessTokenArgumentNames();
     $token->setRequestToken($data[$names['oauth_token']]);
     $token->setRequestTokenSecret($data[$names['oauth_token_secret']]);
     $token->setAccessToken($data[$names['oauth_token']]);
     $token->setAccessTokenSecret($data[$names['oauth_token_secret']]);
     unset($data[$names['oauth_token']], $data[$names['oauth_token_secret']]);
     if (isset($data[$names['oauth_expires_in']])) {
         $token->setLifeTime($data[$names['oauth_expires_in']]);
         unset($data[$names['oauth_expires_in']]);
     } else {
         $token->setLifetime($this->service->getTokenDefaultLifetime());
     }
     $token->setExtraParams($data);
     return $token;
 }
Пример #12
0
 protected function parseAccessTokenResponse($responseBody)
 {
     $token = new StdOAuth1Token();
     $token->setAccessToken($responseBody);
     return $token;
 }
Пример #13
0
 /**
  * @covers OAuth\OAuth1\Token\StdOAuth1Token::setAccessTokenSecret
  * @covers OAuth\OAuth1\Token\StdOAuth1Token::getAccessTokenSecret
  */
 public function testGetAccessTokenSecret()
 {
     $token = new StdOAuth1Token();
     $this->assertNull($token->setAccessTokenSecret('foo'));
     $this->assertSame('foo', $token->getAccessTokenSecret());
 }
Пример #14
0
 /**
  * Will use your own token (you may have stored in DB)
  *
  * @param $accessToken
  * @param $secret
  */
 public function setAccessToken($accessToken, $secret)
 {
     $this->init();
     $token = new StdOAuth1Token();
     $token->setAccessToken($accessToken);
     $token->setAccessTokenSecret($secret);
     $this->storage->storeAccessToken($this->getServiceName(), $token);
 }