|
31 | 31 | use HeadlessChromium\Exception\TargetDestroyed; |
32 | 32 | use HeadlessChromium\Input\Keyboard; |
33 | 33 | use HeadlessChromium\Input\Mouse; |
| 34 | +use HeadlessChromium\PageUtils\AuthenticationScope; |
34 | 35 | use HeadlessChromium\PageUtils\CookiesGetter; |
35 | 36 | use HeadlessChromium\PageUtils\PageEvaluation; |
36 | 37 | use HeadlessChromium\PageUtils\PageLayoutMetrics; |
@@ -79,6 +80,34 @@ class Page |
79 | 80 | */ |
80 | 81 | protected $dom; |
81 | 82 |
|
| 83 | + /** |
| 84 | + * Credentials to be used to answer HTTP authentication challenges. |
| 85 | + * |
| 86 | + * @var array{username: string, password: string}|null |
| 87 | + */ |
| 88 | + protected $authCredentials; |
| 89 | + |
| 90 | + /** |
| 91 | + * Scope the authentication credentials are restricted to. |
| 92 | + * |
| 93 | + * @var AuthenticationScope|null |
| 94 | + */ |
| 95 | + protected $authScope; |
| 96 | + |
| 97 | + /** |
| 98 | + * Challenges that were already provided the credentials. |
| 99 | + * |
| 100 | + * @var array<string, true> |
| 101 | + */ |
| 102 | + protected $attemptedAuthentications = []; |
| 103 | + |
| 104 | + /** |
| 105 | + * Whether the authentication challenge listeners were registered. |
| 106 | + * |
| 107 | + * @var bool |
| 108 | + */ |
| 109 | + protected $authListenersRegistered = false; |
| 110 | + |
82 | 111 | /** |
83 | 112 | * Page constructor. |
84 | 113 | * |
@@ -162,58 +191,137 @@ public function getSession(): Session |
162 | 191 | } |
163 | 192 |
|
164 | 193 | /** |
165 | | - * Sets credentials to be used when the page encounters an HTTP authentication challenge. |
| 194 | + * Sets credentials to be used when the page encounters an HTTP authentication challenge, |
| 195 | + * such as basic authentication or a proxy requiring authentication. |
| 196 | + * |
| 197 | + * When an origin is given, the credentials are only used to answer challenges coming from |
| 198 | + * that origin, compared by scheme, host and port, and only requests within that origin are |
| 199 | + * intercepted. Omit the origin to answer every challenge, e.g. when authenticating to a proxy. |
166 | 200 | * |
167 | | - * @throws AuthenticationFailed |
| 201 | + * Calling this method again replaces the credentials and the origin. If the browser rejects |
| 202 | + * the credentials by repeating a challenge, the request is cancelled and an |
| 203 | + * AuthenticationFailed exception is thrown from the method that is reading the browser |
| 204 | + * messages at that time, usually PageNavigation::waitForNavigation. |
| 205 | + * |
| 206 | + * @throws InvalidArgumentException |
168 | 207 | * @throws CommunicationException |
| 208 | + * @throws NoResponseAvailable |
| 209 | + * @throws OperationTimedOut |
169 | 210 | */ |
170 | | - public function authenticate(string $username, string $password): void |
| 211 | + public function authenticate(string $username, string $password, ?string $origin = null): void |
171 | 212 | { |
172 | 213 | $this->assertNotClosed(); |
173 | 214 |
|
174 | | - $credentialsAttempted = false; |
| 215 | + $scope = null !== $origin ? new AuthenticationScope($origin) : null; |
175 | 216 |
|
176 | | - $this->getSession()->on('method:Fetch.authRequired', function (array $params) use ($username, $password, &$credentialsAttempted) { |
177 | | - if ($credentialsAttempted) { |
178 | | - $this->getSession()->sendMessageSync( |
179 | | - new Message('Fetch.continueWithAuth', [ |
180 | | - 'requestId' => $params['requestId'], |
181 | | - 'authChallengeResponse' => [ |
182 | | - 'response' => 'CancelAuth', |
183 | | - ], |
184 | | - ]) |
185 | | - ); |
| 217 | + $this->authCredentials = ['username' => $username, 'password' => $password]; |
| 218 | + $this->authScope = $scope; |
| 219 | + $this->attemptedAuthentications = []; |
| 220 | + |
| 221 | + $this->registerAuthenticationListeners(); |
| 222 | + |
| 223 | + $this->getSession()->sendMessageSync( |
| 224 | + new Message('Fetch.enable', [ |
| 225 | + 'handleAuthRequests' => true, |
| 226 | + 'patterns' => [ |
| 227 | + ['urlPattern' => null !== $scope ? $scope->getUrlPattern() : '*'], |
| 228 | + ], |
| 229 | + ]) |
| 230 | + ); |
| 231 | + } |
| 232 | + |
| 233 | + /** |
| 234 | + * Removes the credentials set with Page::authenticate and stops intercepting requests. |
| 235 | + * |
| 236 | + * @throws CommunicationException |
| 237 | + * @throws NoResponseAvailable |
| 238 | + * @throws OperationTimedOut |
| 239 | + */ |
| 240 | + public function clearAuthentication(): void |
| 241 | + { |
| 242 | + $this->assertNotClosed(); |
| 243 | + |
| 244 | + if (null === $this->authCredentials) { |
| 245 | + return; |
| 246 | + } |
| 247 | + |
| 248 | + $this->authCredentials = null; |
| 249 | + $this->authScope = null; |
| 250 | + $this->attemptedAuthentications = []; |
| 251 | + |
| 252 | + $this->getSession()->sendMessageSync(new Message('Fetch.disable')); |
| 253 | + } |
| 254 | + |
| 255 | + /** |
| 256 | + * Registers the fetch domain listeners used to answer authentication challenges. |
| 257 | + */ |
| 258 | + private function registerAuthenticationListeners(): void |
| 259 | + { |
| 260 | + if ($this->authListenersRegistered) { |
| 261 | + return; |
| 262 | + } |
| 263 | + |
| 264 | + $this->authListenersRegistered = true; |
| 265 | + |
| 266 | + $this->getSession()->on('method:Fetch.authRequired', function (array $params): void { |
| 267 | + $requestId = $params['requestId']; |
| 268 | + $challenge = $params['authChallenge'] ?? []; |
| 269 | + |
| 270 | + $attemptKey = \implode("\0", [ |
| 271 | + $requestId, |
| 272 | + $challenge['source'] ?? '', |
| 273 | + $challenge['origin'] ?? '', |
| 274 | + $challenge['scheme'] ?? '', |
| 275 | + $challenge['realm'] ?? '', |
| 276 | + ]); |
| 277 | + |
| 278 | + // a challenge that was already provided the credentials means they were rejected |
| 279 | + if (\array_key_exists($attemptKey, $this->attemptedAuthentications)) { |
| 280 | + $this->cancelAuthenticationChallenge($requestId); |
186 | 281 |
|
187 | 282 | throw new AuthenticationFailed('Authentication failed: invalid credentials.'); |
188 | 283 | } |
189 | 284 |
|
190 | | - $credentialsAttempted = true; |
| 285 | + // cancel out of scope challenges without exposing the credentials |
| 286 | + if (null === $this->authCredentials || (null !== $this->authScope && !$this->authScope->matchesOrigin($challenge['origin'] ?? ''))) { |
| 287 | + $this->cancelAuthenticationChallenge($requestId); |
| 288 | + |
| 289 | + return; |
| 290 | + } |
| 291 | + |
| 292 | + $this->attemptedAuthentications[$attemptKey] = true; |
191 | 293 |
|
192 | 294 | $this->getSession()->sendMessageSync( |
193 | 295 | new Message('Fetch.continueWithAuth', [ |
194 | | - 'requestId' => $params['requestId'], |
| 296 | + 'requestId' => $requestId, |
195 | 297 | 'authChallengeResponse' => [ |
196 | 298 | 'response' => 'ProvideCredentials', |
197 | | - 'username' => $username, |
198 | | - 'password' => $password, |
| 299 | + 'username' => $this->authCredentials['username'], |
| 300 | + 'password' => $this->authCredentials['password'], |
199 | 301 | ], |
200 | 302 | ]) |
201 | 303 | ); |
202 | 304 | }); |
203 | 305 |
|
204 | | - $this->getSession()->on('method:Fetch.requestPaused', function (array $params) { |
| 306 | + $this->getSession()->on('method:Fetch.requestPaused', function (array $params): void { |
205 | 307 | $this->getSession()->sendMessageSync( |
206 | 308 | new Message('Fetch.continueRequest', [ |
207 | 309 | 'requestId' => $params['requestId'], |
208 | 310 | ]) |
209 | 311 | ); |
210 | 312 | }); |
| 313 | + } |
211 | 314 |
|
| 315 | + /** |
| 316 | + * Cancels an authentication challenge without exposing the credentials. |
| 317 | + */ |
| 318 | + private function cancelAuthenticationChallenge(string $requestId): void |
| 319 | + { |
212 | 320 | $this->getSession()->sendMessageSync( |
213 | | - new Message('Fetch.enable', [ |
214 | | - 'handleAuthRequests' => true, |
215 | | - 'patterns' => [ |
216 | | - ['urlPattern' => '*'], |
| 321 | + new Message('Fetch.continueWithAuth', [ |
| 322 | + 'requestId' => $requestId, |
| 323 | + 'authChallengeResponse' => [ |
| 324 | + 'response' => 'CancelAuth', |
217 | 325 | ], |
218 | 326 | ]) |
219 | 327 | ); |
|
0 commit comments