import React, { useEffect, useState } from 'react'; import { FormFieldContainer, Label, TextInput, RadioBoxGroup, RadioBox, Checkbox, } from '@mongodb-js/compass-components'; import type ConnectionStringUrl from 'mongodb-connection-string-url'; import type { UpdateConnectionFormField } from '../../../hooks/use-connect-form'; import type { ConnectionFormError } from '../../../utils/validation'; import { errorMessageByFieldName } from '../../../utils/validation'; import { getConnectionStringPassword, getConnectionStringUsername, parseAuthMechanismProperties, } from '../../../utils/connection-string-helpers'; import { useConnectionFormPreference } from '../../../hooks/use-connect-form-preferences'; const GSSAPI_CANONICALIZE_HOST_NAME_OPTIONS: Record< string, { label: string; value: string } > = { none: { label: 'None', value: 'none' }, forward: { label: 'Forward', value: 'forward' }, forwardAndReverse: { label: 'Forward and reverse', value: 'forwardAndReverse', }, }; function AuthenticationGSSAPI({ errors, connectionStringUrl, updateConnectionFormField, }: { connectionStringUrl: ConnectionStringUrl; errors: ConnectionFormError[]; updateConnectionFormField: UpdateConnectionFormField; }): React.ReactElement { const kerberosPrincipalError = errorMessageByFieldName( errors, 'kerberosPrincipal' ); const principal = getConnectionStringUsername(connectionStringUrl); const password = getConnectionStringPassword(connectionStringUrl); const authMechanismProperties = parseAuthMechanismProperties(connectionStringUrl); const serviceName = authMechanismProperties.get('SERVICE_NAME'); const serviceRealm = authMechanismProperties.get('SERVICE_REALM'); const canonicalizeHostname = authMechanismProperties.get('CANONICALIZE_HOST_NAME') || 'none'; const [showPassword, setShowPassword] = useState(false); const showKerberosPasswordField = !!useConnectionFormPreference( 'showKerberosPasswordField' ); useEffect(() => { if (!showPassword && password.length) { setShowPassword(true); } }, [password, showPassword, updateConnectionFormField]); return ( <> ) => { updateConnectionFormField({ type: 'update-username', username: value, }); }} data-testid="gssapi-principal-input" label="Principal" errorMessage={kerberosPrincipalError} state={kerberosPrincipalError ? 'error' : undefined} value={principal || ''} /> ) => { updateConnectionFormField({ type: 'update-auth-mechanism-property', key: 'SERVICE_NAME', value: value, }); }} optional label="Service Name" value={serviceName || ''} /> { updateConnectionFormField({ type: 'update-auth-mechanism-property', key: 'CANONICALIZE_HOST_NAME', value: value === 'none' ? '' : value, }); }} value={canonicalizeHostname} size="compact" > {Object.entries(GSSAPI_CANONICALIZE_HOST_NAME_OPTIONS).map( ([key, { label, value }]) => ( {label} ) )} ) => { updateConnectionFormField({ type: 'update-auth-mechanism-property', key: 'SERVICE_REALM', value: value, }); }} data-testid="gssapi-service-realm-input" label="Service Realm" value={serviceRealm || ''} optional /> {showKerberosPasswordField && ( <> { if (!checked) { updateConnectionFormField({ type: 'update-password', password: '', }); } setShowPassword(checked); }} /> {showPassword && ( ) => { updateConnectionFormField({ type: 'update-password', password: value, }); }} data-testid="gssapi-password-input" label="Password" value={password} type="password" optional /> )} )} ); } export default AuthenticationGSSAPI;