/* * Copyright OpenSearch Contributors * * Licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. * A copy of the License is located at * * http://www.apache.org/licenses/LICENSE-2.0 * * or in the "license" file accompanying this file. This file is distributed * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either * express or implied. See the License for the specific language governing * permissions and limitations under the License. */ import React from 'react'; import { EuiButton, EuiButtonEmpty, EuiCallOut, EuiFieldPassword, EuiModal, EuiModalBody, EuiModalFooter, EuiOverlayMask, EuiSpacer, EuiTitle, } from '@elastic/eui'; import { CoreStart } from 'opensearch-dashboards/public'; import { FormRow } from '../configuration/utils/form-row'; import { logout, updateNewPassword } from './utils'; import { PASSWORD_INSTRUCTION } from '../apps-constants'; import { constructErrorMessageAndLog } from '../error-utils'; import { validateCurrentPassword } from '../../utils/login-utils'; import { getDashboardsInfo } from '../../utils/dashboards-info-utils'; interface PasswordResetPanelProps { coreStart: CoreStart; username: string; handleClose: () => void; logoutUrl?: string; } export function PasswordResetPanel(props: PasswordResetPanelProps) { const [currentPassword, setCurrentPassword] = React.useState(''); // reply on backend response of login call to verify const [isCurrentPasswordInvalid, setIsCurrentPasswordInvalid] = React.useState(false); const [currentPasswordError, setCurrentPasswordError] = React.useState([]); const [newPassword, setNewPassword] = React.useState(''); // reply on backend response of user update call to verify const [isNewPasswordInvalid, setIsNewPasswordInvalid] = React.useState(false); const [repeatNewPassword, setRepeatNewPassword] = React.useState(''); const [isRepeatNewPasswordInvalid, setIsRepeatNewPasswordInvalid] = React.useState( false ); const [errorCallOut, setErrorCallOut] = React.useState(''); const [passwordHelpText, setPasswordHelpText] = React.useState(PASSWORD_INSTRUCTION); React.useEffect(() => { const fetchData = async () => { try { setPasswordHelpText( (await getDashboardsInfo(props.coreStart.http)).password_validation_error_message ); } catch (e) { console.error(e); } }; fetchData(); }, [props.coreStart.http]); const handleReset = async () => { const http = props.coreStart.http; // validate the current password try { await validateCurrentPassword(http, props.username, currentPassword); } catch (e) { setIsCurrentPasswordInvalid(true); setCurrentPasswordError([constructErrorMessageAndLog(e, 'Invalid current password.')]); } // update new password try { await updateNewPassword(http, newPassword, currentPassword); await logout(http, props.logoutUrl); } catch (e) { setErrorCallOut(constructErrorMessageAndLog(e, 'Failed to reset password.')); } }; // TODO: replace the instruction message for new password once UX provides it. return (

Reset password for "{props.username}"

) { setCurrentPassword(e.target.value); setIsCurrentPasswordInvalid(false); }} isInvalid={isCurrentPasswordInvalid} /> ) { setNewPassword(e.target.value); setIsNewPasswordInvalid(false); setIsRepeatNewPasswordInvalid(repeatNewPassword !== newPassword); }} isInvalid={isNewPasswordInvalid} /> ) { const value = e.target.value; setRepeatNewPassword(value); setIsRepeatNewPasswordInvalid(value !== newPassword); }} /> {errorCallOut && ( {errorCallOut} )}
Cancel Reset
); }