Skip to main content

Overview

The Authentication API provides intelligent authentication capabilities that handle various login mechanisms including form-based auth, OAuth, API tokens, and complex multi-step flows. It securely manages credentials and exports session data for use in subsequent testing. Key Features:
  • Automatic credential management via CredentialManager
  • Support for multiple auth schemes (forms, OAuth, JWT, API keys)
  • Browser automation for complex login flows
  • CSRF and anti-bot handling
  • Email verification support
  • Secure credential storage with no raw secrets in prompts
  • Session export (cookies, headers, tokens)

runAuthenticationAgent

Authenticate against a target and persist the session for subsequent operations. Authentication Flow:
  1. Agent navigates to the target/login page
  2. Detects authentication mechanism
  3. Uses browser tools to fill forms and interact with auth flows
  4. Handles CSRF tokens, CAPTCHAs, email verification
  5. Validates successful authentication
  6. Exports cookies and headers
  7. Persists auth data to session directory
import { runAuthenticationAgent } from '@pensar/apex/api/authentication';

// Credentials are managed automatically via session config
const result = await runAuthenticationAgent({
  target: 'https://app.example.com',
  model: 'claude-sonnet-4-20250514',
  session: sessionInfo,  // Session with authCredentials configured
});

if (result.success) {
  console.log('Authentication successful!');
  console.log(`Strategy: ${result.strategy}`);
  console.log(`Cookies: ${result.exportedCookies}`);
}

Parameters

input
AuthenticationAgentInput
required
Configuration for the authentication agent

Response

success
boolean
Whether authentication was successful
summary
string
Human-readable summary of the authentication process
exportedCookies
string
Session cookies in HTTP Cookie header format (e.g., "session=abc123; csrf=xyz456")
exportedHeaders
Record<string, string>
Additional headers required for authenticated requests (e.g., Authorization tokens)Example:
{
  "Authorization": "Bearer eyJhbGc...",
  "X-CSRF-Token": "abc123"
}
strategy
string
Authentication strategy used (e.g., "form-based", "oauth2", "api-key", "session-cookies")
authBarrier
AuthBarrier | undefined
Details about any authentication barrier encountered during the process
authDataPath
string
Absolute path to the persisted auth-data.json file in the session directory

Usage Examples

import { runAuthenticationAgent } from '@pensar/apex/api/authentication';
import { createSession } from '@pensar/apex/session';

// Create session with credentials
const session = await createSession({
  name: 'Auth Test',
  targets: ['https://app.example.com'],
  config: {
    authCredentials: {
      username: 'testuser',
      password: 'testpass',
      loginUrl: 'https://app.example.com/login',
    },
  },
});

// Run authentication
const result = await runAuthenticationAgent({
  target: 'https://app.example.com',
  model: 'claude-sonnet-4-20250514',
  session,
});

if (result.success) {
  console.log('Authenticated successfully!');
  console.log(`Strategy: ${result.strategy}`);
  
  // Use exported credentials in subsequent requests
  const response = await fetch('https://app.example.com/api/user', {
    headers: {
      Cookie: result.exportedCookies,
      ...result.exportedHeaders,
    },
  });
}

Credential Management

Automatic Credential Provisioning

When you create a session with authCredentials, a CredentialManager is automatically created:
const session = await createSession({
  name: 'My Session',
  targets: ['https://example.com'],
  config: {
    authCredentials: {
      username: 'user',
      password: 'pass',
      loginUrl: 'https://example.com/login',
    },
  },
});

// session.credentialManager is now available
The CredentialManager:
  • Stores secrets securely in memory
  • Never exposes raw secrets in AI prompts
  • Provides credential IDs for safe reference
  • Resolves secrets only at tool execution time

Supported Credential Types

  • Username/Password: Traditional form-based authentication
  • API Keys: Bearer tokens, API keys
  • OAuth Tokens: Access tokens, refresh tokens
  • Session Cookies: Pre-authenticated session cookies
  • Custom Fields: Any additional auth fields

Authentication Strategies

The agent automatically detects and handles various authentication mechanisms:

Form-Based Authentication

  • Detects username/password fields
  • Handles CSRF tokens
  • Manages session cookies
  • Follows redirects

OAuth 2.0

  • Handles authorization flow
  • Manages token exchange
  • Exports access tokens

API Key/Bearer Token

  • Validates API key format
  • Tests protected endpoints
  • Exports authorization headers

Session-Based

  • Imports existing cookies
  • Validates session state
  • Exports refreshed session

Blackbox Pentest

Full pentest with automatic auth

Targeted Pentest

Test authenticated endpoints

Attack Surface

Map authenticated attack surface

Build docs developers (and LLMs) love