teodesian / playwright-perl

Perl bindings for playwright
MIT License
22 stars 4 forks source link

Need to be able to set client certificates in browsers. #57

Open teodesian opened 1 year ago

teodesian commented 1 year ago

https://github.com/microsoft/playwright/issues/1799#issuecomment-731734494

suggests there is indeed a way. The core problem I see is that you’d want the file read to get done in JS rather than passing bytes on from perl, as those tend to choke JSON encoders/decoders.

I’ll need a mechanism to hint playwright_server that it ought to read a particular file parameter.

kcaran commented 1 month ago

We use client certificates at my company, so I was able to take a look at this. I think you can close the issue.

Playwright version 1.46.0 handles client certificates natively. You can pass them in via the defineConfig configuration object or through the context declaration. I don't think we have access to defineConfig through playwright_server, but passing them through newContext worked fine for me.

my $host = 'https://my.host.com';
my $handle = Playwright->new();
my $browser = $handle->launch( headless => 0, type => 'chrome' );
my $context = $browser->newContext( {
    clientCertificates => [{
        origin => $host,
        certPath => '/Users/caran/cert/myclientcert.crt',
        keyPath => '/Users/caran/cert/myprivatekey.pem',
        passphrase => 'thepassphrase'
    }] } );

my $page = $context->newPage();
$page->goto($host, { waitUntil => 'networkidle' });

On MacOS, I did have some issues with .pfx files but splitting up the certificate and passing both parts works fine.

teodesian commented 1 month ago

that's great! I'll add this into example.pl so others don't have to look hard for it, and make a note in the POD.