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 (
{ 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 (