-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdao.py
305 lines (209 loc) · 7.04 KB
/
dao.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
import bcrypt
import platform
import smtplib
import os
import string
import random
import clipboardManager_DB as db
from email.message import EmailMessage
class PasswordDecorator:
"""
This is a class for the password decorator.
This class is a decorator for functions that requires password validation before being
processed.
Attributes
function (function): the function to be called after password is checked
"""
def __init__(self, function):
"""
The constructor for the PasswordDecorator class.
Parameters:
function (function): the function to be called after password is checked
"""
self.function = function
def passwordIsValid(self, pwd):
"""
The function to check password validity.
Parameters:
pwd(String): stores the password entered by user to be compared to current
password stored
"""
h = db.getPassword()
return bcrypt.checkpw(pwd.encode('utf-8'), h)
def __call__(self, *args, **kwargs):
"""
The wrapper function to run function if password is valid.
"""
pwd = args[0]
if self.passwordIsValid(pwd):
self.function(*args, **kwargs)
result = True
else:
result = False
return result
class DataAccessor:
"""
Data Accessor class that handles requests to the database
"""
# decryption takes place here
# UI grabs the decryption key from user and pass to data access object
def storeCard(self, cardId, content, dataType, hideCard, favoriteCard):
db.addCard(cardId, content, dataType, hideCard, favoriteCard)
def deleteCard(self, id, cardCategory, cardContent):
"""
deletes card from the database
Parameters:
id (str): key to find the card in the database
"""
if cardCategory == "Image":
os.remove(cardContent)
db.deleteCard(id)
def getAllCards(self):
"""
returns all cards in the database
"""
return db.getAllCards()
def getTextCards(self):
"""
returns all cards with cardCategory 'Text'
"""
return db.getTextCards()
def getImageCards(self):
"""
returns all cards with cardCategory 'Image'
"""
return db.getImageCards()
def getUrlCards(self):
"""
returns all cards with cardCategory 'URL'
"""
return db.getUrlCards()
def hideCard(self, cardStatus, cardId):
"""
sets the hidden status of the card true or false
Parameters:
cardStatus (int): desired card status, value is 1 or 0 indicating true or false respectively
CardId (str): key to find the card in the database
"""
db.hideCard(cardStatus, cardId)
def getFavoriteCards(self):
"""
returns all cards from the db that are favorited
"""
return db.getFavoriteCards()
def favoriteCard(self, cardId, favoriteStatus):
"""
sets the favorite status of the card to true or false
Parameters:
favoriteStatus (int): desired card status, value is 1 or 0 indicating true or false respectively
CardId (str): key to find the card in the database
"""
db.favoriteCard(favoriteStatus, cardId)
def getSearchCards(self, search):
"""
returns all cards that satisfy the search parameter
Parameters:
search (str): the search query
"""
return db.getSearchCards(search)
def getUserStatus(self):
"""
Returns the status of the user
"""
return db.getUserStatus()
def passwordIsValid(self, pwd):
"""
The function to check password validity.
Parameters:
pwd(String): stores the password entered by user to be compared to current
password stored
"""
h = db.getPassword()
return bcrypt.checkpw(pwd.encode('utf-8'), h)
# sends encrypted password to database
@PasswordDecorator
def changePassword(oldpwd, newpwd):
"""
Encrypts and changes the password stored in the database
Parameters:
newpwd(String): the new password that will be encrypted and stored
oldpwd(String): the old password that will be checked for validity before storing
new password
"""
salt = bcrypt.gensalt()
hashedPwd = bcrypt.hashpw(newpwd.encode('utf-8'), salt)
db.changePassword(hashedPwd)
def setPassword(self, newpwd):
"""
Encrypts and stores the new password in the database
Parameters:
newpwd(String): the password that will be encrypted and stored
"""
salt = bcrypt.gensalt()
hashedPwd = bcrypt.hashpw(newpwd.encode('utf-8'), salt)
db.changePassword(hashedPwd)
self.setPasswordState(1)
def setPasswordState(self, state):
"""
sets the state of the password
Parameters:
state(boolean): the current state of the password
"""
db.setPasswordState(state)
def getPasswordState(self):
"""
Returns the state of the password.
Returns:
state(boolean): the current state of the password
"""
return db.getPasswordState()
def getPassword(self):
"""
Returns the current encrypted password obtained from database.
Returns:
password(binary): the current encrypted password
"""
return db.getPassword()
def getEmail(self):
"""
Returns the email stored in the database.
Returns:
email(string): the email stored in the database
"""
return db.getEmail()
def createUser(self, email):
db.createUser(email)
def setEmail(self, email):
"""
Sends the email to the database.
"""
db.setEmail(email)
def resetDb(self):
"""
Erases all contents in database.
"""
db.resetDb()
def sendEmail(self):
"""
The function to send temporary password to user email.
"""
senderEmail = "yourclipboardmanager@gmail.com"
receiverEmail = self.getEmail()
password = "ecqibpmoeknjxwbm"
os = platform.system()
if os == 'Windows':
password = "qourwmshfkltqnnc"
temp = ''.join(random.choices(string.ascii_lowercase + string.digits, k=10))
self.setPassword(temp)
plainText = ("""This is your temporary password: {}. Please use this password to sign in and
remember to change password to your own.""".format(temp))
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(senderEmail, password)
msg = EmailMessage()
message = f'{plainText}\n'
msg.set_content(message)
msg['Subject'] = "Your temporary password."
msg['From'] = senderEmail
msg['To'] = receiverEmail
server.send_message(msg)