diff --git a/package.json b/package.json index 391a2b4..cdd2904 100644 --- a/package.json +++ b/package.json @@ -45,6 +45,6 @@ }, "configuration": { "sasPath": "/opt/sas/sas9/SASHome/SASFoundation/9.4/sasexe/sas", - "sasJsPort": 4000 + "sasJsPort": 5000 } } diff --git a/public/401.html b/public/401.html index 91e1394..8e8ed00 100644 --- a/public/401.html +++ b/public/401.html @@ -1,3 +1,3 @@ Unauthorized -Sign-in +Sign-in diff --git a/src/app.ts b/src/app.ts index ae95653..1145f23 100644 --- a/src/app.ts +++ b/src/app.ts @@ -1,54 +1,26 @@ import path from 'path' import express from 'express' -import session from 'express-session' - -// import msalWrapper from 'msal-express-wrapper' -const msalWrapper = require('msal-express-wrapper') import indexRouter from './routes' - -require('dotenv').config() -import { appSettings } from './appSettings' +import { AuthMechanism } from './types' +import { getAzureSubApp } from './authMechanisms' const app = express() - app.use(express.json({ limit: '50mb' })) - app.use(express.static(path.join(__dirname, '..', 'public'))) -/** - * Using express-session middleware. Be sure to familiarize yourself with available options - * and set them as desired. Visit: https://www.npmjs.com/package/express-session - */ -const sessionConfig = { - secret: appSettings.appCredentials.clientSecret, - resave: false, - saveUninitialized: false, - cookie: { - secure: false // set this to true on production - } +require('dotenv').config() + +const authMechanisms = process.env.AUTH?.split(' ') ?? [ + AuthMechanism.NoSecurity +] + +if (authMechanisms.includes(AuthMechanism.Azure)) { + app.use(getAzureSubApp()) +} else { + app.get('/', indexRouter) } -if (app.get('env') === 'production') { - app.set('trust proxy', 1) // trust first proxy - sessionConfig.cookie.secure = true // serve secure cookies -} - -app.use(session(sessionConfig)) - -// instantiate the wrapper -const authProvider = new msalWrapper.AuthProvider(appSettings) - -// initialize the wrapper -app.use(authProvider.initialize()) - -// authentication routes -app.get('/signin', authProvider.signIn({ successRedirect: '/' })) -app.get('/signout', authProvider.signOut({ successRedirect: '/' })) - -// secure routes -app.get('/', authProvider.isAuthenticated(), indexRouter) - app.get('/error', (req, res) => res.redirect('/500.html')) app.get('/unauthorized', (req, res) => res.redirect('/401.html')) app.get('*', (req, res) => res.status(404).redirect('/404.html')) diff --git a/src/appSettings.ts b/src/appSettings.ts deleted file mode 100644 index 6d206bc..0000000 --- a/src/appSettings.ts +++ /dev/null @@ -1,12 +0,0 @@ -export const appSettings = { - appCredentials: { - clientId: process.env.CLIENTID as string, - tenantId: process.env.TENANTID as string, - clientSecret: process.env.CLIENTSECRET as string - }, - authRoutes: { - redirect: '/redirect', - error: '/error', // the wrapper will redirect to this route in case of any error. - unauthorized: '/unauthorized' // the wrapper will redirect to this route in case of unauthorized access attempt. - } -} diff --git a/src/authMechanisms/azure.ts b/src/authMechanisms/azure.ts new file mode 100644 index 0000000..b606818 --- /dev/null +++ b/src/authMechanisms/azure.ts @@ -0,0 +1,57 @@ +import express from 'express' +import session from 'express-session' +import indexRouter from '../routes' + +export const getAzureSubApp = () => { + console.log('Using Azure Authentication') + const app = express() + + const msalWrapper = require('msal-express-wrapper') + const appSettings = { + appCredentials: { + clientId: process.env.CLIENTID ?? ' ', + tenantId: process.env.TENANTID ?? ' ', + clientSecret: process.env.CLIENTSECRET ?? ' ' + }, + authRoutes: { + redirect: '/redirect', + error: '/error', // the wrapper will redirect to this route in case of any error. + unauthorized: '/unauthorized' // the wrapper will redirect to this route in case of unauthorized access attempt. + } + } + + /** + * Using express-session middleware. Be sure to familiarize yourself with available options + * and set them as desired. Visit: https://www.npmjs.com/package/express-session + */ + const sessionConfig = { + secret: appSettings.appCredentials.clientSecret, + resave: false, + saveUninitialized: false, + cookie: { + secure: false // set this to true on production + } + } + + if (app.get('env') === 'production') { + app.set('trust proxy', 1) // trust first proxy + sessionConfig.cookie.secure = true // serve secure cookies + } + + app.use(session(sessionConfig)) + + // instantiate the wrapper + const authProvider = new msalWrapper.AuthProvider(appSettings) + + // initialize the wrapper + app.use(authProvider.initialize()) + + // authentication routes + app.get('/signin-with-azure', authProvider.signIn({ successRedirect: '/' })) + app.get('/signout-with-azure', authProvider.signOut({ successRedirect: '/' })) + + // secure routes + app.get('/', authProvider.isAuthenticated(), indexRouter) + + return app +} diff --git a/src/authMechanisms/index.ts b/src/authMechanisms/index.ts new file mode 100644 index 0000000..dc119e3 --- /dev/null +++ b/src/authMechanisms/index.ts @@ -0,0 +1 @@ +export * from './azure' diff --git a/src/types/authMechanism.ts b/src/types/authMechanism.ts new file mode 100644 index 0000000..e444b9c --- /dev/null +++ b/src/types/authMechanism.ts @@ -0,0 +1,4 @@ +export enum AuthMechanism { + Azure = 'azure', + NoSecurity = 'nosecurity' +} diff --git a/src/types/index.ts b/src/types/index.ts index 7897c28..16478ea 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -1,3 +1,4 @@ export * from './sas' export * from './request' export * from './fileTree' +export * from './authMechanism'