symfony / panther

A browser testing and web crawling library for PHP and Symfony
MIT License
2.94k stars 221 forks source link

HTTP Auth #237

Open jean-gui opened 5 years ago

jean-gui commented 5 years ago

Hi,

With the WebTestCase client it's possible to use HTTP Basic Authentication using the following code:

$this->adminClient = static::createClient(
   [],
   [
       'PHP_AUTH_USER' => 'login',
       'PHP_AUTH_PW'   => 'password',
   ]
);

It seems that PantherClient doesn't allow to do that, or the documentation for it is hard to find/missing.

I've tried asking on StackOverflow but with not much success: https://stackoverflow.com/questions/57908369/http-auth-with-panther.

Thanks

stof commented 5 years ago

well, passing $_SERVER variables won't work with Panther, as it does not directly call PHP (it calls Selenium). but Selenium supports passing the username and password of basic auth in the URL that you load, so you could use that way for the first URL you load on the domain (after that, the browser will remember the credentials until the end of the browsing session)

jean-gui commented 5 years ago

That seems complicated because there doesn't seem to be an easy way to get the base URL. All I can think about is:

$this->adminClient->get('/');
$baseUrl = $this->adminClient->getCurrentURL();

and then inject the username and password in $baseUrl to make another request to get the browser to store credentials. Is this what you suggest?

As an alternative, would it be possible to set arbitrary request headers such as Authorization?

VincentSureau commented 2 years ago

It is possible to add credentials directly in the request url to bypass the prompt, exemple:

$client = static::createPantherClient();
$client->request('GET', 'https://login:password@mydomain.fr');

source: https://www.browserstack.com/docs/automate/selenium/basic-http-authentication#pass-username-and-password-in-the-url