class UsersJwtKeyRepository in JSON Web Token Authentication (JWT) 8
Class UsersJwtKeyRepository
Hierarchy
- class \Drupal\users_jwt\UsersJwtKeyRepository implements UsersJwtKeyRepositoryInterface uses StringTranslationTrait
Expanded class hierarchy of UsersJwtKeyRepository
1 string reference to 'UsersJwtKeyRepository'
- users_jwt.services.yml in modules/
users_jwt/ users_jwt.services.yml - modules/users_jwt/users_jwt.services.yml
1 service uses UsersJwtKeyRepository
- users_jwt.key_repository in modules/
users_jwt/ users_jwt.services.yml - Drupal\users_jwt\UsersJwtKeyRepository
File
- modules/
users_jwt/ src/ UsersJwtKeyRepository.php, line 13
Namespace
Drupal\users_jwtView source
class UsersJwtKeyRepository implements UsersJwtKeyRepositoryInterface {
use StringTranslationTrait;
/**
* The user data service.
*
* @var \Drupal\user\UserDataInterface
*/
protected $userData;
/**
* The memory cache.
*
* @var \Drupal\Core\Cache\CacheBackendInterface
*/
protected $keyCache;
/**
* Cache tags invalidator service.
*
* @var \Drupal\Core\Cache\CacheTagsInvalidatorInterface
*/
protected $cacheTagsInvalidator;
/**
* Algorithm options.
*
* @var array
*/
protected $options = [];
/**
* UsersJwtKeyRepository constructor.
*
* @param \Drupal\user\UserDataInterface $user_data
* The user data service.
* @param \Drupal\Core\Cache\CacheBackendInterface $key_memory_cache
* A cache for already loaded keys, usually a memory cache (or null cache).
* @param \Drupal\Core\Cache\CacheTagsInvalidatorInterface $cache_tags_invalidator
* Cache tags invalidator service.
*/
public function __construct(UserDataInterface $user_data, CacheBackendInterface $key_memory_cache, CacheTagsInvalidatorInterface $cache_tags_invalidator) {
$this->userData = $user_data;
$this->keyCache = $key_memory_cache;
$this->cacheTagsInvalidator = $cache_tags_invalidator;
}
/**
* {@inheritdoc}
*/
public function getKey($id) : ?UsersKey {
$cached = $this->keyCache
->get($id);
if ($cached) {
$key = $cached->data;
}
else {
$keys = $this->userData
->get('users_jwt', NULL, $id);
// The key ID needs to be unique.
if (empty($keys) || count($keys) > 1) {
$key = NULL;
}
else {
$key = end($keys);
}
$this->keyCache
->set($id, $key);
}
return $key;
}
/**
* {@inheritdoc}
*/
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;
}
/**
* {@inheritdoc}
*/
public function deleteKey($id) {
$keys = $this->userData
->get('users_jwt', NULL, $id);
if ($keys) {
$this->userData
->delete('users_jwt', NULL, $id);
// There should be only one key, but invalidate for any we found.
$cache_tags = [];
foreach ($keys as $key_uid => $key_data) {
$cache_tags[] = 'users_jwt:' . $key_uid;
}
$this->cacheTagsInvalidator
->invalidateTags($cache_tags);
}
}
/**
* {@inheritdoc}
*/
public function deleteUsersKeys($uid) {
$this->userData
->delete('users_jwt', $uid);
$this->cacheTagsInvalidator
->invalidateTags([
'users_jwt:' . $uid,
]);
}
/**
* {@inheritdoc}
*/
public function getUsersKeys($uid) : array {
return $this->userData
->get('users_jwt', $uid);
}
/**
* {@inheritdoc}
*/
public function algorithmOptions() : array {
if (empty($this->options)) {
$this->options['RS256'] = $this
->t('RSA (2048 bits or more)');
}
return $this->options;
}
/**
* {@inheritdoc}
*/
public function offsetExists($offset) {
return (bool) $this
->getKey($offset);
}
/**
* {@inheritdoc}
*/
public function offsetGet($offset) {
$key = $this
->getKey($offset);
return $key ? $key->pubkey : NULL;
}
/**
* {@inheritdoc}
*/
public function offsetSet($offset, $value) {
}
/**
* {@inheritdoc}
*/
public function offsetUnset($offset) {
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UsersJwtKeyRepository:: |
protected | property | Cache tags invalidator service. | |
UsersJwtKeyRepository:: |
protected | property | The memory cache. | |
UsersJwtKeyRepository:: |
protected | property | Algorithm options. | |
UsersJwtKeyRepository:: |
protected | property | The user data service. | |
UsersJwtKeyRepository:: |
public | function |
Get options for supported algorithms. Overrides UsersJwtKeyRepositoryInterface:: |
|
UsersJwtKeyRepository:: |
public | function |
Delete one key. Overrides UsersJwtKeyRepositoryInterface:: |
|
UsersJwtKeyRepository:: |
public | function |
Delete all keys for one user. Overrides UsersJwtKeyRepositoryInterface:: |
|
UsersJwtKeyRepository:: |
public | function |
Get a user key by key ID. Overrides UsersJwtKeyRepositoryInterface:: |
|
UsersJwtKeyRepository:: |
public | function |
Return all keys for one user. Overrides UsersJwtKeyRepositoryInterface:: |
|
UsersJwtKeyRepository:: |
public | function | ||
UsersJwtKeyRepository:: |
public | function |
Extends \ArrayAccess::offsetGet(). Overrides UsersJwtKeyRepositoryInterface:: |
|
UsersJwtKeyRepository:: |
public | function | ||
UsersJwtKeyRepository:: |
public | function | ||
UsersJwtKeyRepository:: |
public | function |
Save a key for a user account. Overrides UsersJwtKeyRepositoryInterface:: |
|
UsersJwtKeyRepository:: |
public | function | UsersJwtKeyRepository constructor. |