import React, { useCallback } from 'react'; import { FormFieldContainer, InlineInfoLink, Label, RadioBox, RadioBoxGroup, TextInput, css, spacing, } from '@mongodb-js/compass-components'; import type ConnectionStringUrl from 'mongodb-connection-string-url'; import type { AuthMechanism } from 'mongodb'; import type { UpdateConnectionFormField } from '../../../hooks/use-connect-form'; import type { ConnectionFormError } from '../../../utils/validation'; import { errorMessageByFieldName } from '../../../utils/validation'; import { getConnectionStringPassword, getConnectionStringUsername, } from '../../../utils/connection-string-helpers'; const textInputWithLabelStyles = css({ marginTop: spacing[1], }); const defaultAuthMechanismOptions: { title: string; value: AuthMechanism; }[] = [ { title: 'Default', value: 'DEFAULT', }, { title: 'SCRAM-SHA-1', value: 'SCRAM-SHA-1', }, { title: 'SCRAM-SHA-256', value: 'SCRAM-SHA-256', }, ]; function AuthenticationDefault({ errors, connectionStringUrl, updateConnectionFormField, }: { connectionStringUrl: ConnectionStringUrl; errors: ConnectionFormError[]; updateConnectionFormField: UpdateConnectionFormField; }): React.ReactElement { const password = getConnectionStringPassword(connectionStringUrl); const username = getConnectionStringUsername(connectionStringUrl); const selectedAuthMechanism = ( connectionStringUrl.searchParams.get('authMechanism') ?? '' ).toUpperCase(); const selectedAuthTab = defaultAuthMechanismOptions.find( ({ value }) => value === selectedAuthMechanism ) ?? defaultAuthMechanismOptions[0]; const onAuthMechanismSelected = useCallback( (event: React.ChangeEvent) => { event.preventDefault(); updateConnectionFormField({ type: 'update-search-param', currentKey: 'authMechanism', value: event.target.value, }); }, [updateConnectionFormField] ); const usernameError = errorMessageByFieldName(errors, 'username'); const passwordError = errorMessageByFieldName(errors, 'password'); return ( <> ) => { updateConnectionFormField({ type: 'update-username', username: value, }); }} label="Username" data-testid="connection-username-input" errorMessage={usernameError} state={usernameError ? 'error' : undefined} value={username || ''} optional /> ) => { updateConnectionFormField({ type: 'update-password', password: value, }); }} label="Password" type="password" data-testid="connection-password-input" value={password || ''} errorMessage={passwordError} state={passwordError ? 'error' : undefined} optional /> ) => { if (value === '') { updateConnectionFormField({ type: 'delete-search-param', key: 'authSource', }); return; } updateConnectionFormField({ type: 'update-search-param', currentKey: 'authSource', value, }); }} id="authSourceInput" aria-labelledby="authSourceLabel" value={connectionStringUrl.searchParams.get('authSource') ?? ''} optional /> {defaultAuthMechanismOptions.map(({ title, value }) => { return ( {title} ); })} ); } export default AuthenticationDefault;