Since: 2.0
Author: Paul Klimov (klimov.paul@gmail.com)
Inheritance: extends yii\base\Object
Exemplo n.º 1
12
 /**
  * Gets new auth token to replace expired one.
  * @param OAuthToken $token expired auth token.
  * @return OAuthToken new auth token.
  */
 public function refreshAccessToken(OAuthToken $token)
 {
     $params = ['client_id' => $this->clientId, 'client_secret' => $this->clientSecret, 'grant_type' => 'refresh_token'];
     $params = array_merge($token->getParams(), $params);
     $response = $this->sendRequest('POST', $this->tokenUrl, $params);
     return $response;
 }
Exemplo n.º 2
11
 /**
  * Gets new auth token to replace expired one.
  * @param OAuthToken $token expired auth token.
  * @return OAuthToken new auth token.
  */
 public function refreshAccessToken(OAuthToken $token)
 {
     $params = ['client_id' => $this->clientId, 'client_secret' => $this->clientSecret, 'grant_type' => 'refresh_token'];
     $params = array_merge($token->getParams(), $params);
     $request = $this->createRequest()->setMethod('POST')->setUrl($this->tokenUrl)->setData($params);
     $response = $this->sendRequest($request);
     $token = $this->createToken(['params' => $response]);
     $this->setAccessToken($token);
     return $token;
 }
Exemplo n.º 3
4
 /**
  * @inheritdoc
  */
 public function fetchAccessToken(OAuthToken $requestToken = null, $oauthVerifier = null, array $params = [])
 {
     $this->removeState('requestToken');
     $tokenConfig = ['class' => OAuthToken::className(), 'params' => ['oauth_token' => $this->token, 'oauth_token_secret' => $this->tokenSecret]];
     $token = Yii::createObject($tokenConfig);
     $this->setAccessToken($token);
     return $token;
 }
Exemplo n.º 4
3
 /**
  * Performs request to the OAuth API.
  * @param OAuthToken $accessToken actual access token.
  * @param string $url absolute API URL.
  * @param string $method request method.
  * @param array $params request parameters.
  * @return array API response.
  * @throws Exception on failure.
  */
 protected function apiInternal($accessToken, $url, $method, array $params)
 {
     $params['oauth_consumer_key'] = $this->consumerKey;
     $params['oauth_token'] = $accessToken->getToken();
     $response = $this->sendSignedRequest($method, $url, $params);
     return $response;
 }
Exemplo n.º 5
2
 /**
  * Handles token request response
  * @author Adegoke Obasa <*****@*****.**>
  * @param OAuthToken $response
  * @return mixed
  * @throws Oauth2ClientException
  */
 private function handleTokenResponse($response)
 {
     $params = $response->getParams();
     $status = ArrayHelper::getValue($params, 'status');
     if (!is_null($status) && $status == 'success') {
         $token = ArrayHelper::getValue($params, 'data');
         if (is_null($token)) {
             throw new Oauth2ClientException(self::CODE_NOT_SET);
         }
         return $token;
     } else {
         $message = ArrayHelper::getValue($params, 'message', self::DEFAULT_ERROR);
         throw new Oauth2ClientException($message);
     }
 }
Exemplo n.º 6
1
 public function testBuildAuthUrl()
 {
     $oauthClient = new OAuth1();
     $authUrl = 'http://test.auth.url';
     $oauthClient->authUrl = $authUrl;
     $requestTokenToken = 'test_request_token';
     $requestToken = new OAuthToken();
     $requestToken->setToken($requestTokenToken);
     $builtAuthUrl = $oauthClient->buildAuthUrl($requestToken);
     $this->assertContains($authUrl, $builtAuthUrl, 'No auth URL present!');
     $this->assertContains($requestTokenToken, $builtAuthUrl, 'No token present!');
 }
Exemplo n.º 7
1
 /**
  * @dataProvider apiUrlDataProvider
  *
  * @param $apiBaseUrl
  * @param $apiSubUrl
  * @param $expectedApiFullUrl
  */
 public function testApiUrl($apiBaseUrl, $apiSubUrl, $expectedApiFullUrl)
 {
     $oauthClient = $this->createOAuthClient();
     $oauthClient->expects($this->any())->method('apiInternal')->will($this->returnArgument(1));
     $accessToken = new OAuthToken();
     $accessToken->setToken('test_access_token');
     $accessToken->setExpireDuration(1000);
     $oauthClient->setAccessToken($accessToken);
     $oauthClient->apiBaseUrl = $apiBaseUrl;
     $this->assertEquals($expectedApiFullUrl, $oauthClient->api($apiSubUrl));
 }
Exemplo n.º 8
1
 /**
  *
  * @param \yii\authclient\OAuthToken $accessToken
  * @param string  $url
  * @param string $method
  * @param array $params
  * @param array $headers
  */
 protected function apiInternal($accessToken, $url, $method, array $params, array $headers)
 {
     $headers[] = 'Authorization: Bearer ' . $accessToken->getToken();
     return $this->sendRequest($method, $url, $params, $headers);
 }
Exemplo n.º 9
0
 /**
  * Creates token from its configuration.
  * @param array $tokenConfig token configuration.
  * @return OAuthToken token instance.
  */
 protected function createToken(array $tokenConfig = [])
 {
     if (!array_key_exists('class', $tokenConfig)) {
         $tokenConfig['class'] = OAuthToken::className();
     }
     return Yii::createObject($tokenConfig);
 }
Exemplo n.º 10
-1
 /**
  * @depends testGetIsExpired
  */
 public function testGetIsValid()
 {
     $oauthToken = new OAuthToken();
     $expireDuration = 3600;
     $oauthToken->setExpireDuration($expireDuration);
     $this->assertFalse($oauthToken->getIsValid(), 'Empty token is valid!');
     $oauthToken->setToken('test_token');
     $this->assertTrue($oauthToken->getIsValid(), 'Filled up token is invalid!');
     $oauthToken->createTimestamp = $oauthToken->createTimestamp - ($expireDuration + 1);
     $this->assertFalse($oauthToken->getIsValid(), 'Expired token is valid!');
 }