mirror of
https://github.com/sasjs/server.git
synced 2026-01-08 23:10:05 +00:00
chore: code refactored
This commit is contained in:
@@ -45,6 +45,6 @@
|
|||||||
},
|
},
|
||||||
"configuration": {
|
"configuration": {
|
||||||
"sasPath": "/opt/sas/sas9/SASHome/SASFoundation/9.4/sasexe/sas",
|
"sasPath": "/opt/sas/sas9/SASHome/SASFoundation/9.4/sasexe/sas",
|
||||||
"sasJsPort": 4000
|
"sasJsPort": 5000
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,3 @@
|
|||||||
Unauthorized
|
Unauthorized
|
||||||
|
|
||||||
<a href="/signin" role="button">Sign-in</a>
|
<a href="/signin-with-azure" role="button">Sign-in</a>
|
||||||
|
|||||||
52
src/app.ts
52
src/app.ts
@@ -1,54 +1,26 @@
|
|||||||
import path from 'path'
|
import path from 'path'
|
||||||
import express from 'express'
|
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'
|
import indexRouter from './routes'
|
||||||
|
import { AuthMechanism } from './types'
|
||||||
require('dotenv').config()
|
import { getAzureSubApp } from './authMechanisms'
|
||||||
import { appSettings } from './appSettings'
|
|
||||||
|
|
||||||
const app = express()
|
const app = express()
|
||||||
|
|
||||||
app.use(express.json({ limit: '50mb' }))
|
app.use(express.json({ limit: '50mb' }))
|
||||||
|
|
||||||
app.use(express.static(path.join(__dirname, '..', 'public')))
|
app.use(express.static(path.join(__dirname, '..', 'public')))
|
||||||
|
|
||||||
/**
|
require('dotenv').config()
|
||||||
* 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 authMechanisms = process.env.AUTH?.split(' ') ?? [
|
||||||
*/
|
AuthMechanism.NoSecurity
|
||||||
const sessionConfig = {
|
]
|
||||||
secret: appSettings.appCredentials.clientSecret,
|
|
||||||
resave: false,
|
if (authMechanisms.includes(AuthMechanism.Azure)) {
|
||||||
saveUninitialized: false,
|
app.use(getAzureSubApp())
|
||||||
cookie: {
|
} else {
|
||||||
secure: false // set this to true on production
|
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('/error', (req, res) => res.redirect('/500.html'))
|
||||||
app.get('/unauthorized', (req, res) => res.redirect('/401.html'))
|
app.get('/unauthorized', (req, res) => res.redirect('/401.html'))
|
||||||
app.get('*', (req, res) => res.status(404).redirect('/404.html'))
|
app.get('*', (req, res) => res.status(404).redirect('/404.html'))
|
||||||
|
|||||||
@@ -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.
|
|
||||||
}
|
|
||||||
}
|
|
||||||
57
src/authMechanisms/azure.ts
Normal file
57
src/authMechanisms/azure.ts
Normal file
@@ -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
|
||||||
|
}
|
||||||
1
src/authMechanisms/index.ts
Normal file
1
src/authMechanisms/index.ts
Normal file
@@ -0,0 +1 @@
|
|||||||
|
export * from './azure'
|
||||||
4
src/types/authMechanism.ts
Normal file
4
src/types/authMechanism.ts
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
export enum AuthMechanism {
|
||||||
|
Azure = 'azure',
|
||||||
|
NoSecurity = 'nosecurity'
|
||||||
|
}
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
export * from './sas'
|
export * from './sas'
|
||||||
export * from './request'
|
export * from './request'
|
||||||
export * from './fileTree'
|
export * from './fileTree'
|
||||||
|
export * from './authMechanism'
|
||||||
|
|||||||
Reference in New Issue
Block a user