Skip to content

Commit 010a917

Browse files
committed
Fixes #5 - better validation on adding password
Now using regular expression to check password's alias and value.
1 parent 272bbb9 commit 010a917

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

python_password/PyPassword.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# -*- coding: utf-8 -*-
22
"""Simple password manager app."""
33
from os import urandom
4+
from re import match
45
from webbrowser import open_new_tab
56

67
from cryptography.fernet import InvalidToken
@@ -15,6 +16,7 @@
1516
from kivymd.uix.list import OneLineListItem, ThreeLineIconListItem
1617
from pyperclip import copy
1718

19+
from python_password.exceptions.validation import *
1820
from python_password.utils.crypto import *
1921
from python_password.utils.database import *
2022
from python_password.utils.files import *
@@ -166,18 +168,23 @@ def add_password(self):
166168
if self.masters_ok():
167169
ok_alias = self.validate_input(alias_box, 3)
168170
ok_value = self.validate_input(value_box, 6)
169-
if not (ok_alias and ok_value):
171+
if ok_alias is ValueTooShort or ok_value is ValueTooShort:
170172
result_dialog = SimpleDialog(
171173
title='Whoops!',
172-
text='The entered values are too short or invalid.'
174+
text='At least one value is too short.'
175+
).alert()
176+
elif ok_alias is PatternError or ok_value is PatternError:
177+
result_dialog = SimpleDialog(
178+
title='Whoops!',
179+
text='At last one value is invalid.'
173180
).alert()
174181
else:
175182
password_alias = alias_box.text.capitalize()
176183
password_value = value_box.text
177184
if already_exists(password_alias):
178185
result_dialog = SimpleDialog(
179186
title='Whoops!',
180-
text='That password already exists or not all settings are set.'
187+
text='That password already exists.'
181188
).alert()
182189
else:
183190
save_password(
@@ -518,14 +525,21 @@ def dismiss_and_back(self, instance, where='passwords'):
518525
self.root.ids.screen_manager.current = where
519526
instance.dismiss()
520527

521-
def validate_input(self, instance, length):
528+
def validate_input(self, instance, length: int):
522529
"""
523530
Checks text input.
524531
:param instance: Which widget has to be checked.
525532
:param length: Minimum length of provided text.
526533
"""
527-
instance.error = True if len(instance.text) < length else False
528-
return not instance.error
534+
if len(instance.text) < length:
535+
instance.error = True
536+
return ValueTooShort
537+
elif match('^[A-Za-z0-9][A-Za-z0-9 &\\-_]+[A-Za-z0-9]$', instance.text) is None:
538+
instance.error = True
539+
return PatternError
540+
else:
541+
instance.error = False
542+
return True
529543

530544

531545
if __name__ == '__main__':

python_password/exceptions/__init__.py

Whitespace-only changes.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# -*- coding: utf-8 -*-
2+
class ValidateError(BaseException):
3+
pass
4+
5+
6+
class ValueTooShort(ValidateError):
7+
pass
8+
9+
10+
class PatternError(ValidateError):
11+
pass

0 commit comments

Comments
 (0)