You are here

public function UsersJwtKeyRepository::saveKey in JSON Web Token Authentication (JWT) 8

Save a key for a user account.

Parameters

int $uid: The user account ID the data is associated with.

string $id: The unique name of the key.

string $alg: The JWT algorithm to use. e.g. 'RS256'.

string $pubkey: The value to store.

Return value

\Drupal\users_jwt\UsersKey The values of the key that were saved.

Throws

\InvalidArgumentException If the $id is empty or used by another user (i.e. not unique).

Overrides UsersJwtKeyRepositoryInterface::saveKey

File

modules/users_jwt/src/UsersJwtKeyRepository.php, line 85

Class

UsersJwtKeyRepository
Class UsersJwtKeyRepository

Namespace

Drupal\users_jwt

Code

public function saveKey($uid, $id, $alg, $pubkey) : UsersKey {
  if (empty($id)) {
    throw new \InvalidArgumentException("Key ID '{$id}' is empty");
  }
  $keys = $this->userData
    ->get('users_jwt', NULL, $id);
  foreach ($keys as $key_uid => $key_data) {
    if ($key_uid !== $uid) {
      throw new \InvalidArgumentException("Key ID '{$id}' is already in use by user with uid {$key_uid}");
    }
  }
  $key = new UsersKey($uid, $id, $alg, $pubkey);
  $this->userData
    ->set('users_jwt', $uid, $id, $key);
  $this->keyCache
    ->delete($id);
  $this->cacheTagsInvalidator
    ->invalidateTags([
    'users_jwt:' . $uid,
  ]);
  return $key;
}