import { FormLabel } from '@automattic/components'; import clsx from 'clsx'; import debugFactory from 'debug'; import { localize } from 'i18n-calypso'; import PropTypes from 'prop-types'; import { QRCodeSVG } from 'qrcode.react'; import { Component } from 'react'; import FormButton from 'calypso/components/forms/form-button'; import FormSettingExplanation from 'calypso/components/forms/form-setting-explanation'; import FormVerificationCodeInput from 'calypso/components/forms/form-verification-code-input'; import Notice from 'calypso/components/notice'; import { gaRecordEvent } from 'calypso/lib/analytics/ga'; import twoStepAuthorization from 'calypso/lib/two-step-authorization'; import wp from 'calypso/lib/wp'; import OneTimeCode from 'calypso/me/security-2fa-enable/one-time-code'; import './style.scss'; const debug = debugFactory( 'calypso:me:security:2fa-enable' ); class Security2faEnable extends Component { static displayName = 'Security2faEnable'; static defaultProps = { isSmsFlow: false, }; static propTypes = { isSmsFlow: PropTypes.bool, onCancel: PropTypes.func.isRequired, onSuccess: PropTypes.func.isRequired, }; state = { lastError: false, lastErrorType: false, method: this.props.isSmsFlow ? 'sms' : 'scan', otpAuthUri: false, smsRequestsAllowed: true, smsRequestPerformed: false, submittingCode: false, oneTimeCode: false, verificationCode: '', }; codeRequestTimer = false; componentDidMount() { debug( this.constructor.displayName + ' React component is mounted.' ); wp.req.get( '/me/two-step/app-auth-setup/', ( error, data ) => { if ( error ) { this.setState( { lastError: this.props.translate( 'Unable to obtain authorization application setup information. Please try again later.' ), lastErrorType: 'is-error', } ); return; } this.setState( { otpAuthUri: data.otpauth_uri, oneTimeCode: data.time_code, } ); } ); if ( this.props.isSmsFlow ) { this.requestSMS(); } } componentWillUnmount() { debug( this.constructor.displayName + ' React component will unmount.' ); this.cancelCodeRequestTimer(); } allowSMSRequests = () => { this.setState( { smsRequestsAllowed: true } ); }; requestSMS = () => { this.setState( { smsRequestsAllowed: false, lastError: false, } ); twoStepAuthorization.sendSMSCode( this.onSMSRequestResponse ); this.codeRequestTimer = setTimeout( this.allowSMSRequests, 60000 ); }; onSMSRequestResponse = ( error ) => { if ( error ) { this.setState( { smsRequestPerformed: false, lastError: this.props.translate( 'Unable to request a code via SMS right now. Please try again after one minute.' ), lastErrorType: 'is-info', } ); } else { this.setState( { smsRequestPerformed: true } ); } }; cancelCodeRequestTimer = () => { if ( this.codeRequestTimer ) { clearTimeout( this.codeRequestTimer ); } }; onResendCode = ( event ) => { event.preventDefault(); if ( this.state.smsRequestsAllowed ) { this.requestSMS(); } }; getFormDisabled = () => { return this.state.submittingCode || 6 > this.state.verificationCode.trim().length; }; onCodeSubmit = ( event ) => { event.preventDefault(); this.setState( { submittingCode: true }, this.onBeginCodeValidation ); }; onBeginCodeValidation = () => { const args = { code: this.state.verificationCode, action: 'enable-two-step', }; twoStepAuthorization.validateCode( args, this.onValidationResponseReceived ); }; onValidationResponseReceived = ( error, data ) => { this.setState( { submittingCode: false } ); if ( error ) { this.setState( { lastError: this.props.translate( 'An unexpected error occurred. Please try again later.' ), lastErrorType: 'is-error', } ); } else if ( ! data.success ) { this.setState( { lastError: this.props.translate( 'You entered an invalid code. Please try again.' ), lastErrorType: 'is-error', } ); } else { this.props.onSuccess(); } }; renderQRCode = () => { const qrClasses = clsx( 'security-2fa-enable__qr-code', { 'is-placeholder': ! this.state.otpAuthUri, } ); return (
  1. { this.props.translate( 'Use your authenticator app to scan the QR code or enter this one time code:' ) }
  2. { this.renderInputBlock() }
{ this.state.otpAuthUri && }
); }; renderCodeBlock = () => { if ( 'sms' === this.state.method ) { return null; } return
{ this.renderQRCode() }
; }; renderInputHelp = () => { if ( 'sms' === this.state.method ) { return ( { this.props.translate( 'Enter the code you receive via SMS:' ) } ); } return

{ this.props.translate( 'Enter the six digit code from the app.' ) }

; }; clearLastError = () => { this.setState( { lastError: false, lastErrorType: false } ); }; possiblyRenderError = () => { if ( ! this.state.lastError ) { return null; } return ( ); }; renderSmsInputBlock = () => { return this.renderInputBlock( { this.props.translate( 'A code has been sent to your device via SMS. ' + 'You may request another code after one minute.' ) } ); }; renderInputBlock = ( children ) => { return (
{ this.renderInputHelp() } { children } { this.possiblyRenderError() }
); }; renderButtons = () => { return (
{ gaRecordEvent( 'Me', 'Clicked On Step 2 Cancel 2fa Button', 'method', this.state.method ); this.props.onCancel( event ); } } > { this.props.translate( 'Cancel' ) } { 'sms' === this.state.method && ( { gaRecordEvent( 'Me', 'Clicked On Resend SMS Button' ); this.onResendCode( event ); } } > { this.props.translate( 'Resend Code', { context: 'A button label to let a user get the SMS code sent again.', } ) } ) } { gaRecordEvent( 'Me', 'Clicked On Enable 2fa Button', 'method', this.state.method ); } } > { this.state.submittingCode ? this.props.translate( 'Enabling…', { context: 'A button label used during Two-Step setup.', } ) : this.props.translate( 'Enable', { context: 'A button label used during Two-Step setup.', } ) }
); }; render() { return (
{ this.state.method === 'sms' ? ( this.renderSmsInputBlock() ) : (
{ this.renderCodeBlock() }
) } { this.renderButtons() }
); } handleChange = ( e ) => { const { name, value } = e.currentTarget; this.setState( { [ name ]: value } ); }; } export default localize( Security2faEnable );