forked from Yara724/api
Fix actor login credential checks with existing sessions
This commit is contained in:
@@ -0,0 +1,82 @@
|
|||||||
|
import { ExecutionContext, UnauthorizedException } from "@nestjs/common";
|
||||||
|
import { RoleEnum } from "src/Types&Enums/role.enum";
|
||||||
|
import { LocalActorAuthGuard } from "./actor-local.guard";
|
||||||
|
|
||||||
|
describe("LocalActorAuthGuard", () => {
|
||||||
|
const credentials = { username: "new-account", password: "test-password" };
|
||||||
|
let auth: { loginFromCredentials: jest.Mock };
|
||||||
|
let jwt: { verifyAsync: jest.Mock };
|
||||||
|
let guard: LocalActorAuthGuard;
|
||||||
|
|
||||||
|
const context = (request: Record<string, unknown>) =>
|
||||||
|
({ switchToHttp: () => ({ getRequest: () => request }) }) as ExecutionContext;
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
auth = { loginFromCredentials: jest.fn() };
|
||||||
|
jwt = { verifyAsync: jest.fn() };
|
||||||
|
guard = new LocalActorAuthGuard(auth as any, jwt as any);
|
||||||
|
});
|
||||||
|
|
||||||
|
it.each([undefined, "Bearer previous-session", "Bearer expired-session"])(
|
||||||
|
"authenticates the submitted login credentials with header %s",
|
||||||
|
async (authorization) => {
|
||||||
|
const loginData = { sub: "new-account", access_token: "new-session" };
|
||||||
|
auth.loginFromCredentials.mockResolvedValue(loginData);
|
||||||
|
const request = {
|
||||||
|
url: "/api/actor/login/?source=panel",
|
||||||
|
headers: { authorization },
|
||||||
|
body: credentials,
|
||||||
|
};
|
||||||
|
|
||||||
|
await expect(guard.canActivate(context(request))).resolves.toBe(true);
|
||||||
|
expect(auth.loginFromCredentials).toHaveBeenCalledWith(credentials);
|
||||||
|
expect(jwt.verifyAsync).not.toHaveBeenCalled();
|
||||||
|
expect(request["user"]).toBe(loginData);
|
||||||
|
expect(request["identity"]).toBe(loginData);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
it("does not let a previous token bypass failed credentials or captcha", async () => {
|
||||||
|
auth.loginFromCredentials.mockRejectedValue(new UnauthorizedException());
|
||||||
|
jwt.verifyAsync.mockResolvedValue({ sub: "old-account", role: RoleEnum.EXPERT });
|
||||||
|
|
||||||
|
await expect(
|
||||||
|
guard.canActivate(context({
|
||||||
|
route: { path: "/actor/login" },
|
||||||
|
headers: { authorization: "Bearer previous-session" },
|
||||||
|
body: credentials,
|
||||||
|
})),
|
||||||
|
).rejects.toBeInstanceOf(UnauthorizedException);
|
||||||
|
expect(jwt.verifyAsync).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("continues verifying bearer tokens for protected actor routes", async () => {
|
||||||
|
const payload = { sub: "expert-id", role: RoleEnum.EXPERT };
|
||||||
|
jwt.verifyAsync.mockResolvedValue(payload);
|
||||||
|
const request = {
|
||||||
|
url: "/actor/profile",
|
||||||
|
headers: { authorization: "Bearer current-session" },
|
||||||
|
};
|
||||||
|
await expect(guard.canActivate(context(request))).resolves.toBe(true);
|
||||||
|
expect(jwt.verifyAsync).toHaveBeenCalledWith("current-session", expect.any(Object));
|
||||||
|
expect(auth.loginFromCredentials).not.toHaveBeenCalled();
|
||||||
|
expect(request["user"]).toBe(payload);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("rejects protected actor routes without a token", async () => {
|
||||||
|
await expect(
|
||||||
|
guard.canActivate(context({ url: "/actor/profile", headers: {} })),
|
||||||
|
).rejects.toBeInstanceOf(UnauthorizedException);
|
||||||
|
expect(auth.loginFromCredentials).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("rejects expired tokens on protected routes", async () => {
|
||||||
|
jwt.verifyAsync.mockRejectedValue(new Error("expired"));
|
||||||
|
await expect(
|
||||||
|
guard.canActivate(context({
|
||||||
|
url: "/actor/profile",
|
||||||
|
headers: { authorization: "Bearer expired-session" },
|
||||||
|
})),
|
||||||
|
).rejects.toBeInstanceOf(UnauthorizedException);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -17,9 +17,8 @@ export class LocalActorAuthGuard implements CanActivate {
|
|||||||
|
|
||||||
async canActivate(context: ExecutionContext): Promise<boolean> {
|
async canActivate(context: ExecutionContext): Promise<boolean> {
|
||||||
const request = context.switchToHttp().getRequest();
|
const request = context.switchToHttp().getRequest();
|
||||||
const token = this.extractTokenFromHeader(request);
|
// Login always checks the submitted credentials/captcha, even if a client
|
||||||
|
// accidentally sends a valid or expired token from a previous session.
|
||||||
if (!token) {
|
|
||||||
if (this.isActorLoginRequest(request)) {
|
if (this.isActorLoginRequest(request)) {
|
||||||
const loginData = await this.actorAuthService.loginFromCredentials(
|
const loginData = await this.actorAuthService.loginFromCredentials(
|
||||||
request.body ?? {},
|
request.body ?? {},
|
||||||
@@ -28,6 +27,10 @@ export class LocalActorAuthGuard implements CanActivate {
|
|||||||
request.identity = loginData;
|
request.identity = loginData;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const token = this.extractTokenFromHeader(request);
|
||||||
|
|
||||||
|
if (!token) {
|
||||||
throw new UnauthorizedException("Token not found");
|
throw new UnauthorizedException("Token not found");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user