0 follower

Class Yiisoft\Auth\Method\HttpHeader

InheritanceYiisoft\Auth\Method\HttpHeader
ImplementsYiisoft\Auth\AuthenticationMethodInterface
SubclassesYiisoft\Auth\Method\HttpBearer

HttpHeader supports HTTP authentication through HTTP Headers.

The default implementation of HttpHeader uses the Yiisoft\Auth\IdentityWithTokenRepositoryInterface::findIdentityByToken() and passes the value of the X-Api-Key header. This implementation is used mainly for authenticating API clients.

Protected Methods

Hide inherited methods

Method Description Defined By
getAuthenticationToken() Yiisoft\Auth\Method\HttpHeader

Property Details

Hide inherited properties

$headerName protected property
protected string $headerName 'X-Api-Key'
$identityRepository protected property
$pattern protected property

A pattern to use to extract the HTTP authentication value.

protected string $pattern '/(.*)/'

Method Details

Hide inherited methods

__construct() public method

public __construct( Yiisoft\Auth\IdentityWithTokenRepositoryInterface $identityRepository ): mixed
$identityRepository Yiisoft\Auth\IdentityWithTokenRepositoryInterface

                public function __construct(protected IdentityWithTokenRepositoryInterface $identityRepository) {}

            
authenticate() public method

public authenticate( \Psr\Http\Message\ServerRequestInterface $request ): Yiisoft\Auth\IdentityInterface|null
$request \Psr\Http\Message\ServerRequestInterface

                public function authenticate(ServerRequestInterface $request): ?IdentityInterface
{
    $authToken = $this->getAuthenticationToken($request);
    if ($authToken !== null) {
        return $this->identityRepository->findIdentityByToken($authToken, $this->tokenType);
    }
    return null;
}

            
challenge() public method

public challenge( \Psr\Http\Message\ResponseInterface $response ): \Psr\Http\Message\ResponseInterface
$response \Psr\Http\Message\ResponseInterface

                public function challenge(ResponseInterface $response): ResponseInterface
{
    return $response;
}

            
getAuthenticationToken() protected method

protected getAuthenticationToken( \Psr\Http\Message\ServerRequestInterface $request ): string|null
$request \Psr\Http\Message\ServerRequestInterface

                protected function getAuthenticationToken(ServerRequestInterface $request): ?string
{
    $authHeaders = $request->getHeader($this->headerName);
    $authHeader = reset($authHeaders);
    if (!empty($authHeader)) {
        if (preg_match($this->pattern, $authHeader, $matches)) {
            $authHeader = $matches[1];
        } else {
            return null;
        }
        return $authHeader;
    }
    return null;
}

            
withHeaderName() public method

public withHeaderName( string $name ): $this
$name string

The HTTP header name.

                public function withHeaderName(string $name): self
{
    $new = clone $this;
    $new->headerName = $name;
    return $new;
}

            
withPattern() public method

public withPattern( string $pattern ): self
$pattern string

A pattern to use to extract the HTTP authentication value.

                public function withPattern(#[Language('RegExp')] string $pattern): self
{
    $new = clone $this;
    $new->pattern = $pattern;
    return $new;
}

            
withTokenType() public method

public withTokenType( string|null $type ): $this
$type string|null

Identity token type

                public function withTokenType(?string $type): self
{
    $new = clone $this;
    $new->tokenType = $type;
    return $new;
}