0 follower

Final Class Yiisoft\Auth\Method\HttpBasic

InheritanceYiisoft\Auth\Method\HttpBasic
ImplementsYiisoft\Auth\AuthenticationMethodInterface

HTTP Basic authentication method.

See also https://tools.ietf.org/html/rfc7617 In case authentication does not work as expected, make sure your web server passes username and password to $request->getServerParams()['PHP_AUTH_USER'] and $request->getServerParams()['PHP_AUTH_PW'] parameters. If you are using Apache with PHP-CGI, you might need to add this line to your .htaccess file: RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L].

Method Details

Hide inherited methods

__construct() public method

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

                public function __construct(private 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
{
    [$username, $password] = $this->getAuthenticationCredentials($request);
    if ($this->authenticationCallback !== null && ($username !== null || $password !== null)) {
        return call_user_func($this->authenticationCallback, $username, $password, $this->identityRepository);
    }
    if ($username !== null) {
        return $this->identityRepository->findIdentityByToken($username, $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, "Basic realm=\"{$this->realm}\"");
}

            
withAuthenticationCallback() public method

public self withAuthenticationCallback ( callable $authenticationCallback )
$authenticationCallback callable

A PHP callable that will authenticate the user with the HTTP basic authentication information. The callable should have the following signature:

static function (
    ?string $username,
    #[\SensitiveParameter] ?string $password,
    \Yiisoft\Auth\IdentityRepositoryInterface $identityRepository
): ?\Yiisoft\Auth\IdentityInterface

It should return an identity object that matches the username and password. Null should be returned if there is no such identity. The callable will be called only if current user is not authenticated.

If not set, the username information will be considered as an access token while the password information will be ignored. The {@see \Yiisoft\Auth\IdentityWithTokenRepositoryInterface::findIdentityByToken()} method will be called to authenticate an identity.

                public function withAuthenticationCallback(callable $authenticationCallback): self
{
    $new = clone $this;
    $new->authenticationCallback = $authenticationCallback;
    return $new;
}

            
withRealm() public method

public $this 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;
}