You are here

public function ApiKeyAuth::authenticate in Services API Key Authentication 8

Same name and namespace in other branches
  1. 8.2 src/Authentication/Provider/ApiKeyAuth.php \Drupal\services_api_key_auth\Authentication\Provider\ApiKeyAuth::authenticate()
  2. 3.0.x src/Authentication/Provider/ApiKeyAuth.php \Drupal\services_api_key_auth\Authentication\Provider\ApiKeyAuth::authenticate()
  3. 2.0.x src/Authentication/Provider/ApiKeyAuth.php \Drupal\services_api_key_auth\Authentication\Provider\ApiKeyAuth::authenticate()

Authenticates the user.

Parameters

\Symfony\Component\HttpFoundation\Request|null $request: The request object.

Return value

\Drupal\Core\Session\AccountInterface|null AccountInterface - in case of a successful authentication. NULL - in case where authentication failed.

Overrides AuthenticationProviderInterface::authenticate

File

src/Authentication/Provider/ApiKeyAuth.php, line 62

Class

ApiKeyAuth
HTTP Basic authentication provider.

Namespace

Drupal\services_api_key_auth\Authentication\Provider

Code

public function authenticate(Request $request) {

  // Load config entity.
  $api_key_entities = \Drupal::entityTypeManager()
    ->getStorage('api_key')
    ->loadMultiple();
  foreach ($api_key_entities as $key_item) {
    if ($this
      ->getKey($request) == $key_item->key) {
      $accounts = $this->entityTypeManager
        ->getStorage('user')
        ->loadByProperties(array(
        'uuid' => $key_item->user_uuid,
      ));
      $account = reset($accounts);
      if (isset($account)) {
        return $account;
      }
      break;
    }
  }
  return [];
}