diff --git a/api/mocks/generic/sas9/public-access-denied b/api/mocks/generic/sas9/public-access-denied new file mode 100644 index 00000000..6efd19eb --- /dev/null +++ b/api/mocks/generic/sas9/public-access-denied @@ -0,0 +1 @@ +Public access has been denied. \ No newline at end of file diff --git a/api/src/app.ts b/api/src/app.ts index a2bfdf9f..843a2541 100644 --- a/api/src/app.ts +++ b/api/src/app.ts @@ -77,6 +77,10 @@ export default setProcessVariables().then(async () => { app.use(express.json({ limit: '100mb' })) app.use(express.static(path.join(__dirname, '../public'))) + // Body parser is used for decoding the formdata on POST request. + // Currently only place we use it is SAS9 Mock - POST /SASLogon/login + app.use(express.urlencoded({ extended: true })) + await setupFolders() await copySASjsCore() diff --git a/api/src/controllers/mock-sas9.ts b/api/src/controllers/mock-sas9.ts index 7136b178..8a787787 100644 --- a/api/src/controllers/mock-sas9.ts +++ b/api/src/controllers/mock-sas9.ts @@ -15,7 +15,7 @@ export interface MockFileRead { } export class MockSas9Controller { - private loggedIn: boolean = false + private loggedIn: string | undefined @Get('/SASStoredProcess') public async sasStoredProcess(): Promise { @@ -46,6 +46,13 @@ export class MockSas9Controller { } } + if (this.isPublicAccount()) { + return { + content: '', + redirect: '/SASLogon/Login' + } + } + let program = req.query._program?.toString() || '' program = program.replace('/', '') @@ -68,6 +75,23 @@ export class MockSas9Controller { @Get('/SASLogon/login') public async loginGet(): Promise { + if (this.loggedIn) { + if (this.isPublicAccount()) { + return { + content: '', + redirect: '/SASStoredProcess/Logoff?publicDenied=true' + } + } else { + return await getMockResponseFromFile([ + process.cwd(), + 'mocks', + 'generic', + 'sas9', + 'logged-in' + ]) + } + } + return await getMockResponseFromFile([ process.cwd(), 'mocks', @@ -78,8 +102,8 @@ export class MockSas9Controller { } @Post('/SASLogon/login') - public async loginPost(): Promise { - this.loggedIn = true + public async loginPost(req: express.Request): Promise { + this.loggedIn = req.body.username return await getMockResponseFromFile([ process.cwd(), @@ -91,8 +115,18 @@ export class MockSas9Controller { } @Get('/SASLogon/logout') - public async logout(): Promise { - this.loggedIn = false + public async logout(req: express.Request): Promise { + this.loggedIn = undefined + + if (req.query.publicDenied === 'true') { + return await getMockResponseFromFile([ + process.cwd(), + 'mocks', + 'generic', + 'sas9', + 'public-access-denied' + ]) + } return await getMockResponseFromFile([ process.cwd(), @@ -102,6 +136,20 @@ export class MockSas9Controller { 'logged-out' ]) } + + @Get('/SASStoredProcess/Logoff') //publicDenied=true + public async logoff(req: express.Request): Promise { + const params = req.query.publicDenied + ? `?publicDenied=${req.query.publicDenied}` + : '' + + return { + content: '', + redirect: '/SASLogon/logout' + params + } + } + + private isPublicAccount = () => this.loggedIn?.toLowerCase() === 'public' } /** diff --git a/api/src/routes/web/sas9-web.ts b/api/src/routes/web/sas9-web.ts index 09f53550..0efc6f30 100644 --- a/api/src/routes/web/sas9-web.ts +++ b/api/src/routes/web/sas9-web.ts @@ -58,6 +58,11 @@ sas9WebRouter.post('/SASStoredProcess/do/', async (req, res) => { sas9WebRouter.get('/SASLogon/login', async (req, res) => { const response = await controller.loginGet() + if (response.redirect) { + res.redirect(response.redirect) + return + } + try { res.send(response.content) } catch (err: any) { @@ -66,7 +71,12 @@ sas9WebRouter.get('/SASLogon/login', async (req, res) => { }) sas9WebRouter.post('/SASLogon/login', async (req, res) => { - const response = await controller.loginPost() + const response = await controller.loginPost(req) + + if (response.redirect) { + res.redirect(response.redirect) + return + } try { res.send(response.content) @@ -76,7 +86,27 @@ sas9WebRouter.post('/SASLogon/login', async (req, res) => { }) sas9WebRouter.get('/SASLogon/logout', async (req, res) => { - const response = await controller.logout() + const response = await controller.logout(req) + + if (response.redirect) { + res.redirect(response.redirect) + return + } + + try { + res.send(response.content) + } catch (err: any) { + res.status(403).send(err.toString()) + } +}) + +sas9WebRouter.get('/SASStoredProcess/Logoff', async (req, res) => { + const response = await controller.logoff(req) + + if (response.redirect) { + res.redirect(response.redirect) + return + } try { res.send(response.content)