-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3197 from MTES-MCT/mod/better-impersonate
Amélioration de l'impersonation
- Loading branch information
Showing
13 changed files
with
413 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
back/src/common/middlewares/__tests__/impersonate.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import { Request, Response, NextFunction } from "express"; | ||
import { impersonateMiddleware } from "../impersonate"; | ||
import { prisma } from "@td/prisma"; | ||
|
||
jest.mock("@td/prisma", () => ({ | ||
prisma: { | ||
user: { | ||
findUniqueOrThrow: jest.fn() | ||
} | ||
} | ||
})); | ||
|
||
describe("impersonateMiddleware", () => { | ||
let req: Request; | ||
let res: Response; | ||
let next: NextFunction; | ||
|
||
beforeEach(() => { | ||
req = { session: {} } as Request; | ||
res = {} as Response; | ||
next = jest.fn(); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it("should not impersonate if user is not connected", async () => { | ||
await impersonateMiddleware(req, res, next); | ||
|
||
expect(next).toHaveBeenCalled(); | ||
expect(req.user).toBeUndefined(); | ||
}); | ||
|
||
it("should not impersonate if user has no session impersonation details", async () => { | ||
req.user = { | ||
id: "original-user-id", | ||
name: "Original User", | ||
isAdmin: false, | ||
auth: "SESSION" | ||
} as any; | ||
|
||
await impersonateMiddleware(req, res, next); | ||
|
||
expect(next).toHaveBeenCalled(); | ||
expect(req.user!.id).toBe("original-user-id"); | ||
}); | ||
|
||
it("should not impersonate if user is not admin", async () => { | ||
req.session = { | ||
impersonatedUserId: "impersonated-user-id", | ||
impersonationStartsAt: Date.now() - (60 * 60 * 1000 + 1), // Exceeds 1 hour | ||
warningMessage: "Impersonation warning" | ||
} as any; | ||
|
||
req.user = { | ||
id: "original-user-id", | ||
name: "Original User", | ||
isAdmin: false, | ||
auth: "SESSION" | ||
} as any; | ||
|
||
await impersonateMiddleware(req, res, next); | ||
|
||
expect(next).toHaveBeenCalled(); | ||
expect(req.user!.id).toBe("original-user-id"); | ||
}); | ||
|
||
it("should not impersonate if impersonation duration has exceeded", async () => { | ||
req.session = { | ||
impersonatedUserId: "impersonated-user-id", | ||
impersonationStartsAt: Date.now() - (60 * 60 * 1000 + 1), // Exceeds 1 hour | ||
warningMessage: "Impersonation warning" | ||
} as any; | ||
|
||
req.user = { | ||
id: "original-user-id", | ||
name: "Original User", | ||
isAdmin: true, | ||
auth: "SESSION" | ||
} as any; | ||
|
||
await impersonateMiddleware(req, res, next); | ||
|
||
expect(next).toHaveBeenCalled(); | ||
expect(req.session.impersonatedUserId).toBeUndefined(); | ||
expect(req.session.impersonationStartsAt).toBeUndefined(); | ||
expect(req.session.warningMessage).toBeUndefined(); | ||
expect(req.user!.id).toBe("original-user-id"); | ||
}); | ||
|
||
it("should impersonate if impersonation is active and duration is within limit", async () => { | ||
const impersonatedUser = { | ||
id: "impersonated-user-id", | ||
name: "Impersonated User", | ||
isAdmin: false | ||
}; | ||
const originalUser = { | ||
id: "original-user-id", | ||
name: "Original User", | ||
isAdmin: true, | ||
auth: "SESSION" | ||
}; | ||
|
||
req.session = { | ||
impersonatedUserId: "impersonated-user-id", | ||
impersonationStartsAt: Date.now(), | ||
warningMessage: "Impersonation warning" | ||
} as any; | ||
req.user = originalUser as any; | ||
|
||
jest | ||
.spyOn(prisma.user, "findUniqueOrThrow") | ||
.mockResolvedValueOnce(impersonatedUser as any); | ||
|
||
await impersonateMiddleware(req, res, next); | ||
|
||
expect(next).toHaveBeenCalled(); | ||
expect(req.session.impersonatedUserId).toBe("impersonated-user-id"); | ||
expect(req.session.impersonationStartsAt).toBeDefined(); | ||
expect(req.session.warningMessage).toBe("Impersonation warning"); | ||
expect(req.user).toEqual({ ...impersonatedUser, auth: originalUser.auth }); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { prisma } from "@td/prisma"; | ||
import { Request, Response, NextFunction } from "express"; | ||
|
||
export async function impersonateMiddleware( | ||
req: Request, | ||
res: Response, | ||
next: NextFunction | ||
) { | ||
if ( | ||
req.session.impersonatedUserId && | ||
req.session.impersonationStartsAt && | ||
req.user?.isAdmin | ||
) { | ||
const maxDuration = 60 * 60 * 1000; // 1 hour | ||
if (Date.now() - req.session.impersonationStartsAt > maxDuration) { | ||
delete req.session.impersonatedUserId; | ||
delete req.session.impersonationStartsAt; | ||
delete req.session.warningMessage; | ||
return next(); | ||
} | ||
|
||
const impersonatedUser = await prisma.user.findUniqueOrThrow({ | ||
where: { id: req.session.impersonatedUserId } | ||
}); | ||
|
||
req.user = { ...impersonatedUser, auth: req.user.auth }; | ||
} | ||
|
||
return next(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import request from "supertest"; | ||
import { app } from "../../server"; | ||
import { userFactory } from "../../__tests__/factories"; | ||
import { logIn } from "../../__tests__/auth.helper"; | ||
import { resetDatabase } from "../../../integration-tests/helper"; | ||
|
||
describe("Auth Router", () => { | ||
afterEach(resetDatabase); | ||
|
||
describe("POST /impersonate", () => { | ||
it("should return 404 if user is logged in", async () => { | ||
const user = await userFactory(); | ||
const { sessionCookie } = await logIn(app, user.email, "pass"); | ||
|
||
const response = await request(app) | ||
.post("/impersonate") | ||
.set("Cookie", sessionCookie) | ||
.set("Content-Type", "application/json") | ||
.send({ email: "[email protected]" }); | ||
|
||
expect(response.status).toBe(404); | ||
}); | ||
|
||
it("should return 404 if user is not admin", async () => { | ||
const user = await userFactory(); | ||
const { sessionCookie } = await logIn(app, user.email, "pass"); | ||
|
||
const response = await request(app) | ||
.post("/impersonate") | ||
.set("Cookie", sessionCookie) | ||
.set("Content-Type", "application/json") | ||
.send({ email: "[email protected]" }); | ||
|
||
expect(response.status).toBe(404); | ||
}); | ||
|
||
it("should return 400 if user is admin and impersonated user is not found", async () => { | ||
const user = await userFactory({ isAdmin: true }); | ||
const { sessionCookie } = await logIn(app, user.email, "pass"); | ||
|
||
const response = await request(app) | ||
.post("/impersonate") | ||
.set("Cookie", sessionCookie) | ||
.set("Content-Type", "application/json") | ||
.send({ email: "[email protected]" }); | ||
|
||
expect(response.status).toBe(400); | ||
expect(response.text).toBe("Unknown email"); | ||
}); | ||
|
||
it("should redirect if user is admin and user is found", async () => { | ||
const impersonatedUser = await userFactory(); | ||
|
||
const user = await userFactory({ isAdmin: true }); | ||
const { sessionCookie } = await logIn(app, user.email, "pass"); | ||
|
||
const response = await request(app) | ||
.post("/impersonate") | ||
.set("Cookie", sessionCookie) | ||
.set("Content-Type", "application/json") | ||
.send({ email: impersonatedUser.email }); | ||
|
||
expect(response.status).toBe(302); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.