import { Button, Card, Dialog, FormInputValidation, FormLabel } from '@automattic/components'; import { supported } from '@github/webauthn-json'; import debugFactory from 'debug'; import { localize } from 'i18n-calypso'; import PropTypes from 'prop-types'; import { Component } from 'react'; import { connect } from 'react-redux'; import CardHeading from 'calypso/components/card-heading'; import FormButton from 'calypso/components/forms/form-button'; import FormCheckbox from 'calypso/components/forms/form-checkbox'; import FormFieldset from 'calypso/components/forms/form-fieldset'; import FormVerificationCodeInput from 'calypso/components/forms/form-verification-code-input'; import Notice from 'calypso/components/notice'; import WarningCard from 'calypso/components/warning-card'; import { recordGoogleEvent } from 'calypso/state/analytics/actions'; import { redirectToLogout } from 'calypso/state/current-user/actions'; import SecurityKeyForm from './security-key-form'; import TwoFactorActions from './two-factor-actions'; import './style.scss'; const debug = debugFactory( 'calypso:me:reauth-required' ); // autofocus is used for tracking purposes, not an a11y issue /* eslint-disable jsx-a11y/no-autofocus, jsx-a11y/click-events-have-key-events, jsx-a11y/no-static-element-interactions, jsx-a11y/anchor-is-valid */ class ReauthRequired extends Component { codeRequestTimer = false; constructor( props ) { super( props ); this.state = { remember2fa: false, // Should the 2fa be remembered for 30 days? code: '', // User's generated 2fa code smsRequestsAllowed: true, // Can the user request another SMS code? twoFactorAuthType: 'authenticator', }; } componentDidMount() { this.props.twoStepAuthorization.on( 'change', this.update ); } componentWillUnmount() { clearTimeout( this.codeRequestTimer ); this.props.twoStepAuthorization.off( 'change', this.update ); } update = () => { this.forceUpdate(); }; getClickHandler = ( action, callback ) => () => { this.props.recordGoogleEvent( 'Me', 'Clicked on ' + action ); if ( callback ) { callback(); } }; getCheckboxHandler = ( checkboxName ) => ( event ) => { const action = 'Clicked ' + checkboxName + ' checkbox'; const value = event.target.checked ? 1 : 0; this.props.recordGoogleEvent( 'Me', action, 'checked', value ); }; getFocusHandler = ( action ) => () => this.props.recordGoogleEvent( 'Me', 'Focused on ' + action ); renderCodeMessage() { if ( this.props.twoStepAuthorization.isTwoStepSMSEnabled() ) { return this.props.translate( 'Press the button below to request an SMS verification code. ' + 'Once you receive our text message at your phone number ending with ' + '{{strong}}%(smsLastFour)s{{/strong}} , enter the code below.', { args: { smsLastFour: this.props.twoStepAuthorization.getSMSLastFour(), }, components: { strong: , }, } ); } if ( this.state.twoFactorAuthType === 'sms' ) { return this.props.translate( 'We just sent you a verification code to your phone number on file, please enter the code below.' ); } return this.props.translate( 'Please enter the verification code generated by your authenticator app.' ); } submitForm = ( event ) => { event.preventDefault(); this.setState( { validatingCode: true } ); this.props.twoStepAuthorization.validateCode( { code: this.state.code, remember2fa: this.state.remember2fa, }, ( error, data ) => { this.setState( { validatingCode: false } ); if ( error ) { debug( 'There was an error validating that code: ' + JSON.stringify( error ) ); } else { debug( 'The code validated!' + JSON.stringify( data ) ); } } ); }; sendSMSCode() { this.setState( { smsRequestsAllowed: false } ); this.codeRequestTimer = setTimeout( () => { this.setState( { smsRequestsAllowed: true } ); }, 60000 ); this.props.twoStepAuthorization.sendSMSCode( ( error, data ) => { if ( ! error && data.sent ) { debug( 'SMS code successfully sent' ); } else { debug( 'There was a failure sending the SMS code.' ); } } ); } preValidateAuthCode() { return this.state.code.length && this.state.code.length > 5; } renderFailedValidationMsg() { if ( ! this.props.twoStepAuthorization.codeValidationFailed() ) { return null; } return ( ); } renderSMSResendThrottled() { if ( ! this.props.twoStepAuthorization.isSMSResendThrottled() ) { return null; } return (
); } renderVerificationForm() { const enhancedSecurity = this.props.twoStepAuthorization.data?.two_step_enhanced_security; if ( enhancedSecurity ) { return this.renderSecurityKeyUnsupported(); } const method = this.props.twoStepAuthorization.isTwoStepSMSEnabled() ? 'sms' : 'app'; return (

{ this.renderCodeMessage() }

{ this.props.translate( 'Not you? Log out' ) }

{ this.props.translate( 'Verification Code' ) } { this.renderFailedValidationMsg() } { this.props.translate( 'Remember for 30 days.' ) } { this.renderSMSResendThrottled() } { this.props.translate( 'Verify' ) }
); } renderSecurityKeyUnsupported() { const { translate } = this.props; return ( { translate( 'Two Factor Authentication Required' ) } ); } renderDialog() { const enhancedSecurity = this.props.twoStepAuthorization.data?.two_step_enhanced_security; const method = this.props.twoStepAuthorization.isTwoStepSMSEnabled() ? 'sms' : 'authenticator'; const isSecurityKeySupported = this.props.twoStepAuthorization.isSecurityKeyEnabled() && supported(); const twoFactorAuthType = enhancedSecurity ? 'webauthn' : this.state.twoFactorAuthType; // This enables the SMS button on the security key form regardless if we can send SMS or not. // Otherwise, there's no way to go back to the verification form if smsRequestsAllowed is false. const shouldEnableSmsButton = this.state.smsRequestsAllowed || ( method === 'sms' && twoFactorAuthType === 'webauthn' ); const hasSmsRecoveryNumber = !! this.props?.twoStepAuthorization?.data?.two_step_sms_last_four?.length; return ( { isSecurityKeySupported && twoFactorAuthType === 'webauthn' ? ( ) : ( this.renderVerificationForm() ) } ); } render() { return ( <> { this.renderDialog() } { this.props.twoStepAuthorization.initialized && ! this.props.twoStepAuthorization.isReauthRequired() && this.props.children?.() } ); } handleAuthSwitch = ( authType ) => { this.setState( { twoFactorAuthType: authType } ); if ( authType === 'sms' ) { this.sendSMSCode(); } }; handleChange = ( e ) => { const { name, value } = e.currentTarget; this.setState( { [ name ]: value } ); }; handleCheckedChange = ( e ) => { const { name, checked } = e.currentTarget; this.setState( { [ name ]: checked } ); }; } ReauthRequired.propTypes = { twoStepAuthorization: PropTypes.object.isRequired, }; /* eslint-enable jsx-a11y/no-autofocus, jsx-a11y/click-events-have-key-events, jsx-a11y/no-static-element-interactions, jsx-a11y/anchor-is-valid */ export default connect( null, { redirectToLogout, recordGoogleEvent, } )( localize( ReauthRequired ) );