Skip to content

Commit 45095ab

Browse files
committed
Update AuthAction.php
1 parent 7d4e6a1 commit 45095ab

File tree

1 file changed

+41
-10
lines changed

1 file changed

+41
-10
lines changed

src/AuthAction.php

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Yiisoft\Http\Status;
1616
use Yiisoft\View\Exception\ViewNotFoundException;
1717
use Yiisoft\View\WebView;
18+
use Yiisoft\Yii\AuthClient\Collection;
1819
use Yiisoft\Yii\AuthClient\Exception\InvalidConfigException;
1920
use Yiisoft\Yii\AuthClient\Exception\NotSupportedException;
2021

@@ -54,11 +55,6 @@
5455
final class AuthAction implements MiddlewareInterface
5556
{
5657
public const string AUTH_NAME = 'auth_displayname';
57-
/**
58-
* @var Collection
59-
* It should point to {@see Collection} instance.
60-
*/
61-
private readonly Collection $clientCollection;
6258
/**
6359
* @var string name of the GET param, which is used to passed auth client id to this action.
6460
* Note: watch for the naming, make sure you do not choose name used in some auth protocol.
@@ -119,10 +115,45 @@ final class AuthAction implements MiddlewareInterface
119115
* @var string the redirect url after unsuccessful authorization (e.g. user canceled).
120116
*/
121117
private readonly string $cancelUrl;
122-
private readonly ResponseFactoryInterface $responseFactory;
123-
private readonly Aliases $aliases;
124-
private readonly WebView $view;
118+
119+
public function __construct(
120+
/**
121+
* @var Collection
122+
* It should point to {@see Collection} instance.
123+
*/
124+
private readonly Collection $clientCollection,
125+
private readonly Aliases $aliases,
126+
private readonly WebView $view,
127+
private readonly ResponseFactoryInterface $responseFactory
128+
)
129+
{
130+
}
131+
132+
/**
133+
* @param string $url successful URL.
134+
*
135+
* @return AuthAction
136+
*/
137+
public function withSuccessUrl(string $url): self
138+
{
139+
$new = clone $this;
140+
$new->successUrl = $url;
141+
return $new;
142+
}
143+
144+
/**
145+
* @param string $url cancel URL.
146+
*
147+
* @return AuthAction
148+
*/
149+
public function withCancelUrl(string $url): self
150+
{
151+
$new = clone $this;
152+
$new->cancelUrl = $url;
153+
return $new;
154+
}
125155

156+
#[\Override]
126157
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
127158
{
128159
$clientId = (string)$request->getAttribute($this->clientIdGetParamName);
@@ -160,7 +191,7 @@ private function auth(AuthClientInterface $client, ServerRequestInterface $reque
160191
/**
161192
* @psalm-suppress MixedArgument $client
162193
*/
163-
throw new NotSupportedException('Provider "' . get_class($client) . '" is not supported.');
194+
throw new NotSupportedException('Provider "' . $client::class . '" is not supported.');
164195
}
165196

166197
/**
@@ -197,7 +228,7 @@ private function authOAuth2(OAuth2 $client, ServerRequestInterface $request): Re
197228
// Get the access_token and save them to the session.
198229
if (isset($queryParams['code']) && (strlen($code = (string)$queryParams['code']) > 0)) {
199230
$token = $client->fetchAccessToken($request, $code);
200-
if (strlen($token->getToken()) > 0) {
231+
if (strlen((string) $token->getToken()) > 0) {
201232
return $this->authSuccess($client);
202233
}
203234
return $this->authCancel($client);

0 commit comments

Comments
 (0)