-
Notifications
You must be signed in to change notification settings - Fork 989
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
774371a
commit ba1fd8a
Showing
9 changed files
with
573 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { authenticator } from "otplib"; | ||
import { HashAlgorithms } from "otplib/core"; | ||
|
||
import { BadRequestError } from "@app/lib/errors"; | ||
import { alphaNumericNanoId } from "@app/lib/nanoid"; | ||
|
||
import { DynamicSecretTotpSchema, TDynamicProviderFns } from "./models"; | ||
|
||
export const TotpProvider = (): TDynamicProviderFns => { | ||
const validateProviderInputs = async (inputs: unknown) => { | ||
const providerInputs = await DynamicSecretTotpSchema.parseAsync(inputs); | ||
|
||
const urlObj = new URL(providerInputs.url); | ||
const secret = urlObj.searchParams.get("secret"); | ||
if (!secret) { | ||
throw new BadRequestError({ | ||
message: "TOTP secret is missing from URL" | ||
}); | ||
} | ||
|
||
return providerInputs; | ||
}; | ||
|
||
const validateConnection = async () => { | ||
return true; | ||
}; | ||
|
||
const create = async (inputs: unknown) => { | ||
const providerInputs = await validateProviderInputs(inputs); | ||
|
||
const entityId = alphaNumericNanoId(32); | ||
const authenticatorInstance = authenticator.clone(); | ||
|
||
const urlObj = new URL(providerInputs.url); | ||
const secret = urlObj.searchParams.get("secret") as string; | ||
const periodFromUrl = urlObj.searchParams.get("period"); | ||
const digitsFromUrl = urlObj.searchParams.get("digits"); | ||
const algorithm = urlObj.searchParams.get("algorithm"); | ||
|
||
authenticatorInstance.options = { | ||
digits: digitsFromUrl ? +digitsFromUrl : undefined, | ||
algorithm: algorithm ? (algorithm.toLowerCase() as HashAlgorithms) : undefined, | ||
step: periodFromUrl ? +periodFromUrl : undefined | ||
}; | ||
|
||
return { entityId, data: { TOTP: authenticatorInstance.generate(secret) } }; | ||
}; | ||
|
||
const revoke = async (inputs: unknown, entityId: string) => { | ||
return { entityId }; | ||
}; | ||
|
||
const renew = async (inputs: unknown, entityId: string) => { | ||
return { entityId }; | ||
}; | ||
|
||
return { | ||
validateProviderInputs, | ||
validateConnection, | ||
create, | ||
revoke, | ||
renew | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
197 changes: 197 additions & 0 deletions
197
...d/src/views/SecretMainPage/components/ActionBar/CreateDynamicSecretForm/TotpInputForm.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,197 @@ | ||
import { Controller, useForm } from "react-hook-form"; | ||
import Link from "next/link"; | ||
import { faArrowUpRightFromSquare, faBookOpen } from "@fortawesome/free-solid-svg-icons"; | ||
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; | ||
import { zodResolver } from "@hookform/resolvers/zod"; | ||
import ms from "ms"; | ||
import { z } from "zod"; | ||
|
||
import { TtlFormLabel } from "@app/components/features"; | ||
import { createNotification } from "@app/components/notifications"; | ||
import { Button, FormControl, Input } from "@app/components/v2"; | ||
import { useCreateDynamicSecret } from "@app/hooks/api"; | ||
import { DynamicSecretProviders } from "@app/hooks/api/dynamicSecret/types"; | ||
|
||
const formSchema = z.object({ | ||
provider: z.object({ | ||
url: z.string().trim().min(1) | ||
}), | ||
defaultTTL: z.string().superRefine((val, ctx) => { | ||
const valMs = ms(val); | ||
if (valMs < 60 * 1000) | ||
ctx.addIssue({ code: z.ZodIssueCode.custom, message: "TTL must be a greater than 1min" }); | ||
// a day | ||
if (valMs > 24 * 60 * 60 * 1000) | ||
ctx.addIssue({ code: z.ZodIssueCode.custom, message: "TTL must be less than a day" }); | ||
}), | ||
maxTTL: z | ||
.string() | ||
.optional() | ||
.superRefine((val, ctx) => { | ||
if (!val) return; | ||
const valMs = ms(val); | ||
if (valMs < 60 * 1000) | ||
ctx.addIssue({ code: z.ZodIssueCode.custom, message: "TTL must be a greater than 1min" }); | ||
// a day | ||
if (valMs > 24 * 60 * 60 * 1000) | ||
ctx.addIssue({ code: z.ZodIssueCode.custom, message: "TTL must be less than a day" }); | ||
}), | ||
name: z | ||
.string() | ||
.trim() | ||
.min(1) | ||
.refine((val) => val.toLowerCase() === val, "Must be lowercase") | ||
}); | ||
type TForm = z.infer<typeof formSchema>; | ||
|
||
type Props = { | ||
onCompleted: () => void; | ||
onCancel: () => void; | ||
secretPath: string; | ||
projectSlug: string; | ||
environment: string; | ||
}; | ||
|
||
export const TotpInputForm = ({ | ||
onCompleted, | ||
onCancel, | ||
environment, | ||
secretPath, | ||
projectSlug | ||
}: Props) => { | ||
const { | ||
control, | ||
formState: { isSubmitting }, | ||
handleSubmit | ||
} = useForm<TForm>({ | ||
resolver: zodResolver(formSchema) | ||
}); | ||
|
||
const createDynamicSecret = useCreateDynamicSecret(); | ||
|
||
const handleCreateDynamicSecret = async ({ name, maxTTL, provider, defaultTTL }: TForm) => { | ||
// wait till previous request is finished | ||
if (createDynamicSecret.isLoading) return; | ||
try { | ||
await createDynamicSecret.mutateAsync({ | ||
provider: { type: DynamicSecretProviders.Totp, inputs: provider }, | ||
maxTTL, | ||
name, | ||
path: secretPath, | ||
defaultTTL, | ||
projectSlug, | ||
environmentSlug: environment | ||
}); | ||
onCompleted(); | ||
} catch (err) { | ||
createNotification({ | ||
type: "error", | ||
text: err instanceof Error ? err.message : "Failed to create dynamic secret" | ||
}); | ||
} | ||
}; | ||
|
||
return ( | ||
<div> | ||
<form onSubmit={handleSubmit(handleCreateDynamicSecret)} autoComplete="off"> | ||
<div> | ||
<div className="flex items-center space-x-2"> | ||
<div className="flex-grow"> | ||
<Controller | ||
control={control} | ||
defaultValue="" | ||
name="name" | ||
render={({ field, fieldState: { error } }) => ( | ||
<FormControl | ||
label="Secret Name" | ||
isError={Boolean(error)} | ||
errorText={error?.message} | ||
> | ||
<Input {...field} placeholder="dynamic-secret" /> | ||
</FormControl> | ||
)} | ||
/> | ||
</div> | ||
<div className="w-32"> | ||
<Controller | ||
control={control} | ||
name="defaultTTL" | ||
defaultValue="1m" | ||
render={({ field, fieldState: { error } }) => ( | ||
<FormControl | ||
label={<TtlFormLabel label="Default TTL" />} | ||
isError={Boolean(error?.message)} | ||
errorText={error?.message} | ||
> | ||
<Input {...field} /> | ||
</FormControl> | ||
)} | ||
/> | ||
</div> | ||
<div className="w-32"> | ||
<Controller | ||
control={control} | ||
name="maxTTL" | ||
defaultValue="24h" | ||
render={({ field, fieldState: { error } }) => ( | ||
<FormControl | ||
label={<TtlFormLabel label="Max TTL" />} | ||
isError={Boolean(error?.message)} | ||
errorText={error?.message} | ||
> | ||
<Input {...field} /> | ||
</FormControl> | ||
)} | ||
/> | ||
</div> | ||
</div> | ||
<div> | ||
<div className="mb-4 mt-4 border-b border-mineshaft-500 pb-2 pl-1 font-medium text-mineshaft-200"> | ||
Configuration | ||
<Link | ||
href="https://infisical.com/docs/documentation/platform/dynamic-secrets/totp" | ||
passHref | ||
> | ||
<a target="_blank" rel="noopener noreferrer"> | ||
<div className="ml-2 mb-1 inline-block rounded-md bg-yellow/20 px-1.5 pb-[0.03rem] pt-[0.04rem] text-sm text-yellow opacity-80 hover:opacity-100"> | ||
<FontAwesomeIcon icon={faBookOpen} className="mr-1.5" /> | ||
Docs | ||
<FontAwesomeIcon | ||
icon={faArrowUpRightFromSquare} | ||
className="ml-1.5 mb-[0.07rem] text-xxs" | ||
/> | ||
</div> | ||
</a> | ||
</Link> | ||
</div> | ||
<div className="flex flex-col"> | ||
<Controller | ||
control={control} | ||
name="provider.url" | ||
defaultValue="" | ||
render={({ field, fieldState: { error } }) => ( | ||
<FormControl | ||
label="URL" | ||
className="flex-grow" | ||
isError={Boolean(error?.message)} | ||
errorText={error?.message} | ||
> | ||
<Input {...field} /> | ||
</FormControl> | ||
)} | ||
/> | ||
</div> | ||
</div> | ||
</div> | ||
<div className="mt-4 flex items-center space-x-4"> | ||
<Button type="submit" isLoading={isSubmitting}> | ||
Submit | ||
</Button> | ||
<Button variant="outline_bg" onClick={onCancel}> | ||
Cancel | ||
</Button> | ||
</div> | ||
</form> | ||
</div> | ||
); | ||
}; |
Oops, something went wrong.