Skip to content

AuthorizationHeader

Viames Marino edited this page Nov 6, 2026 · 2 revisions

Pair framework: AuthorizationHeader

Pair\Http\AuthorizationHeader centralizes parsing for HTTP Authorization headers.

It is shared by Request, OAuth2Token, and the Pair API bootstrap path so Basic and Bearer credentials are read consistently across SAPIs.

Why it exists

Different web servers expose the Authorization header through different PHP variables. Pair checks the common server keys and falls back to apache_request_headers() when available.

This avoids having one auth path read HTTP_AUTHORIZATION while another misses the same request.

Main methods

fromGlobals(): ?string

Reads the raw Authorization header from the current request environment.

It checks:

  • Authorization
  • HTTP_AUTHORIZATION
  • REDIRECT_HTTP_AUTHORIZATION
  • apache_request_headers() fallback

The returned value is trimmed, and empty values become null.

fromServer(array $server): ?string

Reads the raw header from a server-like array. This is useful for tests and for code that wants deterministic input.

$header = \Pair\Http\AuthorizationHeader::fromServer([
    'HTTP_AUTHORIZATION' => 'Bearer opaque-token',
]);

bearerToken(?string $header): ?string

Extracts a Bearer token case-insensitively.

$token = \Pair\Http\AuthorizationHeader::bearerToken('Bearer opaque-token');

The RFC 6750 b64token grammar is enforced. Spaces separate the scheme and token; embedded whitespace and invalid token characters are rejected. The scheme remains case-insensitive.

basicCredentials(?string $header): ?array

Extracts Basic credentials from a valid Authorization header.

$credentials = \Pair\Http\AuthorizationHeader::basicCredentials(
    'Basic ' . base64_encode('client-id:client-secret')
);

The returned array contains:

  • id
  • secret

Invalid base64 data, missing separators, and empty IDs or secrets return null. Whitespace inside a decoded credential is significant and is never trimmed. This generic Basic parser does not perform OAuth form decoding: use Pair\Api\OAuth2::clientCredentials() for client_secret_basic.

Framework behavior

Request delegates header('Authorization') and bearerToken() to this helper.

OAuth2Token uses the shared raw-header lookup. Its deprecated static parsing facades deliberately preserve historical behavior for frozen callers; use the strict helpers for new endpoints.

Common pitfalls

  • Reading only $_SERVER['HTTP_AUTHORIZATION'] in custom code.
  • Treating any value after Bearer as valid without checking the standard shape.
  • Logging raw Authorization headers while debugging authentication failures.

See also: Request, OAuth2Token, ApiController, ApiToken, API.

Clone this wiki locally