Playwright Client Certificate Authentication

0.0.3 · active · verified Tue Apr 21

playwright-client-certificate-login is a Node.js utility, currently at version 0.0.3, designed to streamline client certificate-based authentication within Playwright automation scripts. It abstracts the complexities of configuring Playwright's browser context to utilize PFX/PKCS12 files or separate PEM certificate and key files, including their associated passphrases. The library provides a `CertificateAuthSession` class that manages the entire authentication process, from launching a browser and navigating to the specified URL, to making the authenticated browser context, page, cookies, and headers accessible. Given its early version, the package is in active development with an implied irregular release cadence, primarily focusing on robust client certificate handling for scenarios such as SAP BTP and other enterprise authentication systems.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates authenticating to a web application using a PFX client certificate, capturing cookies, headers, and taking a screenshot of the authenticated page.

import { CertificateAuthSession } from 'playwright-client-certificate-login';
import path from 'path';

async function authenticateWithCert() {
  const pfxPath = process.env.PFX_CERT_PATH ?? path.resolve(__dirname, './your-certificate.pfx');
  const passphrase = process.env.CERT_PASSPHRASE ?? 'your-certificate-passphrase';

  if (!pfxPath || !passphrase) {
    console.error('Environment variables PFX_CERT_PATH and CERT_PASSPHRASE must be set, or provide hardcoded defaults.');
    return;
  }

  const options = {
    origin: 'https://your-secure-domain.com',
    url: 'https://your-secure-domain.com/dashboard',
    pfxPath: pfxPath,
    passphrase: passphrase
  };

  const session = new CertificateAuthSession(options);
  try {
    console.log('Attempting authentication...');
    await session.authenticate();

    const cookies = await session.getCookies();
    const headers = session.getHeaders();
    console.log('Authentication successful. Cookies:', cookies.map(c => c.name));
    console.log('Prepared Headers:', headers);

    // Example: Interact with the authenticated page
    const page = session.getPage();
    await page.screenshot({ path: 'authenticated-dashboard.png' });
    console.log('Screenshot taken: authenticated-dashboard.png');

  } catch (error) {
    console.error('Authentication failed:', error);
  } finally {
    console.log('Closing browser session.');
    await session.close();
  }
}

authenticateWithCert();

view raw JSON →