-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathengine.py
More file actions
269 lines (221 loc) · 9.08 KB
/
Copy pathengine.py
File metadata and controls
269 lines (221 loc) · 9.08 KB
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
from os import urandom
import string
from config_parser import GetConfigPaster
from encryption import EncryptPassword, DecryptPassword
import sqlite3
DATAFILE = "database.db"
special_characters ="!\"#$%&'()*+,-./:;<=>?@[\]^_`{|}~"
upper_case_characters = string.ascii_uppercase
lower_case_characters = string.ascii_lowercase
digit_characters = string.digits
class Account(object):
def __init__(self, service, username, password=''):
'''
Initializes an Account object
service (string): the service where the account is signed up
an Account object has three attribute:
self.service (string, determined by input text)
self.username (string, determined by input text)
self.password (string, determined by input text)
If the website service contains unnecessary parts, attempt to remove them
'''
self.service = service.strip().replace('https://','')
self.username = username.strip()
self.password = password.strip()
def SaveAccount(self):
'''
Encrypt the password and save the Account into the database.
Update the password if there is an existing account with the same service and username.
Create a new database if the file is not found.
Create a new table if the Table is not found.
DATAFILE: path to where the Account(with encrypted password) is saved
Returns: nothing
'''
new_acc = (self.service,self.username,EncryptPassword(self.password))
connection = sqlite3.connect(DATAFILE)
cur = connection.cursor()
#Check if table/ database file is not found and create new if it is not there
cur.execute(''' SELECT count(name) FROM sqlite_master WHERE type='table' AND name='ACCOUNT' ''')
if cur.fetchone()[0]==1 :
pass
else:
cur.execute("""CREATE TABLE ACCOUNT(
service text,
username text,
password text)
""")
#Check if the account already exists and update if it is
if self.GetPassword() == None:
cur.executemany("INSERT INTO ACCOUNT (service, username, password) VALUES (?,?,?)", [new_acc])
else:
cur.execute("""UPDATE ACCOUNT SET password=(?) WHERE (service, username)=(?,?)""",
[EncryptPassword(self.password),self.service,self.username])
connection.commit()
cur.close()
connection.close()
def GetPassword(self):
'''
Load and decrypt the password of the Account from the database
Return None if the password or Account is not found
DATAFILE: path to where the Account(with encrypted password) is saved
Returns: string or None
'''
connection = sqlite3.connect(DATAFILE)
cursor = connection.cursor()
cursor.execute("""SELECT * FROM ACCOUNT WHERE (service,username) = (?,?)""", [self.service,self.username])
records = cursor.fetchall()
if len(records) == 1:
for row in records:
return DecryptPassword(row[2])
else:
return None
cursor.close()
connection.close()
def DeleteAccount(self):
'''
Delete an account from the database
DATAFILE: path to where the Account is saved
Returns: nothing
'''
connection = sqlite3.connect(DATAFILE)
cursor = connection.cursor()
cursor.execute("""DELETE FROM ACCOUNT WHERE (service,username) = (?,?)""", [self.service,self.username])
connection.commit()
cursor.close()
connection.close()
def MeetRequirements(password):
'''
Check if the password meets the requirements in the configuration
password: string
Returns: bool
'''
specialchars = eval(GetConfigPaster('PASSWORD_PREFERENCE', 'special'))
upperchars = eval(GetConfigPaster('PASSWORD_PREFERENCE', 'upper'))
lowerchars = eval(GetConfigPaster('PASSWORD_PREFERENCE', 'lower'))
digitchars = eval(GetConfigPaster('PASSWORD_PREFERENCE', 'digit'))
specialcharscount = 0
uppercharscount = 0
lowercharscount = 0
digitcharscount = 0
for character in password:
if character in special_characters:
specialcharscount +=1
elif character in upper_case_characters:
uppercharscount +=1
elif character in lower_case_characters:
lowercharscount +=1
elif character in digit_characters:
digitcharscount +=1
if ((specialcharscount > 0 or specialchars == False)
and (uppercharscount > 0 or upperchars == False)
and (lowercharscount > 0 or lowerchars == False)
and (digitcharscount > 0 or digitchars == False)):
return True
else:
return False
def MeetStandardRequirements(password):
'''
Check if the password meets the standard requirements
Standard requirements: password contains
at least one special character,
at least one uppercase character,
at least one lowercase character,
at least one digit
Length of at least 6
password: string
Returns: bool
'''
specialcharscount = 0
uppercharscount = 0
lowercharscount = 0
digitcharscount = 0
for character in password:
if character in special_characters:
specialcharscount +=1
elif character in upper_case_characters:
uppercharscount +=1
elif character in lower_case_characters:
lowercharscount +=1
elif character in digit_characters:
digitcharscount +=1
if specialcharscount > 0 and uppercharscount > 0 and lowercharscount > 0 and digitcharscount > 0 and len(password)>=6:
return True
else:
return False
def GeneratePassword():
'''
Generade a password that meets the requirements in the configuration
Returns: string
'''
specialchars = eval(GetConfigPaster('PASSWORD_PREFERENCE', 'special'))
upperchars = eval(GetConfigPaster('PASSWORD_PREFERENCE', 'upper'))
lowerchars = eval(GetConfigPaster('PASSWORD_PREFERENCE', 'lower'))
digitchars = eval(GetConfigPaster('PASSWORD_PREFERENCE', 'digit'))
size = int(GetConfigPaster('PASSWORD_PREFERENCE', 'pass_length'))
chars = ''
if specialchars == True:
chars += special_characters
if upperchars == True:
chars += upper_case_characters
if lowerchars == True:
chars += lower_case_characters
if digitchars == True:
chars += digit_characters
password =''
while not MeetRequirements(password):
password = "".join(chars[c % len(chars)] for c in urandom(size))
return password
def GetAllAccount():
'''
Print a list of all accounts in the database (without decrypted password)
DATAFILE: path to where the Accounts(with encrypted password) are saved
'''
connection = sqlite3.connect(DATAFILE)
cursor = connection.cursor()
cursor.execute("""SELECT * FROM ACCOUNT;""")
records = cursor.fetchall()
for row in records:
print("Service: ", row[0])
print("Username: ", row[1])
print("Password: ", row[2])
print("\n")
cursor.close()
connection.close()
def GetAccountList(service):
'''
Get a list of accounts registered to a specific service in the database
service (string): the service entitled with the accounts
DATAFILE: path to where the Accounts(with encrypted password) are saved
Returns: list
'''
acc_list = []
connection = sqlite3.connect(DATAFILE)
cursor = connection.cursor()
cursor.execute("""SELECT * FROM ACCOUNT WHERE (service) = (?)""", [service])
records = cursor.fetchall()
for row in records:
acc_list.append(row[1])
cursor.close()
connection.close()
return acc_list
def DeleteAllAccounts():
'''
Delete all accounts saved in the database
DATAFILE: path to where the Accounts(with encrypted password) are saved
Returns: nothing
'''
connection = sqlite3.connect(DATAFILE)
connection.cursor().execute('DELETE FROM ACCOUNT;');
connection.commit()
connection.close()
def DeleteService(service):
'''
Delete all accounts registered to a specific service in the database
service (string): the service entitled with the accounts that will be deleted
DATAFILE: path to where the Accounts(with encrypted password) are saved
Returns: nothing
'''
connection = sqlite3.connect(DATAFILE)
connection.cursor().execute("""DELETE FROM ACCOUNT WHERE (service)=(?);""",[service]);
connection.commit()
connection.close()