Skip to content

Commit 899e25e

Browse files
Merge pull request #10 from cabinetoffice/NTRNL-499-extract-user-email-from-cola-jwt
Ntrnl 499 extract user email from cola jwt
2 parents 7977e41 + d82d8a8 commit 899e25e

4 files changed

Lines changed: 24 additions & 3 deletions

File tree

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@co-digital/login",
3-
"version": "1.0.3",
3+
"version": "1.0.4",
44
"description": "A login library for Node.JS applications in CO Digital.",
55
"homepage": "https://github.com/cabinetoffice/node-login#README.md",
66
"main": "./lib/index.js",

src/middleware/cola/authentication.middleware.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
import {
99
getCookieValue,
1010
getUnsignedCookie,
11+
getUserEmailFromColaJwt,
1112
validateUnsignedCookie
1213
} from '../../utils/cookie';
1314

@@ -19,6 +20,8 @@ export const authentication = ( req: Request, res: Response, next: NextFunction
1920
const unsignedCookie = getUnsignedCookie(cookieSignedValue, COOKIE_PARSER_SECRET);
2021

2122
if (validateUnsignedCookie(unsignedCookie)) {
23+
const userEmailAuth = getUserEmailFromColaJwt(unsignedCookie as string);
24+
res.locals.userEmailAuth = userEmailAuth;
2225
log.debugRequest(req, `Successfully verified signature for ${COOKIE_ID_NAME}, cookie value: ${unsignedCookie}`);
2326
} else {
2427
log.errorRequest(req, `Failed to verify signature for ${COOKIE_ID_NAME}, cookie value: ${cookieSignedValue}, redirect to ${AUTH_SIGN_IN_URL}`);

test/unit/middleware/cola/authentication.middleware.spec.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ import { log } from '../../../../src/utils/logger';
1616
import {
1717
getCookieValue,
1818
getUnsignedCookie,
19-
validateUnsignedCookie
19+
validateUnsignedCookie,
20+
getUserEmailFromColaJwt,
2021
} from '../../../../src/utils/cookie';
2122
import { cookieSignedValue, req } from '../../../mock/data.mock';
2223

@@ -26,10 +27,12 @@ const logErrorRequestMock = log.errorRequest as jest.Mock;
2627
const getCookieValueMock = getCookieValue as jest.Mock;
2728
const getUnsignedCookieMock = getUnsignedCookie as jest.Mock;
2829
const validateUnsignedCookieMock = validateUnsignedCookie as jest.Mock;
30+
const getUserEmailFromColaJwtMock = getUserEmailFromColaJwt as jest.Mock;
2931

3032
export const mockResponse = () => {
3133
const res = {} as Response;
3234
res.redirect = jest.fn() as any;
35+
res.locals = {};
3336
return res;
3437
};
3538

@@ -88,6 +91,21 @@ describe('Cola Authentication Middleware test suites', () => {
8891
expect(res.redirect).toHaveBeenCalledTimes(0);
8992
});
9093

94+
test('should attach userEmailAuth property to res.locals if validation is successful', () => {
95+
const unsignedCookie = 'xyz.123';
96+
const email = 'placeholder@fake.com';
97+
98+
getUnsignedCookieMock.mockReturnValueOnce(unsignedCookie);
99+
validateUnsignedCookieMock.mockReturnValueOnce(true);
100+
getUserEmailFromColaJwtMock.mockReturnValueOnce(email);
101+
102+
authentication(req, res, next);
103+
104+
expect(getUserEmailFromColaJwtMock).toHaveBeenCalledTimes(1);
105+
expect(getUserEmailFromColaJwtMock).toHaveBeenCalledWith(unsignedCookie);
106+
expect(res.locals.userEmailAuth).toBe(email);
107+
});
108+
91109
test('should call next with error object if error is thrown', () => {
92110
getCookieValueMock.mockReturnValueOnce(cookieSignedValue);
93111
validateUnsignedCookieMock.mockReturnValueOnce(false);

0 commit comments

Comments
 (0)