Skip to content

Commit 2bc7a67

Browse files
author
Tim-Luca Lagmöller
committed
Refactored Code and fixed form issue
I refactored the code so that it matches the style of the project. I also modified the algorithm and fixed a security issue in the form cleanup process.
1 parent b8fc01d commit 2bc7a67

File tree

5 files changed

+91
-66
lines changed

5 files changed

+91
-66
lines changed

src/components/modal/PasswordModal/PasswordMeter.jsx

Lines changed: 0 additions & 59 deletions
This file was deleted.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import React from 'react';
2+
import PasswordMeterCSS from './PasswordMeter.module.css';
3+
import * as PasswordMeterLogic from './PasswordMeter.logic';
4+
5+
export default ({ password }) => {
6+
const strength = PasswordMeterLogic.scorePassword(password);
7+
const strengthMap = {
8+
null: PasswordMeterCSS.invisible,
9+
0: PasswordMeterCSS.weak,
10+
1: PasswordMeterCSS.good,
11+
2: PasswordMeterCSS.strong
12+
};
13+
14+
return (
15+
<div className={PasswordMeterCSS.meter}>
16+
<div className={PasswordMeterCSS.bar + ' ' + strengthMap[strength]} />
17+
</div>
18+
);
19+
};
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
export function scorePassword(password) {
2+
var score = 0;
3+
if (!password)
4+
return null;
5+
// award every unique letter until 5 repetitions
6+
var letters = {};
7+
for (var i = 0; i < password.length; i++) {
8+
letters[password[i]] = (letters[password[i]] || 0) + 1;
9+
score += 5.0 / letters[password[i]];
10+
}
11+
12+
// bonus points for mixing it up
13+
var variations = {
14+
digits: /\d/.test(password),
15+
lower: /[a-z]/.test(password),
16+
upper: /[A-Z]/.test(password),
17+
nonWords: /\W/.test(password),
18+
}
19+
20+
let variationCount = 0;
21+
for (var check in variations) {
22+
variationCount += (variations[check] === true) ? 1 : 0;
23+
}
24+
score += (variationCount - 1) * 10;
25+
if (score > 80)
26+
return 2;
27+
if (score > 60)
28+
return 1;
29+
if (score >= 0)
30+
return 0;
31+
32+
return null;
33+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
.meter {
2+
width: 100%;
3+
display: flex;
4+
}
5+
6+
.bar {
7+
width: 100%;
8+
margin: 1px;
9+
height: 6px;
10+
border-radius: 5px;
11+
}
12+
13+
.invisible {
14+
height: 0;
15+
margin: 0;
16+
}
17+
18+
.weak {
19+
background: #e74c3c;
20+
}
21+
22+
.good {
23+
background: #f1c40f;
24+
}
25+
26+
.strong {
27+
background: #27ae60;
28+
}

src/components/modal/PasswordModal/PasswordModal.jsx

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ import { Modal, Form, Input, Icon } from 'antd';
33
import { connect } from 'react-redux';
44
import { mapState, mapDispatch } from './PasswordModal.map';
55
import * as PasswordModalLogic from './PasswordModal.logic';
6-
import PasswordMeter from './PasswordMeter';
6+
import PasswordMeter from './PasswordMeter/PasswordMeter';
77

88
const PasswordModalForm = Form.create({ name: 'form_in_modal' })(
99
// eslint-disable-next-line
1010
class extends Component {
1111
render() {
1212
const { visible, onCancel, onCreate, form, hint, mode } = this.props;
1313
const { getFieldDecorator } = form;
14-
let currentPasswordValue = this.props.form.getFieldValue('password');
14+
let passwordField = this.props.form.getFieldValue('password');
1515

1616
return (
1717
<Modal
@@ -26,10 +26,8 @@ const PasswordModalForm = Form.create({ name: 'form_in_modal' })(
2626
{getFieldDecorator('password', {
2727
rules: [ { required: true, message: 'Please input your Password!' } ]
2828
})(<Input.Password placeholder="Input Password" />)}
29-
30-
{mode !== 'decrypt' ? (
31-
<PasswordMeter value={ currentPasswordValue }/>
32-
): ''}
29+
30+
{mode !== 'decrypt' ? <PasswordMeter password={passwordField} /> : ''}
3331
</Form.Item>
3432
{hint !== null || mode !== 'decrypt' ? (
3533
<Form.Item label="Hint">
@@ -71,12 +69,18 @@ class PasswordModal extends Component {
7169
this.formRef = formRef;
7270
};
7371

72+
onReset = () => {
73+
const form = this.formRef.props.form;
74+
form.resetFields();
75+
this.props.onReset();
76+
};
77+
7478
render() {
7579
return (
7680
<PasswordModalForm
7781
wrappedComponentRef={this.saveFormRef}
7882
visible={this.props.modal.show}
79-
onCancel={this.props.onReset}
83+
onCancel={this.onReset}
8084
onCreate={this.handleCreate}
8185
hint={this.props.hint}
8286
mode={this.props.mode}

0 commit comments

Comments
 (0)