File size: 4,955 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 |
import { Button, Card, Gridicon } from '@automattic/components';
import { localize } from 'i18n-calypso';
import { get } from 'lodash';
import { Component } from 'react';
import { connect } from 'react-redux';
import InlineSupportLink from 'calypso/components/inline-support-link';
import Notice from 'calypso/components/notice';
import SectionHeader from 'calypso/components/section-header';
import { isWebAuthnSupported } from 'calypso/lib/webauthn';
import wpcom from 'calypso/lib/wp';
import { recordGoogleEvent } from 'calypso/state/analytics/actions';
import Security2faKeyAdd from './add';
import Security2faKeyList from './list';
class Security2faKey extends Component {
state = {
isEnabled: false,
addingKey: false,
isBrowserSupported: isWebAuthnSupported(),
errorMessage: null,
security2faChallenge: {},
security2faKeys: [],
};
componentDidMount = () => {
this.getKeysFromServer();
};
getClickHandler = ( action, callback ) => {
return ( event ) => {
this.props.recordGoogleEvent( 'Me', 'Clicked on ' + action );
if ( callback ) {
callback( event );
}
};
};
addKeyStart = ( event ) => {
event.preventDefault();
this.setState( { addingKey: true, errorMessage: null } );
};
addKeyRegister = () => {
this.getKeysFromServer();
};
deleteKeyRegister = ( keyData ) => {
const { translate } = this.props;
this.setState( { errorMessage: false } );
wpcom.req.get( '/me/two-step/security-key/delete', { credential_id: keyData.id }, ( err ) => {
if ( null === err ) {
this.getKeysFromServer();
} else {
const errorMessage =
'invalid_operation' === err.error
? translate(
`Unable to delete the last WordPress.com security key while enhanced account security is active. Deleting it may result in losing access to your account. If you still want to remove it, please disable enhanced account security.`
)
: translate( 'The key could not be deleted. Please try again later.' );
this.setState( {
errorMessage,
} );
}
} );
};
addKeyCancel = () => {
this.setState( { addingKey: false } );
};
keysFromServer = ( err, data ) => {
if ( null === err ) {
this.setState( {
isEnabled: true,
addingKey: false,
security2faKeys: get( data, 'registrations', [] ),
} );
}
};
getChallenge = () => {
wpcom.req.get( '/me/two-step/security-key/registration_challenge', {}, this.setChallenge );
};
setChallenge = ( error, data ) => {
this.setState( { security2faChallenge: data } );
};
getKeysFromServer = () => {
wpcom.req.get( '/me/two-step/security-key/get', {}, this.keysFromServer );
};
render() {
const { translate } = this.props;
const {
isEnabled,
addingKey,
isBrowserSupported,
errorMessage,
security2faKeys,
security2faChallenge,
} = this.state;
if ( ! isEnabled ) {
return null;
}
return (
<div className="security-2fa-key">
<SectionHeader label={ translate( 'Security key' ) }>
{ ! addingKey && isBrowserSupported && (
<Button
compact
onClick={ this.getClickHandler( 'Register New Key Button', this.addKeyStart ) }
>
{ /* eslint-disable wpcalypso/jsx-gridicon-size */ }
<Gridicon icon="plus-small" size={ 16 } />
{ /* eslint-enable wpcalypso/jsx-gridicon-size */ }
{ translate( 'Register key' ) }
</Button>
) }
</SectionHeader>
{ addingKey && this.state.security2faChallenge && (
<Security2faKeyAdd
onRegister={ this.addKeyRegister }
onCancel={ this.addKeyCancel }
registerRequests={ security2faChallenge }
/>
) }
{ errorMessage && (
<Notice
status="is-error"
icon="notice"
text={ errorMessage }
onDismissClick={ () => this.setState( { errorMessage: null } ) }
/>
) }
{ ! addingKey && ! security2faKeys.length && (
<Card>
{ isBrowserSupported && (
<p>
{ translate(
'Security keys offer a more robust form of two-step authentication. Your security key may be a physical device, or you can use passkey support built into your browser.'
) }{ ' ' }
<InlineSupportLink
showIcon={ false }
supportContext="two-step-authentication-security-key"
>
{ translate( 'Learn more' ) }
</InlineSupportLink>
</p>
) }
{ ! isBrowserSupported && (
<p>
{ translate(
"Your browser doesn't support the FIDO2 security key standard yet. To use a second factor security key to sign in please try a supported browser like Chrome, Safari, or Firefox."
) }
</p>
) }
</Card>
) }
{ ! addingKey && !! security2faKeys.length && (
<Security2faKeyList
securityKeys={ security2faKeys }
onDelete={ this.deleteKeyRegister }
/>
) }
</div>
);
}
}
export default connect( null, {
recordGoogleEvent,
} )( localize( Security2faKey ) );
|