|
1 | 1 | # -*- coding: utf-8 -*- |
2 | 2 | """Simple password manager app.""" |
3 | 3 | from os import urandom |
| 4 | +from re import match |
4 | 5 | from webbrowser import open_new_tab |
5 | 6 |
|
6 | 7 | from cryptography.fernet import InvalidToken |
|
15 | 16 | from kivymd.uix.list import OneLineListItem, ThreeLineIconListItem |
16 | 17 | from pyperclip import copy |
17 | 18 |
|
| 19 | +from python_password.exceptions.validation import * |
18 | 20 | from python_password.utils.crypto import * |
19 | 21 | from python_password.utils.database import * |
20 | 22 | from python_password.utils.files import * |
@@ -166,18 +168,23 @@ def add_password(self): |
166 | 168 | if self.masters_ok(): |
167 | 169 | ok_alias = self.validate_input(alias_box, 3) |
168 | 170 | 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: |
170 | 172 | result_dialog = SimpleDialog( |
171 | 173 | 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.' |
173 | 180 | ).alert() |
174 | 181 | else: |
175 | 182 | password_alias = alias_box.text.capitalize() |
176 | 183 | password_value = value_box.text |
177 | 184 | if already_exists(password_alias): |
178 | 185 | result_dialog = SimpleDialog( |
179 | 186 | title='Whoops!', |
180 | | - text='That password already exists or not all settings are set.' |
| 187 | + text='That password already exists.' |
181 | 188 | ).alert() |
182 | 189 | else: |
183 | 190 | save_password( |
@@ -518,14 +525,21 @@ def dismiss_and_back(self, instance, where='passwords'): |
518 | 525 | self.root.ids.screen_manager.current = where |
519 | 526 | instance.dismiss() |
520 | 527 |
|
521 | | - def validate_input(self, instance, length): |
| 528 | + def validate_input(self, instance, length: int): |
522 | 529 | """ |
523 | 530 | Checks text input. |
524 | 531 | :param instance: Which widget has to be checked. |
525 | 532 | :param length: Minimum length of provided text. |
526 | 533 | """ |
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 |
529 | 543 |
|
530 | 544 |
|
531 | 545 | if __name__ == '__main__': |
|
0 commit comments