MCPcopy Create free account
hub / github.com/Cosmian/kms / fetchAuthMethods

Function fetchAuthMethods

ui/src/utils/utils.ts:88–115  ·  view source on GitHub ↗
(serverUrl: string)

Source from the content-addressed store, hash-verified

86export const shouldAutoLoginWithCert = (methods: AuthMethod[]): boolean => methods.length === 1 && methods[0] === "CERT";
87
88export const fetchAuthMethods = async (serverUrl: string): Promise<AuthMethod[] | undefined> => {
89 // Skip the fetch in dev mode to avoid unnecessary friction (no auth enforced).
90 if (import.meta.env.VITE_DEV_MODE === "true") {
91 return [];
92 }
93 try {
94 const kmsUrl = serverUrl + "/ui/auth_method";
95 const response = await fetch(kmsUrl, {
96 method: "GET",
97 credentials: "include",
98 });
99 if (!response.ok) throw new Error("Failed to fetch auth methods");
100
101 const data: { auth_method?: AuthMethod; auth_methods?: AuthMethod[] } = await response.json();
102 if (Array.isArray(data.auth_methods)) {
103 // Filter out the "None" sentinel: an empty array means no auth configured.
104 return data.auth_methods.filter((m): m is AuthMethod => m !== undefined && m !== "None");
105 }
106 // Backward-compatibility: older servers only return the singular field.
107 if (data.auth_method && data.auth_method !== "None") {
108 return [data.auth_method];
109 }
110 return [];
111 } catch (error) {
112 console.error(error);
113 return undefined;
114 }
115};
116
117/** Outcome of a `POST /ui/login_as` call — mirrors the KMS server's `AuthVerifierLoginResponse`. */
118type AuthVerifierLoginNextStep = "Authenticated" | "TotpRequired";

Callers 1

fetchUserFunction · 0.90

Calls 2

jsonMethod · 0.80
filterMethod · 0.45

Tested by

no test coverage detected