0

Final Class Yiisoft\Yii\AuthClient\AuthAction

InheritanceYiisoft\Yii\AuthClient\AuthAction
ImplementsPsr\Http\Server\MiddlewareInterface

AuthAction is a PSR-15 middleware, which performs authentication via {@see OAuth2} auth clients (including {@see OpenIdConnect}).

Usage, registered as a DI definition and attached to a route:

// config/di.php
AuthAction::class => static fn (
    Collection $clientCollection,
    Aliases $aliases,
    WebView $view,
    ResponseFactoryInterface $responseFactory,
    CurrentRoute $currentRoute,
) => (new AuthAction($clientCollection, $aliases, $view, $responseFactory, $currentRoute))
    ->withSuccessUrl('/site/index')
    ->withCancelUrl('/site/login')
    ->withSuccessCallback(function (AuthClientInterface $client) {
        $attributes = $client->getUserAttributes();
        // user login or signup comes here
    })
    ->withCancelCallback(function (AuthClientInterface $client) {
        // set flash, logging, etc.
    }),

// config/routes.php
Route::methods(['GET', 'POST'], '/auth/{authclient}')->action(AuthAction::class),

Usually authentication via external services is performed inside the popup window. This action handles the redirection and closing of popup window correctly.

The matched {authclient} route placeholder is read from {@see \Yiisoft\Router\CurrentRoute}, not from a PSR-7 request attribute: yiisoft/router (a hard dependency of this package) never populates request attributes for matched route arguments, it exposes them exclusively through CurrentRoute.

See also:

Constants

Hide inherited constants

Constant Value Description Defined By
AUTH_NAME 'auth_displayname' Yiisoft\Yii\AuthClient\AuthAction

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( Yiisoft\Yii\AuthClient\Collection $clientCollection, \Yiisoft\Aliases\Aliases $aliases, \Yiisoft\View\WebView $view, \Psr\Http\Message\ResponseFactoryInterface $responseFactory, \Yiisoft\Router\CurrentRoute $currentRoute )
$clientCollection Yiisoft\Yii\AuthClient\Collection
$aliases \Yiisoft\Aliases\Aliases
$view \Yiisoft\View\WebView
$responseFactory \Psr\Http\Message\ResponseFactoryInterface
$currentRoute \Yiisoft\Router\CurrentRoute

                public function __construct(
    /**
     * @var Collection
     * It should point to {@see Collection} instance.
     */
    private readonly Collection $clientCollection,
    private readonly Aliases $aliases,
    private readonly WebView $view,
    private readonly ResponseFactoryInterface $responseFactory,
    private readonly CurrentRoute $currentRoute,
) {}

            
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
{
    $clientId = (string) $this->currentRoute->getArgument($this->clientIdGetParamName);
    if (strlen($clientId) > 0) {
        if (!$this->clientCollection->hasClient($clientId)) {
            return $this->responseFactory->createResponse(Status::NOT_FOUND, "Unknown auth client '{$clientId}'");
        }
        $client = $this->clientCollection->getClient($clientId);
        return $this->auth($client, $request);
    }
    return $this->responseFactory->createResponse(Status::NOT_FOUND);
}

            
withCancelCallback() public method

public Yiisoft\Yii\AuthClient\AuthAction withCancelCallback ( callable $callback )
$callback callable

PHP callback, which should be triggered in case of authentication cancellation. This callback should accept {@see \Yiisoft\Yii\AuthClient\AuthClientInterface} instance as an argument. If it returns a {@see \Psr\Http\Message\ResponseInterface} instance, it will be used as action response, otherwise redirection to {@see \Yiisoft\Yii\AuthClient\cancelUrl} will be performed.

                public function withCancelCallback(callable $callback): self
{
    $new = clone $this;
    $new->cancelCallback = $callback;
    return $new;
}

            
withCancelUrl() public method

public Yiisoft\Yii\AuthClient\AuthAction withCancelUrl ( string $url )
$url string

Cancel URL.

                public function withCancelUrl(string $url): self
{
    $new = clone $this;
    $new->cancelUrl = $url;
    return $new;
}

            
withSuccessCallback() public method

public Yiisoft\Yii\AuthClient\AuthAction withSuccessCallback ( callable $callback )
$callback callable

PHP callback, which should be triggered in case of successful authentication. This callback should accept {@see \Yiisoft\Yii\AuthClient\AuthClientInterface} instance as an argument. If it returns a {@see \Psr\Http\Message\ResponseInterface} instance, it will be used as action response, otherwise redirection to {@see \Yiisoft\Yii\AuthClient\successUrl} will be performed.

                public function withSuccessCallback(callable $callback): self
{
    $new = clone $this;
    $new->successCallback = $callback;
    return $new;
}

            
withSuccessUrl() public method

public Yiisoft\Yii\AuthClient\AuthAction withSuccessUrl ( string $url )
$url string

Successful URL.

                public function withSuccessUrl(string $url): self
{
    $new = clone $this;
    $new->successUrl = $url;
    return $new;
}