You are here

public function OAuth2Storage::setAuthorizationCode in OAuth2 Server 8

Same name and namespace in other branches
  1. 2.0.x src/OAuth2Storage.php \Drupal\oauth2_server\OAuth2Storage::setAuthorizationCode()

Set authorization code.

Parameters

string $code: The authorization code string.

mixed $client_id: The client id string.

int $uid: The user uid.

string $redirect_uri: The redirect uri string.

int $expires: The timestamp the authorization code expires.

string|null $scope: The scope string.

string|null $id_token: The token string.

Return value

int Whether the authorization code could be saved or not.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

\Drupal\Core\Entity\EntityStorageException

File

src/OAuth2Storage.php, line 519

Class

OAuth2Storage
Provides Drupal OAuth2 storage for the library.

Namespace

Drupal\oauth2_server

Code

public function setAuthorizationCode($code, $client_id, $uid, $redirect_uri, $expires, $scope = NULL, $id_token = NULL) {

  /** @var \Drupal\oauth2_server\ClientInterface $client */
  $client = $this
    ->getStorageClient($client_id);
  if (!$client) {
    throw new \InvalidArgumentException("The supplied client couldn't be loaded.");
  }

  // If no code was found, start with a new entity.

  /** @var \Drupal\oauth2_server\AuthorizationCodeInterface $authorization_code */
  $authorization_code = $this
    ->getStorageAuthorizationCode($code);
  if (!$authorization_code) {

    /** @var \Drupal\user\UserInterface $user */
    $user = $this->entityTypeManager
      ->getStorage('user')
      ->load($uid);
    if (!$user) {
      throw new \InvalidArgumentException("The supplied user couldn't be loaded.");
    }

    /** @var \Drupal\oauth2_server\AuthorizationCodeInterface $authorization_code */
    $authorization_code = $this->entityTypeManager
      ->getStorage('oauth2_server_authorization_code')
      ->create([]);
    $authorization_code->client_id = $client
      ->id();
    $authorization_code->uid = $user
      ->id();
    $authorization_code->code = $code;
    $authorization_code->id_token = $id_token;
  }
  $authorization_code->redirect_uri = $redirect_uri;
  $authorization_code->expires = $expires;
  $this
    ->setScopeData($authorization_code, $client
    ->getServer(), $scope);
  return $authorization_code
    ->save();
}