class KeyAuth in Key auth 8
Same name in this branch
- 8 src/KeyAuth.php \Drupal\key_auth\KeyAuth
- 8 src/Authentication/Provider/KeyAuth.php \Drupal\key_auth\Authentication\Provider\KeyAuth
Class KeyAuth.
Handles all functionality regarding key authentication.
Hierarchy
- class \Drupal\key_auth\KeyAuth implements KeyAuthInterface
Expanded class hierarchy of KeyAuth
3 files declare their use of KeyAuth
- KeyAuthSettingsForm.php in src/
Form/ KeyAuthSettingsForm.php - KeyAuthTest.php in tests/
src/ Functional/ KeyAuthTest.php - UserKeyAuthForm.php in src/
Form/ UserKeyAuthForm.php
1 string reference to 'KeyAuth'
1 service uses KeyAuth
File
- src/
KeyAuth.php, line 15
Namespace
Drupal\key_authView source
class KeyAuth implements KeyAuthInterface {
/**
* Key detection method: Header.
*
* @var string
*/
const DETECTION_METHOD_HEADER = 'header';
/**
* Key detection method: Query.
*
* @var string
*/
const DETECTION_METHOD_QUERY = 'query';
/**
* The module configuration.
*
* @var \Drupal\Core\Config\ImmutableConfig
*/
protected $config;
/**
* The entity manager service.
*
* @var \Drupal\Core\Entity\EntityTypeManager
*/
protected $entityTypeManager;
/**
* Constructs a new KeyAuth object.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory service.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager service.
*/
public function __construct(ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager) {
$this->config = $config_factory
->get('key_auth.settings');
$this->entityTypeManager = $entity_type_manager;
}
/**
* {@inheritdoc}
*/
public function getKey(Request $request) {
// Extract the configured detection methods.
$methods = $this->config
->get('detection_methods');
// Extract the paramater name.
$param_name = $this->config
->get('param_name');
// Check if header detection is enabled.
if (in_array(self::DETECTION_METHOD_HEADER, $methods)) {
if ($key = $request->headers
->get($param_name)) {
return $key;
}
}
// Check if query detection is enabled.
if (in_array(self::DETECTION_METHOD_QUERY, $methods)) {
if ($key = $request->query
->get($param_name)) {
return $key;
}
}
return FALSE;
}
/**
* {@inheritdoc}
*/
public function getUserByKey($key) {
// Load user storage.
$storage = $this->entityTypeManager
->getStorage('user');
// Query to find a user with this key.
$user = $storage
->getQuery()
->condition('api_key', $key)
->execute();
// Check if a user was found.
if ($user) {
// Load and return the user.
return $storage
->load(reset($user));
}
return NULL;
}
/**
* {@inheritdoc}
*/
public function access(UserInterface $user) {
return $user
->hasPermission('use key authentication');
}
/**
* {@inheritdoc}
*/
public function generateKey() {
// Determine the key length.
$length = $this->config
->get('key_length');
// Load user storage.
$storage = $this->entityTypeManager
->getStorage('user');
do {
// Generate a key.
$key = substr(bin2hex(random_bytes($length)), 0, $length);
// Query to see if this key is in use.
$in_use = $storage
->getQuery()
->condition('api_key', $key)
->execute();
} while ($in_use);
return $key;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
KeyAuth:: |
protected | property | The module configuration. | |
KeyAuth:: |
protected | property | The entity manager service. | |
KeyAuth:: |
public | function |
Determine if a user has access to use key authentication. Overrides KeyAuthInterface:: |
|
KeyAuth:: |
constant | Key detection method: Header. | ||
KeyAuth:: |
constant | Key detection method: Query. | ||
KeyAuth:: |
public | function |
Generate a new unique key. Overrides KeyAuthInterface:: |
|
KeyAuth:: |
public | function |
Get the key provided in the current request. Overrides KeyAuthInterface:: |
|
KeyAuth:: |
public | function |
Load the user associated with a given key. Overrides KeyAuthInterface:: |
|
KeyAuth:: |
public | function | Constructs a new KeyAuth object. |