import { useEffect } from "react"; import { Controller, useForm } from "react-hook-form"; import { yupResolver } from "@hookform/resolvers/yup"; import * as yup from "yup"; import { useNotificationContext } from "@app/components/context/Notifications/NotificationProvider"; import { Button, FormControl, Input, Modal, ModalContent, Select, SelectItem, TextArea} from "@app/components/v2"; import { useOrganization } from "@app/context"; import { useCreateSSOConfig, useGetSSOConfig, useUpdateSSOConfig } from "@app/hooks/api"; import { UsePopUpState } from "@app/hooks/usePopUp"; enum AuthProvider { OKTA_SAML = "okta-saml", AZURE_SAML = "azure-saml" } const ssoAuthProviders = [ { label: "Okta SAML", value: AuthProvider.OKTA_SAML }, { label: "Azure SAML", value: AuthProvider.AZURE_SAML } ]; const schema = yup.object({ authProvider: yup.string().required("SSO Type is required"), entryPoint: yup.string().required("IdP entrypoint is required"), issuer: yup.string().required("Issuer string is required"), cert: yup.string().required("IdP's public signing certificate is required") }).required(); export type AddSSOFormData = yup.InferType; type Props = { popUp: UsePopUpState<["addSSO"]>; handlePopUpClose: (popUpName: keyof UsePopUpState<["addSSO"]>) => void; handlePopUpToggle: (popUpName: keyof UsePopUpState<["addSSO"]>, state?: boolean) => void; }; export const SSOModal = ({ popUp, handlePopUpClose, handlePopUpToggle }: Props) => { const { currentOrg } = useOrganization(); const { createNotification } = useNotificationContext(); const { mutateAsync: createMutateAsync, isLoading: createIsLoading } = useCreateSSOConfig(); const { mutateAsync: updateMutateAsync, isLoading: updateIsLoading } = useUpdateSSOConfig(); const { data } = useGetSSOConfig(currentOrg?._id ?? ""); const { control, handleSubmit, reset, watch, } = useForm({ defaultValues: { authProvider: AuthProvider.OKTA_SAML }, resolver: yupResolver(schema) }); useEffect(() => { if (data) { reset({ authProvider: data?.authProvider ?? "", entryPoint: data?.entryPoint ?? "", issuer: data?.issuer ?? "", cert: data?.cert ?? "" }); } }, [data]); const onSSOModalSubmit = async ({ authProvider, entryPoint, issuer, cert }: AddSSOFormData) => { try { if (!currentOrg) return; if (!data) { await createMutateAsync({ organizationId: currentOrg._id, authProvider, isActive: false, entryPoint, issuer, cert }); } else { await updateMutateAsync({ organizationId: currentOrg._id, authProvider, isActive: false, entryPoint, issuer, cert }); } handlePopUpClose("addSSO"); createNotification({ text: `Successfully ${!data ? "added" : "updated"} SAML SSO configuration`, type: "success" }); } catch (err) { console.error(err); createNotification({ text: `Failed to ${!data ? "add" : "update"} SAML SSO configuration`, type: "error" }); } } const renderLabels = (authProvider: string) => { switch (authProvider){ case AuthProvider.OKTA_SAML: return ({ acsUrl: "Single sign-on URL", entityId: "Audience URI (SP Entity ID)", entryPoint: "Identity Provider Single Sign-On URL", entryPointPlaceholder: "https://your-domain.okta.com/app/app-name/xxx/sso/saml", issuer: "Identity Provider Issuer", issuerPlaceholder: "http://www.okta.com/xxx" }); case AuthProvider.AZURE_SAML: return ({ acsUrl: "Reply URL (Assertion Consumer Service URL)", entityId: "Identifier (Entity ID)", entryPoint: "Login URL", entryPointPlaceholder: "https://login.microsoftonline.com/xxx/saml2", issuer: "Azure AD Identifier", issuerPlaceholder: "https://sts.windows.net/xxx/" }); default: return ({ acsUrl: "ACS URL", entityId: "Entity ID", entryPoint: "Entrypoint", entryPointPlaceholder: "Enter entrypoint...", issuer: "Issuer", issuerPlaceholder: "Enter placeholder..." }); } } const authProvider = watch("authProvider"); return ( { handlePopUpToggle("addSSO", isOpen); reset(); }} >
( )} /> {authProvider && data && ( <>

{renderLabels(authProvider).acsUrl}

{`${window.origin}/api/v1/sso/saml2/${data._id}`}

{renderLabels(authProvider).entityId}

{window.origin}

( )} /> ( )} /> (