/** * Get Access Token * @return AccessToken * @throws \Exception */ private function getAccessToken() { if ($this->provider === null) { throw new EmptyProviderException(); } if ($this->token == null || $this->token->hasExpired()) { // Get and store a new token $this->token = $this->provider->getAccessToken('client_credentials'); } return $this->token; }
protected function loadToken() { // Carregar token da sessão $this->token = \Session::get('oauth_token'); // Verificar se token foi gerado if (is_null($this->token)) { return; } // Verificar se token jah expirou, se sim tentar atualizar o token if ($this->token->hasExpired() && is_null($this->token->getRefreshToken()) != true) { $this->token = $this->provider->getAccessToken('refresh_token', ['refresh_token' => $this->token->getRefreshToken()]); \Session::set('oauth_token', $this->token); } }
/** * @return bool * @throws FitbitTokenMissingException */ public function hasTokenExpired() { if (empty($this->access_token)) { throw new FitbitTokenMissingException(); } return $this->access_token->hasExpired(); }
/** * Check if the user access token connected to payout exists. If it does not exist, returns null. * * If it exist, check for its expiration. If the token has been expired, refresh it with new one. * * @param array $apiKeys * @param Payout $payout * @param int $expires * * @return Registry|AccessToken|null */ public static function getPayoutAccessToken($apiKeys, Payout $payout, $expires = 7) { try { $token = $payout->getStripe(); // Try to get an access token (using the authorization code grant) $alias = !$apiKeys['test'] ? 'production' : 'test'; if ($token === null or !$token->get('stripeconnect.' . $alias . '.access_token')) { return null; } $options = array('access_token' => $token->get('stripeconnect.' . $alias . '.access_token'), 'refresh_token' => $token->get('stripeconnect.' . $alias . '.refresh_token'), 'expires' => $token->get('stripeconnect.' . $alias . '.expires')); $accessToken = new AccessToken($options); if ($accessToken->hasExpired()) { $provider = new Stripe(['clientId' => $apiKeys['client_id'], 'clientSecret' => $apiKeys['secret_key']]); $accessToken = $provider->getAccessToken('refresh_token', ['refresh_token' => $token->get('stripeconnect.' . $alias . '.refresh_token')]); // Prepare expiration date. $date = new \JDate(); $date->add(new \DateInterval('P' . $expires . 'D')); $token->set('stripeconnect.' . $alias . '.access_token', $accessToken->getToken()); $token->set('stripeconnect.' . $alias . '.refresh_token', $accessToken->getRefreshToken()); $token->set('stripeconnect.' . $alias . '.expires', $date->getTimestamp()); $payout->setStripe($token); $payout->storeStripe(); } } catch (\Exception $e) { \JLog::add($e->getMessage()); return null; } return $accessToken; }
/** * Returns the Guzzle client * * @return Client * @throws LoginException */ public function getClient() { if ($this->_client === null) { if (!$this->_accessToken) { throw new LoginException('The storage did not return a token. Please check your storage provider settings or sign in again'); } if ($this->_accessToken->getExpires() !== null && $this->_accessToken->hasExpired()) { throw new LoginException('The current auth token has expired'); } $this->_client = new Client(['base_uri' => App::ENDPOINT . App::VERSION . '/', 'allow_redirects' => false, 'headers' => ['User-Agent' => 'ApiClient-php/' . self::CLIENT_VERSION, 'Accept' => 'application/json', 'Authorization' => 'Bearer ' . $this->_accessToken->getToken()]]); } return $this->_client; }
/** * @throws Exception * @param sring $method * @param string $path * @param array $options * @return ResponseInterface */ public function call($method, $path, array $options = []) { $doRequest = function ($method, $path, $accessToken, $options) { $request = $this->provider->getAuthenticatedRequest($method, $this->provider->domain . "/{$path}", $accessToken, $options); return $this->provider->getHttpClient()->send($request); }; try { $response = $doRequest($method, $path, $this->accessToken, $options); } catch (ClientException $e) { if (!$this->accessToken instanceof AccessToken || !$this->accessToken->hasExpired()) { throw new Exception($e->getMessage(), $e->getCode(), $e); } $newAccessToken = $this->provider->getAccessToken('refresh_token', ['refresh_token' => $this->accessToken->getRefreshToken()]); $response = $doRequest($method, $path, $newAccessToken, $options); } return $response; }
/** * Get a refresh token from the OAuth provider. * * @param AccessToken $accessToken * * @throws IdentityProviderException * * @return AccessToken */ protected function getRefreshToken(AccessToken $accessToken) { if ($accessToken->hasExpired()) { // Try to get an access token using the authorization code grant. $accessToken = $this->getProvider()->getAccessToken('refresh_token', ['refresh_token' => $accessToken->getRefreshToken()]); } return $accessToken; }
/** * @param AccessToken $accessToken * * @return AccessToken */ public function refreshAccessTokenIfExpired(AccessToken $accessToken) { if ($accessToken->hasExpired()) { $accessToken = $this->refreshAccessToken($accessToken); } return $accessToken; }