Skip to content

Commit d37a1e1

Browse files
authored
PI-2883 Restrict contact search to trial users (#293)
1 parent 15e73e5 commit d37a1e1

File tree

2 files changed

+42
-5
lines changed

2 files changed

+42
-5
lines changed

server/middleware/authorisationMiddleware.test.ts

+31-4
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ import type { Request, Response } from 'express'
33

44
import authorisationMiddleware from './authorisationMiddleware'
55

6-
function createToken(authorities: string[]) {
6+
function createToken(authorities: string[], sub: string = 'USER1') {
77
const payload = {
8+
sub,
89
user_name: 'USER1',
910
scope: ['read', 'write'],
1011
auth_source: 'nomis',
@@ -17,14 +18,16 @@ function createToken(authorities: string[]) {
1718
}
1819

1920
describe('authorisationMiddleware', () => {
20-
let req: Request
21+
let req: Request = {
22+
path: '/index',
23+
} as unknown as jest.Mocked<Request>
2124
const next = jest.fn()
2225

23-
function createResWithToken({ authorities }: { authorities: string[] }): Response {
26+
function createResWithToken({ authorities, sub = 'USER1' }: { authorities: string[]; sub?: string }): Response {
2427
return {
2528
locals: {
2629
user: {
27-
token: createToken(authorities),
30+
token: createToken(authorities, sub),
2831
},
2932
},
3033
redirect: jest.fn(),
@@ -61,4 +64,28 @@ describe('authorisationMiddleware', () => {
6164
expect(next).toHaveBeenCalled()
6265
expect(res.redirect).not.toHaveBeenCalled()
6366
})
67+
68+
it('should redirect when trying to access contact search', async () => {
69+
const res = createResWithToken({ authorities: ['SOME_REQUIRED_ROLE'], sub: 'OTHER_USER' })
70+
req = {
71+
path: '/contacts/something',
72+
} as unknown as jest.Mocked<Request>
73+
74+
await authorisationMiddleware(['SOME_REQUIRED_ROLE'])(req, res, next)
75+
76+
expect(next).not.toHaveBeenCalled()
77+
expect(res.redirect).toHaveBeenCalled()
78+
})
79+
80+
it('should return next when trying to access contact search as authorised username', async () => {
81+
const res = createResWithToken({ authorities: ['SOME_REQUIRED_ROLE'], sub: 'MARCUSASPIN' })
82+
req = {
83+
path: '/contacts/something',
84+
} as unknown as jest.Mocked<Request>
85+
86+
await authorisationMiddleware(['SOME_REQUIRED_ROLE'])(req, res, next)
87+
88+
expect(next).toHaveBeenCalled()
89+
expect(res.redirect).not.toHaveBeenCalled()
90+
})
6491
})

server/middleware/authorisationMiddleware.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,26 @@ import type { RequestHandler } from 'express'
44
import logger from '../../logger'
55
import asyncMiddleware from './asyncMiddleware'
66

7+
const authorisedContactSearchUsers = ['ZOEWALKERNPS', 'JOEPRINOLD1HMPPS', 'MARCUSASPIN', 'AOJ19Y', 'ANDREWLOGANMOJ']
8+
79
export default function authorisationMiddleware(authorisedRoles: string[] = []): RequestHandler {
810
return asyncMiddleware((req, res, next) => {
911
if (res.locals?.user?.token) {
10-
const { authorities: roles = [] } = jwtDecode(res.locals.user.token) as { authorities?: string[] }
12+
const { authorities: roles = [], sub } = jwtDecode(res.locals.user.token) as {
13+
authorities?: string[]
14+
sub?: string
15+
}
1116

1217
if (authorisedRoles.length && !roles.some(role => authorisedRoles.includes(role))) {
1318
logger.error('User is not authorised to access this')
1419
return res.redirect('/authError')
1520
}
1621

22+
if (req.path.includes('/contacts/') && sub && !authorisedContactSearchUsers.includes(sub.toUpperCase())) {
23+
logger.error(`User '${sub}' is not authorised to access contact search`)
24+
return res.redirect('/authError')
25+
}
26+
1727
return next()
1828
}
1929

0 commit comments

Comments
 (0)