Middleware

API Key Middleware

Add API Key Authentication to your Kipchak API.

Introduction

The API Key middleware provides a lightweight, static API key authentication mechanism. It is ideal for service-to-service communication or simple API protection where OAuth or JWT complexity is not required.

Key features include:

  • Flexible Extraction: Can retrieve API keys from HTTP headers or URL query parameters, or, from 1.43, from the header only.
  • Constant-Time Comparison: From 1.43, the time taken does not reveal which key matched or how long it is.
  • Client Name on the Request: From 1.43, your route knows which client's key matched.
  • CORS Support: Built-in handling to allow OPTIONS preflight requests.
  • Path Whitelisting: Easily exclude specific public routes (e.g., health checks).

Installation

Install the middleware via composer by running:

composer require kipchak/middleware-auth-key

Configuration

The configuration in auth.key.php can be used to configure this middleware.

Global Settings

  • enabled (bool): Master switch. Set to true to enforce API key checks globally.
  • header (string): The name of the HTTP header to inspect.
    • Default: 'apikey'
  • query_param (string): The name of the URL query parameter to inspect.
    • Default: 'apikey'
  • accept_query_param (bool): From 1.43. Set to false to accept the key from the header only.
    • Default: true, so that an existing configuration keeps working unchanged.

We recommend setting accept_query_param to false. A key in a URL ends up in access logs, proxy logs and browser history, where anyone who reads them can use it.

Authorised Keys

  • authorised_keys (array): An associative array of valid API keys.
    • Format: ['actual_api_key_secret' => 'client_identifier']
  • Note: A key whose value is null is not authorised.

From 1.43, keys are compared in constant time: the presented key and every authorised key are hashed with SHA-256 and compared with hash_equals(), against every entry, so the time taken does not reveal which key matched or how long it is.

'authorised_keys' => [
    'secret-key-123' => 'mobile-app',
    'secret-key-456' => 'billing-service',
],

Request Handling

  • ignore_options (bool): If true, OPTIONS requests (used for CORS preflight checks) are allowed without a key.
  • ignore_paths (array): A list of URI paths that should be publicly accessible.
    • Default: ['/status']

Usage

Once enabled, the middleware checks incoming requests for the configured credentials. The middleware checks the header first, and if not found, checks the query parameter.

1. Authentication via Header

By default, the middleware looks for the apikey header.

    GET /api/resource HTTP/1.1
    Host: example.com
    apikey: secret-key-123

2. Authentication via Query Parameter

Alternatively, clients can pass the key in the URL (useful for webhooks or simple GET requests).

    GET /api/resource?apikey=secret-key-123 HTTP/1.1
    Host: example.com

Customizing Key Names

If you want to use standard headers like X-API-Key, update your configuration:

    'header' => 'X-API-Key',
    'query_param' => 'key',

Now the request would look like:

    GET /api/resource HTTP/1.1
    Host: example.com
    X-API-Key: secret-key-123

The Client Name on the Request

From 1.43, a request whose key matched reaches your route with the client name, the value the key maps to in authorised_keys, in the request attribute kipchak.auth.key.client (the constant ApiKey::ATTRIBUTE_CLIENT):

use Kipchak\Middleware\Auth\Key\Handlers\ApiKey;

$client = $request->getAttribute(ApiKey::ATTRIBUTE_CLIENT); // e.g. "billing-service"
  • When the value in authorised_keys is an empty string or not a string, the attribute is null. The request is still authorised.
  • Requests let through by ignore_paths or ignore_options do not carry the attribute.
  • The client name is never sent as a header, and neither the 401 response nor any log line contains the presented key.

Upgrading to 1.43

No change for an existing configuration: the same keys are accepted, from the header first and then the query parameter, with the same ignore_paths, ignore_options and the same 401. One fix: a request whose query parameter is an array (?apikey[]=...) is refused with the usual 401, where 1.41 failed with a PHP error.

Git Repository

The source code for this middleware is available on 1x.ax at https://1x.ax/mamluk/kipchak/middlewares/auth-key.

Previous
Auth - JWKS