Node.js CAS Client Middleware

0.4.3 · active · verified Tue Apr 21

http-cas-client provides a comprehensive Central Authentication Service (CAS) client middleware for Node.js environments, supporting CAS 1.0, 2.0+, and 3.0+ protocols. As of version 0.4.3, it offers core features like Single Sign-On (SSO), CAS Proxy capabilities (including proxy chain checking), and Single Logout (SLO). The library is designed to be framework-agnostic, providing direct integration with Node's native `http` module and specific wrappers for popular frameworks like Koa2, with support for both session-based and no-session modes. While actively maintained, the package is still in a pre-1.0 state, indicating potential for further evolution before a stable API is declared. Key differentiators include its explicit support for various CAS protocol versions and its flexibility in integration patterns, including cluster-friendliness (though this feature is marked 'TODO' in the README) and principal adaptation for debugging.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates basic CAS client integration with Node.js's native HTTP server, authenticating users and accessing their principal information.

import http from 'http';
import httpCasClient from 'http-cas-client';

const casServerUrlPrefix = process.env.CAS_SERVER_URL_PREFIX || 'http://localhost:9000/cas';
const serverName = process.env.APP_SERVER_NAME || 'http://127.0.0.1';

const handler = httpCasClient({
	casServerUrlPrefix: casServerUrlPrefix,
	serverName: serverName,
    // Example of setting a custom logger
    logger: console
});

http.createServer(async (req, res) => {
	if (!await handler(req, res)) {
		// If the handler returns false, it means a redirect or other action was taken,
		// and the response has already been handled. Stop further processing.
		return res.end();
	}

	// The principal and ticket are attached to the request object after successful authentication.
	const { principal, ticket } = req;

	console.log('Authenticated Principal:', principal);
	console.log('CAS Ticket:', ticket);

	// Your application logic for authenticated users
	res.writeHead(200, { 'Content-Type': 'text/html' });
	res.end(`<h1>Hello, ${principal?.user || 'Guest'}!</h1><p>Attributes: ${JSON.stringify(principal?.attributes)}</p><p><a href="${casServerUrlPrefix}/logout">Logout</a></p>`);
}).listen(80,
    () => console.log(`Application listening on port 80. CAS Server: ${casServerUrlPrefix}`)
);

view raw JSON →