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 mixed __construct ( Yiisoft\Auth\IdentityWithTokenRepositoryInterface $identityRepository )
$identityRepository Yiisoft\Auth\IdentityWithTokenRepositoryInterface

                public function __construct(protected IdentityWithTokenRepositoryInterface $identityRepository)
{
}

            
authenticate() public method
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;
}

            
challenge() public method

public \Psr\Http\Message\ResponseInterface challenge ( \Psr\Http\Message\ResponseInterface $response )
$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 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;
}

            
withHeaderName() public method
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;
}

            
withPattern() public method
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;
}

            
withRealm() public method

public self withRealm ( string $realm )
$realm string

The HTTP authentication realm.

                public function withRealm(string $realm): self
{
    $new = clone $this;
    $new->realm = $realm;
    return $new;
}

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

Identity token type

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