Secure Sakani portal and show full album titles
This commit is contained in:
1 parent
c40ccf77cf
commit
69264ce165
49 files changed
+1362
-601
No files matched your search
@@ -121,3 +121,85 @@ test("keeps Better Auth login limits separate for clients forwarded by Zoraxy",
|
||||
|
||||
expect(otherClient.status).not.toBe(429);
|
||||
});
|
||||
|
||||
test("Better Auth grants admin access only to the session that completed MFA", async () => {
|
||||
const password = "dummy-admin-password";
|
||||
const email = "mfa-admin@example.test";
|
||||
const userId = crypto.randomUUID();
|
||||
const { hashPassword, symmetricDecrypt } = await import("better-auth/crypto");
|
||||
const { createPrincipalGuard } = await import("./principal");
|
||||
const state = {
|
||||
user: [{ id: userId, email, name: "Test Admin", role: "ADMIN" as const, emailVerified: true,
|
||||
twoFactorEnabled: false, createdAt: new Date(), updatedAt: new Date() }],
|
||||
account: [{ id: crypto.randomUUID(), userId, providerId: "credential", accountId: userId,
|
||||
password: await hashPassword(password), createdAt: new Date(), updatedAt: new Date() }],
|
||||
session: [],
|
||||
verification: [],
|
||||
twoFactor: [] as Array<{ secret: string }>,
|
||||
};
|
||||
const { auth } = createPortalAuth({
|
||||
database: memoryAdapter(state),
|
||||
mailerService: { sendPasswordReset: vi.fn(), sendVerification: vi.fn() },
|
||||
registrationRepository: {
|
||||
createEditorial: vi.fn(), markPendingApproval: vi.fn(), removeUnverifiedAuthIdentity: vi.fn(),
|
||||
recordRegistrationCompensationFailure: vi.fn(), recordVerificationInconsistency: vi.fn(),
|
||||
canCreateSession: vi.fn().mockResolvedValue(true),
|
||||
},
|
||||
});
|
||||
const post = (path: string, body: object, cookie?: string) =>
|
||||
auth.handler(new Request(`http://localhost:3000/api/auth/${path}`, {
|
||||
method: "POST",
|
||||
headers: { "content-type": "application/json", ...(cookie ? { cookie } : {}) },
|
||||
body: JSON.stringify(body),
|
||||
}));
|
||||
function responseCookie(response: Response, name: string) {
|
||||
const header = response.headers.getSetCookie().find((value) => value.startsWith(`better-auth.${name}=`));
|
||||
expect(header).toBeDefined();
|
||||
return header!.split(";")[0];
|
||||
}
|
||||
const headers = (cookie: string) => new Headers({ cookie });
|
||||
const guard = createPrincipalGuard({
|
||||
async getSession(requestHeaders) {
|
||||
const session = await auth.api.getSession({ headers: requestHeaders });
|
||||
return session?.user.role === "ADMIN"
|
||||
? { ...session, user: { ...session.user, role: "ADMIN", twoFactorEnabled: session.user.twoFactorEnabled === true } }
|
||||
: null;
|
||||
},
|
||||
getAccount: async () => null,
|
||||
});
|
||||
const login = () => post("sign-in/email", { email, password });
|
||||
const first = responseCookie(await login(), "session_token");
|
||||
const second = responseCookie(await login(), "session_token");
|
||||
expect(first).not.toEqual(second);
|
||||
const setup = await post("two-factor/enable", { password }, first);
|
||||
expect(setup.status).toBe(200);
|
||||
const { backupCodes } = await setup.json();
|
||||
const secret = await symmetricDecrypt({
|
||||
key: (await auth.$context).secretConfig, data: state.twoFactor[0].secret,
|
||||
});
|
||||
const { code } = await auth.api.generateTOTP({ body: { secret } });
|
||||
const verified = await post("two-factor/verify-totp", { code }, first);
|
||||
expect(verified.status).toBe(200);
|
||||
const rotated = responseCookie(verified, "session_token");
|
||||
|
||||
await expect(guard.requireAdmin(headers(rotated))).resolves.toMatchObject({ userId });
|
||||
await expect(guard.requireAdmin(headers(second))).rejects.toMatchObject({ code: "AUTHENTICATION_REQUIRED" });
|
||||
expect((await post("two-factor/disable", { password }, second)).status).toBe(401);
|
||||
|
||||
const challenge = await login();
|
||||
expect(await challenge.clone().json()).toMatchObject({ twoFactorRedirect: true });
|
||||
const challengeCookie = responseCookie(challenge, "two_factor");
|
||||
const signedIn = await post("two-factor/verify-totp", { code }, challengeCookie);
|
||||
expect(signedIn.status).toBe(200);
|
||||
const signedInCookie = responseCookie(signedIn, "session_token");
|
||||
await expect(guard.requireAdmin(headers(signedInCookie))).resolves.toMatchObject({ userId });
|
||||
|
||||
const backupChallenge = responseCookie(await login(), "two_factor");
|
||||
const backup = await post("two-factor/verify-backup-code", { code: backupCodes[0] }, backupChallenge);
|
||||
expect(backup.status).toBe(200);
|
||||
await expect(guard.requireAdmin(headers(responseCookie(backup, "session_token"))))
|
||||
.resolves.toMatchObject({ userId });
|
||||
|
||||
expect((await post("two-factor/disable", { password }, signedInCookie)).status).toBe(200);
|
||||
expect(await auth.api.getSession({ headers: headers(rotated) })).toBeNull();
|
||||
});
|
||||
Reference in new issue
Block a user