0 follower

Final Class Yiisoft\Auth\Method\HttpBearer

InheritanceYiisoft\Auth\Method\HttpBearer » Yiisoft\Auth\Method\HttpHeader
ImplementsYiisoft\Auth\AuthenticationMethodInterface

Authentication method based on HTTP Bearer token.

See also https://tools.ietf.org/html/rfc6750.

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 = \Yiisoft\Http\Header::AUTHORIZATION
$pattern protected property
protected string $pattern '/^Bearer\s+(.*?)$/'

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->withHeader(Header::WWW_AUTHENTICATE, "{$this->headerName} realm=\"{$this->realm}\"");
}

            
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;
}

            
withRealm() public method

public withRealm( string $realm ): self
$realm string

The HTTP authentication realm.

                public function withRealm(string $realm): self
{
    $new = clone $this;
    $new->realm = $realm;
    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;
}