You are here

class KeyAuth in Key auth 8

Same name in this branch
  1. 8 src/KeyAuth.php \Drupal\key_auth\KeyAuth
  2. 8 src/Authentication/Provider/KeyAuth.php \Drupal\key_auth\Authentication\Provider\KeyAuth

Class KeyAuth.

Handles all functionality regarding key authentication.

Hierarchy

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'
key_auth.services.yml in ./key_auth.services.yml
key_auth.services.yml
1 service uses KeyAuth
key_auth in ./key_auth.services.yml
Drupal\key_auth\KeyAuth

File

src/KeyAuth.php, line 15

Namespace

Drupal\key_auth
View 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

Namesort descending Modifiers Type Description Overrides
KeyAuth::$config protected property The module configuration.
KeyAuth::$entityTypeManager protected property The entity manager service.
KeyAuth::access public function Determine if a user has access to use key authentication. Overrides KeyAuthInterface::access
KeyAuth::DETECTION_METHOD_HEADER constant Key detection method: Header.
KeyAuth::DETECTION_METHOD_QUERY constant Key detection method: Query.
KeyAuth::generateKey public function Generate a new unique key. Overrides KeyAuthInterface::generateKey
KeyAuth::getKey public function Get the key provided in the current request. Overrides KeyAuthInterface::getKey
KeyAuth::getUserByKey public function Load the user associated with a given key. Overrides KeyAuthInterface::getUserByKey
KeyAuth::__construct public function Constructs a new KeyAuth object.