Remix Auth SAML Strategy

1.2.0 · active · verified Wed Apr 22

remix-auth-saml provides a SAML 2.0 authentication strategy for Remix Auth, enabling single sign-on (SSO) integration in Remix applications. This library abstracts the complexities of SAML authentication flows, including service provider (SP) and identity provider (IdP) interactions, metadata exchange, and assertion processing. The current stable version is 1.2.0, with releases occurring periodically, primarily for dependency updates and minor fixes as seen in recent changelogs. Key differentiators include its direct integration with the Remix Auth ecosystem, providing a familiar API for developers already using `remix-auth`, and its explicit support for both Node.js and Cloudflare runtimes, making it versatile for various deployment targets.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates the core setup for `remix-auth-saml`, including initializing `Authenticator`, configuring `SamlStrategy` with essential SAML parameters, and providing a `verify` callback to process user data post-authentication. It also shows how to expose the Service Provider (SP) metadata for your Identity Provider (IdP).

import { Authenticator } from "remix-auth";
import { sessionStorage } from "~/services/session.server"; // Assuming a session storage setup
import { SamlStrategy } from "remix-auth-saml";
import * as validator from "@authenio/samlify-node-xmllint"; // Or another SAML XML validator

// Create an Authenticator instance
export let authenticator = new Authenticator<any>(sessionStorage);

// Initialize the SAML strategy
let samlStrategy = new SamlStrategy(
  {
    validator,
    authURL: "http://localhost:3000/auth/saml",
    callbackURL: "http://localhost:3000/auth/saml/callback",
    idpMetadataURL: "http://localhost:7000/metadata", // URL to your Identity Provider's metadata
    spAuthnRequestSigned: false,
    spWantAssertionSigned: false,
    spWantMessageSigned: false,
    spWantLogoutRequestSigned: false,
    spWantLogoutResponseSigned: false,
    spIsAssertionEncrypted: false,
    // Optional: Specify private keys and certificates for signing/encryption
    // privateKey: "./path/to/sp-private-key.pem",
    // signingCert: "./path/to/sp-public-cert.pem"
  },
  async ({ extract, data }) => {
    // This verify callback runs after successful SAML authentication
    // 'extract' contains parsed user profile data from the SAML assertion
    // 'data' is the raw IdP response, useful for backend verification or decryption
    console.log("User profile extracted:", extract);
    console.log("Raw IdP response data:", data);

    // Here, you would typically find or create a user in your database
    // based on 'extract' data and return the user object.
    // Example: const user = await userService.findOrCreate(extract);
    // return user;

    // For this example, we'll just return a placeholder
    return { id: extract.nameID, email: extract.attributes['urn:oid:0.9.2342.19200300.100.1.3'] };
  }
);

// Register the strategy with the Authenticator
authenticator.use(samlStrategy, "saml");

// Export SP metadata for the IdP
export let spMetadata = samlStrategy.metadata();

view raw JSON →