Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

feat: simplified ui for password based secret sharing

=3ddb4cd27a8c
diff --git a/frontend/src/hooks/api/secretSharing/queries.ts b/frontend/src/hooks/api/secretSharing/queries.ts
index d94ca243d1..c349d39f56 100644
--- a/frontend/src/hooks/api/secretSharing/queries.ts
+++ b/frontend/src/hooks/api/secretSharing/queries.ts
@@ -7,7 +7,11 @@ import { TSharedSecret, TViewSharedSecretResponse } from "./types";
export const secretSharingKeys = {
allSharedSecrets: () => ["sharedSecrets"] as const,
specificSharedSecrets: ({ offset, limit }: { offset: number; limit: number }) =>
- [...secretSharingKeys.allSharedSecrets(), { offset, limit }] as const
+ [...secretSharingKeys.allSharedSecrets(), { offset, limit }] as const,
+ getSecretById: (arg: { id: string; hashedHex: string; password?: string }) => [
+ "shared-secret",
+ arg
+ ]
};
export const useGetSharedSecrets = ({
@@ -38,51 +42,28 @@ export const useGetSharedSecrets = ({
export const useGetActiveSharedSecretById = ({
sharedSecretId,
- hashedHex
+ hashedHex,
+ password
}: {
sharedSecretId: string;
hashedHex: string;
+ password?: string;
}) => {
- return useQuery<TViewSharedSecretResponse | null>(
- [`sharedSecret-${sharedSecretId}`],
+ return useQuery<TViewSharedSecretResponse>(
+ secretSharingKeys.getSecretById({ id: sharedSecretId, hashedHex, password }),
async () => {
- const params = new URLSearchParams({ hashedHex });
- const { data } = await apiRequest.get<TViewSharedSecretResponse>(
+ const { data } = await apiRequest.post<TViewSharedSecretResponse>(
`/api/v1/secret-sharing/public/${sharedSecretId}`,
{
- params
+ hashedHex,
+ password
}
);
-
- if (!data) return null
-
- return {
- encryptedValue: data.encryptedValue,
- iv: data.iv,
- tag: data.tag,
- accessType: data.accessType,
- orgName: data.orgName
- };
+
+ return data;
},
{
enabled: Boolean(sharedSecretId) && Boolean(hashedHex)
}
);
};
-
-// returns a secret (secret or undefined if password doesn't match)
-export const fetchSecretIfPasswordIsValid = async (
- sharedSecretId: string,
- hashedHex: string,
- password: string,
-) => {
- const { data } = await apiRequest.post<TViewSharedSecretResponse>(
- `/api/v1/secret-sharing/public/${sharedSecretId}/validate`,
- {
- hashedHex,
- password
- }
- );
-
- return data;
-};
diff --git a/frontend/src/hooks/api/secretSharing/types.ts b/frontend/src/hooks/api/secretSharing/types.ts
index 708780fe4c..0dd4a95555 100644
--- a/frontend/src/hooks/api/secretSharing/types.ts
+++ b/frontend/src/hooks/api/secretSharing/types.ts
@@ -26,11 +26,14 @@ export type TCreateSharedSecretRequest = {
};
export type TViewSharedSecretResponse = {
- encryptedValue: string;
- iv: string;
- tag: string;
- accessType: SecretSharingAccessType;
- orgName?: string;
+ isPasswordProtected: boolean;
+ secret: {
+ encryptedValue: string;
+ iv: string;
+ tag: string;
+ accessType: SecretSharingAccessType;
+ orgName?: string;
+ };
};
export type TDeleteSharedSecretRequest = {
@@ -40,4 +43,5 @@ export type TDeleteSharedSecretRequest = {
export enum SecretSharingAccessType {
Anyone = "anyone",
Organization = "organization"
-}
\ No newline at end of file
+}
+
diff --git a/frontend/src/views/ShareSecretPublicPage/components/ShareSecretForm.tsx b/frontend/src/views/ShareSecretPublicPage/components/ShareSecretForm.tsx
index 1a850c07e2..ea39e12651 100644
--- a/frontend/src/views/ShareSecretPublicPage/components/ShareSecretForm.tsx
+++ b/frontend/src/views/ShareSecretPublicPage/components/ShareSecretForm.tsx
@@ -33,7 +33,7 @@ const viewLimitOptions = [
const schema = z.object({
name: z.string().optional(),
password: z.string().optional(),
- secret: z.string(),
+ secret: z.string().min(1),
expiresIn: z.string(),
viewLimit: z.string(),
accessType: z.nativeEnum(SecretSharingAccessType).optional()
@@ -68,7 +68,14 @@ export const ShareSecretForm = ({ isPublic, value }: Props) => {
}
});
- const onFormSubmit = async ({ name, password, secret, expiresIn, viewLimit, accessType }: FormData) => {
+ const onFormSubmit = async ({
+ name,
+ password,
+ secret,
+ expiresIn,
+ viewLimit,
+ accessType
+ }: FormData) => {
try {
const expiresAt = new Date(new Date().getTime() + Number(expiresIn));
@@ -159,7 +166,7 @@ export const ShareSecretForm = ({ isPublic, value }: Props) => {
label="Password"
isError={Boolean(error)}
errorText={error?.message}
- isOptional={true}
+ isOptional
>
<Input {...field} placeholder="Password" type="password" />
</FormControl>
diff --git a/frontend/src/views/ViewSecretPublicPage/ViewSecretPublicPage.tsx b/frontend/src/views/ViewSecretPublicPage/ViewSecretPublicPage.tsx
index 78ce15d14d..999f2d3423 100644
--- a/frontend/src/views/ViewSecretPublicPage/ViewSecretPublicPage.tsx
+++ b/frontend/src/views/ViewSecretPublicPage/ViewSecretPublicPage.tsx
@@ -1,35 +1,42 @@
-import { useState, useCallback, useEffect } from 'react'
+import { useState } from "react";
import Image from "next/image";
import Link from "next/link";
import { useRouter } from "next/router";
import { faArrowRight } from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
+import { AxiosError } from "axios";
-import { TViewSharedSecretResponse, useGetActiveSharedSecretById } from "@app/hooks/api/secretSharing";
+import { useGetActiveSharedSecretById } from "@app/hooks/api/secretSharing";
-import { SecretContainer, SecretErrorContainer, PasswordContainer } from "./components";
+import { PasswordContainer,SecretContainer, SecretErrorContainer } from "./components";
export const ViewSecretPublicPage = () => {
- const [secret, setSecret] = useState<TViewSharedSecretResponse | null>(null);
const router = useRouter();
+ const [password, setPassword] = useState<string>();
const { id, key: urlEncodedPublicKey } = router.query;
const [hashedHex, key] = urlEncodedPublicKey
? urlEncodedPublicKey.toString().split("-")
: ["", ""];
- const { data: fetchSecret, error, isLoading } = useGetActiveSharedSecretById({
+ const {
+ data: fetchSecret,
+ error,
+ isLoading,
+ isFetching
+ } = useGetActiveSharedSecretById({
sharedSecretId: id as string,
- hashedHex
+ hashedHex,
+ password
});
- useEffect(() => {
- if (fetchSecret) setSecret(fetchSecret)
- }, [fetchSecret, error])
+ const isInvalidCredential =
+ ((error as AxiosError)?.response?.data as { message: string })?.message ===
+ "Invalid credentials";
- const handleSecret = useCallback((value: TViewSharedSecretResponse) => {
- setSecret(value)
- }, [setSecret])
+ const shouldShowPasswordPrompt =
+ isInvalidCredential || (fetchSecret?.isPasswordProtected && !fetchSecret.secret);
+ const isValidatingPassword = Boolean(password) && isFetching;
return (
<div className="flex h-screen flex-col justify-between overflow-auto bg-gradient-to-tr from-mineshaft-700 to-bunker-800 text-gray-200 dark:[color-scheme:dark]">
@@ -62,17 +69,21 @@ export const ViewSecretPublicPage = () => {
</a>
</p>
</div>
+ {(shouldShowPasswordPrompt || isValidatingPassword) && (
+ <PasswordContainer
+ isSubmitting={isValidatingPassword}
+ onPasswordSubmit={(el) => {
+ setPassword(el);
+ }}
+ isInvalidCredential={!isFetching && isInvalidCredential}
+ />
+ )}
{!isLoading && (
<>
- {!error && !secret && (
- <PasswordContainer
- secretId={id as string}
- hashedHex={hashedHex}
- handleSecret={handleSecret}
- />
+ {!error && fetchSecret?.secret && key && (
+ <SecretContainer secret={fetchSecret.secret} secretKey={key} />
)}
- {!error && secret && key && <SecretContainer secret={secret} secretKey={key} />}
- {error && <SecretErrorContainer />}
+ {error && !isInvalidCredential && <SecretErrorContainer />}
</>
)}
<div className="m-auto my-8 flex w-full">
diff --git a/frontend/src/views/ViewSecretPublicPage/components/PasswordContainer.tsx b/frontend/src/views/ViewSecretPublicPage/components/PasswordContainer.tsx
index 00876225d1..edf60447b0 100644
--- a/frontend/src/views/ViewSecretPublicPage/components/PasswordContainer.tsx
+++ b/frontend/src/views/ViewSecretPublicPage/components/PasswordContainer.tsx
@@ -1,60 +1,34 @@
-import { z } from "zod";
import { Controller, useForm } from "react-hook-form";
-
import { faArrowRight, faSpinner } from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { zodResolver } from "@hookform/resolvers/zod";
+import { z } from "zod";
import { Button, FormControl, IconButton, Input } from "@app/components/v2";
-import { fetchSecretIfPasswordIsValid, TViewSharedSecretResponse } from "@app/hooks/api/secretSharing";
-import { createNotification } from "@app/components/notifications";
type Props = {
- secretId: string;
- hashedHex: string;
- handleSecret: (val: any) => void;
+ onPasswordSubmit: (val: any) => void;
+ isSubmitting?: boolean;
+ isInvalidCredential?: boolean;
};
const formSchema = z.object({
password: z.string()
-})
+});
export type FormData = z.infer<typeof formSchema>;
-export const PasswordContainer = ({ secretId, hashedHex, handleSecret }: Props) => {
- const {
- control,
- reset,
- handleSubmit,
- formState: { isSubmitting }
- } = useForm<FormData>({
- resolver: zodResolver(formSchema),
+export const PasswordContainer = ({
+ onPasswordSubmit,
+ isSubmitting,
+ isInvalidCredential
+}: Props) => {
+ const { control, handleSubmit } = useForm<FormData>({
+ resolver: zodResolver(formSchema)
});
const onFormSubmit = async ({ password }: FormData) => {
- try {
- const secret: TViewSharedSecretResponse = await fetchSecretIfPasswordIsValid(
- secretId,
- hashedHex,
- password,
- )
-
- if (secret) {
- handleSecret(secret);
- } else {
- reset({ password: "" });
- createNotification({
- text: "Password is Invalid. Try again",
- type: "error"
- })
- }
- } catch (error) {
- console.error("Failed to validate password:", error);
- createNotification({
- text: "Failed to validate password",
- type: "error"
- })
- }
+ onPasswordSubmit(password);
};
return (
@@ -63,15 +37,16 @@ export const PasswordContainer = ({ secretId, hashedHex, handleSecret }: Props)
<Controller
control={control}
name="password"
+ defaultValue=""
render={({ field, fieldState: { error } }) => (
<FormControl
- isError={Boolean(error)}
- errorText={error?.message}
+ isError={Boolean(error) || isInvalidCredential}
+ errorText={isInvalidCredential ? "Invalid credential" : error?.message}
isRequired
label="Password"
>
- <div className="flex items-center gap-2 justify-between rounded-md">
- <Input {...field} placeholder="Enter Password to view secret" type="password"></Input>
+ <div className="flex items-center justify-between gap-2 rounded-md">
+ <Input {...field} placeholder="Enter Password to view secret" type="password" />
<div className="flex">
<IconButton
ariaLabel="copy icon"
@@ -79,9 +54,9 @@ export const PasswordContainer = ({ secretId, hashedHex, handleSecret }: Props)
className="group relative"
onClick={handleSubmit(onFormSubmit)}
>
- <FontAwesomeIcon
- className={isSubmitting ? 'fa-spin' : ''}
- icon={isSubmitting ? faSpinner : faArrowRight}
+ <FontAwesomeIcon
+ className={isSubmitting ? "fa-spin" : ""}
+ icon={isSubmitting ? faSpinner : faArrowRight}
/>
</IconButton>
</div>
diff --git a/frontend/src/views/ViewSecretPublicPage/components/SecretContainer.tsx b/frontend/src/views/ViewSecretPublicPage/components/SecretContainer.tsx
index 58c6c57c25..a920c9d933 100644
--- a/frontend/src/views/ViewSecretPublicPage/components/SecretContainer.tsx
+++ b/frontend/src/views/ViewSecretPublicPage/components/SecretContainer.tsx
@@ -14,7 +14,7 @@ import { useTimedReset, useToggle } from "@app/hooks";
import { TViewSharedSecretResponse } from "@app/hooks/api/secretSharing";
type Props = {
- secret: TViewSharedSecretResponse;
+ secret: TViewSharedSecretResponse["secret"];
secretKey: string;
};
diff --git a/frontend/src/views/ViewSecretPublicPage/components/index.tsx b/frontend/src/views/ViewSecretPublicPage/components/index.tsx
index fd66da812a..8ba8b27c0a 100644
--- a/frontend/src/views/ViewSecretPublicPage/components/index.tsx
+++ b/frontend/src/views/ViewSecretPublicPage/components/index.tsx
@@ -1,3 +1,3 @@
+export { PasswordContainer } from "./PasswordContainer";
export { SecretContainer } from "./SecretContainer";
export { SecretErrorContainer } from "./SecretErrorContainer";
-export { PasswordContainer } from "./PasswordContainer";