1.0.3 • Published 6 years ago
@cervval/tpfmc-sso v1.0.3
This module wraps DSI's specific SSO authentication for a Node.JS Express application.
This is not a general purpose SSO client.
Usage Example:
import * as express from "express";
import * as session from "express-session";
import * as tpfmcSSO from "@cervval/tpfmc-sso";
// Consts
let cookieName = "apppName";
let sessionSecret = 'a secret';//replace with a new generated key for each application
let httpPort = process.env.PORT || 8080;
tpfmcSSO.SETTINGS.ssoUrl = "url to sso server"
tpfmcSSO.SETTINGS.clientId = "client id";
tpfmcSSO.SETTINGS.clientSecret = "client secret";
tpfmcSSO.SETTINGS.publicPaths = ["/public"];
// Init HTTP Server
let expressApp: express.Express = express();
// Sessions
expressApp.use(session({
secret: sessionSecret,
name: cookieName + "_" + httpPort + "_sessionId",
resave: false,
saveUninitialized: true
}));
// Install authentication
expressApp.use(tpfmcSSO.authenticate);
// Create HTTP Server
expressApp.listen(httpPort, () => {
let url = "http://localhost:" + httpPort + tpfmcSSO.SETTINGS.rootUrl;
console.info("HTTP server listening on %s in %s mode", url, expressApp.get('env'));
});
// SSO routes
tpfmcSSO.registerLandingAndLogoutRoutes(expressApp);
// Get User Data route - authenticated
expressApp.post("/getUserInfos", (req, res) => {
let user = tpfmcSSO.getLoggedInUser(req);
res.json(user);
});
// Private - authenticated
expressApp.get("/", (req, res) => {
let user = tpfmcSSO.getLoggedInUser(req);
res.send("Hello " + user.fullName + "!");
});
// Public - not authenticated (because in SETTINGS.publicPaths)
expressApp.get("/public", (req, res) => {
res.send("Hello World!")
});