mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-18 08:02:12 +00:00
Polar and Oura resolved their OAuth client id/secret from the server
environment only, forcing every user on a deploy to share one app. Both
now read per-user BYO keys first, falling back to the shared env app when
unset, mirroring the WHOOP and Fitbit model.
- Add DB-first-then-env resolvers in src/lib/{polar,oura}/credentials.ts,
encrypting the client id/secret at rest on User.
- Repoint the connect, callback, status routes and the Oura refresh path
to the user-scoped resolver.
- Add /api/{polar,oura}/credentials (GET/PUT/DELETE) with Zod-validated
input and userId narrowed from the session.
- Render an optional 'your OAuth app credentials' form on the Polar and
Oura cards; the status read exposes hasOwnCredentials for the saved
placeholder. Existing env-only deploys keep working unchanged.
- Cover both resolvers (DB hit, env fallback, no creds) and the card form
with unit tests; add the new strings across all six locales.
84 lines
2.8 KiB
TypeScript
84 lines
2.8 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
|
const findUnique = vi.fn();
|
|
const getPolarCredentials = vi.fn();
|
|
|
|
vi.mock("@/lib/db", () => ({
|
|
prisma: { user: { findUnique: (...args: unknown[]) => findUnique(...args) } },
|
|
}));
|
|
|
|
// decrypt() is the fail-closed AES-256-GCM reader; the resolver only needs it
|
|
// to round-trip the stored ciphertext, so a `dec(<x>)` shim is sufficient here.
|
|
vi.mock("@/lib/crypto", () => ({
|
|
decrypt: (cipher: string) => `dec(${cipher})`,
|
|
encrypt: (plain: string) => `enc(${plain})`,
|
|
}));
|
|
|
|
// The env fallback lives in ./client; stub it so the precedence is observable.
|
|
vi.mock("../client", () => ({
|
|
getPolarCredentials: () => getPolarCredentials(),
|
|
}));
|
|
|
|
import { getPolarClientCredentials } from "../credentials";
|
|
|
|
describe("getPolarClientCredentials — DB-first then env", () => {
|
|
beforeEach(() => {
|
|
findUnique.mockReset();
|
|
getPolarCredentials.mockReset();
|
|
});
|
|
|
|
it("returns the per-user BYO client id/secret when both columns are set", async () => {
|
|
findUnique.mockResolvedValue({
|
|
polarClientIdEncrypted: "enc-id",
|
|
polarClientSecretEncrypted: "enc-secret",
|
|
});
|
|
const creds = await getPolarClientCredentials("user-1");
|
|
expect(creds).toEqual({
|
|
clientId: "dec(enc-id)",
|
|
clientSecret: "dec(enc-secret)",
|
|
});
|
|
// DB hit must not consult the env fallback.
|
|
expect(getPolarCredentials).not.toHaveBeenCalled();
|
|
expect(findUnique).toHaveBeenCalledWith({
|
|
where: { id: "user-1" },
|
|
select: {
|
|
polarClientIdEncrypted: true,
|
|
polarClientSecretEncrypted: true,
|
|
},
|
|
});
|
|
});
|
|
|
|
it("falls back to the shared env app when the user has no BYO pair", async () => {
|
|
findUnique.mockResolvedValue({
|
|
polarClientIdEncrypted: null,
|
|
polarClientSecretEncrypted: null,
|
|
});
|
|
getPolarCredentials.mockReturnValue({
|
|
clientId: "env-id",
|
|
clientSecret: "env-secret",
|
|
});
|
|
const creds = await getPolarClientCredentials("user-1");
|
|
expect(creds).toEqual({ clientId: "env-id", clientSecret: "env-secret" });
|
|
expect(getPolarCredentials).toHaveBeenCalledOnce();
|
|
});
|
|
|
|
it("falls back to env when only one half of the BYO pair is present", async () => {
|
|
findUnique.mockResolvedValue({
|
|
polarClientIdEncrypted: "enc-id",
|
|
polarClientSecretEncrypted: null,
|
|
});
|
|
getPolarCredentials.mockReturnValue({
|
|
clientId: "env-id",
|
|
clientSecret: "env-secret",
|
|
});
|
|
const creds = await getPolarClientCredentials("user-1");
|
|
expect(creds).toEqual({ clientId: "env-id", clientSecret: "env-secret" });
|
|
});
|
|
|
|
it("returns null when neither the user nor the env is configured", async () => {
|
|
findUnique.mockResolvedValue(null);
|
|
getPolarCredentials.mockReturnValue(null);
|
|
expect(await getPolarClientCredentials("ghost")).toBeNull();
|
|
});
|
|
});
|