Skip to content

Commit

Permalink
Merge pull request #10 from cabinetoffice/NTRNL-499-extract-user-emai…
Browse files Browse the repository at this point in the history
…l-from-cola-jwt

Ntrnl 499 extract user email from cola jwt
  • Loading branch information
harley-harris authored Jul 1, 2024
2 parents 7977e41 + d82d8a8 commit 899e25e
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
2 changes: 1 addition & 1 deletion 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": "@co-digital/login",
"version": "1.0.3",
"version": "1.0.4",
"description": "A login library for Node.JS applications in CO Digital.",
"homepage": "https://github.com/cabinetoffice/node-login#README.md",
"main": "./lib/index.js",
Expand Down
3 changes: 3 additions & 0 deletions src/middleware/cola/authentication.middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
import {
getCookieValue,
getUnsignedCookie,
getUserEmailFromColaJwt,
validateUnsignedCookie
} from '../../utils/cookie';

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

if (validateUnsignedCookie(unsignedCookie)) {
const userEmailAuth = getUserEmailFromColaJwt(unsignedCookie as string);
res.locals.userEmailAuth = userEmailAuth;
log.debugRequest(req, `Successfully verified signature for ${COOKIE_ID_NAME}, cookie value: ${unsignedCookie}`);
} else {
log.errorRequest(req, `Failed to verify signature for ${COOKIE_ID_NAME}, cookie value: ${cookieSignedValue}, redirect to ${AUTH_SIGN_IN_URL}`);
Expand Down
20 changes: 19 additions & 1 deletion test/unit/middleware/cola/authentication.middleware.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import { log } from '../../../../src/utils/logger';
import {
getCookieValue,
getUnsignedCookie,
validateUnsignedCookie
validateUnsignedCookie,
getUserEmailFromColaJwt,
} from '../../../../src/utils/cookie';
import { cookieSignedValue, req } from '../../../mock/data.mock';

Expand All @@ -26,10 +27,12 @@ const logErrorRequestMock = log.errorRequest as jest.Mock;
const getCookieValueMock = getCookieValue as jest.Mock;
const getUnsignedCookieMock = getUnsignedCookie as jest.Mock;
const validateUnsignedCookieMock = validateUnsignedCookie as jest.Mock;
const getUserEmailFromColaJwtMock = getUserEmailFromColaJwt as jest.Mock;

export const mockResponse = () => {
const res = {} as Response;
res.redirect = jest.fn() as any;
res.locals = {};
return res;
};

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

test('should attach userEmailAuth property to res.locals if validation is successful', () => {
const unsignedCookie = 'xyz.123';
const email = '[email protected]';

getUnsignedCookieMock.mockReturnValueOnce(unsignedCookie);
validateUnsignedCookieMock.mockReturnValueOnce(true);
getUserEmailFromColaJwtMock.mockReturnValueOnce(email);

authentication(req, res, next);

expect(getUserEmailFromColaJwtMock).toHaveBeenCalledTimes(1);
expect(getUserEmailFromColaJwtMock).toHaveBeenCalledWith(unsignedCookie);
expect(res.locals.userEmailAuth).toBe(email);
});

test('should call next with error object if error is thrown', () => {
getCookieValueMock.mockReturnValueOnce(cookieSignedValue);
validateUnsignedCookieMock.mockReturnValueOnce(false);
Expand Down

0 comments on commit 899e25e

Please sign in to comment.