Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

fix(web): refetch gitlab projects when the token changes

zaralXf602f3d87119
diff --git a/apps/web/src/components/project/gitlab-project-browser-modal.tsx b/apps/web/src/components/project/gitlab-project-browser-modal.tsx
index 9463e9d16..16f151f08 100644
--- a/apps/web/src/components/project/gitlab-project-browser-modal.tsx
+++ b/apps/web/src/components/project/gitlab-project-browser-modal.tsx
@@ -16,6 +16,7 @@ import listGitlabProjects, {
type ListGitlabProjectsResponse,
} from "@/fetchers/gitlab-integration/list-gitlab-projects";
import { cn } from "@/lib/cn";
+import { tokenFingerprint } from "@/lib/token-fingerprint";
type GitlabProjectBrowserModalProps = {
open: boolean;
@@ -45,11 +46,16 @@ export function GitlabProjectBrowserModal({
open && baseUrl.trim().length > 0 && accessToken.trim().length > 0;
const { data, isLoading, error, refetch } = useQuery({
- queryKey: ["gitlab-projects", projectId, baseUrl, tokenType],
+ queryKey: [
+ "gitlab-projects",
+ projectId,
+ baseUrl,
+ tokenType,
+ tokenFingerprint(accessToken),
+ ],
queryFn: () =>
listGitlabProjects({ projectId, baseUrl, accessToken, tokenType }),
enabled: canFetch,
- // Token is not in the key; gcTime: 0 drops the result when the modal closes.
gcTime: 0,
});
diff --git a/apps/web/src/lib/token-fingerprint.test.ts b/apps/web/src/lib/token-fingerprint.test.ts
new file mode 100644
index 000000000..e6c976ba8
--- /dev/null
+++ b/apps/web/src/lib/token-fingerprint.test.ts
@@ -0,0 +1,20 @@
+import { describe, expect, it } from "vitest";
+import { tokenFingerprint } from "./token-fingerprint";
+
+describe("tokenFingerprint", () => {
+ it("is stable for the same token", () => {
+ expect(tokenFingerprint("glpat-abc123")).toBe(
+ tokenFingerprint("glpat-abc123"),
+ );
+ });
+
+ it("changes when the token changes", () => {
+ expect(tokenFingerprint("glpat-abc123")).not.toBe(
+ tokenFingerprint("glpat-abc124"),
+ );
+ });
+
+ it("returns a short hex digest rather than the token", () => {
+ expect(tokenFingerprint("glpat-abc123")).toMatch(/^[0-9a-f]{8}$/);
+ });
+});
diff --git a/apps/web/src/lib/token-fingerprint.ts b/apps/web/src/lib/token-fingerprint.ts
new file mode 100644
index 000000000..bfa659ba1
--- /dev/null
+++ b/apps/web/src/lib/token-fingerprint.ts
@@ -0,0 +1,10 @@
+// Lets a query key change with the credential without holding the credential
+// itself. FNV-1a is not a secure hash; it only has to differ between tokens.
+export function tokenFingerprint(token: string): string {
+ let hash = 0x811c9dc5;
+ for (let i = 0; i < token.length; i++) {
+ hash ^= token.charCodeAt(i);
+ hash = Math.imul(hash, 0x01000193);
+ }
+ return (hash >>> 0).toString(16).padStart(8, "0");
+}