File size: 5,249 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
import { Card } from '@automattic/components';
import { Button } from '@wordpress/components';
import { localize } from 'i18n-calypso';
import PropTypes from 'prop-types';
import { Component, Fragment } from 'react';
import { connect } from 'react-redux';
import { FormDivider } from 'calypso/blocks/authentication';
import getGravatarOAuth2Flow from 'calypso/lib/get-gravatar-oauth2-flow';
import { isGravPoweredOAuth2Client } from 'calypso/lib/oauth2-clients';
import { isWebAuthnSupported } from 'calypso/lib/webauthn';
import { recordTracksEventWithClientId as recordTracksEvent } from 'calypso/state/analytics/actions';
import { sendSmsCode } from 'calypso/state/login/actions';
import { isTwoFactorAuthTypeSupported } from 'calypso/state/login/selectors';
import { getCurrentOAuth2Client } from 'calypso/state/oauth2-clients/ui/selectors';
import getIsWoo from 'calypso/state/selectors/get-is-woo';

import './two-factor-actions.scss';

class TwoFactorActions extends Component {
	static propTypes = {
		oauth2Client: PropTypes.object.isRequired,
		isAuthenticatorSupported: PropTypes.bool.isRequired,
		isSecurityKeySupported: PropTypes.bool.isRequired,
		isSmsSupported: PropTypes.bool.isRequired,
		isWoo: PropTypes.bool.isRequired,
		recordTracksEvent: PropTypes.func.isRequired,
		sendSmsCode: PropTypes.func.isRequired,
		switchTwoFactorAuthType: PropTypes.func.isRequired,
		translate: PropTypes.func.isRequired,
		twoFactorAuthType: PropTypes.string.isRequired,
	};

	sendSmsCode = ( event ) => {
		event.preventDefault();

		this.props.recordTracksEvent( 'calypso_login_two_factor_switch_to_sms_link_click' );

		this.props.switchTwoFactorAuthType( 'sms' );

		if ( isGravPoweredOAuth2Client( this.props.oauth2Client ) ) {
			// Pass the OAuth2 client's flow name to customize the SMS message for Gravatar-powered OAuth2 clients.
			this.props.sendSmsCode( getGravatarOAuth2Flow( this.props.oauth2Client ) );
		} else {
			this.props.sendSmsCode();
		}
	};

	recordAuthenticatorLinkClick = ( event ) => {
		event.preventDefault();

		this.props.recordTracksEvent( 'calypso_login_two_factor_switch_to_authenticator_link_click' );

		this.props.switchTwoFactorAuthType( 'authenticator' );
	};

	recordBackupLinkClick = ( event ) => {
		event.preventDefault();

		this.props.recordTracksEvent( 'calypso_login_two_factor_switch_to_backup_link_click' );

		this.props.switchTwoFactorAuthType( 'backup' );
	};

	recordSecurityKey = ( event ) => {
		event.preventDefault();
		this.props.switchTwoFactorAuthType( 'webauthn' );
	};

	render() {
		const {
			isAuthenticatorSupported,
			isBackupCodeSupported,
			isSecurityKeySupported,
			isSmsSupported,
			isWoo,
			translate,
			twoFactorAuthType,
		} = this.props;

		const isSmsAvailable = isSmsSupported && twoFactorAuthType !== 'sms';
		const isBackupCodeAvailable = isBackupCodeSupported && twoFactorAuthType !== 'backup';
		const isAuthenticatorAvailable =
			isAuthenticatorSupported && twoFactorAuthType !== 'authenticator';
		const isSecurityKeyAvailable =
			isWebAuthnSupported() && isSecurityKeySupported && twoFactorAuthType !== 'webauthn';

		if (
			! isSmsAvailable &&
			! isAuthenticatorAvailable &&
			! isSecurityKeyAvailable &&
			! isBackupCodeAvailable
		) {
			return null;
		}

		return (
			<Fragment>
				{ isWoo && twoFactorAuthType !== 'push' && <FormDivider /> }
				<Card className="two-factor-authentication__actions">
					{ isSecurityKeyAvailable && (
						<Button
							variant="secondary"
							className="a8c-components-wp-button"
							data-e2e-link="2fa-security-key-link"
							onClick={ this.recordSecurityKey }
							__next40pxDefaultSize
						>
							{ translate( 'Continue with your security\u00A0key' ) }
						</Button>
					) }

					{ isSmsAvailable && (
						<Button
							variant="secondary"
							className="a8c-components-wp-button"
							data-e2e-link="2fa-sms-link"
							onClick={ this.sendSmsCode }
							__next40pxDefaultSize
						>
							{ translate( 'Send code via\u00A0text\u00A0message' ) }
						</Button>
					) }

					{ isAuthenticatorAvailable && (
						<Button
							variant="secondary"
							className="a8c-components-wp-button"
							data-e2e-link="2fa-otp-link"
							onClick={ this.recordAuthenticatorLinkClick }
							__next40pxDefaultSize
						>
							{ translate( 'Use authenticator\u00A0app instead' ) }
						</Button>
					) }

					{ isBackupCodeAvailable && (
						<Button
							variant="secondary"
							className="a8c-components-wp-button"
							onClick={ this.recordBackupLinkClick }
							__next40pxDefaultSize
						>
							{ translate( 'Use a backup code instead' ) }
						</Button>
					) }
				</Card>
			</Fragment>
		);
	}
}

export default connect(
	( state ) => {
		const oauth2Client = getCurrentOAuth2Client( state );

		return {
			oauth2Client,
			isAuthenticatorSupported: isTwoFactorAuthTypeSupported( state, 'authenticator' ),
			isBackupCodeSupported: isTwoFactorAuthTypeSupported( state, 'backup' ),
			isSmsSupported: isTwoFactorAuthTypeSupported( state, 'sms' ),
			isSecurityKeySupported: isTwoFactorAuthTypeSupported( state, 'webauthn' ),
			isWoo: getIsWoo( state ),
		};
	},
	{
		recordTracksEvent,
		sendSmsCode,
	}
)( localize( TwoFactorActions ) );