|
15 | 15 | use Yiisoft\Http\Status; |
16 | 16 | use Yiisoft\View\Exception\ViewNotFoundException; |
17 | 17 | use Yiisoft\View\WebView; |
| 18 | +use Yiisoft\Yii\AuthClient\Collection; |
18 | 19 | use Yiisoft\Yii\AuthClient\Exception\InvalidConfigException; |
19 | 20 | use Yiisoft\Yii\AuthClient\Exception\NotSupportedException; |
20 | 21 |
|
|
54 | 55 | final class AuthAction implements MiddlewareInterface |
55 | 56 | { |
56 | 57 | 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; |
62 | 58 | /** |
63 | 59 | * @var string name of the GET param, which is used to passed auth client id to this action. |
64 | 60 | * 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 |
119 | 115 | * @var string the redirect url after unsuccessful authorization (e.g. user canceled). |
120 | 116 | */ |
121 | 117 | 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 | + } |
125 | 155 |
|
| 156 | + #[\Override] |
126 | 157 | public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
127 | 158 | { |
128 | 159 | $clientId = (string)$request->getAttribute($this->clientIdGetParamName); |
@@ -160,7 +191,7 @@ private function auth(AuthClientInterface $client, ServerRequestInterface $reque |
160 | 191 | /** |
161 | 192 | * @psalm-suppress MixedArgument $client |
162 | 193 | */ |
163 | | - throw new NotSupportedException('Provider "' . get_class($client) . '" is not supported.'); |
| 194 | + throw new NotSupportedException('Provider "' . $client::class . '" is not supported.'); |
164 | 195 | } |
165 | 196 |
|
166 | 197 | /** |
@@ -197,7 +228,7 @@ private function authOAuth2(OAuth2 $client, ServerRequestInterface $request): Re |
197 | 228 | // Get the access_token and save them to the session. |
198 | 229 | if (isset($queryParams['code']) && (strlen($code = (string)$queryParams['code']) > 0)) { |
199 | 230 | $token = $client->fetchAccessToken($request, $code); |
200 | | - if (strlen($token->getToken()) > 0) { |
| 231 | + if (strlen((string) $token->getToken()) > 0) { |
201 | 232 | return $this->authSuccess($client); |
202 | 233 | } |
203 | 234 | return $this->authCancel($client); |
|
0 commit comments