|
| 1 | +/* |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. |
| 4 | + */ |
| 5 | + |
| 6 | +import { NextFunction, Request, Response } from "express"; |
| 7 | +import { WebAppAuthProvider } from "../../../src/provider/WebAppAuthProvider"; |
| 8 | +import redirectHandler from "../../../src/middleware/handlers/redirectHandler"; |
| 9 | +import { ErrorMessages } from "../../../src/utils/Constants"; |
| 10 | + |
| 11 | +describe("redirectHandler", () => { |
| 12 | + const state = "encoded-state"; |
| 13 | + const account = { homeAccountId: "home-account-id" }; |
| 14 | + |
| 15 | + const createHandler = () => { |
| 16 | + const acquireTokenByCode = jest.fn().mockResolvedValue({ account }); |
| 17 | + const tokenCache = { |
| 18 | + deserialize: jest.fn(), |
| 19 | + serialize: jest.fn().mockReturnValue("serialized-cache"), |
| 20 | + }; |
| 21 | + const provider = { |
| 22 | + getLogger: () => ({ trace: jest.fn() }), |
| 23 | + getMsalClient: () => ({ |
| 24 | + acquireTokenByCode, |
| 25 | + getTokenCache: () => tokenCache, |
| 26 | + }), |
| 27 | + getCryptoProvider: () => ({ |
| 28 | + base64Decode: jest.fn().mockReturnValue(JSON.stringify({ redirectTo: "/target" })), |
| 29 | + }), |
| 30 | + } as unknown as WebAppAuthProvider; |
| 31 | + |
| 32 | + return { |
| 33 | + acquireTokenByCode, |
| 34 | + handler: redirectHandler.call(provider), |
| 35 | + }; |
| 36 | + }; |
| 37 | + |
| 38 | + it("redeems the code with the stored request when state matches", async () => { |
| 39 | + const { acquireTokenByCode, handler } = createHandler(); |
| 40 | + const req = { |
| 41 | + body: { |
| 42 | + code: "authorization-code", |
| 43 | + state, |
| 44 | + unexpected: "authorization-response-data", |
| 45 | + }, |
| 46 | + session: { |
| 47 | + tokenRequestParams: { |
| 48 | + code: "", |
| 49 | + scopes: ["openid"], |
| 50 | + state, |
| 51 | + redirectUri: "http://localhost/redirect", |
| 52 | + }, |
| 53 | + }, |
| 54 | + } as unknown as Request; |
| 55 | + const res = { |
| 56 | + redirect: jest.fn(), |
| 57 | + } as unknown as Response; |
| 58 | + const next = jest.fn() as NextFunction; |
| 59 | + |
| 60 | + await handler(req, res, next); |
| 61 | + |
| 62 | + expect(acquireTokenByCode).toHaveBeenCalledTimes(1); |
| 63 | + expect(acquireTokenByCode).toHaveBeenCalledWith({ |
| 64 | + code: "authorization-code", |
| 65 | + scopes: ["openid"], |
| 66 | + state, |
| 67 | + redirectUri: "http://localhost/redirect", |
| 68 | + }); |
| 69 | + expect(req.session.tokenCache).toBe("serialized-cache"); |
| 70 | + expect(req.session.account).toBe(account); |
| 71 | + expect(req.session.isAuthenticated).toBe(true); |
| 72 | + expect(res.redirect).toHaveBeenCalledWith("/target"); |
| 73 | + expect(next).not.toHaveBeenCalled(); |
| 74 | + }); |
| 75 | + |
| 76 | + it.each([ |
| 77 | + ["missing", undefined], |
| 78 | + ["mismatched", "different-state"], |
| 79 | + ])("rejects a %s state before redeeming the code", async (_description, responseState) => { |
| 80 | + const { acquireTokenByCode, handler } = createHandler(); |
| 81 | + const req = { |
| 82 | + body: { |
| 83 | + code: "authorization-code", |
| 84 | + state: responseState, |
| 85 | + }, |
| 86 | + session: { |
| 87 | + tokenRequestParams: { |
| 88 | + state, |
| 89 | + }, |
| 90 | + }, |
| 91 | + } as unknown as Request; |
| 92 | + const res = {} as Response; |
| 93 | + const next = jest.fn() as NextFunction; |
| 94 | + |
| 95 | + await handler(req, res, next); |
| 96 | + |
| 97 | + expect(next).toHaveBeenCalledWith(new Error(ErrorMessages.CSRF_TOKEN_MISMATCH)); |
| 98 | + expect(acquireTokenByCode).not.toHaveBeenCalled(); |
| 99 | + }); |
| 100 | +}); |
0 commit comments