SAML API Reference
This document provides a complete API reference for the @authhero/saml package.
Types
Parsed AuthnRequest
parseSamlRequestQuery returns the AuthnRequest as parsed XML, validated by samlRequestSchema. The package does not export a named type for it; use z.infer<typeof samlRequestSchema>:
type ParsedAuthnRequest = {
"samlp:AuthnRequest": {
"saml:Issuer": { "#text": string };
"@_ID": string;
"@_IssueInstant": string;
"@_Destination": string;
"@_AssertionConsumerServiceURL"?: string;
"@_ProtocolBinding"?: string;
"@_ForceAuthn"?: boolean;
"@_Version": string;
};
};Properties of samlp:AuthnRequest:
saml:Issuer- The entity ID of the service provider requesting authentication@_ID- Unique identifier for the SAML request@_IssueInstant- Timestamp when the request was created@_Destination- The intended destination of the request@_AssertionConsumerServiceURL- Where the response should be sent@_ForceAuthn- Whether to force re-authentication@_ProtocolBinding- The SAML protocol binding to use
SAMLResponseJSON
SAML response data structure.
interface SAMLResponseJSON {
audience: string;
sessionIndex: string;
nameIdentifier: string;
nameIdentifierFormat: string;
acsUrl: string;
recipient: string;
issuer: string;
inResponseTo?: string;
attributes?: Record<string, string>;
signResponse?: boolean;
}Properties:
audience- The intended audience (typically the SP entity ID)sessionIndex- Unique session identifiernameIdentifier- The subject identifier (usually user ID)nameIdentifierFormat- Format of the name identifieracsUrl- Assertion Consumer Service URLrecipient- The recipient of the assertionissuer- The identity provider issuerinResponseTo- ID of the request being responded toattributes- Custom SAML attributes to includesignResponse- Whether to sign the response
SamlSigner
Interface for SAML signing implementations.
interface SamlSigner {
signSAML(
xmlContent: string,
privateKey: string,
publicCert: string,
): Promise<string>;
}Methods:
signSAML(xmlContent, privateKey, publicCert)- Sign SAML XML and return signed XMLxmlContent- The XML content to signprivateKey- The private key in PEM formatpublicCert- The public certificate
Classes
HttpSamlSigner
HTTP-based SAML signer that delegates signing to an external service.
class HttpSamlSigner implements SamlSigner {
constructor(url: string);
signSAML(
xmlContent: string,
privateKey: string,
publicCert: string,
): Promise<string>;
}Constructor:
url- HTTP endpoint URL for signing
Example:
import { HttpSamlSigner } from "authhero";
const signer = new HttpSamlSigner("https://signing-service.com/sign");
const signedXml = await signer.signSAML(xmlContent, privateKey, publicCert);LocalSamlSigner
Local SAML signer using xml-crypto (Node.js only).
class LocalSamlSigner implements SamlSigner {
constructor();
signSAML(
xmlContent: string,
privateKey: string,
publicCert: string,
): Promise<string>;
}Example:
import { LocalSamlSigner } from "@authhero/saml/local-signer";
const signer = new LocalSamlSigner();
const signedXml = await signer.signSAML(xmlContent, privateKey, publicCert);WARNING
LocalSamlSigner requires Node.js and cannot be used in edge/serverless environments.
Functions
parseSamlRequestQuery
Parse the SAMLRequest query parameter of an HTTP-Redirect request.
function parseSamlRequestQuery(
samlRequestQuery: string,
): Promise<z.infer<typeof samlRequestSchema>>;Parameters:
samlRequestQuery- The value of theSAMLRequestquery parameter: base64, deflate-compressed or not
Returns: The parsed AuthnRequest
Throws: Error if parsing fails
Example:
import { parseSamlRequestQuery } from "@authhero/saml";
const url = new URL(request.url);
const parsed = await parseSamlRequestQuery(url.searchParams.get("SAMLRequest")!);
const authnRequest = parsed["samlp:AuthnRequest"];
console.log(authnRequest["saml:Issuer"]["#text"]); // SP entity ID
console.log(authnRequest["@_AssertionConsumerServiceURL"]); // Where to send responsecreateSamlResponse
Create a SAML response XML string.
function createSamlResponse(
params: SAMLResponseJSON,
signer?: SamlSigner,
): Promise<string>;Parameters:
params- SAML response parameterssigner- Optional signer to sign the response
Returns: SAML response XML string (signed if signer provided)
Example:
import { createSamlResponse, HttpSamlSigner } from "@authhero/saml";
const signer = new HttpSamlSigner("https://signing-service.com/sign");
const response = await createSamlResponse(
{
audience: "https://sp.example.com",
sessionIndex: "session-123",
nameIdentifier: "user@example.com",
nameIdentifierFormat:
"urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress",
acsUrl: "https://sp.example.com/acs",
recipient: "https://sp.example.com/acs",
issuer: "https://idp.example.com",
inResponseTo: "request-id-123",
attributes: {
email: "user@example.com",
firstName: "John",
lastName: "Doe",
},
signResponse: true,
},
signer,
);
console.log(response); // Signed SAML XMLcreateSamlMetadata
Generate SAML identity provider (IdP) metadata — an IDPSSODescriptor for the service providers that log in through AuthHero.
function createSamlMetadata(params: {
issuer: string;
callbackUrl: string;
certificate: string;
}): string;Parameters:
issuer- The entity ID of the identity providercallbackUrl- The callback URL (ACS URL)certificate- X.509 certificate for signing
Returns: SAML metadata XML string
Example:
import { createSamlMetadata } from "@authhero/saml";
const metadata = createSamlMetadata({
issuer: "https://idp.example.com",
callbackUrl: "https://idp.example.com/callback",
certificate: "-----BEGIN CERTIFICATE-----\n...\n-----END CERTIFICATE-----",
});
console.log(metadata); // SAML metadata XMLinflateDecompress
Decompress and inflate a SAML request string.
function inflateDecompress(input: string): Promise<string>;Parameters:
input- Base64-encoded, deflated SAML request
Returns: Decompressed XML string
Example:
import { inflateDecompress } from "@authhero/saml";
const compressed = "nZFBb4MwDIX..."; // Base64 deflated
const xml = await inflateDecompress(compressed);
console.log(xml); // <samlp:AuthnRequest ...>Zod Schemas
The package includes Zod schemas for runtime validation:
samlRequestSchema
import { samlRequestSchema } from "@authhero/saml";
const parsed = samlRequestSchema.parse(data);samlResponseJsonSchema
import { samlResponseJsonSchema } from "@authhero/saml";
const validated = samlResponseJsonSchema.parse(responseData);Entry Points
@authhero/saml
Main entry point with all functionality:
import {
// Types
type SAMLResponseJSON,
type SamlSigner,
// Classes
HttpSamlSigner,
LocalSamlSigner,
// Functions
parseSamlRequestQuery,
createSamlResponse,
createSamlMetadata,
inflateDecompress,
// Schemas
samlRequestSchema,
samlResponseJsonSchema,
} from "@authhero/saml";@authhero/saml/core
Core functionality without LocalSamlSigner (no xml-crypto):
import {
// Types
type SAMLResponseJSON,
type SamlSigner,
// Classes
HttpSamlSigner,
// Note: LocalSamlSigner NOT included
// Functions
parseSamlRequestQuery,
createSamlResponse,
createSamlMetadata,
inflateDecompress,
// Schemas
samlRequestSchema,
samlResponseJsonSchema,
} from "@authhero/saml/core";@authhero/saml/local-signer
Only LocalSamlSigner class:
import { LocalSamlSigner } from "@authhero/saml/local-signer";Error Handling
All functions may throw errors that should be handled:
import { parseSamlRequestQuery } from "@authhero/saml";
try {
const request = parseSamlRequestQuery(query);
// Process request
} catch (error) {
console.error("Failed to parse SAML request:", error);
// Handle error appropriately
}Common error scenarios:
- Invalid base64 encoding in SAMLRequest
- Malformed XML in SAML request
- Missing required fields
- Signing service unavailable (HttpSamlSigner)
- Invalid parameters