0 follower

Final Class Yiisoft\Auth\Middleware\Authentication

InheritanceYiisoft\Auth\Middleware\Authentication
ImplementsPsr\Http\Server\MiddlewareInterface

Authentication middleware tries to authenticate and identity using request data.

If identity is found, it is set to request attribute allowing further middleware to obtain and use it. If identity is not found failure handler is called. By default it is {@see \Yiisoft\Auth\Handler\AuthenticationFailureHandler}.

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( Yiisoft\Auth\AuthenticationMethodInterface $authenticationMethod, \Psr\Http\Message\ResponseFactoryInterface $responseFactory, \Psr\Http\Server\RequestHandlerInterface|null $authenticationFailureHandler null )
$authenticationMethod Yiisoft\Auth\AuthenticationMethodInterface
$responseFactory \Psr\Http\Message\ResponseFactoryInterface
$authenticationFailureHandler \Psr\Http\Server\RequestHandlerInterface|null

                public function __construct(
    private AuthenticationMethodInterface $authenticationMethod,
    ResponseFactoryInterface $responseFactory,
    ?RequestHandlerInterface $authenticationFailureHandler = null,
) {
    $this->failureHandler = $authenticationFailureHandler ?? new AuthenticationFailureHandler(
        $responseFactory
    );
}

            
process() public method

public \Psr\Http\Message\ResponseInterface process ( \Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Server\RequestHandlerInterface $handler )
$request \Psr\Http\Message\ServerRequestInterface
$handler \Psr\Http\Server\RequestHandlerInterface

                public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
    $identity = $this->authenticationMethod->authenticate($request);
    $request = $request->withAttribute(self::class, $identity);
    if ($identity === null && !$this->isOptional($request)) {
        return $this->authenticationMethod->challenge(
            $this->failureHandler->handle($request)
        );
    }
    return $handler->handle($request);
}

            
withOptionalPatterns() public method

See also \Yiisoft\Strings\WildcardPattern.

public self withOptionalPatterns ( array $optional )
$optional array

Patterns to match to consider the given request URI path optional.

                public function withOptionalPatterns(array $optional): self
{
    $new = clone $this;
    $new->optionalPatterns = $optional;
    return $new;
}