2017-01-25 18:51:41 +01:00
|
|
|
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
|
2016-10-25 17:54:01 +02:00
|
|
|
// This file is part of Parity.
|
|
|
|
|
|
|
|
// Parity is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// Parity is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2016-12-28 18:09:45 +01:00
|
|
|
import { observer } from 'mobx-react';
|
2017-07-17 18:37:33 +02:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2016-12-28 18:09:45 +01:00
|
|
|
import { FormattedMessage } from 'react-intl';
|
2017-01-03 17:41:21 +01:00
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import { bindActionCreators } from 'redux';
|
2016-10-25 17:54:01 +02:00
|
|
|
|
2017-05-09 12:01:44 +02:00
|
|
|
import { newError, openSnackbar } from '@parity/shared/redux/actions';
|
2017-05-12 17:06:53 +02:00
|
|
|
import { Button, IdentityName, IdentityIcon, Portal, Tabs } from '@parity/ui';
|
2017-05-12 12:06:16 +02:00
|
|
|
import PasswordStrength from '@parity/ui/Form/PasswordStrength';
|
|
|
|
import Form, { Input } from '@parity/ui/Form';
|
|
|
|
import { CancelIcon, CheckIcon, SendIcon } from '@parity/ui/Icons';
|
2016-10-25 17:54:01 +02:00
|
|
|
|
2017-05-12 17:06:53 +02:00
|
|
|
import Store from './store';
|
2016-10-25 17:54:01 +02:00
|
|
|
import styles from './passwordManager.css';
|
|
|
|
|
2016-12-28 18:09:45 +01:00
|
|
|
const MSG_SUCCESS_STYLE = {
|
|
|
|
backgroundColor: 'rgba(174, 213, 129, 0.75)'
|
|
|
|
};
|
|
|
|
const MSG_FAILURE_STYLE = {
|
|
|
|
backgroundColor: 'rgba(229, 115, 115, 0.75)'
|
|
|
|
};
|
2017-05-04 11:03:55 +02:00
|
|
|
|
2016-12-28 18:09:45 +01:00
|
|
|
@observer
|
2017-01-03 17:41:21 +01:00
|
|
|
class PasswordManager extends Component {
|
2016-10-25 17:54:01 +02:00
|
|
|
static contextTypes = {
|
|
|
|
api: PropTypes.object.isRequired
|
|
|
|
}
|
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
account: PropTypes.object.isRequired,
|
2017-01-03 17:41:21 +01:00
|
|
|
openSnackbar: PropTypes.func.isRequired,
|
|
|
|
newError: PropTypes.func.isRequired,
|
2016-10-25 17:54:01 +02:00
|
|
|
onClose: PropTypes.func
|
|
|
|
}
|
|
|
|
|
2016-12-28 18:09:45 +01:00
|
|
|
store = new Store(this.context.api, this.props.account);
|
2016-10-25 17:54:01 +02:00
|
|
|
|
|
|
|
render () {
|
2017-03-22 16:39:49 +01:00
|
|
|
const { busy } = this.store;
|
|
|
|
|
2016-10-25 17:54:01 +02:00
|
|
|
return (
|
2017-02-22 15:26:58 +01:00
|
|
|
<Portal
|
2017-03-22 16:39:49 +01:00
|
|
|
busy={ busy }
|
2017-02-22 15:26:58 +01:00
|
|
|
buttons={ this.renderDialogActions() }
|
|
|
|
onClose={ this.onClose }
|
|
|
|
open
|
2016-12-28 18:09:45 +01:00
|
|
|
title={
|
|
|
|
<FormattedMessage
|
|
|
|
id='passwordChange.title'
|
2017-01-18 13:05:01 +01:00
|
|
|
defaultMessage='Password Manager'
|
|
|
|
/>
|
2016-12-28 18:09:45 +01:00
|
|
|
}
|
2017-01-18 13:05:01 +01:00
|
|
|
>
|
2016-10-25 17:54:01 +02:00
|
|
|
{ this.renderAccount() }
|
|
|
|
{ this.renderPage() }
|
|
|
|
{ this.renderMessage() }
|
2017-02-22 15:26:58 +01:00
|
|
|
</Portal>
|
2016-10-25 17:54:01 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderMessage () {
|
2016-12-28 18:09:45 +01:00
|
|
|
const { infoMessage } = this.store;
|
2016-10-25 17:54:01 +02:00
|
|
|
|
2016-12-28 18:09:45 +01:00
|
|
|
if (!infoMessage) {
|
|
|
|
return null;
|
2016-10-25 17:54:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2017-05-11 14:02:27 +02:00
|
|
|
<div
|
|
|
|
className={ styles.message }
|
2016-12-28 18:09:45 +01:00
|
|
|
style={
|
|
|
|
infoMessage.success
|
|
|
|
? MSG_SUCCESS_STYLE
|
|
|
|
: MSG_FAILURE_STYLE
|
|
|
|
}
|
2017-01-18 13:05:01 +01:00
|
|
|
>
|
2016-12-28 18:09:45 +01:00
|
|
|
{ infoMessage.value }
|
2017-05-11 14:02:27 +02:00
|
|
|
</div>
|
2016-10-25 17:54:01 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderAccount () {
|
2016-12-28 18:09:45 +01:00
|
|
|
const { address, passwordHint } = this.store;
|
2016-10-25 17:54:01 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div className={ styles.accountContainer }>
|
2016-12-28 18:09:45 +01:00
|
|
|
<IdentityIcon address={ address } />
|
2016-10-25 17:54:01 +02:00
|
|
|
<div className={ styles.accountInfos }>
|
|
|
|
<IdentityName
|
|
|
|
address={ address }
|
2016-12-28 18:09:45 +01:00
|
|
|
className={ styles.accountName }
|
2017-01-18 13:05:01 +01:00
|
|
|
unknown
|
|
|
|
/>
|
2016-10-25 17:54:01 +02:00
|
|
|
<span className={ styles.accountAddress }>
|
|
|
|
{ address }
|
|
|
|
</span>
|
2016-12-28 18:09:45 +01:00
|
|
|
<span className={ styles.passwordHint }>
|
2017-03-02 13:29:32 +01:00
|
|
|
<span className={ styles.hintLabel }>
|
|
|
|
<FormattedMessage
|
2017-03-27 11:42:59 +02:00
|
|
|
id='passwordChange.passwordHint.display'
|
2017-03-02 13:29:32 +01:00
|
|
|
defaultMessage='Hint {hint}'
|
|
|
|
values={ {
|
|
|
|
hint: passwordHint || '-'
|
|
|
|
} }
|
|
|
|
/>
|
|
|
|
</span>
|
2016-12-28 18:09:45 +01:00
|
|
|
</span>
|
2016-10-25 17:54:01 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderPage () {
|
2017-05-12 17:06:53 +02:00
|
|
|
const { activeTab } = this.store;
|
2017-05-04 11:03:55 +02:00
|
|
|
|
2016-10-25 17:54:01 +02:00
|
|
|
return (
|
2017-05-04 11:03:55 +02:00
|
|
|
<div>
|
2017-05-12 17:06:53 +02:00
|
|
|
<Tabs
|
|
|
|
activeTab={ activeTab }
|
|
|
|
tabs={ [
|
|
|
|
<FormattedMessage
|
|
|
|
id='passwordChange.tabChange'
|
|
|
|
defaultMessage='Change Password'
|
|
|
|
/>,
|
|
|
|
<FormattedMessage
|
|
|
|
id='passwordChange.tabTest'
|
|
|
|
defaultMessage='TestPassword'
|
|
|
|
/>
|
|
|
|
] }
|
|
|
|
onChange={ this.onChangeTab }
|
|
|
|
/>
|
|
|
|
{
|
|
|
|
activeTab === 1
|
|
|
|
? this.renderTabTest()
|
|
|
|
: this.renderTabChange()
|
|
|
|
}
|
2017-05-04 11:03:55 +02:00
|
|
|
</div>
|
2017-03-22 16:39:49 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-05-12 17:06:53 +02:00
|
|
|
onChangeTab = (event, activeTab) => {
|
|
|
|
this.store.setActiveTab(activeTab);
|
2017-05-04 11:03:55 +02:00
|
|
|
}
|
|
|
|
|
2017-03-22 16:39:49 +01:00
|
|
|
renderTabTest () {
|
2017-05-12 17:06:53 +02:00
|
|
|
const { busy } = this.store;
|
2017-03-22 16:39:49 +01:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Form className={ styles.form }>
|
|
|
|
<div>
|
|
|
|
<Input
|
|
|
|
autoFocus
|
|
|
|
disabled={ busy }
|
|
|
|
hint={
|
|
|
|
<FormattedMessage
|
|
|
|
id='passwordChange.testPassword.hint'
|
|
|
|
defaultMessage='your account password'
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
label={
|
|
|
|
<FormattedMessage
|
|
|
|
id='passwordChange.testPassword.label'
|
|
|
|
defaultMessage='password'
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
onChange={ this.onEditTestPassword }
|
|
|
|
onSubmit={ this.testPassword }
|
|
|
|
submitOnBlur={ false }
|
|
|
|
type='password'
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</Form>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderTabChange () {
|
2017-05-12 17:06:53 +02:00
|
|
|
const { busy, isRepeatValid, newPassword, passwordHint } = this.store;
|
2017-03-22 16:39:49 +01:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Form className={ styles.form }>
|
|
|
|
<div>
|
|
|
|
<Input
|
|
|
|
autoFocus
|
|
|
|
disabled={ busy }
|
|
|
|
hint={
|
|
|
|
<FormattedMessage
|
|
|
|
id='passwordChange.currentPassword.hint'
|
|
|
|
defaultMessage='your current password for this account'
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
label={
|
|
|
|
<FormattedMessage
|
|
|
|
id='passwordChange.currentPassword.label'
|
|
|
|
defaultMessage='current password'
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
onChange={ this.onEditCurrentPassword }
|
|
|
|
type='password'
|
|
|
|
/>
|
|
|
|
<Input
|
|
|
|
disabled={ busy }
|
|
|
|
hint={
|
|
|
|
<FormattedMessage
|
|
|
|
id='passwordChange.passwordHint.hint'
|
|
|
|
defaultMessage='hint for the new password'
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
label={
|
|
|
|
<FormattedMessage
|
|
|
|
id='passwordChange.passwordHint.label'
|
|
|
|
defaultMessage='(optional) new password hint'
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
onChange={ this.onEditNewPasswordHint }
|
|
|
|
value={ passwordHint }
|
|
|
|
/>
|
|
|
|
<div className={ styles.passwords }>
|
|
|
|
<div className={ styles.password }>
|
2016-10-25 17:54:01 +02:00
|
|
|
<Input
|
2016-12-28 18:09:45 +01:00
|
|
|
disabled={ busy }
|
|
|
|
hint={
|
|
|
|
<FormattedMessage
|
2017-03-22 16:39:49 +01:00
|
|
|
id='passwordChange.newPassword.hint'
|
|
|
|
defaultMessage='the new password for this account'
|
2017-01-18 13:05:01 +01:00
|
|
|
/>
|
2016-12-28 18:09:45 +01:00
|
|
|
}
|
|
|
|
label={
|
|
|
|
<FormattedMessage
|
2017-03-22 16:39:49 +01:00
|
|
|
id='passwordChange.newPassword.label'
|
|
|
|
defaultMessage='new password'
|
2017-01-18 13:05:01 +01:00
|
|
|
/>
|
2016-12-28 18:09:45 +01:00
|
|
|
}
|
2017-03-22 16:39:49 +01:00
|
|
|
onChange={ this.onEditNewPassword }
|
|
|
|
onSubmit={ this.changePassword }
|
|
|
|
submitOnBlur={ false }
|
2017-01-18 13:05:01 +01:00
|
|
|
type='password'
|
|
|
|
/>
|
2017-03-22 16:39:49 +01:00
|
|
|
</div>
|
|
|
|
<div className={ styles.password }>
|
2016-10-25 17:54:01 +02:00
|
|
|
<Input
|
2016-12-28 18:09:45 +01:00
|
|
|
disabled={ busy }
|
2017-03-22 16:39:49 +01:00
|
|
|
error={
|
|
|
|
isRepeatValid
|
|
|
|
? null
|
|
|
|
: <FormattedMessage
|
|
|
|
id='passwordChange.repeatPassword.error'
|
|
|
|
defaultMessage='the supplied passwords do not match'
|
|
|
|
/>
|
|
|
|
}
|
2016-12-28 18:09:45 +01:00
|
|
|
hint={
|
|
|
|
<FormattedMessage
|
2017-03-22 16:39:49 +01:00
|
|
|
id='passwordChange.repeatPassword.hint'
|
|
|
|
defaultMessage='repeat the new password for this account'
|
2017-01-18 13:05:01 +01:00
|
|
|
/>
|
2016-12-28 18:09:45 +01:00
|
|
|
}
|
|
|
|
label={
|
|
|
|
<FormattedMessage
|
2017-03-22 16:39:49 +01:00
|
|
|
id='passwordChange.repeatPassword.label'
|
|
|
|
defaultMessage='repeat new password'
|
2017-01-18 13:05:01 +01:00
|
|
|
/>
|
2016-12-28 18:09:45 +01:00
|
|
|
}
|
2017-03-22 16:39:49 +01:00
|
|
|
onChange={ this.onEditNewPasswordRepeat }
|
|
|
|
onSubmit={ this.changePassword }
|
|
|
|
submitOnBlur={ false }
|
|
|
|
type='password'
|
2017-01-18 13:05:01 +01:00
|
|
|
/>
|
2016-10-25 17:54:01 +02:00
|
|
|
</div>
|
2017-03-22 16:39:49 +01:00
|
|
|
</div>
|
|
|
|
|
|
|
|
<PasswordStrength input={ newPassword } />
|
|
|
|
</div>
|
|
|
|
</Form>
|
2016-10-25 17:54:01 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderDialogActions () {
|
2017-05-12 17:06:53 +02:00
|
|
|
const { activeTab, busy, isRepeatValid } = this.store;
|
2016-10-25 17:54:01 +02:00
|
|
|
|
|
|
|
const cancelBtn = (
|
|
|
|
<Button
|
2016-12-28 18:09:45 +01:00
|
|
|
icon={ <CancelIcon /> }
|
|
|
|
key='cancel'
|
|
|
|
label={
|
|
|
|
<FormattedMessage
|
|
|
|
id='passwordChange.button.cancel'
|
2017-01-18 13:05:01 +01:00
|
|
|
defaultMessage='Cancel'
|
|
|
|
/>
|
2016-12-28 18:09:45 +01:00
|
|
|
}
|
2017-02-22 15:26:58 +01:00
|
|
|
onClick={ this.onClose }
|
2017-01-18 13:05:01 +01:00
|
|
|
/>
|
2016-10-25 17:54:01 +02:00
|
|
|
);
|
|
|
|
|
2016-12-28 18:09:45 +01:00
|
|
|
if (busy) {
|
|
|
|
return [
|
|
|
|
cancelBtn,
|
2016-10-25 17:54:01 +02:00
|
|
|
<Button
|
|
|
|
disabled
|
2016-12-28 18:09:45 +01:00
|
|
|
key='wait'
|
|
|
|
label={
|
|
|
|
<FormattedMessage
|
|
|
|
id='passwordChange.button.wait'
|
2017-01-18 13:05:01 +01:00
|
|
|
defaultMessage='Wait...'
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
/>
|
2016-12-28 18:09:45 +01:00
|
|
|
];
|
2016-10-25 17:54:01 +02:00
|
|
|
}
|
|
|
|
|
2017-05-12 17:06:53 +02:00
|
|
|
if (activeTab === 1) {
|
2016-12-28 18:09:45 +01:00
|
|
|
return [
|
|
|
|
cancelBtn,
|
2016-10-25 17:54:01 +02:00
|
|
|
<Button
|
|
|
|
icon={ <CheckIcon /> }
|
2016-12-28 18:09:45 +01:00
|
|
|
key='test'
|
|
|
|
label={
|
|
|
|
<FormattedMessage
|
|
|
|
id='passwordChange.button.test'
|
2017-01-18 13:05:01 +01:00
|
|
|
defaultMessage='Test'
|
|
|
|
/>
|
2016-12-28 18:09:45 +01:00
|
|
|
}
|
2017-01-18 13:05:01 +01:00
|
|
|
onClick={ this.testPassword }
|
|
|
|
/>
|
2016-12-28 18:09:45 +01:00
|
|
|
];
|
2016-10-25 17:54:01 +02:00
|
|
|
}
|
|
|
|
|
2016-12-28 18:09:45 +01:00
|
|
|
return [
|
|
|
|
cancelBtn,
|
2016-10-25 17:54:01 +02:00
|
|
|
<Button
|
2016-12-28 18:09:45 +01:00
|
|
|
disabled={ !isRepeatValid }
|
2016-10-25 17:54:01 +02:00
|
|
|
icon={ <SendIcon /> }
|
2016-12-28 18:09:45 +01:00
|
|
|
key='change'
|
|
|
|
label={
|
|
|
|
<FormattedMessage
|
|
|
|
id='passwordChange.button.change'
|
2017-01-18 13:05:01 +01:00
|
|
|
defaultMessage='Change'
|
|
|
|
/>
|
2016-12-28 18:09:45 +01:00
|
|
|
}
|
2017-01-18 13:05:01 +01:00
|
|
|
onClick={ this.changePassword }
|
|
|
|
/>
|
2016-12-28 18:09:45 +01:00
|
|
|
];
|
2016-10-25 17:54:01 +02:00
|
|
|
}
|
|
|
|
|
2016-12-28 18:09:45 +01:00
|
|
|
onEditCurrentPassword = (event, password) => {
|
|
|
|
this.store.setPassword(password);
|
2016-10-25 17:54:01 +02:00
|
|
|
}
|
|
|
|
|
2016-12-28 18:09:45 +01:00
|
|
|
onEditNewPassword = (event, password) => {
|
|
|
|
this.store.setNewPassword(password);
|
2016-10-25 17:54:01 +02:00
|
|
|
}
|
|
|
|
|
2016-12-28 18:09:45 +01:00
|
|
|
onEditNewPasswordHint = (event, passwordHint) => {
|
|
|
|
this.store.setNewPasswordHint(passwordHint);
|
2016-10-25 17:54:01 +02:00
|
|
|
}
|
|
|
|
|
2016-12-28 18:09:45 +01:00
|
|
|
onEditNewPasswordRepeat = (event, password) => {
|
|
|
|
this.store.setNewPasswordRepeat(password);
|
2016-10-25 17:54:01 +02:00
|
|
|
}
|
|
|
|
|
2016-12-28 18:09:45 +01:00
|
|
|
onEditTestPassword = (event, password) => {
|
|
|
|
this.store.setValidatePassword(password);
|
2016-10-25 17:54:01 +02:00
|
|
|
}
|
|
|
|
|
2017-02-22 15:26:58 +01:00
|
|
|
onClose = () => {
|
|
|
|
this.props.onClose();
|
|
|
|
}
|
|
|
|
|
2016-12-28 18:09:45 +01:00
|
|
|
changePassword = () => {
|
|
|
|
return this.store
|
|
|
|
.changePassword()
|
|
|
|
.then((result) => {
|
|
|
|
if (result) {
|
2017-01-03 17:41:21 +01:00
|
|
|
this.props.openSnackbar(
|
2016-12-28 18:09:45 +01:00
|
|
|
<div>
|
|
|
|
<FormattedMessage
|
|
|
|
id='passwordChange.success'
|
2017-01-18 13:05:01 +01:00
|
|
|
defaultMessage='Your password has been successfully changed'
|
|
|
|
/>
|
2016-12-28 18:09:45 +01:00
|
|
|
</div>
|
|
|
|
);
|
2017-02-22 15:26:58 +01:00
|
|
|
this.onClose();
|
2016-10-25 17:54:01 +02:00
|
|
|
}
|
|
|
|
})
|
2016-12-28 18:09:45 +01:00
|
|
|
.catch((error) => {
|
2017-01-03 17:41:21 +01:00
|
|
|
this.props.newError(error);
|
2016-10-25 17:54:01 +02:00
|
|
|
});
|
|
|
|
}
|
2016-11-29 00:21:54 +01:00
|
|
|
|
2016-12-28 18:09:45 +01:00
|
|
|
testPassword = () => {
|
|
|
|
return this.store
|
|
|
|
.testPassword()
|
|
|
|
.catch((error) => {
|
2017-01-03 17:41:21 +01:00
|
|
|
this.props.newError(error);
|
2016-12-28 18:09:45 +01:00
|
|
|
});
|
|
|
|
}
|
2016-11-29 00:21:54 +01:00
|
|
|
}
|
2017-01-03 17:41:21 +01:00
|
|
|
|
|
|
|
function mapDispatchToProps (dispatch) {
|
|
|
|
return bindActionCreators({
|
|
|
|
openSnackbar,
|
|
|
|
newError
|
|
|
|
}, dispatch);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default connect(
|
|
|
|
null,
|
|
|
|
mapDispatchToProps
|
|
|
|
)(PasswordManager);
|