innmind/http-authentication

HTTP authentication

5.0.0 2025-08-24 19:55 UTC

README

Build Status codecov Type Coverage

Simple tool to authenticate a request.

Installation

composer require innmind/http-authentication

Usage

use Innmind\HttpAuthentication\ViaBasicAuthorization;
use Innmind\Router\{
    Router,
    Component,
    Collect,
    Handle,
};
use Innmind\Http\{
    ServerRequest,
    Response,
};
use Innmind\Immutable\Attempt;

function login(string $user, string $password): Attempt
{
    // find the user
}

$router = Router::of(
    Component::of(ViaBasicAuthorization::of(
        static fn(string $user, string $password) => login($user, $password),
    ))
        ->map(Collect::of('user'))
        ->pipe(Handle::of(
            static fn(ServerRequest $request, mixed $user) => Attempt::result(
                Response::of(
                    // build response
                ),
            ),
        ));
);

$response = $router(/* an instance of Innmind\Http\Message\ServerRequest */)->unwrap();