public function testAccessTokenSetExpiresDirect()
 {
     $token = new AccessToken('testToken', 'bearer', ['expires' => 500]);
     $this->assertTrue($token->isExpired());
     $token = new AccessToken('testToken', 'bearer', ['expires' => time() + 500]);
     $this->assertFalse($token->isExpired());
 }
 /**
  * Get the access token.
  *
  * @return AccessToken|null Oauth2 access token
  */
 public function getAccessToken()
 {
     if ($this->accessToken && $this->accessToken->isExpired()) {
         // The access token has expired.
         $this->accessToken = null;
     }
     if (null === $this->accessToken) {
         // Try to acquire a new access token from the server.
         $this->accessToken = $this->acquireAccessToken();
         if ($this->accessToken) {
             $this->refreshToken = $this->accessToken->getRefreshToken() ?: null;
         }
     }
     return $this->accessToken;
 }
 /**
  * Set the refresh token.
  *
  * @param AccessToken|string $refreshToken The refresh token
  *
  * @return self
  */
 public function setRefreshToken($refreshToken)
 {
     if (!$this->accessToken instanceof AccessToken) {
         throw new \InvalidArgumentException('Unable to update the refresh token. You have never set first the access token.');
     }
     if (is_string($refreshToken)) {
         $refreshToken = new AccessToken($refreshToken, 'refresh_token');
     } elseif (!$refreshToken instanceof AccessToken) {
         throw new \InvalidArgumentException('Invalid refresh token');
     }
     $this->accessToken->setRefreshToken($refreshToken);
     if ($this->refreshTokenGrantType instanceof RefreshTokenGrantTypeInterface) {
         $this->refreshTokenGrantType->setRefreshToken($refreshToken->getToken());
     }
     return $this;
 }
Пример #4
0
 /**
  * Save an access token to the session.
  *
  * @param AccessToken $token
  */
 protected function addTokenToSession(AccessToken $token)
 {
     $this->session->set('accessToken', $token->getToken());
     $this->session->set('tokenType', $token->getType());
     if ($token->getExpires()) {
         $this->session->set('expires', $token->getExpires()->getTimestamp());
     }
     if ($token->getRefreshToken()) {
         $this->session->set('refreshToken', $token->getRefreshToken()->getToken());
     }
 }
 /**
  * Set the access token.
  *
  * @param AccessToken|string $accessToken
  * @param string             $type
  * @param int                $expires
  */
 public function setAccessToken($accessToken, $type = null, $expires = null)
 {
     if (is_string($accessToken)) {
         $accessToken = new AccessToken($accessToken, $type, ['expires' => $expires]);
     } elseif (!$accessToken instanceof AccessToken) {
         throw new \InvalidArgumentException('Invalid access token');
     }
     $this->accessToken = $accessToken;
     $this->refreshToken = $accessToken->getRefreshToken();
 }
 /**
  * @param AccessToken $refreshToken
  *
  * @return self
  */
 public function setRefreshToken(AccessToken $refreshToken)
 {
     if ($refreshToken->getType() != 'refresh_token') {
         throw new InvalidArgumentException('Expected AccessToken to be "refresh_token" type, got "%s"', $refreshToken->getType());
     }
     $this->refreshToken = $refreshToken;
     return $this;
 }