fkooman\OAuth\Client\Api::getAccessToken PHP Method

getAccessToken() public method

public getAccessToken ( Context $context )
$context Context
    public function getAccessToken(Context $context)
    {
        // do we have a valid access token?
        $accessToken = $this->tokenStorage->getAccessToken($this->clientConfigId, $context);
        if (false !== $accessToken) {
            if (null === $accessToken->getExpiresIn()) {
                // no expiry set, assume always valid
                return $accessToken;
            }
            // check if expired
            if (time() < $accessToken->getIssueTime() + $accessToken->getExpiresIn()) {
                // not expired
                return $accessToken;
            }
            // expired, delete it and continue
            $this->tokenStorage->deleteAccessToken($accessToken);
        }
        // no valid access token, is there a refresh_token?
        $refreshToken = $this->getRefreshToken($context);
        if (false !== $refreshToken) {
            // obtain a new access token with refresh token
            $tokenRequest = new TokenRequest($this->httpClient, $this->clientConfig);
            $tokenResponse = $tokenRequest->withRefreshToken($refreshToken->getRefreshToken());
            if (false === $tokenResponse) {
                // unable to fetch with RefreshToken, delete it
                $this->tokenStorage->deleteRefreshToken($refreshToken);
                return false;
            }
            if (null === $tokenResponse->getScope()) {
                // no scope in response, we assume we got the requested scope
                $scope = $context->getScope();
            } else {
                // the scope we got should be a superset of what we requested
                $scope = $tokenResponse->getScope();
                if (!$scope->hasScope($context->getScope())) {
                    // we didn't get the scope we requested, stop for now
                    // FIXME: we need to implement a way to request certain
                    // scope as being optional, while others need to be
                    // required
                    throw new ApiException('requested scope not obtained');
                }
            }
            $accessToken = new AccessToken(array('client_config_id' => $this->clientConfigId, 'user_id' => $context->getUserId(), 'scope' => $scope, 'access_token' => $tokenResponse->getAccessToken(), 'token_type' => $tokenResponse->getTokenType(), 'issue_time' => time(), 'expires_in' => $tokenResponse->getExpiresIn()));
            $this->tokenStorage->storeAccessToken($accessToken);
            if (null !== $tokenResponse->getRefreshToken()) {
                // delete the existing refresh token as we'll store a new one
                $this->tokenStorage->deleteRefreshToken($refreshToken);
                $refreshToken = new RefreshToken(array('client_config_id' => $this->clientConfigId, 'user_id' => $context->getUserId(), 'scope' => $scope, 'refresh_token' => $tokenResponse->getRefreshToken(), 'issue_time' => time()));
                $this->tokenStorage->storeRefreshToken($refreshToken);
            }
            return $accessToken;
        }
        // no access token, and refresh token didn't work either or was not there, probably the tokens were revoked
        return false;
    }

Usage Example

示例#1
0
 public function testGetAccessTokenWithExpiredAccessTokenAndRefreshToken()
 {
     $client = new Client();
     $mock = new MockPlugin();
     $mock->addResponse(new Response(200, null, json_encode(array("access_token" => "my_new_access_token_value", "token_type" => "Bearer"))));
     $client->addSubscriber($mock);
     $api = new Api("foo", $this->clientConfig[0], $this->storage, $client);
     $context = new Context("a_user", array("foo", "bar"));
     $accessToken = new AccessToken(array("client_config_id" => "foo", "user_id" => "a_user", "token_type" => "bearer", "access_token" => "my_token_value", "scope" => Scope::fromString("foo bar"), "issue_time" => time() - 4000, "expires_in" => 3600));
     $this->storage->storeAccessToken($accessToken);
     $refreshToken = new RefreshToken(array("client_config_id" => "foo", "user_id" => "a_user", "refresh_token" => "my_refresh_token_value", "scope" => Scope::fromString("foo bar"), "issue_time" => time() - 10000));
     $this->storage->storeRefreshToken($refreshToken);
     $accessToken = $api->getAccessToken($context);
     $this->assertEquals("my_new_access_token_value", $accessToken->getAccessToken());
     //$this->assertFalse($accessToken);
 }
All Usage Examples Of fkooman\OAuth\Client\Api::getAccessToken