You are here

public function KeyAuth::getKey in Key auth 8

Get the key provided in the current request.

Parameters

\Symfony\Component\HttpFoundation\Request $request: The current request.

Return value

string|false The API key provided in the request, or FALSE if there was not one.

Overrides KeyAuthInterface::getKey

File

src/KeyAuth.php, line 61

Class

KeyAuth
Class KeyAuth.

Namespace

Drupal\key_auth

Code

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;
}