/**
  * {@inheritdoc}
  */
 public function completeFlow()
 {
     $clientId = $this->server->getRequest()->request->get('client_id', $this->server->getRequest()->getUser());
     if (is_null($clientId)) {
         throw new Exception\InvalidRequestException('client_id');
     }
     $clientSecret = $this->server->getRequest()->request->get('client_secret', $this->server->getRequest()->getPassword());
     if (is_null($clientSecret)) {
         throw new Exception\InvalidRequestException('client_secret');
     }
     // Validate client ID and client secret
     $client = $this->server->getClientStorage()->get($clientId, $clientSecret, null, $this->getIdentifier());
     if ($client instanceof ClientEntity === false) {
         $this->server->getEventEmitter()->emit(new Event\ClientAuthenticationFailedEvent($this->server->getRequest()));
         throw new Exception\InvalidClientException();
     }
     $oldRefreshTokenParam = $this->server->getRequest()->request->get('refresh_token', null);
     if ($oldRefreshTokenParam === null) {
         throw new Exception\InvalidRequestException('refresh_token');
     }
     // Validate refresh token
     $oldRefreshToken = $this->server->getRefreshTokenStorage()->get($oldRefreshTokenParam);
     if ($oldRefreshToken instanceof RefreshTokenEntity === false) {
         throw new Exception\InvalidRefreshException();
     }
     // Ensure the old refresh token hasn't expired
     if ($oldRefreshToken->isExpired() === true) {
         throw new Exception\InvalidRefreshException();
     }
     $oldAccessToken = $oldRefreshToken->getAccessToken();
     // Get the scopes for the original session
     $session = $oldAccessToken->getSession();
     $scopes = $this->formatScopes($session->getScopes());
     // Get and validate any requested scopes
     $requestedScopesString = $this->server->getRequest()->request->get('scope', '');
     $requestedScopes = $this->validateScopes($requestedScopesString, $client);
     // If no new scopes are requested then give the access token the original session scopes
     if (count($requestedScopes) === 0) {
         $newScopes = $scopes;
     } else {
         // The OAuth spec says that a refreshed access token can have the original scopes or fewer so ensure
         //  the request doesn't include any new scopes
         foreach ($requestedScopes as $requestedScope) {
             if (!isset($scopes[$requestedScope->getId()])) {
                 throw new Exception\InvalidScopeException($requestedScope->getId());
             }
         }
         $newScopes = $requestedScopes;
     }
     // Generate a new access token and assign it the correct sessions
     $newAccessToken = new AccessTokenEntity($this->server);
     $newAccessToken->setId(SecureKey::generate());
     $newAccessToken->setExpireTime($this->getAccessTokenTTL() + time());
     $newAccessToken->setSession($session);
     foreach ($newScopes as $newScope) {
         $newAccessToken->associateScope($newScope);
     }
     // Expire the old token and save the new one
     $oldAccessToken->expire();
     $newAccessToken->save();
     $this->server->getTokenType()->setSession($session);
     $this->server->getTokenType()->setParam('access_token', $newAccessToken->getId());
     $this->server->getTokenType()->setParam('expire_access_token', $this->getAccessTokenTTL() + time());
     if ($this->shouldRotateRefreshTokens()) {
         // Expire the old refresh token
         $oldRefreshToken->expire();
         // Generate a new refresh token
         $newRefreshToken = new RefreshTokenEntity($this->server);
         $newRefreshToken->setId(SecureKey::generate());
         $newRefreshToken->setExpireTime($this->getRefreshTokenTTL() + time());
         $newRefreshToken->setAccessToken($newAccessToken);
         $newRefreshToken->save();
         $this->server->getTokenType()->setParam('refresh_token', $newRefreshToken->getId());
         $this->server->getTokenType()->setParam('expire_refresh_token', $newRefreshToken->getExpireTime());
     } else {
         $this->server->getTokenType()->setParam('refresh_token', $oldRefreshToken->getId());
         $this->server->getTokenType()->setParam('expire_refresh_token', $oldRefreshToken->getExpireTime());
     }
     return $this->server->getTokenType()->generateResponse();
 }
 /**
  * Complete the password grant
  *
  * @return array
  *
  * @throws
  */
 public function completeFlow()
 {
     // Get the required params
     $clientId = $this->server->getRequest()->request->get('client_id', $this->server->getRequest()->getUser());
     if (is_null($clientId)) {
         throw new Exception\InvalidRequestException('client_id');
     }
     $clientSecret = $this->server->getRequest()->request->get('client_secret', $this->server->getRequest()->getPassword());
     if (is_null($clientSecret)) {
         throw new Exception\InvalidRequestException('client_secret');
     }
     // Validate client ID and client secret
     $client = $this->server->getClientStorage()->get($clientId, $clientSecret, null, $this->getIdentifier());
     if ($client instanceof ClientEntity === false) {
         $this->server->getEventEmitter()->emit(new Event\ClientAuthenticationFailedEvent($this->server->getRequest()));
         throw new Exception\InvalidClientException();
     }
     $username = $this->server->getRequest()->request->get('username', null);
     if (is_null($username)) {
         throw new Exception\InvalidRequestException('username');
     }
     $password = $this->server->getRequest()->request->get('password', null);
     if (is_null($password)) {
         throw new Exception\InvalidRequestException('password');
     }
     // Check if user's username and password are correct
     $userId = call_user_func($this->getVerifyCredentialsCallback(), $username, $password);
     if ($userId === false) {
         $this->server->getEventEmitter()->emit(new Event\UserAuthenticationFailedEvent($this->server->getRequest()));
         throw new Exception\InvalidCredentialsException();
     }
     // Validate any scopes that are in the request
     $scopeParam = $this->server->getRequest()->request->get('scope', '');
     $scopes = $this->validateScopes($scopeParam, $client);
     // Create a new session
     $session = new SessionEntity($this->server);
     $session->setOwner('user', $userId);
     $session->associateClient($client);
     // Generate an access token
     $accessToken = new AccessTokenEntity($this->server);
     $accessToken->setId(SecureKey::generate());
     $accessToken->setExpireTime($this->getAccessTokenTTL() + time());
     // Associate scopes with the session and access token
     foreach ($scopes as $scope) {
         $session->associateScope($scope);
     }
     foreach ($session->getScopes() as $scope) {
         $accessToken->associateScope($scope);
     }
     $this->server->getTokenType()->setSession($session);
     $this->server->getTokenType()->setParam('access_token', $accessToken->getId());
     $this->server->getTokenType()->setParam('expire_access_token', $this->getAccessTokenTTL() + time());
     // Associate a refresh token if set
     if ($this->server->hasGrantType('refresh_token')) {
         $refreshToken = new RefreshTokenEntity($this->server);
         $refreshToken->setId(SecureKey::generate());
         $refreshToken->setExpireTime($this->server->getGrantType('refresh_token')->getRefreshTokenTTL() + time());
         $this->server->getTokenType()->setParam('refresh_token', $refreshToken->getId());
         $this->server->getTokenType()->setParam('expire_refresh_token', $refreshToken->getExpireTime());
     }
     // Save everything
     $session->save();
     $accessToken->setSession($session);
     $accessToken->save();
     if ($this->server->hasGrantType('refresh_token')) {
         $refreshToken->setAccessToken($accessToken);
         $refreshToken->save();
     }
     return $this->server->getTokenType()->generateResponse();
 }