The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
import { downloadFile } from '@/activities/files/utils/downloadFile';
import { parseSamlMetadataFromXmlFile } from '@/settings/security/utils/parseSamlMetadataFromXmlFile';
import { useBuildServerUrl } from '@/settings/security/hooks/useBuildServerUrl';
import { SettingsTextInput } from '@/ui/input/components/SettingsTextInput';
import { styled } from '@linaria/react';
import { useLingui } from '@lingui/react/macro';
import { type ChangeEvent, useRef } from 'react';
import { useFormContext } from 'react-hook-form';
import { isDefined } from 'twenty-shared/utils';
import { useToast } from 'twenty-ui/components/feedback';
import { LabeledSeparator, Section } from 'twenty-ui/components/layout';
import { IconCheck, IconCopy, IconDownload, IconUpload } from 'twenty-ui/icon';
import { Button } from 'twenty-ui/primitives/input';
import { useTheme, themeCssVariables } from 'twenty-ui/theme';
import { REACT_APP_SERVER_BASE_URL } from '~/config';
import { useCopyToClipboard } from '~/hooks/useCopyToClipboard';
/* @license Enterprise */
const StyledUploadFileContainer = styled.div`
align-items: center;
display: flex;
gap: ${themeCssVariables.spacing[2]};
`;
const StyledFileInput = styled.input`
display: none;
`;
const StyledInputsContainer = styled.div`
display: flex;
flex-direction: column;
gap: ${themeCssVariables.spacing[2]} ${themeCssVariables.spacing[4]};
width: 100%;
`;
const StyledContainer = styled.div`
display: flex;
flex-direction: row;
`;
const StyledLinkContainer = styled.div`
flex: 1;
margin-right: ${themeCssVariables.spacing[2]};
`;
const StyledButtonCopy = styled.div`
align-items: end;
display: flex;
margin-bottom: ${themeCssVariables.spacing[1]};
`;
const StyledDividerContainer = styled.div`
margin-block: ${themeCssVariables.spacing[3]};
`;
export const SettingsSsoSamlForm = () => {
const theme = useTheme();
const { enqueueToast } = useToast();
const { setValue, getValues, watch, trigger } = useFormContext();
const { t } = useLingui();
const { copyToClipboard } = useCopyToClipboard();
const buildServerUrl = useBuildServerUrl();
const handleFileChange = async (e: ChangeEvent<HTMLInputElement>) => {
if (isDefined(e.target.files)) {
const text = await e.target.files[0].text();
const samlMetadataParsed = parseSamlMetadataFromXmlFile(text);
e.target.value = '';
if (!samlMetadataParsed.success) {
return enqueueToast({
status: 'error',
children: t`Invalid file: ${samlMetadataParsed.reason}`,
duration: 5000,
});
}
setValue('ssoURL', samlMetadataParsed.data.ssoUrl);
setValue('certificate', samlMetadataParsed.data.certificate);
setValue('issuer', samlMetadataParsed.data.entityID);
trigger();
}
};
const entityID = buildServerUrl(`/auth/saml/login/${getValues('id')}`);
const acsUrl = buildServerUrl(`/auth/saml/callback/${getValues('id')}`);
const inputFileRef = useRef<HTMLInputElement>(null);
const handleUploadFileClick = () => {
inputFileRef?.current?.click?.();
};
const ssoURL = watch('ssoURL');
const certificate = watch('certificate');
const issuer = watch('issuer');
const isXMLMetadataValid = () => {
return [ssoURL, certificate, issuer].every(
(field) => isDefined(field) && field.length > 0,
);
};
const downloadMetadata = async () => {
try {
await downloadFile(
`${REACT_APP_SERVER_BASE_URL}/auth/saml/metadata/${getValues('id')}`,
'metadata.xml',
);
} catch {
enqueueToast({
status: 'error',
children: t`Metadata file generation failed`,
duration: 2000,
});
}
};
return (
<>
<Section.Root>
<Section.Header
title={t`Identity Provider Metadata XML`}
description={t`Upload the XML file with your connection infos`}
/>
<StyledUploadFileContainer>
<StyledFileInput
ref={inputFileRef}
onChange={handleFileChange}
type="file"
accept=".xml"
/>
<Button
startIcon={<IconUpload />}
onClick={handleUploadFileClick}
type="button"
>{t`Upload file`}</Button>
{isXMLMetadataValid() && (
<IconCheck
size={theme.icon.size.md}
stroke={theme.icon.stroke.lg}
color={theme.color.blue}
/>
)}
</StyledUploadFileContainer>
</Section.Root>
<Section.Root>
<Section.Header
title={t`Service Provider Details`}
description={t`Enter the infos to set the connection`}
/>
<StyledInputsContainer>
<StyledContainer>
<Button
startIcon={<IconDownload />}
onClick={downloadMetadata}
type="button"
>{t`Download file`}</Button>
</StyledContainer>
<StyledDividerContainer>
<LabeledSeparator>{t`Or`}</LabeledSeparator>
</StyledDividerContainer>
<StyledContainer>
<StyledLinkContainer>
<SettingsTextInput
instanceId="sso-saml-acs-url"
disabled={true}
label={t`ACS URL`}
value={acsUrl}
fullWidth
/>
</StyledLinkContainer>
<StyledButtonCopy>
<Button
startIcon={<IconCopy />}
onClick={() => {
copyToClipboard(acsUrl, t`ACS URL copied to clipboard`);
}}
type="button"
>{t`Copy`}</Button>
</StyledButtonCopy>
</StyledContainer>
<StyledContainer>
<StyledLinkContainer>
<SettingsTextInput
instanceId="sso-saml-entity-id"
disabled={true}
label={t`Entity ID`}
value={entityID}
fullWidth
/>
</StyledLinkContainer>
<StyledButtonCopy>
<Button
startIcon={<IconCopy />}
onClick={() => {
copyToClipboard(entityID, t`Entity ID copied to clipboard`);
}}
type="button"
>{t`Copy`}</Button>
</StyledButtonCopy>
</StyledContainer>
</StyledInputsContainer>
</Section.Root>
</>
);
};