Class Yiisoft\Auth\Method\HttpHeader
| Inheritance | Yiisoft\Auth\Method\HttpHeader |
|---|---|
| Implements | Yiisoft\Auth\AuthenticationMethodInterface |
| Subclasses | Yiisoft\Auth\Method\HttpBearer |
HttpHeader supports HTTP authentication through HTTP Headers.
The default implementation of HttpHeader uses the
{@see \Yiisoft\Auth\IdentityWithTokenRepositoryInterface::findIdentityByToken()}
and passes the value of the X-Api-Key header. This implementation is used mainly for authenticating API clients.
Protected Properties
| Property | Type | Description | Defined By |
|---|---|---|---|
| $headerName | string | Yiisoft\Auth\Method\HttpHeader | |
| $identityRepository | Yiisoft\Auth\IdentityWithTokenRepositoryInterface | Yiisoft\Auth\Method\HttpHeader | |
| $pattern | string | A pattern to use to extract the HTTP authentication value. | Yiisoft\Auth\Method\HttpHeader |
Public Methods
Protected Methods
| Method | Description | Defined By |
|---|---|---|
| getAuthenticationToken() | Yiisoft\Auth\Method\HttpHeader |
Property Details
Method Details
| public mixed __construct ( Yiisoft\Auth\IdentityWithTokenRepositoryInterface $identityRepository ) | ||
| $identityRepository | Yiisoft\Auth\IdentityWithTokenRepositoryInterface | |
public function __construct(protected IdentityWithTokenRepositoryInterface $identityRepository)
{
}
| public Yiisoft\Auth\IdentityInterface|null authenticate ( \Psr\Http\Message\ServerRequestInterface $request ) | ||
| $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;
}
| public \Psr\Http\Message\ResponseInterface challenge ( \Psr\Http\Message\ResponseInterface $response ) | ||
| $response | \Psr\Http\Message\ResponseInterface | |
public function challenge(ResponseInterface $response): ResponseInterface
{
return $response;
}
| protected string|null getAuthenticationToken ( \Psr\Http\Message\ServerRequestInterface $request ) | ||
| $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;
}
| public $this withHeaderName ( string $name ) | ||
| $name | string |
The HTTP header name. |
public function withHeaderName(string $name): self
{
$new = clone $this;
$new->headerName = $name;
return $new;
}
| public self withPattern ( string $pattern ) | ||
| $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;
}
Signup or Login in order to comment.