examples/redteam-auth/README.md
You can run this example with:
npx promptfoo@latest init --example redteam-auth
cd redteam-auth
This example demonstrates how to configure authentication for red team evaluations against HTTP endpoints. It includes three configuration files showing different authentication methods:
promptfooconfig.oauth.yaml) - Client credentials flow for server-to-server authenticationpromptfooconfig.api_key.yaml) - API key authentication via headerspromptfooconfig.file.yaml) - Custom token loading via JavaScript, TypeScript, or Pythonpromptfooconfig.digital_signature.yaml) - Digital signature authentication using private keysWhen running red team evaluations against protected HTTP endpoints, you need to configure authentication. This example shows how to set up OAuth, API key, file-based, and digital signature authentication in your red team target configuration.
The promptfooconfig.oauth.yaml file demonstrates OAuth 2.0 client credentials flow:
targets:
- id: http
config:
url: https://example-app.promptfoo.app/minnow/chat?auth_type=bearer
method: POST
auth:
type: oauth
grantType: client_credentials
clientId: '{{env.PROMPTFOO_TARGET_CLIENT_ID}}'
clientSecret: '{{env.PROMPTFOO_TARGET_CLIENT_SECRET}}'
tokenUrl: https://example-app.promptfoo.app/oauth/token
scopes: []
The promptfooconfig.api_key.yaml file demonstrates API key authentication:
targets:
- id: http
config:
url: https://example-app.promptfoo.app/minnow/chat?auth_type=api_key
method: POST
auth:
type: api_key
value: '{{env.PROMPTFOO_TARGET_API_KEY}}'
placement: header
keyName: X-API-Key
The promptfooconfig.file.yaml file demonstrates file-based authentication:
targets:
- id: http
config:
url: https://example-app.promptfoo.app/minnow/chat?auth_type=bearer
method: POST
headers:
Content-Type: application/json
Authorization: Bearer {{token}}
body:
messages: '{{prompt}}'
auth:
type: file
path: ./auth/get-token.js
The bundled auth scripts simulate the client credentials grant used by the OAuth example. They call https://example-app.promptfoo.app/oauth/token, return the access_token as token, and convert expires_in into the optional expiration field.
The auth file returns an object shaped like:
{
token: string;
expiration?: number | null;
}
You can also use:
./auth/get-token.ts for TypeScript default exports./auth/get-token.py for Python get_authfile://./auth/get-token.ts:buildAuth for named exportsThe promptfooconfig.digital_signature.yaml file demonstrates digital signature authentication:
targets:
- id: http
config:
url: https://example-app.promptfoo.app/minnow/chat?auth_type=digital_signature
method: POST
headers:
'timestamp': '{{signatureTimestamp}}'
'signature': '{{signature}}'
signatureAuth:
enabled: true
certificateType: pem
keyInputType: base64
type: pem
privateKey: '{{env.PROMPTFOO_AUTH_PRIVATE_KEY}}'
signatureValidityMs: 80000
signatureDataTemplate: 'promptfoo-app{{signatureTimestamp}}'
This example requires environment variables depending on which authentication method you use:
PROMPTFOO_TARGET_CLIENT_ID - Your OAuth client IDPROMPTFOO_TARGET_CLIENT_SECRET - Your OAuth client secretPROMPTFOO_TARGET_API_KEY - Your API keyPROMPTFOO_TARGET_CLIENT_ID - OAuth client ID used by the bundled auth scriptsPROMPTFOO_TARGET_CLIENT_SECRET - OAuth client secret used by the bundled auth scriptsPROMPTFOO_TARGET_SCOPES - Optional space-delimited OAuth scopesPROMPTFOO_AUTH_PRIVATE_KEY - Your base64-encoded private key (PEM format)NOTE: The values for these environment variables are available upon request.
# For OAuth
export PROMPTFOO_TARGET_CLIENT_ID=your-client-id
export PROMPTFOO_TARGET_CLIENT_SECRET=your-client-secret
# For API Key
export PROMPTFOO_TARGET_API_KEY=your-api-key
# For File Auth
export PROMPTFOO_TARGET_CLIENT_ID=your-client-id
export PROMPTFOO_TARGET_CLIENT_SECRET=your-client-secret
export PROMPTFOO_TARGET_SCOPES=
# For Digital Signature
export PROMPTFOO_AUTH_PRIVATE_KEY=your-base64-encoded-private-key
# Using OAuth configuration
promptfoo redteam run -c promptfooconfig.oauth.yaml
# Using API Key configuration
promptfoo redteam run -c promptfooconfig.api_key.yaml
# Using file-based authentication
promptfoo redteam run -c promptfooconfig.file.yaml
# Using Digital Signature configuration
promptfoo redteam run -c promptfooconfig.digital_signature.yaml
promptfoo view
When using OAuth authentication:
tokenUrl using client credentialsAuthorization: Bearer <token> headerWhen using API key authentication:
X-API-Key: <key>)header or query (query parameters)When using digital signature authentication:
signatureDataTemplate (e.g., promptfoo-app{{signatureTimestamp}})signatureValidityMs (80 seconds in the example)When using file-based authentication:
token and optional expirationexpiration is omitted, the token is reused indefinitelyexpiration is provided, the auth function is called again when the token is close to expiring{{token}} is available for templating into headers, query params, bodies, and raw requestsTo use this example with your own endpoint:
url in the target configurationtokenUrl for OAuth (if applicable)body structure to match your API's expected formattransformResponse function to extract the response from your API's formatsignatureDataTemplate to match your API's expected signature formatauth/ to fetch or mint tokens for your APIFor more information, see the HTTP Provider documentation and Red Team documentation.