File size: 4,047 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
import { localize } from 'i18n-calypso';
import PropTypes from 'prop-types';
import { Component } from 'react';
import { connect } from 'react-redux';
import { isCurrentUserEmailVerified } from 'calypso/state/current-user/selectors';
import getUserSetting from 'calypso/state/selectors/get-user-setting';
import hasUserSettings from 'calypso/state/selectors/has-user-settings';
import isTwoStepEnabled from 'calypso/state/selectors/is-two-step-enabled';
import isTwoStepSmsEnabled from 'calypso/state/selectors/is-two-step-sms-enabled';
import { getOKIcon, getWarningIcon } from './icons.js';
import SecurityCheckupNavigationItem from './navigation-item';

class SecurityCheckupTwoFactorAuthentication extends Component {
	static propTypes = {
		areUserSettingsLoaded: PropTypes.bool,
		hasTwoStepEnabled: PropTypes.bool,
		hasTwoStepSmsEnabled: PropTypes.bool,
		hasTwoStepSecurityKeyEnabled: PropTypes.bool,
		hasTwoStepEnhancedSecurity: PropTypes.bool,
		translate: PropTypes.func.isRequired,
		twoStepSmsPhoneNumber: PropTypes.string,
		isEmailVerified: PropTypes.bool,
	};

	render() {
		const {
			areUserSettingsLoaded,
			hasTwoStepEnabled,
			hasTwoStepSmsEnabled,
			translate,
			twoStepSmsPhoneNumber,
			hasTwoStepSecurityKeyEnabled,
			hasTwoStepEnhancedSecurity,
			isEmailVerified,
		} = this.props;

		if ( ! areUserSettingsLoaded ) {
			return <SecurityCheckupNavigationItem isPlaceholder />;
		}

		let icon;
		let description;

		// Email verification is prioritized over other two-factor authentication conditions.
		if ( ! isEmailVerified ) {
			icon = getWarningIcon();
			description = translate(
				'To enable Two-Step Authentication, please verify your email address first.'
			);
		} else if ( ! hasTwoStepSmsEnabled && ! hasTwoStepEnabled ) {
			icon = getWarningIcon();
			description = translate( 'You do not have two-step authentication enabled.' );
		} else {
			icon = getOKIcon();

			if ( hasTwoStepEnhancedSecurity ) {
				description = translate(
					'You have two-step authentication {{strong}}enabled{{/strong}} using security keys.',
					{
						components: {
							strong: <strong />,
						},
					}
				);
			} else if ( hasTwoStepSmsEnabled ) {
				const options = {
					args: {
						phoneNumber: twoStepSmsPhoneNumber,
					},
					components: {
						strong: <strong />,
					},
				};

				description = hasTwoStepSecurityKeyEnabled
					? translate(
							'You have two-step authentication {{strong}}enabled{{/strong}} using SMS messages to {{strong}}%(phoneNumber)s{{/strong}}, and security keys have been registered.',
							options
					  )
					: translate(
							'You have two-step authentication {{strong}}enabled{{/strong}} using SMS messages to {{strong}}%(phoneNumber)s{{/strong}}.',
							options
					  );
			} else if ( hasTwoStepEnabled ) {
				const options = {
					components: {
						strong: <strong />,
					},
				};

				description = hasTwoStepSecurityKeyEnabled
					? translate(
							'You have two-step authentication {{strong}}enabled{{/strong}} using an app, and security keys have been registered.',
							options
					  )
					: translate(
							'You have two-step authentication {{strong}}enabled{{/strong}} using an app.',
							options
					  );
			}
		}

		return (
			<SecurityCheckupNavigationItem
				path="/me/security/two-step"
				materialIcon={ icon }
				text={ translate( 'Two-Step Authentication' ) }
				description={ description }
				disabled={ ! isEmailVerified }
			/>
		);
	}
}

export default connect( ( state ) => ( {
	areUserSettingsLoaded: hasUserSettings( state ),
	hasTwoStepEnabled: isTwoStepEnabled( state ),
	hasTwoStepSmsEnabled: isTwoStepSmsEnabled( state ),
	twoStepSmsPhoneNumber: getUserSetting( state, 'two_step_sms_phone_number' ),
	hasTwoStepSecurityKeyEnabled: getUserSetting( state, 'two_step_security_key_enabled' ),
	hasTwoStepEnhancedSecurity: getUserSetting( state, 'two_step_enhanced_security' ),
	isEmailVerified: isCurrentUserEmailVerified( state ),
} ) )( localize( SecurityCheckupTwoFactorAuthentication ) );