import PropTypes from 'prop-types'; import QRCode from 'qrcode.react'; import React from 'react'; import ImmutablePropTypes from 'react-immutable-proptypes'; import ImmutablePureComponent from 'react-immutable-pure-component'; import { defineMessages, injectIntl, FormattedMessage } from 'react-intl'; import { connect } from 'react-redux'; import snackbar from 'soapbox/actions/snackbar'; import Button from 'soapbox/components/button'; import LoadingIndicator from 'soapbox/components/loading_indicator'; import ShowablePassword from 'soapbox/components/showable_password'; import { SimpleForm, FieldsGroup, TextInput, } from 'soapbox/features/forms'; import { fetchMfa, fetchBackupCodes, setupMfa, confirmMfa, disableMfa, } from '../../actions/mfa'; import Column from '../ui/components/column'; import ColumnSubheading from '../ui/components/column_subheading'; /* Security settings page for user account Routed to /auth/mfa Includes following features: - Set up Multi-factor Auth */ const messages = defineMessages({ heading: { id: 'column.security', defaultMessage: 'Security' }, subheading: { id: 'column.mfa', defaultMessage: 'Multi-Factor Authentication' }, mfa_cancel_button: { id: 'column.mfa_cancel', defaultMessage: 'Cancel' }, mfa_setup_button: { id: 'column.mfa_setup', defaultMessage: 'Proceed to Setup' }, mfa_setup_confirm_button: { id: 'column.mfa_confirm_button', defaultMessage: 'Confirm' }, mfa_setup_disable_button: { id: 'column.mfa_disable_button', defaultMessage: 'Disable' }, passwordFieldLabel: { id: 'security.fields.password.label', defaultMessage: 'Password' }, confirmFail: { id: 'security.confirm.fail', defaultMessage: 'Incorrect code or password. Try again.' }, qrFail: { id: 'security.qr.fail', defaultMessage: 'Failed to fetch setup key' }, codesFail: { id: 'security.codes.fail', defaultMessage: 'Failed to fetch backup codes' }, disableFail: { id: 'security.disable.fail', defaultMessage: 'Incorrect password. Try again.' }, mfaDisableSuccess: { id: 'mfa.disable.success_message', defaultMessage: 'MFA disabled' }, mfaConfirmSuccess: { id: 'mfa.confirm.success_message', defaultMessage: 'MFA confirmed' }, }); const mapStateToProps = state => ({ backup_codes: state.getIn(['auth', 'backup_codes', 'codes']), mfa: state.getIn(['security', 'mfa']), }); export default @connect(mapStateToProps) @injectIntl class MfaForm extends ImmutablePureComponent { static contextTypes = { router: PropTypes.object, }; static propTypes = { intl: PropTypes.object.isRequired, dispatch: PropTypes.func.isRequired, mfa: ImmutablePropTypes.map.isRequired, }; state = { displayOtpForm: false, } handleSetupProceedClick = e => { this.setState({ displayOtpForm: true }); e.preventDefault(); } componentDidMount() { this.props.dispatch(fetchMfa()); } render() { const { intl, mfa } = this.props; const { displayOtpForm } = this.state; return ( {mfa.getIn(['settings', 'totp']) ? ( ) : ( <> {displayOtpForm && } > )} ); } } @connect() @injectIntl class DisableOtpForm extends ImmutablePureComponent { static contextTypes = { router: PropTypes.object, }; static propTypes = { intl: PropTypes.object.isRequired, dispatch: PropTypes.func.isRequired, }; state = { password: '', isLoading: false, } handleInputChange = e => { this.setState({ [e.target.name]: e.target.value }); } handleSubmit = e => { const { password } = this.state; const { dispatch, intl } = this.props; this.setState({ isLoading: true }); dispatch(disableMfa('totp', password)).then(() => { dispatch(snackbar.success(intl.formatMessage(messages.mfaDisableSuccess))); this.context.router.history.push('../auth/edit'); }).catch(error => { dispatch(snackbar.error(intl.formatMessage(messages.disableFail))); this.setState({ isLoading: false }); }); e.preventDefault(); } render() { const { intl } = this.props; const { isLoading, password } = this.state; return ( ); } } @connect() @injectIntl class EnableOtpForm extends ImmutablePureComponent { static contextTypes = { router: PropTypes.object, }; static propTypes = { intl: PropTypes.object.isRequired, dispatch: PropTypes.func.isRequired, }; state = { backupCodes: [], } componentDidMount() { const { dispatch, intl } = this.props; dispatch(fetchBackupCodes()).then(({ codes: backupCodes }) => { this.setState({ backupCodes }); }).catch(error => { dispatch(snackbar.error(intl.formatMessage(messages.codesFail))); }); } handleCancelClick = e => { this.context.router.history.push('../auth/edit'); } render() { const { intl } = this.props; const { backupCodes, displayOtpForm } = this.state; return ( {backupCodes.length > 0 ? ( {backupCodes.map((code, i) => ( {code} ))} ) : ( )} {!displayOtpForm && ( {backupCodes.length > 0 && ( )} )} ); } } @connect() @injectIntl class OtpConfirmForm extends ImmutablePureComponent { static contextTypes = { router: PropTypes.object, }; static propTypes = { intl: PropTypes.object.isRequired, }; state = { password: '', isLoading: false, code: '', qrCodeURI: '', confirm_key: '', } componentDidMount() { const { dispatch, intl } = this.props; dispatch(setupMfa('totp')).then(data => { this.setState({ qrCodeURI: data.provisioning_uri, confirm_key: data.key }); }).catch(error => { dispatch(snackbar.error(intl.formatMessage(messages.qrFail))); }); } handleInputChange = e => { this.setState({ [e.target.name]: e.target.value }); } handleSubmit = e => { const { dispatch, intl } = this.props; const { code, password } = this.state; this.setState({ isLoading: true }); dispatch(confirmMfa('totp', code, password)).then(() => { dispatch(snackbar.success(intl.formatMessage(messages.mfaConfirmSuccess))); this.context.router.history.push('../auth/edit'); }).catch(error => { dispatch(snackbar.error(intl.formatMessage(messages.confirmFail))); this.setState({ isLoading: false }); }); e.preventDefault(); } render() { const { intl } = this.props; const { isLoading, qrCodeURI, confirm_key, password, code } = this.state; return ( {confirm_key} ); } }