File size: 7,354 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
import { FormLabel } from '@automattic/components';
import debugFactory from 'debug';
import { localize } from 'i18n-calypso';
import PropTypes from 'prop-types';
import { Component } from 'react';
import FormButton from 'calypso/components/forms/form-button';
import FormButtonsBar from 'calypso/components/forms/form-buttons-bar';
import FormFieldset from 'calypso/components/forms/form-fieldset';
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 './style.scss';
const debug = debugFactory( 'calypso:me:security:2fa-code-prompt' );
class Security2faCodePrompt extends Component {
static displayName = 'Security2faCodePrompt';
static defaultProps = {
action: false,
requestSMSOnMount: false,
showCancelButton: true,
showSMSButton: true,
};
static propTypes = {
action: PropTypes.string,
onCancel: PropTypes.func,
onSuccess: PropTypes.func.isRequired,
requestSMSOnMount: PropTypes.bool,
showCancelButton: PropTypes.bool,
};
state = {
codeRequestPerformed: false,
codeRequestsAllowed: false,
lastError: false,
lastErrorType: false,
submittingCode: false,
verificationCode: '',
};
codeRequestTimer = false;
componentDidMount() {
debug( this.constructor.displayName + ' React component is mounted.' );
if ( this.props.requestSMSOnMount ) {
this.requestCode();
} else {
this.allowCodeRequests();
}
}
componentWillUnmount() {
debug( this.constructor.displayName + ' React component will unmount.' );
this.cancelCodeRequestTimer();
}
cancelCodeRequestTimer = () => {
if ( this.codeRequestTimer ) {
clearTimeout( this.codeRequestTimer );
}
};
allowCodeRequests = () => {
this.setState( { codeRequestsAllowed: true } );
};
onRequestCode = ( event ) => {
event.preventDefault();
this.requestCode();
};
onCancel = ( event ) => {
event.preventDefault();
if ( this.props.onCancel ) {
this.props.onCancel();
}
};
requestCode = () => {
this.setState( {
codeRequestsAllowed: false,
codeRequestPerformed: true,
lastError: false,
} );
twoStepAuthorization.sendSMSCode( this.onCodeRequestResponse );
this.codeRequestTimer = setTimeout( this.allowCodeRequests, 60000 );
};
onCodeRequestResponse = ( error ) => {
if ( error ) {
this.setState( {
codeRequestPerformed: false,
lastError: this.props.translate(
'Unable to request a code via SMS right now. Please try again after one minute.'
),
lastErrorType: 'is-info',
} );
}
};
onSubmit = ( event ) => {
event.preventDefault();
this.setState( { submittingCode: true }, this.onBeginCodeValidation );
};
onBeginCodeValidation = () => {
const args = {
code: this.state.verificationCode,
};
if ( this.props.action ) {
args.action = this.props.action;
}
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();
}
};
getSubmitButtonLabel = () => {
switch ( this.props.action ) {
case 'disable-two-step':
return this.state.submittingCode
? this.props.translate( 'Disabling Two-Step…' )
: this.props.translate( 'Disable Two-Step' );
case 'enable-two-step':
return this.state.submittingCode
? this.props.translate( 'Enabling Two-Step…' )
: this.props.translate( 'Enable Two-Step' );
default:
return this.state.submittingCode
? this.props.translate( 'Submitting…' )
: this.props.translate( 'Submit' );
}
};
clearLastError = () => {
this.setState( { lastError: false, lastErrorType: false } );
};
getFormDisabled = () => {
return this.state.submittingCode || 6 > this.state.verificationCode.trim().length;
};
possiblyRenderError = () => {
if ( ! this.state.lastError ) {
return null;
}
return (
<Notice
status={ this.state.lastErrorType }
onDismissClick={ this.clearLastError }
text={ this.state.lastError }
/>
);
};
render() {
const method = twoStepAuthorization.isTwoStepSMSEnabled() ? 'sms' : 'app';
const hasSmsRecoveryNumber = !! twoStepAuthorization?.data?.two_step_sms_last_four?.length;
return (
<form className="security-2fa-code-prompt" onSubmit={ this.onSubmit }>
<FormFieldset>
<FormLabel htmlFor="verification-code">
{ this.props.translate( 'Verification Code' ) }
</FormLabel>
<FormVerificationCodeInput
className="security-2fa-code-prompt__verification-code"
disabled={ this.state.submittingForm }
id="verification-code"
method={ method }
name="verificationCode"
onFocus={ function () {
gaRecordEvent( 'Me', 'Focused On 2fa Disable Code Verification Input' );
} }
value={ this.state.verificationCode }
onChange={ this.handleChange }
/>
{ this.state.codeRequestPerformed ? (
<FormSettingExplanation>
{ this.props.translate(
'A code has been sent to your device via SMS. ' +
'You may request another code after one minute.'
) }
</FormSettingExplanation>
) : null }
{ this.possiblyRenderError() }
</FormFieldset>
<FormButtonsBar className="security-2fa-code-prompt__buttons-bar">
<FormButton
className="security-2fa-code-prompt__verify-code"
disabled={ this.getFormDisabled() }
onClick={ function () {
gaRecordEvent( 'Me', 'Clicked On 2fa Code Prompt Verify Button' );
} }
>
{ this.getSubmitButtonLabel() }
</FormButton>
{ hasSmsRecoveryNumber && this.props.showSMSButton ? (
<FormButton
className="security-2fa-code-prompt__send-code"
disabled={ ! this.state.codeRequestsAllowed }
isPrimary={ false }
onClick={ ( event ) => {
gaRecordEvent( 'Me', 'Clicked On 2fa Code Prompt Send Code Via SMS Button' );
this.onRequestCode( event );
} }
>
{ this.state.codeRequestPerformed
? this.props.translate( 'Resend Code' )
: this.props.translate( 'Send Code via SMS' ) }
</FormButton>
) : null }
{ this.props.showCancelButton ? (
<FormButton
className="security-2fa-code-prompt__cancel"
isPrimary={ false }
onClick={ ( event ) => {
gaRecordEvent( 'Me', 'Clicked On Disable 2fa Cancel Button' );
this.onCancel( event );
} }
>
{ this.props.translate( 'Cancel' ) }
</FormButton>
) : null }
</FormButtonsBar>
</form>
);
}
handleChange = ( e ) => {
const { name, value } = e.currentTarget;
this.setState( { [ name ]: value } );
};
}
export default localize( Security2faCodePrompt );
|