2016-12-11 19:31:31 +01:00
|
|
|
// Copyright 2015, 2016 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/>.
|
|
|
|
|
|
|
|
import Paper from 'material-ui/Paper';
|
2016-12-28 18:09:45 +01:00
|
|
|
import { Tabs, Tab } from 'material-ui/Tabs';
|
|
|
|
import { observer } from 'mobx-react';
|
|
|
|
import React, { Component, PropTypes } from 'react';
|
|
|
|
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-01-03 17:41:21 +01:00
|
|
|
import { newError, openSnackbar } from '~/redux/actions';
|
2017-01-13 15:52:42 +01:00
|
|
|
import { Button, Modal, IdentityName, IdentityIcon, PasswordStrength } from '~/ui';
|
2016-12-28 18:09:45 +01:00
|
|
|
import Form, { Input } from '~/ui/Form';
|
|
|
|
import { CancelIcon, CheckIcon, SendIcon } from '~/ui/Icons';
|
2016-10-25 17:54:01 +02:00
|
|
|
|
2016-12-28 18:09:45 +01:00
|
|
|
import Store, { CHANGE_ACTION, TEST_ACTION } 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)'
|
|
|
|
};
|
|
|
|
const TABS_INKBAR_STYLE = {
|
|
|
|
backgroundColor: 'rgba(255, 255, 255, 0.55)'
|
|
|
|
};
|
|
|
|
const TABS_ITEM_STYLE = {
|
|
|
|
backgroundColor: 'rgba(255, 255, 255, 0.05)'
|
|
|
|
};
|
|
|
|
|
|
|
|
@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 () {
|
|
|
|
return (
|
|
|
|
<Modal
|
|
|
|
actions={ this.renderDialogActions() }
|
2016-12-28 18:09:45 +01:00
|
|
|
title={
|
|
|
|
<FormattedMessage
|
|
|
|
id='passwordChange.title'
|
|
|
|
defaultMessage='Password Manager' />
|
|
|
|
}
|
2016-10-25 17:54:01 +02:00
|
|
|
visible>
|
|
|
|
{ this.renderAccount() }
|
|
|
|
{ this.renderPage() }
|
|
|
|
{ this.renderMessage() }
|
|
|
|
</Modal>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
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 (
|
|
|
|
<Paper
|
2016-12-28 18:09:45 +01:00
|
|
|
className={ `${styles.message}` }
|
|
|
|
style={
|
|
|
|
infoMessage.success
|
|
|
|
? MSG_SUCCESS_STYLE
|
|
|
|
: MSG_FAILURE_STYLE
|
|
|
|
}
|
|
|
|
zDepth={ 1 }>
|
|
|
|
{ infoMessage.value }
|
2016-10-25 17:54:01 +02:00
|
|
|
</Paper>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
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 }
|
|
|
|
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 }>
|
|
|
|
<span className={ styles.hintLabel }>Hint </span>
|
|
|
|
{ passwordHint || '-' }
|
|
|
|
</span>
|
2016-10-25 17:54:01 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderPage () {
|
2017-01-13 15:52:42 +01:00
|
|
|
const { busy, isRepeatValid, newPassword, passwordHint } = this.store;
|
2016-10-25 17:54:01 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Tabs
|
2016-12-28 18:09:45 +01:00
|
|
|
inkBarStyle={ TABS_INKBAR_STYLE }
|
|
|
|
tabItemContainerStyle={ TABS_ITEM_STYLE }>
|
2016-10-25 17:54:01 +02:00
|
|
|
<Tab
|
2016-12-28 18:09:45 +01:00
|
|
|
label={
|
|
|
|
<FormattedMessage
|
|
|
|
id='passwordChange.tabTest.label'
|
|
|
|
defaultMessage='Test Password' />
|
|
|
|
}
|
|
|
|
onActive={ this.onActivateTestTab }>
|
|
|
|
<Form className={ styles.form }>
|
2016-10-25 17:54:01 +02:00
|
|
|
<div>
|
|
|
|
<Input
|
2016-12-28 18:09:45 +01:00
|
|
|
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 }
|
2016-10-25 17:54:01 +02:00
|
|
|
submitOnBlur={ false }
|
2016-12-28 18:09:45 +01:00
|
|
|
type='password' />
|
2016-10-25 17:54:01 +02:00
|
|
|
</div>
|
|
|
|
</Form>
|
|
|
|
</Tab>
|
|
|
|
<Tab
|
2016-12-28 18:09:45 +01:00
|
|
|
label={
|
|
|
|
<FormattedMessage
|
|
|
|
id='passwordChange.tabChange.label'
|
|
|
|
defaultMessage='Change Password' />
|
|
|
|
}
|
|
|
|
onActive={ this.onActivateChangeTab }>
|
|
|
|
<Form className={ styles.form }>
|
2016-10-25 17:54:01 +02:00
|
|
|
<div>
|
|
|
|
<Input
|
2016-12-28 18:09:45 +01:00
|
|
|
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' />
|
2016-10-25 17:54:01 +02:00
|
|
|
<Input
|
2016-12-28 18:09:45 +01:00
|
|
|
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 } />
|
2016-10-29 17:39:08 +02:00
|
|
|
<div className={ styles.passwords }>
|
|
|
|
<div className={ styles.password }>
|
|
|
|
<Input
|
2016-12-28 18:09:45 +01:00
|
|
|
disabled={ busy }
|
|
|
|
hint={
|
|
|
|
<FormattedMessage
|
|
|
|
id='passwordChange.newPassword.hint'
|
|
|
|
defaultMessage='the new password for this account' />
|
|
|
|
}
|
|
|
|
label={
|
|
|
|
<FormattedMessage
|
|
|
|
id='passwordChange.newPassword.label'
|
|
|
|
defaultMessage='new password' />
|
|
|
|
}
|
|
|
|
onChange={ this.onEditNewPassword }
|
|
|
|
onSubmit={ this.changePassword }
|
2016-10-29 17:39:08 +02:00
|
|
|
submitOnBlur={ false }
|
2016-12-28 18:09:45 +01:00
|
|
|
type='password' />
|
2016-10-29 17:39:08 +02:00
|
|
|
</div>
|
|
|
|
<div className={ styles.password }>
|
|
|
|
<Input
|
2016-12-28 18:09:45 +01:00
|
|
|
disabled={ busy }
|
|
|
|
error={
|
|
|
|
isRepeatValid
|
|
|
|
? null
|
|
|
|
: <FormattedMessage
|
|
|
|
id='passwordChange.repeatPassword.error'
|
|
|
|
defaultMessage='the supplied passwords do not match' />
|
|
|
|
}
|
|
|
|
hint={
|
|
|
|
<FormattedMessage
|
|
|
|
id='passwordChange.repeatPassword.hint'
|
|
|
|
defaultMessage='repeat the new password for this account' />
|
|
|
|
}
|
|
|
|
label={
|
|
|
|
<FormattedMessage
|
|
|
|
id='passwordChange.repeatPassword.label'
|
|
|
|
defaultMessage='repeat new password' />
|
|
|
|
}
|
|
|
|
onChange={ this.onEditNewPasswordRepeat }
|
|
|
|
onSubmit={ this.changePassword }
|
2016-10-29 17:39:08 +02:00
|
|
|
submitOnBlur={ false }
|
2016-12-28 18:09:45 +01:00
|
|
|
type='password' />
|
2016-10-29 17:39:08 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
2017-01-13 15:52:42 +01:00
|
|
|
|
|
|
|
<PasswordStrength input={ newPassword } />
|
2016-10-25 17:54:01 +02:00
|
|
|
</div>
|
|
|
|
</Form>
|
|
|
|
</Tab>
|
|
|
|
</Tabs>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderDialogActions () {
|
2016-12-28 18:09:45 +01:00
|
|
|
const { actionTab, busy, isRepeatValid } = this.store;
|
2016-10-25 17:54:01 +02:00
|
|
|
const { onClose } = this.props;
|
|
|
|
|
|
|
|
const cancelBtn = (
|
|
|
|
<Button
|
2016-12-28 18:09:45 +01:00
|
|
|
icon={ <CancelIcon /> }
|
|
|
|
key='cancel'
|
|
|
|
label={
|
|
|
|
<FormattedMessage
|
|
|
|
id='passwordChange.button.cancel'
|
|
|
|
defaultMessage='Cancel' />
|
|
|
|
}
|
2016-10-25 17:54:01 +02:00
|
|
|
onClick={ onClose } />
|
|
|
|
);
|
|
|
|
|
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'
|
|
|
|
defaultMessage='Wait...' />
|
|
|
|
} />
|
|
|
|
];
|
2016-10-25 17:54:01 +02:00
|
|
|
}
|
|
|
|
|
2016-12-28 18:09:45 +01:00
|
|
|
if (actionTab === TEST_ACTION) {
|
|
|
|
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'
|
|
|
|
defaultMessage='Test' />
|
|
|
|
}
|
|
|
|
onClick={ this.testPassword } />
|
|
|
|
];
|
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'
|
|
|
|
defaultMessage='Change' />
|
|
|
|
}
|
|
|
|
onClick={ this.changePassword } />
|
|
|
|
];
|
2016-10-25 17:54:01 +02:00
|
|
|
}
|
|
|
|
|
2016-12-28 18:09:45 +01:00
|
|
|
onActivateChangeTab = () => {
|
|
|
|
this.store.setActionTab(CHANGE_ACTION);
|
2016-10-25 17:54:01 +02:00
|
|
|
}
|
|
|
|
|
2016-12-28 18:09:45 +01:00
|
|
|
onActivateTestTab = () => {
|
|
|
|
this.store.setActionTab(TEST_ACTION);
|
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
|
|
|
}
|
|
|
|
|
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'
|
|
|
|
defaultMessage='Your password has been successfully changed' />
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
this.props.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);
|