mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-14 18:41:21 +00:00
187 lines
5.1 KiB
TypeScript
187 lines
5.1 KiB
TypeScript
import { useMutation, useQueryClient } from "@tanstack/react-query";
|
|
|
|
import {
|
|
decryptAssymmetric,
|
|
encryptAssymmetric
|
|
} from "@app/components/utilities/cryptography/crypto";
|
|
import { apiRequest } from "@app/config/request";
|
|
|
|
import { workspaceKeys } from "../workspace";
|
|
import { userKeys } from "./query-keys";
|
|
import { AddUserToWsDTOE2EE, AddUserToWsDTONonE2EE } from "./types";
|
|
|
|
export const useAddUserToWsE2EE = () => {
|
|
const queryClient = useQueryClient();
|
|
|
|
return useMutation<object, object, AddUserToWsDTOE2EE>({
|
|
mutationFn: async ({ workspaceId, members, decryptKey, userPrivateKey }) => {
|
|
// assymmetrically decrypt symmetric key with local private key
|
|
const key = decryptAssymmetric({
|
|
ciphertext: decryptKey.encryptedKey,
|
|
nonce: decryptKey.nonce,
|
|
publicKey: decryptKey.sender.publicKey,
|
|
privateKey: userPrivateKey
|
|
});
|
|
|
|
const newWsMembers = members.map(({ orgMembershipId, userPublicKey }) => {
|
|
const { ciphertext: inviteeCipherText, nonce: inviteeNonce } = encryptAssymmetric({
|
|
plaintext: key,
|
|
publicKey: userPublicKey,
|
|
privateKey: userPrivateKey
|
|
});
|
|
|
|
return {
|
|
orgMembershipId,
|
|
workspaceEncryptedKey: inviteeCipherText,
|
|
workspaceEncryptedNonce: inviteeNonce
|
|
};
|
|
});
|
|
const { data } = await apiRequest.post(`/api/v1/workspace/${workspaceId}/memberships`, {
|
|
members: newWsMembers
|
|
});
|
|
return data;
|
|
},
|
|
onSuccess: (_, { workspaceId }) => {
|
|
queryClient.invalidateQueries({ queryKey: workspaceKeys.getWorkspaceUsers(workspaceId) });
|
|
}
|
|
});
|
|
};
|
|
|
|
export const useAddUserToWsNonE2EE = () => {
|
|
const queryClient = useQueryClient();
|
|
|
|
return useMutation<object, object, AddUserToWsDTONonE2EE>({
|
|
mutationFn: async ({ projectId, usernames, roleSlugs }) => {
|
|
const { data } = await apiRequest.post(`/api/v2/workspace/${projectId}/memberships`, {
|
|
usernames,
|
|
roleSlugs
|
|
});
|
|
return data;
|
|
},
|
|
onSuccess: (_, { orgId, projectId }) => {
|
|
queryClient.invalidateQueries({ queryKey: workspaceKeys.getWorkspaceUsers(projectId) });
|
|
queryClient.invalidateQueries({
|
|
queryKey: userKeys.allOrgMembershipProjectMemberships(orgId)
|
|
});
|
|
}
|
|
});
|
|
};
|
|
|
|
export const sendEmailVerificationCode = async (username: string) => {
|
|
return apiRequest.post("/api/v2/users/me/emails/code", {
|
|
username
|
|
});
|
|
};
|
|
|
|
export const useSendEmailVerificationCode = () => {
|
|
return useMutation({
|
|
mutationFn: async (username: string) => {
|
|
await sendEmailVerificationCode(username);
|
|
return {};
|
|
}
|
|
});
|
|
};
|
|
|
|
export const useVerifyEmailVerificationCode = () => {
|
|
return useMutation({
|
|
mutationFn: async ({ username, code }: { username: string; code: string }) => {
|
|
await apiRequest.post("/api/v2/users/me/emails/verify", {
|
|
username,
|
|
code
|
|
});
|
|
return {};
|
|
}
|
|
});
|
|
};
|
|
|
|
export const useUpdateUserProjectFavorites = () => {
|
|
const queryClient = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: async ({
|
|
orgId,
|
|
projectFavorites
|
|
}: {
|
|
orgId: string;
|
|
projectFavorites: string[];
|
|
}) => {
|
|
await apiRequest.put("/api/v1/user/me/project-favorites", {
|
|
orgId,
|
|
projectFavorites
|
|
});
|
|
|
|
return {};
|
|
},
|
|
onSuccess: (_, { orgId }) => {
|
|
queryClient.invalidateQueries({ queryKey: userKeys.userProjectFavorites(orgId) });
|
|
}
|
|
});
|
|
};
|
|
|
|
export const useVerifyUserTotpRegistration = () => {
|
|
return useMutation({
|
|
mutationFn: async ({ totp }: { totp: string }) => {
|
|
await apiRequest.post("/api/v1/user/me/totp/verify", {
|
|
totp
|
|
});
|
|
|
|
return {};
|
|
}
|
|
});
|
|
};
|
|
|
|
export const useDeleteUserTotpConfiguration = () => {
|
|
const queryClient = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: async () => {
|
|
await apiRequest.delete("/api/v1/user/me/totp");
|
|
|
|
return {};
|
|
},
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: userKeys.totpConfiguration });
|
|
}
|
|
});
|
|
};
|
|
|
|
export const useCreateNewTotpRecoveryCodes = () => {
|
|
const queryClient = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: async () => {
|
|
await apiRequest.post("/api/v1/user/me/totp/recovery-codes");
|
|
|
|
return {};
|
|
},
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: userKeys.totpConfiguration });
|
|
}
|
|
});
|
|
};
|
|
|
|
export const useResendOrgMemberInvitation = () => {
|
|
return useMutation({
|
|
mutationFn: async (dto: { membershipId: string }) => {
|
|
const { data } = await apiRequest.post<{
|
|
signupToken?: {
|
|
email: string;
|
|
link: string;
|
|
};
|
|
}>("/api/v1/invite-org/signup-resend", dto);
|
|
|
|
return data.signupToken;
|
|
}
|
|
});
|
|
};
|
|
|
|
export const useRevokeMySessionById = () => {
|
|
const queryClient = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: async (sessionId: string) => {
|
|
const { data } = await apiRequest.delete(`/api/v2/users/me/sessions/${sessionId}`);
|
|
return data;
|
|
},
|
|
onSuccess() {
|
|
queryClient.invalidateQueries({ queryKey: userKeys.mySessions });
|
|
}
|
|
});
|
|
};
|