mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-17 06:34:57 +00:00
164 lines
5.7 KiB
TypeScript
164 lines
5.7 KiB
TypeScript
import { useMutation, useQueryClient } from "@tanstack/react-query";
|
|
|
|
import { apiRequest } from "@app/config/request";
|
|
|
|
import { organizationKeys } from "../organization/queries";
|
|
import { identitiesKeys } from "./queries";
|
|
import {
|
|
AddIdentityUniversalAuthDTO,
|
|
ClientSecretData,
|
|
CreateIdentityDTO,
|
|
CreateIdentityUniversalAuthClientSecretDTO,
|
|
CreateIdentityUniversalAuthClientSecretRes,
|
|
DeleteIdentityDTO,
|
|
DeleteIdentityUniversalAuthClientSecretDTO,
|
|
Identity,
|
|
IdentityUniversalAuth,
|
|
UpdateIdentityDTO,
|
|
UpdateIdentityUniversalAuthDTO} from "./types";
|
|
|
|
export const useCreateIdentity = () => {
|
|
const queryClient = useQueryClient();
|
|
return useMutation<Identity, {}, CreateIdentityDTO>({
|
|
mutationFn: async (body) => {
|
|
const { data: { identity } } = await apiRequest.post("/api/v1/identities/", body);
|
|
return identity;
|
|
},
|
|
onSuccess: (_, { organizationId }) => {
|
|
queryClient.invalidateQueries(organizationKeys.getOrgIdentityMemberships(organizationId));
|
|
}
|
|
});
|
|
};
|
|
|
|
export const useUpdateIdentity = () => {
|
|
const queryClient = useQueryClient();
|
|
return useMutation<Identity, {}, UpdateIdentityDTO>({
|
|
mutationFn: async ({
|
|
identityId,
|
|
name,
|
|
role
|
|
}) => {
|
|
|
|
const { data: { identity } } = await apiRequest.patch(`/api/v1/identities/${identityId}`, {
|
|
name,
|
|
role
|
|
});
|
|
|
|
return identity;
|
|
},
|
|
onSuccess: (_, { organizationId }) => {
|
|
queryClient.invalidateQueries(organizationKeys.getOrgIdentityMemberships(organizationId));
|
|
}
|
|
});
|
|
}
|
|
|
|
export const useDeleteIdentity = () => {
|
|
const queryClient = useQueryClient();
|
|
return useMutation<Identity, {}, DeleteIdentityDTO>({
|
|
mutationFn: async ({
|
|
identityId,
|
|
}) => {
|
|
const { data: { identity } } = await apiRequest.delete(`/api/v1/identities/${identityId}`);
|
|
return identity;
|
|
},
|
|
onSuccess: (_, { organizationId }) => {
|
|
queryClient.invalidateQueries(organizationKeys.getOrgIdentityMemberships(organizationId));
|
|
}
|
|
});
|
|
};
|
|
|
|
// TODO: move these to /auth
|
|
|
|
export const useAddIdentityUniversalAuth = () => {
|
|
const queryClient = useQueryClient();
|
|
return useMutation<IdentityUniversalAuth, {}, AddIdentityUniversalAuthDTO>({
|
|
mutationFn: async ({
|
|
identityId,
|
|
clientSecretTrustedIps,
|
|
accessTokenTTL,
|
|
accessTokenMaxTTL,
|
|
accessTokenNumUsesLimit,
|
|
accessTokenTrustedIps,
|
|
}) => {
|
|
const { data: { identityUniversalAuth } } = await apiRequest.post(`/api/v1/auth/universal-auth/identities/${identityId}`,
|
|
{
|
|
clientSecretTrustedIps,
|
|
accessTokenTTL,
|
|
accessTokenMaxTTL,
|
|
accessTokenNumUsesLimit,
|
|
accessTokenTrustedIps,
|
|
}
|
|
);
|
|
return identityUniversalAuth;
|
|
},
|
|
onSuccess: (_, { organizationId }) => {
|
|
queryClient.invalidateQueries(organizationKeys.getOrgIdentityMemberships(organizationId));
|
|
}
|
|
});
|
|
};
|
|
|
|
export const useUpdateIdentityUniversalAuth = () => {
|
|
const queryClient = useQueryClient();
|
|
return useMutation<IdentityUniversalAuth, {}, UpdateIdentityUniversalAuthDTO>({
|
|
mutationFn: async ({
|
|
identityId,
|
|
clientSecretTrustedIps,
|
|
accessTokenTTL,
|
|
accessTokenMaxTTL,
|
|
accessTokenNumUsesLimit,
|
|
accessTokenTrustedIps,
|
|
}) => {
|
|
const { data: { identityUniversalAuth } } = await apiRequest.patch(`/api/v1/auth/universal-auth/identities/${identityId}`,
|
|
{
|
|
clientSecretTrustedIps,
|
|
accessTokenTTL,
|
|
accessTokenMaxTTL,
|
|
accessTokenNumUsesLimit,
|
|
accessTokenTrustedIps,
|
|
}
|
|
);
|
|
return identityUniversalAuth;
|
|
},
|
|
onSuccess: (_, { organizationId }) => {
|
|
queryClient.invalidateQueries(organizationKeys.getOrgIdentityMemberships(organizationId));
|
|
}
|
|
});
|
|
};
|
|
|
|
export const useCreateIdentityUniversalAuthClientSecret = () => {
|
|
const queryClient = useQueryClient();
|
|
return useMutation<CreateIdentityUniversalAuthClientSecretRes, {}, CreateIdentityUniversalAuthClientSecretDTO>({
|
|
mutationFn: async ({
|
|
identityId,
|
|
description,
|
|
ttl,
|
|
numUsesLimit
|
|
}) => {
|
|
const { data } = await apiRequest.post(`/api/v1/auth/universal-auth/identities/${identityId}/client-secrets`, {
|
|
description,
|
|
ttl,
|
|
numUsesLimit
|
|
});
|
|
return data;
|
|
},
|
|
onSuccess: (_, { identityId }) => {
|
|
queryClient.invalidateQueries(identitiesKeys.getIdentityUniversalAuthClientSecrets(identityId));
|
|
}
|
|
});
|
|
};
|
|
|
|
export const useRevokeIdentityUniversalAuthClientSecret = () => {
|
|
const queryClient = useQueryClient();
|
|
return useMutation<ClientSecretData, {}, DeleteIdentityUniversalAuthClientSecretDTO>({
|
|
mutationFn: async ({
|
|
identityId,
|
|
clientSecretId
|
|
}) => {
|
|
const { data: { clientSecretData } } = await apiRequest.post<{ clientSecretData: ClientSecretData }>(`/api/v1/auth/universal-auth/identities/${identityId}/client-secrets/${clientSecretId}/revoke`);
|
|
return clientSecretData;
|
|
},
|
|
onSuccess: (_, { identityId }) => {
|
|
queryClient.invalidateQueries(identitiesKeys.getIdentityUniversalAuthClientSecrets(identityId));
|
|
}
|
|
});
|
|
}; |