Unify and clarify how to write "api-key" vs. "api_key" vs. "apikey" and stick to (Swagger) standards?

Created on 3 April 2024, about 1 year ago

Problem/Motivation

The code currently has different wordings for the api_key / apikey / api-key which is super confusing.

The module page for example says:

3) Making a request to the endpoint with a key
The module will look for either a request parameter or header (depending on how the module was configured) called api-key and look to compare this value against the key defined for the endpoint. Any requests without this value or with an invalid value will be blocked.

The implementation on the other hand is:

  /**
   * Retrieve key from request.
   *
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   The request object that the service will respond to.
   *
   * @return bool
   *   True if api key is present
   */
  public function getKey(Request $request) {
    // Exempt edit/delete form route.
    $route_name = $this->currentRouteMatch->getRouteName();
    if (str_contains($route_name ?? '', 'entity.api_key')) {
      return FALSE;
    }

    $form_api_key = $request->request->get('api_key');
    if (!empty($form_api_key)) {
      return $form_api_key;
    }

    $query_api_key = $request->query->get('api_key');
    if (!empty($query_api_key)) {
      return $query_api_key;
    }

    $header_api_key = $request->headers->get('apikey');
    if (!empty($header_api_key)) {
      return $header_api_key;
    }
    return FALSE;
  }

Steps to reproduce

Proposed resolution

Stick to standards and for the future use the Swagger defaults: https://swagger.io/docs/specification/authentication/api-keys/

Additionally (or alternatively) make the other variations configurable. This could also be a good way to keep the old keys for existing implementations.

Remaining tasks

Decide which key to use
Implement
Release, probably as new major release?

User interface changes

API changes

Data model changes

šŸ“Œ Task
Status

Active

Version

3.0

Component

Code

Created by

šŸ‡©šŸ‡ŖGermany Anybody Porta Westfalica

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024