forked from pinae/ctSESAM-pyside
-
Notifications
You must be signed in to change notification settings - Fork 0
/
password_setting.py
636 lines (549 loc) · 20.2 KB
/
password_setting.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
Sets of password settings for a domain.
"""
from datetime import datetime
import getpass
import string
import re
import binascii
from base64 import b64encode, b64decode
from random import shuffle
from crypter import Crypter
DEFAULT_CHARACTER_SET_LOWER_CASE = string.ascii_lowercase
DEFAULT_CHARACTER_SET_UPPER_CASE = string.ascii_uppercase
DEFAULT_CHARACTER_SET_DIGITS = string.digits
DEFAULT_CHARACTER_SET_EXTRA = '#!"§$%&/()[]{}=-_+*<>;:.'
class PasswordSetting(object):
"""
This saves one set of settings for a certain domain. Use a PasswordSettingsManager to save the settings to a file.
"""
def __init__(self, domain):
self.domain = domain
self.url = None
self.username = None
self.legacy_password = None
self.notes = None
self.iterations = 4096
self.salt = Crypter.createSalt()
self.creation_date = datetime.now()
self.modification_date = self.creation_date
self.extra_characters = DEFAULT_CHARACTER_SET_EXTRA
self.template = 'x'*10
self.calculate_template(True, True, True, True)
self.synced = False
def __str__(self):
output = "<" + self.domain + ": ("
if self.username:
output += "username: " + str(self.username) + ", "
if self.legacy_password:
output += "legacy password: " + str(self.legacy_password) + ", "
if self.notes:
output += "notes: " + str(self.notes) + ", "
output += "iterations: " + str(self.iterations) + ", "
output += "salt: " + str(binascii.hexlify(self.salt)) + ", "
output += "template: " + str(self.template) + ", "
output += "modification date: " + self.get_modification_date() + ", "
output += "creation date: " + self.get_creation_date() + ", "
if self.extra_characters:
output += "extra characters: \"" + self.extra_characters + "\", "
if self.synced:
output += "synced"
else:
output += "not synced"
output += ")>"
return output
def get_domain(self):
"""
Returns the domain name or another string used in the domain field.
:return: the domain
:rtype: str
"""
return self.domain
def set_domain(self, domain):
"""
Change the domain string.
:param domain: the domain
:type domain: str
"""
self.domain = domain
self.synced = False
def has_username(self):
"""
Returns True if the username is set.
:return:
:rtype: bool
"""
return self.username and len(str(self.username)) > 0
def get_username(self):
"""
Returns the username or an empty string if there was no username.
:return: the username
:rtype: str
"""
if self.username:
return self.username
else:
return ""
def set_username(self, username):
"""
Set the username.
:param username: the username
:type username: str
"""
if username != self.username:
self.synced = False
self.username = username
def has_legacy_password(self):
"""
Returns True if the legacy password is set.
:return:
:rtype: bool
"""
return self.legacy_password and len(str(self.legacy_password)) > 0
def get_legacy_password(self):
"""
Returns the legacy password if set or an empty string otherwise.
:return: the legacy password
:rtype: str
"""
if self.legacy_password:
return self.legacy_password
else:
return ""
def set_legacy_password(self, legacy_password):
"""
Set a legacy password.
:param legacy_password: a legacy password
:type legacy_password: str
"""
if legacy_password != self.legacy_password:
self.synced = False
self.legacy_password = legacy_password
@staticmethod
def get_default_character_set():
"""
Returns the default character set.
:return: the default character set
:rtype: str
"""
return DEFAULT_CHARACTER_SET_DIGITS + DEFAULT_CHARACTER_SET_LOWER_CASE + DEFAULT_CHARACTER_SET_UPPER_CASE + \
DEFAULT_CHARACTER_SET_EXTRA
@staticmethod
def get_lower_case_character_set():
"""
Returns the lower case character set.
:return: the lower case character set
:rtype: str
"""
return DEFAULT_CHARACTER_SET_LOWER_CASE
@staticmethod
def get_upper_case_character_set():
"""
Returns the upper case character set.
:return: the upper case character set
:rtype: str
"""
return DEFAULT_CHARACTER_SET_UPPER_CASE
@staticmethod
def get_digits_character_set():
"""
Returns the digits character set.
:return: the digits character set
:rtype: str
"""
return DEFAULT_CHARACTER_SET_DIGITS
def get_character_set(self):
"""
Returns the character set as a string.
:return: character set
:rtype: str
"""
used_characters = ""
if 'n' in self.get_template():
used_characters += DEFAULT_CHARACTER_SET_DIGITS
if 'a' in self.get_template():
used_characters += DEFAULT_CHARACTER_SET_LOWER_CASE
if 'A' in self.get_template():
used_characters += DEFAULT_CHARACTER_SET_UPPER_CASE
if 'o' in self.get_template():
used_characters += self.get_extra_character_set()
return used_characters
def get_extra_character_set(self):
"""
Returns the set of special characters.
:return: set of special characters
:rtype: str
"""
return self.extra_characters
def set_extra_character_set(self, extra_set):
"""
Sets the set of special characters. This function does not check if these characters are in the whole
character set.
:param extra_set: string of special characters
:type extra_set: str
"""
if extra_set is None or len(extra_set) <= 0:
self.extra_characters = DEFAULT_CHARACTER_SET_EXTRA
else:
self.extra_characters = extra_set
def get_salt(self):
"""
Returns the salt.
:return: the salt
:rtype: bytes
"""
return self.salt
def set_salt(self, salt):
"""
You should normally pass bytes as a salt. For convenience this method also accepts strings which get
UTF-8 encoded and stored in binary format. If in doubt pass bytes.
:param salt:
:type salt: bytes or str
"""
if type(salt) == bytes:
if self.salt != salt:
self.synced = False
self.salt = salt
elif type(salt) == str:
if self.salt != salt.encode('utf-8'):
self.synced = False
self.salt = salt.encode('utf-8')
else:
raise TypeError("The salt should be bytes.")
def new_salt(self):
"""
Creates a new salt for the setting.
"""
self.salt = Crypter.createSalt()
def get_length(self):
"""
Returns the desired password length.
:return: length
:rtype: int
"""
return len(self.get_template())
def get_iterations(self):
"""
Returns the iteration count which is to be used.
:return: iteration count
:rtype: int
"""
return self.iterations
def set_iterations(self, iterations):
"""
Sets the iteration count integer.
:param iterations:
:type iterations: int
"""
if self.iterations != iterations:
self.synced = False
self.iterations = iterations
def get_c_date(self):
"""
Returns the creation date as a datetime object.
:return: the creation date
:rtype: datetime
"""
return self.creation_date
def get_creation_date(self):
"""
Returns the creation date as string.
:return: the creation date
:rtype: str
"""
return self.creation_date.strftime("%Y-%m-%dT%H:%M:%S")
def set_creation_date(self, creation_date):
"""
Sets the creation date passed as string.
:param creation_date:
:type creation_date: str
"""
if self.creation_date != creation_date:
self.synced = False
try:
self.creation_date = datetime.strptime(creation_date, "%Y-%m-%dT%H:%M:%S")
except ValueError:
print("This date has a wrong format: " + creation_date)
if self.modification_date < self.creation_date:
self.modification_date = self.creation_date
def get_m_date(self):
"""
Returns the modification date as a datetime object.
:return: the modification date
:rtype: datetime
"""
return self.modification_date
def get_modification_date(self):
"""
Returns the modification date as string.
:return: the modification date
:rtype: str
"""
return self.modification_date.strftime("%Y-%m-%dT%H:%M:%S")
def set_modification_date(self, modification_date=None):
"""
Sets the modification date passed as string.
:param modification_date:
:type modification_date: str
"""
if modification_date and self.modification_date != modification_date:
self.synced = False
if type(modification_date) == str:
try:
self.modification_date = datetime.strptime(modification_date, "%Y-%m-%dT%H:%M:%S")
except ValueError:
print("This date has a wrong format: " + modification_date)
else:
self.modification_date = datetime.now()
if self.modification_date < self.creation_date:
print("The modification date was before the creation Date. " +
"Setting the creation date to the earlier date.")
self.creation_date = self.modification_date
def get_notes(self):
"""
Returns the notes.
:return: the notes
:rtype: str
"""
if self.notes:
return self.notes
else:
return ""
def set_notes(self, notes):
"""
Sets some note. This overwrites existing notes.
:param notes:
:type notes: str
"""
if notes != self.notes:
self.synced = False
self.notes = notes
def get_url(self):
"""
Returns a url if there is one.
:return: the url
:rtype: str
"""
if self.url:
return self.url
else:
return ""
def set_url(self, url):
"""
Sets a url.
:param url: the url
:type url: str
"""
if url != self.url:
self.synced = False
else:
return self.url
def get_full_template(self):
"""
Constructs a template string with digit and semicolon.
:return: template string
:rtype: str
"""
complexity = self.get_complexity()
if complexity >= 0:
return str(complexity) + ";" + self.get_template()
else:
return self.get_template()
def calculate_template(self, use_lower_case=None, use_upper_case=None, use_digits=None, use_extra=None):
"""
Calculates a new template based on the character set configuration and the length.
:param use_extra: Gets this setting from the current template if None.
:type use_extra: bool
:param use_digits: Gets this setting from the current template if None.
:type use_digits: bool
:param use_upper_case: Gets this setting from the current template if None.
:type use_upper_case: bool
:param use_lower_case: Gets this setting from the current template if None.
:type use_lower_case: bool
"""
if use_lower_case is None:
use_lower_case = 'a' in self.get_template()
if use_upper_case is None:
use_upper_case = 'A' in self.get_template()
if use_digits is None:
use_digits = 'n' in self.get_template()
if use_extra is None:
use_extra = 'o' in self.get_template()
l = []
inserted_lower = False
inserted_upper = False
inserted_digit = False
inserted_extra = False
for _ in range(self.get_length()):
if use_lower_case and not inserted_lower:
l.append('a')
inserted_lower = True
elif use_upper_case and not inserted_upper:
l.append('A')
inserted_upper = True
elif use_digits and not inserted_digit:
l.append('n')
inserted_digit = True
elif use_extra and not inserted_extra:
l.append('o')
inserted_extra = True
else:
l.append('x')
shuffle(l)
self.template = ''.join(l)
def get_template(self):
"""
Returns the template without digit and semicolon.
:return: template
:rtype: str
"""
return self.template
def set_template(self, full_template):
"""
Sets a template from a complete template string with digit and semicolon. This also preferences the template
so other settings might get ignored.
:param full_template: complete template string
:type full_template: str
"""
matches = re.compile("(([01234567]);)?([aAnox]+)").match(full_template)
if matches and len(matches.groups()) >= 3:
if matches.group(2):
self.set_complexity(int(matches.group(2)))
self.template = matches.group(3)
def set_complexity(self, complexity):
"""
Sets the complexity by activating the appropriate character groups.
:param complexity: 0, 1, 2, 3, 4, 5, 6 or 7
:type complexity: int
"""
if not 0 <= complexity <= 7:
ValueError("The complexity must be in the range 0 to 7.")
def get_complexity(self):
"""
Returns the complexity as a digit from 0 to 6. If the character selection does not match a complexity
group -1 is returned.
:return: a digit from 0 to 6 or -1
:rtype: int
"""
if 'n' in self.get_template() and 'a' not in self.get_template() and \
'A' not in self.get_template() and 'o' not in self.get_template():
return 0
elif 'n' not in self.get_template() and 'a' in self.get_template() and \
'A' not in self.get_template() and 'o' not in self.get_template():
return 1
elif 'n' not in self.get_template() and 'a' not in self.get_template() and \
'A' in self.get_template() and 'o' not in self.get_template():
return 2
elif 'n' in self.get_template() and 'a' in self.get_template() and \
'A' not in self.get_template() and 'o' not in self.get_template():
return 3
elif 'n' not in self.get_template() and 'a' in self.get_template() and \
'A' in self.get_template() and 'o' not in self.get_template():
return 4
elif 'n' in self.get_template() and 'a' in self.get_template() and \
'A' in self.get_template() and 'o' not in self.get_template():
return 5
elif 'n' in self.get_template() and 'a' in self.get_template() and \
'A' in self.get_template() and 'o' in self.get_template():
return 6
elif 'n' not in self.get_template() and 'a' not in self.get_template() and \
'A' not in self.get_template() and 'o' in self.get_template():
return 7
else:
return -1
def is_synced(self):
"""
Query if the synced flag is set. The flag switches to false if settings are changed.
:return: is synced?
:rtype: bool
"""
return self.synced
def set_synced(self, is_synced=True):
"""
Sets the synced state. Call this after syncing.
:param is_synced:
:type is_synced: bool
"""
self.synced = is_synced
def to_dict(self):
"""
Returns a dictionary with settings to be saved.
:return: a dictionary with settings to be saved
:rtype: dict
"""
domain_object = {"domain": self.get_domain()}
if self.get_url():
domain_object["url"] = self.get_url()
if self.get_username():
domain_object["username"] = self.get_username()
if self.get_legacy_password():
domain_object["legacyPassword"] = self.get_legacy_password()
if self.notes:
domain_object["notes"] = self.get_notes()
domain_object["iterations"] = self.get_iterations()
if self.salt:
domain_object["salt"] = str(b64encode(self.get_salt()), encoding='utf-8')
domain_object["cDate"] = self.get_creation_date()
domain_object["mDate"] = self.get_modification_date()
domain_object["extras"] = self.get_extra_character_set()
domain_object["passwordTemplate"] = self.get_template()
return domain_object
def load_from_dict(self, loaded_setting):
"""
Loads the setting from a dictionary.
:param loaded_setting:
:type loaded_setting: dict
"""
if "url" in loaded_setting:
self.set_url(loaded_setting["url"])
if "username" in loaded_setting:
self.set_username(loaded_setting["username"])
if "legacyPassword" in loaded_setting:
self.set_legacy_password(loaded_setting["legacyPassword"])
if "notes" in loaded_setting:
self.set_notes(loaded_setting["notes"])
if "iterations" in loaded_setting:
self.set_iterations(loaded_setting["iterations"])
if "salt" in loaded_setting:
self.set_salt(b64decode(loaded_setting["salt"]))
if "cDate" in loaded_setting:
self.set_creation_date(loaded_setting["cDate"])
if "mDate" in loaded_setting:
self.set_modification_date(loaded_setting["mDate"])
if "extras" in loaded_setting:
self.set_extra_character_set(loaded_setting["extras"])
if "passwordTemplate" in loaded_setting:
self.set_template(loaded_setting["passwordTemplate"])
if "length" in loaded_setting and "usedCharacters" in loaded_setting and \
"passwordTemplate" not in loaded_setting:
self.template = "o"*int(loaded_setting["length"])
self.set_extra_character_set(loaded_setting["usedCharacters"])
self.calculate_template(False, False, False, True)
def ask_for_input(self):
"""
Displays some input prompts for the settings properties.
"""
self.set_username(input('Benutzername: '))
wants_legacy_password = input('Möchten Sie ein Passwort generieren (Alternative: nur speichern)? [J/n] ')
if wants_legacy_password in ['n', 'N', 'speichern', 'save', 'no', 'nein', 'Nein', 'No', 'Nay']:
self.set_legacy_password(getpass.getpass('klassisches Passwort: '))
else:
length_str = input('Passwortlänge [' + str(self.get_length()) + ']: ')
try:
length = int(length_str)
if length <= 0:
length = self.get_length()
except ValueError:
length = self.get_length()
self.set_template("6;" + "x"*length)
self.calculate_template(True, True, True, True)
iterations_str = input('Iterationszahl [' + str(self.get_iterations()) + ']: ')
try:
iterations = int(iterations_str)
if iterations <= 0:
iterations = self.get_iterations()
except ValueError:
iterations = self.get_iterations()
self.set_iterations(iterations)