Skip to content

Commit

Permalink
Merge pull request #276 from sasjs/sas9-mock
Browse files Browse the repository at this point in the history
SAS9 mocker improved - public access denied scenario
  • Loading branch information
saadjutt01 authored Sep 19, 2022
2 parents a9c9b73 + e65cba9 commit 8b12f31
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 7 deletions.
1 change: 1 addition & 0 deletions api/mocks/generic/sas9/public-access-denied
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Public access has been denied.
4 changes: 4 additions & 0 deletions api/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
58 changes: 53 additions & 5 deletions api/src/controllers/mock-sas9.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export interface MockFileRead {
}

export class MockSas9Controller {
private loggedIn: boolean = false
private loggedIn: string | undefined

@Get('/SASStoredProcess')
public async sasStoredProcess(): Promise<Sas9Response> {
Expand Down Expand Up @@ -46,6 +46,13 @@ export class MockSas9Controller {
}
}

if (this.isPublicAccount()) {
return {
content: '',
redirect: '/SASLogon/Login'
}
}

let program = req.query._program?.toString() || ''
program = program.replace('/', '')

Expand All @@ -68,6 +75,23 @@ export class MockSas9Controller {

@Get('/SASLogon/login')
public async loginGet(): Promise<Sas9Response> {
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',
Expand All @@ -78,8 +102,8 @@ export class MockSas9Controller {
}

@Post('/SASLogon/login')
public async loginPost(): Promise<Sas9Response> {
this.loggedIn = true
public async loginPost(req: express.Request): Promise<Sas9Response> {
this.loggedIn = req.body.username

return await getMockResponseFromFile([
process.cwd(),
Expand All @@ -91,8 +115,18 @@ export class MockSas9Controller {
}

@Get('/SASLogon/logout')
public async logout(): Promise<Sas9Response> {
this.loggedIn = false
public async logout(req: express.Request): Promise<Sas9Response> {
this.loggedIn = undefined

if (req.query.publicDenied === 'true') {
return await getMockResponseFromFile([
process.cwd(),
'mocks',
'generic',
'sas9',
'public-access-denied'
])
}

return await getMockResponseFromFile([
process.cwd(),
Expand All @@ -102,6 +136,20 @@ export class MockSas9Controller {
'logged-out'
])
}

@Get('/SASStoredProcess/Logoff') //publicDenied=true
public async logoff(req: express.Request): Promise<Sas9Response> {
const params = req.query.publicDenied
? `?publicDenied=${req.query.publicDenied}`
: ''

return {
content: '',
redirect: '/SASLogon/logout' + params
}
}

private isPublicAccount = () => this.loggedIn?.toLowerCase() === 'public'
}

/**
Expand Down
34 changes: 32 additions & 2 deletions api/src/routes/web/sas9-web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 8b12f31

Please sign in to comment.