Skip to content

Commit

Permalink
Merge pull request #95 from DEFRA/fix/response-log-payload
Browse files Browse the repository at this point in the history
Fix/response log payload
  • Loading branch information
jack-mccabe-defra authored Oct 28, 2024
2 parents 0454e35 + 8515699 commit 53c54dc
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 16 deletions.
33 changes: 28 additions & 5 deletions app/auth/authenticate.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,44 @@ export async function getAuth (request, getJwtPublicKeyFunc = getJwtPublicKey) {
if (!authHeader) {
return {}
}
logger.verbose('#DAL - Request authentication - Check verification', { code: DAL_REQUEST_AUTHENTICATION_001 })
logger.verbose('#DAL - Request authentication - Check verification', {
code: DAL_REQUEST_AUTHENTICATION_001,
request: {
remoteAddress: request?.info?.remoteAddress
}
})
const token = authHeader.split(' ')[1]
const decodedToken = jwt.decode(token, { complete: true })
const header = decodedToken.header
const requestStart = Date.now()
const signingKey = await getJwtPublicKeyFunc(header.kid)
const requestTimeMs = (Date.now() - requestStart)
const requestTimeMs = Date.now() - requestStart
const verified = jwt.verify(token, signingKey)
logger.http('#DAL Request authentication - JWT verified', { code: DAL_REQUEST_AUTHENTICATION_001, requestTimeMs })
logger.http('#DAL Request authentication - JWT verified', {
code: DAL_REQUEST_AUTHENTICATION_001,
requestTimeMs,
request: {
remoteAddress: request?.info?.remoteAddress
}
})
return verified
} catch (error) {
if (error.name === 'TokenExpiredError') {
logger.warn('#DAL - request authentication - token expired', { error, code: DAL_REQUEST_AUTHENTICATION_001 })
logger.warn('#DAL - request authentication - token expired', {
error,
code: DAL_REQUEST_AUTHENTICATION_001,
request: {
remoteAddress: request?.info?.remoteAddress
}
})
} else {
logger.error('#DAL - request authentication - Error verifying jwt', { error, code: DAL_REQUEST_AUTHENTICATION_001 })
logger.error('#DAL - request authentication - Error verifying jwt', {
error,
code: DAL_REQUEST_AUTHENTICATION_001,
request: {
remoteAddress: request?.info?.remoteAddress
}
})
}
return {}
}
Expand Down
9 changes: 6 additions & 3 deletions app/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,13 @@ server.events.on('response', function (request) {
requestTimeMs,
requestId: request.id,
request: {
headers: request.headers,
path: request.path,
method: request.method.toUpperCase(),
body: request.payload
path: request.path,
params: request.params,
payload: request.payload,
body: request.body,
headers: request.headers,
remoteAddress: request.info.remoteAddress
},
response: {
statusCode: request.response.statusCode
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fcp-data-access-layer-api",
"version": "1.3.23",
"version": "1.3.24",
"description": "Customer Registry GraphQL Service",
"homepage": "https://github.com/DEFRA/fcp-data-access-layer-api",
"main": "app/index.js",
Expand Down
26 changes: 21 additions & 5 deletions test/auth/authenticate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,33 @@ const tokenPayload = {

const token = jwt.sign(tokenPayload, 'secret', { expiresIn: '1h' })
const tokenDiffSecret = jwt.sign(tokenPayload, 'secret2', { expiresIn: '1h' })
const mockRequest = { headers: { authorization: `Bearer ${token}` } }
const mockRequest = {
headers: {
authorization: `Bearer ${token}`
},
info: { remoteAddress: '0.0.0.0' }
}
const mockRequestWrongSign = {
headers: { authorization: `Bearer ${tokenDiffSecret}` }
headers: {
authorization: `Bearer ${tokenDiffSecret}`
},
info: { remoteAddress: '0.0.0.0' }
}
const incorrectTokenReq = {
headers: { authorization: 'Bearer WRONG' },
info: { remoteAddress: '0.0.0.0' }
}
const incorrectTokenReq = { headers: { authorization: 'Bearer WRONG' } }
const decodedToken = jwt.decode(token, 'secret')
const mockPublicKeyFunc = jest.fn()

describe('getJwtPublicKey', () => {
let jwksMock
let stopMock
beforeEach(() => {
jwksMock = createJWKSMock('https://login.microsoftonline.com', `/${process.env.API_TENANT_ID}/discovery/v2.0/keys`)
jwksMock = createJWKSMock(
'https://login.microsoftonline.com',
`/${process.env.API_TENANT_ID}/discovery/v2.0/keys`
)
stopMock = jwksMock.start()
})

Expand Down Expand Up @@ -100,7 +114,9 @@ describe('getAuth', () => {
test('should return an empty object when token verification fails, due to token expiry', async () => {
const error = new Error('TokenExpiredError')
error.name = 'TokenExpiredError'
mockPublicKeyFunc.mockImplementation(() => { throw error })
mockPublicKeyFunc.mockImplementation(() => {
throw error
})
expect(await getAuth(mockRequestWrongSign, mockPublicKeyFunc)).toEqual({})
expect(mockPublicKeyFunc).toHaveBeenCalledWith(undefined)
})
Expand Down

0 comments on commit 53c54dc

Please sign in to comment.