0.0.4 • Published 7 years ago

logical-cas-client v0.0.4

Weekly downloads
1
License
MIT
Repository
github
Last release
7 years ago

The Logical Central-Authentication-Service (CAS) Client, is a control-first interface in tandem with CAS services for your Express web-application. While authored at RPI the Logical-CAS-Client should be compatible with any CAS server.

Instead of abstracting underlying auth-strategies like other libraries, Logical-CAS-Client allows you to choose how to handle end-authentication whether with sessions, cookies, etc.

Authored by:

  • Aaron J. Shapiro

Getting Started

Install Logical-CAS-Client using yarn:

yarn add logical-cas-client

Or via npm:

npm install logical-cas-client

Let's get started by importing the library. This library is built with TypeScript, so no additional type-definitions are necessary!

var CasClient = require("logical-cas-client");
import CasClient from "logical-cas-client";

Prepare your configuration:

  • host: The hostname of your web-application (for development typically localhost)
  • port: The port of your web-application
  • secure: Whether or not users should be redirected to your service with HTTPS as the protocol (again for development typically no)
  • endpoints: The url-mapping that will be used with your CAS Client
  • ticketVerificationPath: The url users will be redirected to once they've logged into CAS V2/V3 which will verify their ticket server-to-server.
  • server (port, host, secure): The above parameters but in relation to the remote CAS.
var config = {
  host: "localhost",
  port: 8080,
  secure: false,
  endpoints: {
    ticketVerificationPath: "/auth/ticket"
  },
  server: {
    host: "cas-auth.rpi.edu",
    secure: true,
    version: "2.0",
  }
};

Prepare your callback functions for user-login success and failure cases:

/**
 * Authentication Success Callback
 * This function will be invoked whenever a user successfully authenticated with the CAS.
 * 
 * @param req {Express.Request}
 * @param res {Express.Response}
 * @param user {string}
 */
function authSuccess(req: Request, res: Response, user: string): any {
  // Generate JWT... Set Cookie...
}

/**
 * Authentication Error Callback
 * This function will be invoked whenever a user fails to authenticate 
 * with the CAS or the CAS returns an error.
 * 
 * @param req {Express.Request}
 * @param res {Express.Response}
 * @param error {any}
 */
function authError(req: Request, res: Response, error: any) {
  // Redirect to front-end error page...
}

Map your express-server to the ticket-verification and login-redirect endpoints:

const client = CasClient(config, authSuccess, authError);
app.use(config.endpoints.ticketVerificationPath, client.verifyTicket);
app.use("/auth/login", client.redirectToCASLogin);

You just integrated CAS Authentication with your web-app!