Skip to content

Commit f3b1ce1

Browse files
committed
Merge branch 'main' into feature/CCM-10893-add-account-header
2 parents 5f23006 + caf1c40 commit f3b1ce1

File tree

4 files changed

+68
-4
lines changed

4 files changed

+68
-4
lines changed

frontend/src/__tests__/middleware.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ describe('middleware function', () => {
7474
const csp = getCsp(response);
7575

7676
expect(response.status).toBe(200);
77-
expect(getClientIdFromTokenMock).toHaveBeenCalled();
77+
expect(getClientIdFromTokenMock).toHaveBeenCalledTimes(1);
7878

7979
expect(csp).toEqual([
8080
"base-uri 'self'",

lambdas/authorizer/src/__tests__/index.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ const allowPolicy = {
4343
},
4444
context: {
4545
user: 'sub',
46+
clientId: 'client-123',
4647
},
4748
};
4849

@@ -72,10 +73,11 @@ afterEach(() => {
7273
process.env = originalEnv;
7374
});
7475

75-
test('returns Allow policy on valid token', async () => {
76+
test('returns Allow policy on valid token with clientId', async () => {
7677
lambdaCognitoAuthorizer.authorize.mockResolvedValue({
7778
success: true,
7879
subject: 'sub',
80+
clientId: 'client-123',
7981
});
8082

8183
const res = await handler(

utils/utils/src/__tests__/lambda-cognito-authorizer.test.ts

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ describe('LambdaCognitoAuthorizer', () => {
175175
);
176176
});
177177

178-
test('returns failure on token with incorrect client_id claim', async () => {
178+
test('returns failure on token with incorrect cognito client_id claim', async () => {
179179
const jwt = sign(
180180
{
181181
token_use: 'access',
@@ -282,6 +282,68 @@ describe('LambdaCognitoAuthorizer', () => {
282282
);
283283
});
284284

285+
test('returns failure when NHS Notify client ID claim is empty string', async () => {
286+
const jwt = sign(
287+
{
288+
token_use: 'access',
289+
client_id: 'user-pool-client-id',
290+
iss: 'https://cognito-idp.eu-west-2.amazonaws.com/user-pool-id',
291+
'nhs-notify:client-id': '',
292+
},
293+
'key',
294+
{
295+
keyid: 'key-id',
296+
}
297+
);
298+
299+
const res = await authorizer.authorize(userPoolId, userPoolClientId, jwt);
300+
301+
expect(res).toEqual({ success: false });
302+
expect(mockLogger.logMessages).toContainEqual(
303+
expect.objectContaining({
304+
level: 'error',
305+
message: expect.stringContaining('Failed to authorize'),
306+
issues: expect.arrayContaining([
307+
expect.objectContaining({
308+
path: ['nhs-notify:client-id'],
309+
message: 'Too small: expected string to have >=1 characters',
310+
}),
311+
]),
312+
})
313+
);
314+
});
315+
316+
test('returns failure when NHS Notify client ID claim is whitespace', async () => {
317+
const jwt = sign(
318+
{
319+
token_use: 'access',
320+
client_id: 'user-pool-client-id',
321+
iss: 'https://cognito-idp.eu-west-2.amazonaws.com/user-pool-id',
322+
'nhs-notify:client-id': ' ',
323+
},
324+
'key',
325+
{
326+
keyid: 'key-id',
327+
}
328+
);
329+
330+
const res = await authorizer.authorize(userPoolId, userPoolClientId, jwt);
331+
332+
expect(res).toEqual({ success: false });
333+
expect(mockLogger.logMessages).toContainEqual(
334+
expect.objectContaining({
335+
level: 'error',
336+
message: expect.stringContaining('Failed to authorize'),
337+
issues: expect.arrayContaining([
338+
expect.objectContaining({
339+
path: ['nhs-notify:client-id'],
340+
message: 'Too small: expected string to have >=1 characters',
341+
}),
342+
]),
343+
})
344+
);
345+
});
346+
285347
test('returns failure on Cognito not validating the token', async () => {
286348
const cognitoErrorUserPool = 'user-pool-id-cognito-error';
287349

utils/utils/src/lambda-cognito-authorizer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const $AccessToken = z.object({
1212
client_id: z.string(),
1313
iss: z.string(),
1414
token_use: z.string(),
15-
'nhs-notify:client-id': z.string(),
15+
'nhs-notify:client-id': z.string().trim().nonempty(),
1616
});
1717

1818
export class LambdaCognitoAuthorizer {

0 commit comments

Comments
 (0)