0.0.3 • Published 12 months ago

playwright-client-certificate-login v0.0.3

Weekly downloads
-
License
MIT
Repository
-
Last release
12 months ago

Playwright Client Certificate Authentication

A Node.js utility for authenticating using client certificates via Playwright.

Installation

npm install playwright-client-certificate-login

Usage

const { CertificateAuthSession } = require('playwright-client-certificate-login');

// Using PFX/PKCS12 certificate
const options = {
  origin: 'https://your-domain.com',
  url: 'https://your-domain.com/path',
  pfxPath: '/path/to/your/certificate.pfx',
  passphrase: 'your-certificate-passphrase'
};

// Or using separate cert and key files
const options = {
  origin: 'https://your-domain.com',
  url: 'https://your-domain.com/path',
  certPath: '/path/to/your/certificate.pem',
  keyPath: '/path/to/your/private-key.pem',
  passphrase: 'your-key-passphrase'
};

async function authenticate() {
  const session = new CertificateAuthSession(options);
  try {
    await session.authenticate();
    
    // Access authenticated session data
    const cookies = session.getCookies();
    const headers = session.getHeaders();
    const browser = session.getBrowser();
    const context = session.getContext();
    const page = session.getPage();
    
    console.log('Authentication successful:', cookies);
    
    // Don't forget to close the session
    await session.close();
  } catch (error) {
    console.error('Authentication failed:', error);
  }
}

API Reference

Class: CertificateAuthSession

Constructor Options

ParameterTypeRequiredDescription
originstringYesThe exact origin that the certificate is valid for
urlstringYesThe URL to navigate to after authentication
certPathstringNo*Path to the certificate file in PEM format
keyPathstringNo*Path to the private key file in PEM format
pfxPathstringNo*Path to the PFX/PKCS12 certificate file
passphrasestringNoPassphrase for the private key
pfxBufferBufferNo*Direct value of the PFX/PKCS12 certificate
certBufferBufferNo*Direct value of the certificate in PEM format
keyBufferBufferNo*Direct value of the private key in PEM format
timeoutnumberNoTimeout in milliseconds for page load (default: 30000)

* You must provide either:

  • pfxPath or pfxBuffer, OR
  • Both certPath and keyPath, OR
  • Both certBuffer and keyBuffer

Methods

MethodReturnsDescription
authenticate()PromiseAuthenticates using the provided client certificate
getCookies()ArrayReturns the cookies obtained after authentication
getHeaders()ObjectReturns the headers prepared for API calls
getBrowser()BrowserReturns the Playwright browser instance
getContext()BrowserContextReturns the Playwright browser context
getPage()PageReturns the Playwright page instance
close()PromiseCloses the browser instance

Error Handling

The class will throw an error if:

  • Required options are missing
  • Certificate files cannot be read
  • Authentication process fails
  • Page load timeout is exceeded
0.0.3

12 months ago

0.0.2

1 year ago

0.0.1

1 year ago