Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.
TypeScript acmeagentsai-agentscertificate-managementdevsecopsenvironment-variableskubernetesopen-sourcepampkipostgresprivate-caprivileged-access-managementsecret-managementsecret-managersecretssecrets-managementsecuritysecurity-toolsvault
import { MutationCache, QueryClient } from "@tanstack/react-query";
import axios from "axios";
import { createNotification, dismissNotification } from "@app/components/notifications";
// akhilmhdh: doing individual imports to avoid cyclic import error
import { Badge } from "@app/components/v3/generic/Badge";
import { Button } from "@app/components/v3/generic/Button";
import {
Dialog,
DialogContent,
DialogDescription,
DialogHeader,
DialogTitle,
DialogTrigger
} from "@app/components/v3/generic/Dialog";
import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow
} from "@app/components/v3/generic/Table";
import {
formatedConditionsOperatorNames,
PermissionConditionOperators
} from "@app/context/ProjectPermissionContext/types";
import { formatValidationErrorPath } from "@app/lib/fn/permission";
import { camelCaseToSpaces } from "@app/lib/fn/string";
import { ApiErrorTypes, TApiErrors } from "./types";
// this is saved in react-query cache
export const SIGNUP_TEMP_TOKEN_CACHE_KEY = ["infisical__signup-temp-token"];
export const MFA_TEMP_TOKEN_CACHE_KEY = ["infisical__mfa-temp-token"];
export const AUTH_TOKEN_CACHE_KEY = ["infisical__auth-token"];
function ValidationErrorModal({
serverResponse,
requestBody,
toastIdRef
}: {
serverResponse: TApiErrors;
requestBody?: Record<string, unknown> | null;
toastIdRef: { current: string | number | undefined };
}) {
if (serverResponse.error !== ApiErrorTypes.ValidationError) {
return null;
}
return (
<Dialog
defaultOpen
onOpenChange={(open) => {
// The modal auto-opens inside the toast, whose overlay keeps sonner's dismiss timer
// paused while open. Dismiss the toast on close so it does not linger afterwards.
if (!open && toastIdRef.current !== undefined) {
dismissNotification(toastIdRef.current);
}
}}
>
<DialogContent className="sm:max-w-2xl">
<DialogHeader>
<DialogTitle>Validation Error Details</DialogTitle>
<DialogDescription>These fields did not pass validation.</DialogDescription>
</DialogHeader>
<Table>
<TableHeader>
<TableRow>
<TableHead>Field</TableHead>
<TableHead>Issue</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{serverResponse.message?.map(({ message, path }) => (
<TableRow key={path.join(".")}>
<TableCell className="whitespace-normal">
{formatValidationErrorPath(path, requestBody)}
</TableCell>
<TableCell className="whitespace-normal">{message.toLowerCase()}</TableCell>
</TableRow>
))}
</TableBody>
</Table>
</DialogContent>
</Dialog>
);
}
type TPermissionConditions = Record<
string,
string | { [K in PermissionConditionOperators]: string | string[] }
>;
function PermissionConditionsList({
conditions,
keyPrefix
}: {
conditions?: unknown;
keyPrefix: string;
}) {
const fields = Object.keys((conditions as TPermissionConditions) || {});
if (!fields.length) return null;
return (
<ul className="mt-2 flex list-disc flex-col gap-1.5 pl-5 marker:text-muted">
{fields.flatMap((field, fieldIndex) => {
const operators = (conditions as TPermissionConditions)[field];
const formattedFieldName = camelCaseToSpaces(field).toLowerCase();
if (typeof operators === "string") {
return (
<li key={`${keyPrefix}-${fieldIndex + 1}`}>
<span className="font-medium capitalize">{formattedFieldName}</span>{" "}
<span className="text-muted">equal to</span>{" "}
<Badge variant="neutral">{operators}</Badge>
</li>
);
}
return Object.keys(operators).map((operator, operatorIndex) => (
<li key={`${keyPrefix}-${fieldIndex + 1}-${operatorIndex + 1}`}>
<span className="font-medium capitalize">{formattedFieldName}</span>{" "}
<span className="text-muted">
{formatedConditionsOperatorNames[operator as PermissionConditionOperators]}
</span>{" "}
<Badge variant="neutral">
{operators[operator as PermissionConditionOperators].toString()}
</Badge>
</li>
));
})}
</ul>
);
}
export const onRequestError = (
error: unknown,
_variables?: unknown,
_context?: unknown,
mutation?: { meta?: Record<string, unknown> }
) => {
// For a mutation whose caller shows every failure in place, where a toast would repeat it.
if (mutation?.meta?.skipErrorToast) return;
if (axios.isAxiosError(error)) {
const serverResponse = error.response?.data as TApiErrors;
const handledErrorCodes = mutation?.meta?.handledErrorCodes;
if (
typeof serverResponse?.error === "string" &&
Array.isArray(handledErrorCodes) &&
handledErrorCodes.includes(serverResponse.error)
) {
return;
}
if (serverResponse?.error === ApiErrorTypes.ValidationError) {
if (mutation?.meta?.skipValidationToast) {
return;
}
let requestBody: Record<string, unknown> | undefined;
try {
const configData = error.config?.data;
requestBody =
typeof configData === "string"
? JSON.parse(configData)
: (configData as Record<string, unknown>);
} catch {
requestBody = undefined;
}
const toastIdRef: { current: string | number | undefined } = { current: undefined };
toastIdRef.current = createNotification({
title: "Validation Error",
type: "error",
text: "Please check the input and try again.",
callToAction: (
<ValidationErrorModal
serverResponse={serverResponse}
requestBody={requestBody}
toastIdRef={toastIdRef}
/>
),
copyActions: [
{
value: serverResponse.reqId,
name: "Request ID",
label: `Request ID: ${serverResponse.reqId}`
}
]
});
return;
}
if (serverResponse?.error === ApiErrorTypes.ForbiddenError) {
const toastIdRef: { current: string | number | undefined } = { current: undefined };
toastIdRef.current = createNotification({
title: "Forbidden Access",
type: "error",
text: `${serverResponse.message}.`,
callToAction: serverResponse?.details?.length ? (
<Dialog
onOpenChange={(open) => {
// The dialog renders inside the toast; its overlay pins sonner's dismiss timer
// while open. Dismiss the toast on close so it does not linger afterwards.
if (!open && toastIdRef.current !== undefined) {
dismissNotification(toastIdRef.current);
}
}}
>
<DialogTrigger asChild>
<Button variant="outline" size="xs">
Show more
</Button>
</DialogTrigger>
<DialogContent className="sm:max-w-2xl">
<DialogHeader>
<DialogTitle>Validation Rules</DialogTitle>
<DialogDescription>Please review the allowed rules below.</DialogDescription>
</DialogHeader>
<div className="flex flex-col gap-2">
{serverResponse.details?.map((el, index) => {
const hasConditions = Boolean(Object.keys(el.conditions || {}).length);
const actions = Array.isArray(el.action) ? el.action : [el.action];
return (
<div
key={`Forbidden-error-details-${index + 1}`}
className="rounded-md border border-border bg-card p-4 text-sm"
>
<div className="flex flex-wrap items-center gap-x-1.5 gap-y-1">
<span>{el.inverted ? "Cannot" : "Can"}</span>
{actions.map((action, actionIndex) => (
<Badge
key={`Forbidden-error-details-${index + 1}-action-${actionIndex + 1}`}
variant={el.inverted ? "danger" : "success"}
>
{action.toString()}
</Badge>
))}
<span>{el.subject.toString()}</span>
{hasConditions && <span className="text-muted">with conditions:</span>}
</div>
<PermissionConditionsList
conditions={el.conditions}
keyPrefix={`Forbidden-error-details-${index + 1}`}
/>
</div>
);
})}
</div>
</DialogContent>
</Dialog>
) : undefined,
copyActions: [
{
value: serverResponse.reqId,
name: "Request ID",
label: `Request ID: ${serverResponse.reqId}`
}
]
});
return;
}
if (serverResponse?.error === ApiErrorTypes.PermissionBoundaryError) {
const missingPermissions = serverResponse.details?.missingPermissions ?? [];
const toastIdRef: { current: string | number | undefined } = { current: undefined };
toastIdRef.current = createNotification({
title: "Permission Denied",
type: "error",
text: `${serverResponse.message}${serverResponse.message.endsWith(".") ? "" : "."}`,
callToAction: missingPermissions.length ? (
<Dialog
onOpenChange={(open) => {
if (!open && toastIdRef.current !== undefined) {
dismissNotification(toastIdRef.current);
}
}}
>
<DialogTrigger asChild>
<Button variant="outline" size="xs">
Show more
</Button>
</DialogTrigger>
<DialogContent className="sm:max-w-2xl">
<DialogHeader>
<DialogTitle>Missing Permissions</DialogTitle>
<DialogDescription>
Your role needs the permissions below to perform this action.
</DialogDescription>
</DialogHeader>
<div className="flex flex-col gap-2">
{missingPermissions.map((el, index) => (
<div
key={`Permission-boundary-details-${index + 1}`}
className="rounded-md border border-border bg-card p-4 text-sm"
>
<div className="flex flex-wrap items-center gap-x-1.5 gap-y-1">
<span>Can</span>
<Badge variant="danger">{el.action}</Badge>
<span>{el.subject}</span>
{Boolean(Object.keys(el.conditions || {}).length) && (
<span className="text-muted">with conditions:</span>
)}
</div>
<PermissionConditionsList
conditions={el.conditions}
keyPrefix={`Permission-boundary-details-${index + 1}`}
/>
</div>
))}
</div>
</DialogContent>
</Dialog>
) : undefined,
copyActions: [
{
value: serverResponse.reqId,
name: "Request ID",
label: `Request ID: ${serverResponse.reqId}`
}
]
});
return;
}
const errorMessage =
serverResponse?.message || "An unexpected error occurred. Please try again.";
createNotification({
title: "Bad Request",
type: "error",
text: `${errorMessage}${errorMessage.endsWith(".") ? "" : "."}`,
copyActions: serverResponse?.reqId
? [
{
value: serverResponse.reqId,
name: "Request ID",
label: `Request ID: ${serverResponse.reqId}`
}
]
: undefined
});
}
};
export const queryClient = new QueryClient({
mutationCache: new MutationCache({
onError: onRequestError
}),
defaultOptions: {
queries: {
refetchOnWindowFocus: false,
retry: 1,
staleTime: 60_000 // 60s — prevents refetch on every component mount/remount
}
}
});
// memory token storage will be moved to apiRequest module until securityclient is completely depreciated
// then all the getters will be also hidden scoped to apiRequest only
const MemoryTokenStorage = () => {
let authToken: string;
return {
setToken: (token: string) => {
authToken = token;
},
getToken: () => authToken
};
};
const signUpTempTokenStorage = MemoryTokenStorage();
const mfaAuthTokenStorage = MemoryTokenStorage();
const authTokenStorage = MemoryTokenStorage();
// set token in memory cache
export const setSignupTempToken = signUpTempTokenStorage.setToken;
export const setMfaTempToken = mfaAuthTokenStorage.setToken;
export const setAuthToken = authTokenStorage.setToken;
export const getSignupTempToken = signUpTempTokenStorage.getToken;
export const getMfaTempToken = mfaAuthTokenStorage.getToken;
export const getAuthToken = authTokenStorage.getToken;
export const isLoggedIn = () => Boolean(getAuthToken());