refactor(auth): address server review feedback
- Extract auth DTOs, cookie/JWT helpers and the generic duration parser. - Configure the session cookie and map domain auth failures to HTTP responses. - Add typed Swagger responses and focused regression coverage.
This commit is contained in:
1 parent
72e127888c
commit
1830feb71b
18 files changed
+399
-208
No files matched your search
@@ -1,8 +1,6 @@
|
||||
import "reflect-metadata";
|
||||
import {
|
||||
authDurationSeconds,
|
||||
validateAppConfig,
|
||||
} from "../src/config/app.config";
|
||||
import { validateAppConfig } from "../src/config/app.config";
|
||||
import { durationSeconds } from "../src/common/interval";
|
||||
|
||||
describe("Authentication configuration", () => {
|
||||
it("defaults authentication off with a 24-hour token lifetime and cleanup schedule", () => {
|
||||
@@ -10,6 +8,7 @@ describe("Authentication configuration", () => {
|
||||
|
||||
expect(config.AUTH_ENABLED).toBe(false);
|
||||
expect(config.AUTH_JWT_EXPIRES_IN).toBe("24h");
|
||||
expect(config.AUTH_COOKIE_NAME).toBe("liqa_auth");
|
||||
expect(config.AUTH_REVOKED_TOKEN_CLEANUP_CRON).toBe("0 3 * * *");
|
||||
});
|
||||
|
||||
@@ -46,9 +45,9 @@ describe("Authentication configuration", () => {
|
||||
expect(() => validateAppConfig({ AUTH_ENABLED: "yes" })).toThrow(
|
||||
"AUTH_ENABLED must be either true or false",
|
||||
);
|
||||
expect(authDurationSeconds("30m")).toBe(1800);
|
||||
expect(authDurationSeconds("24h")).toBe(86400);
|
||||
expect(authDurationSeconds("2d")).toBe(172800);
|
||||
expect(authDurationSeconds("0s")).toBeNull();
|
||||
expect(durationSeconds("30m")).toBe(1800);
|
||||
expect(durationSeconds("24h")).toBe(86400);
|
||||
expect(durationSeconds("2d")).toBe(172800);
|
||||
expect(durationSeconds("0s")).toBeNull();
|
||||
});
|
||||
});
|
||||
@@ -31,4 +31,11 @@ describe("authentication disabled", () => {
|
||||
.expect(200);
|
||||
expect(manifest.body.auth).toEqual({ enabled: false });
|
||||
});
|
||||
|
||||
it("returns not found for login while authentication is disabled", async () => {
|
||||
await request(app.getHttpServer())
|
||||
.post("/auth/login")
|
||||
.send({ username: "nobody", password: "synthetic" })
|
||||
.expect(404);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,57 @@
|
||||
import { AuthSession } from "../src/auth/auth.service";
|
||||
import {
|
||||
clearedSessionCookie,
|
||||
cookieToken,
|
||||
requestToken,
|
||||
sessionCookie,
|
||||
} from "../src/auth/auth.helpers";
|
||||
import { Public, IS_PUBLIC_KEY } from "../src/auth/public.decorator";
|
||||
|
||||
describe("authentication helpers", () => {
|
||||
const request = {
|
||||
secure: false,
|
||||
get: (name: string) =>
|
||||
name === "x-forwarded-proto" ? undefined : undefined,
|
||||
} as never;
|
||||
const session: AuthSession = {
|
||||
user: {
|
||||
id: "u",
|
||||
username: "user",
|
||||
createdAt: new Date(0),
|
||||
updatedAt: new Date(0),
|
||||
},
|
||||
token: "a.token.value",
|
||||
expiresAt: new Date(Date.now() + 60_000),
|
||||
};
|
||||
|
||||
it("sets and clears a configurable cookie name", () => {
|
||||
expect(sessionCookie(request, session, "custom_session")).toContain(
|
||||
"custom_session=a.token.value",
|
||||
);
|
||||
expect(clearedSessionCookie(request, "custom_session")).toContain(
|
||||
"custom_session=;",
|
||||
);
|
||||
});
|
||||
|
||||
it("reads only the selected cookie and supports bearer credentials", () => {
|
||||
expect(
|
||||
cookieToken("other=x; custom_session=encoded%20value", "custom_session"),
|
||||
).toBe("encoded value");
|
||||
expect(
|
||||
requestToken(
|
||||
{
|
||||
get: (name: string) =>
|
||||
name === "authorization" ? "Bearer token" : undefined,
|
||||
} as never,
|
||||
"x",
|
||||
),
|
||||
).toBe("token");
|
||||
});
|
||||
|
||||
it("stores public-route metadata under a Symbol key", () => {
|
||||
class PublicController {}
|
||||
Public()(PublicController);
|
||||
expect(typeof IS_PUBLIC_KEY).toBe("symbol");
|
||||
expect(Reflect.getMetadata(IS_PUBLIC_KEY, PublicController)).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -4,7 +4,7 @@ import request from "supertest";
|
||||
import { Repository } from "typeorm";
|
||||
import { filter, firstValueFrom, timeout } from "rxjs";
|
||||
import { AuthService } from "../src/auth/auth.service";
|
||||
import { tokenHash } from "../src/auth/auth.service";
|
||||
import { tokenHash } from "../src/auth/jwt.helpers";
|
||||
import { PasswordService } from "../src/auth/password.service";
|
||||
import { RevokedTokenEntity } from "../src/auth/revoked-token.entity";
|
||||
import { EnvironmentService } from "../src/environment/environment.service";
|
||||
|
||||
@@ -1,9 +1,22 @@
|
||||
import { INestApplication } from "@nestjs/common";
|
||||
import request from "supertest";
|
||||
import { appPackage } from "../src/common/app-package";
|
||||
import { ManifestDto } from "../src/manifest/manifest.dto";
|
||||
import { buildTestApp } from "./app.harness";
|
||||
|
||||
describe("GET /manifest.json", () => {
|
||||
it("describes the manifest auth flag without describing authentication as optional", () => {
|
||||
const authProperty = Reflect.getMetadata(
|
||||
"swagger/apiModelProperties",
|
||||
ManifestDto.prototype,
|
||||
"auth",
|
||||
) as { description: string };
|
||||
expect(authProperty.description).toBe(
|
||||
"Whether account authentication is enabled.",
|
||||
);
|
||||
expect(authProperty.description).not.toContain("optional");
|
||||
});
|
||||
|
||||
let app: INestApplication;
|
||||
|
||||
afterEach(async () => {
|
||||
|
||||
Reference in new issue
Block a user