import PropTypes from 'prop-types'; import LoadingButton from '@mui/lab/LoadingButton'; import Stack from '@mui/material/Stack'; import MuiTextField from '@mui/material/TextField'; import Alert from '@mui/material/Alert'; import * as React from 'react'; import { yupResolver } from '@hookform/resolvers/yup'; import * as yup from 'yup'; import ControlledAutocomplete from 'components/ControlledAutocomplete'; import Form from 'components/Form'; import Switch from 'components/Switch'; import TextField from 'components/TextField'; import useFormatMessage from 'hooks/useFormatMessage'; import useAdminCreateSamlAuthProvider from 'hooks/useAdminCreateSamlAuthProvider'; import useAdminUpdateSamlAuthProvider from 'hooks/useAdminUpdateSamlAuthProvider'; import useRoles from 'hooks/useRoles.ee'; const defaultValues = { active: false, name: '', certificate: '', signatureAlgorithm: 'sha1', issuer: '', entryPoint: '', firstnameAttributeName: '', surnameAttributeName: '', emailAttributeName: '', roleAttributeName: '', defaultRoleId: '', }; const getValidationSchema = (formatMessage) => { const getMandatoryFieldMessage = (fieldTranslationId) => formatMessage('authenticationForm.mandatoryInput', { inputName: formatMessage(fieldTranslationId), }); return yup.object().shape({ active: yup.boolean(), name: yup .string() .trim() .required(getMandatoryFieldMessage('authenticationForm.name')), certificate: yup .string() .trim() .required(getMandatoryFieldMessage('authenticationForm.certificate')), signatureAlgorithm: yup .string() .trim() .required( getMandatoryFieldMessage('authenticationForm.signatureAlgorithm'), ), issuer: yup .string() .trim() .required(getMandatoryFieldMessage('authenticationForm.issuer')), entryPoint: yup .string() .trim() .required(getMandatoryFieldMessage('authenticationForm.entryPoint')), firstnameAttributeName: yup .string() .trim() .required( getMandatoryFieldMessage('authenticationForm.firstnameAttributeName'), ), surnameAttributeName: yup .string() .trim() .required( getMandatoryFieldMessage('authenticationForm.surnameAttributeName'), ), emailAttributeName: yup .string() .trim() .required( getMandatoryFieldMessage('authenticationForm.emailAttributeName'), ), roleAttributeName: yup .string() .trim() .required( getMandatoryFieldMessage('authenticationForm.roleAttributeName'), ), defaultRoleId: yup .string() .trim() .required(getMandatoryFieldMessage('authenticationForm.defaultRole')), }); }; function generateRoleOptions(roles) { return roles?.map(({ name: label, id: value }) => ({ label, value })); } function SamlConfiguration({ provider, providerLoading }) { const formatMessage = useFormatMessage(); const { data, isLoading: isRolesLoading } = useRoles(); const roles = data?.data; const { mutateAsync: createSamlAuthProvider, isPending: isCreateSamlAuthProviderPending, isSuccess: isCreateSamlAuthProviderSuccess, } = useAdminCreateSamlAuthProvider(); const { mutateAsync: updateSamlAuthProvider, isPending: isUpdateSamlAuthProviderPending, isSuccess: isUpdateSamlAuthProviderSuccess, } = useAdminUpdateSamlAuthProvider(provider?.id); const isPending = isCreateSamlAuthProviderPending || isUpdateSamlAuthProviderPending; const isSuccess = isCreateSamlAuthProviderSuccess || isUpdateSamlAuthProviderSuccess; const handleSubmit = async (providerData) => { try { if (provider?.id) { await updateSamlAuthProvider(providerData); } else { await createSamlAuthProvider(providerData); } } catch (error) { const errors = error?.response?.data?.errors; throw errors || error; } }; if (providerLoading) { return null; } return (
( ( )} /> ( )} loading={isRolesLoading} /> {errors?.root?.general && ( {errors.root.general.message} )} {isSuccess && !isDirty && ( {formatMessage('authenticationForm.successfullySaved')} )} {formatMessage('authenticationForm.save')} )} /> ); } SamlConfiguration.propTypes = { provider: PropTypes.shape({ id: PropTypes.string, active: PropTypes.bool, name: PropTypes.string, certificate: PropTypes.string, signatureAlgorithm: PropTypes.oneOf(['sha1', 'sha256', 'sha512']), issuer: PropTypes.string, entryPoint: PropTypes.string, firstnameAttributeName: PropTypes.string, surnameAttributeName: PropTypes.string, emailAttributeName: PropTypes.string, roleAttributeName: PropTypes.string, defaultRoleId: PropTypes.string, }), providerLoading: PropTypes.bool, }; export default SamlConfiguration;