-
Notifications
You must be signed in to change notification settings - Fork 5
/
create.py
408 lines (321 loc) · 10.6 KB
/
create.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
#! C:\Python38-32\python.exe -u
import cgi, cgitb, re, encryptionlib as enc, os, mysql.connector as mysql
from connectlib import connect_db
def valid_name(fname, lname):
"""
Checks if a valid firstname and lastname was entered
"""
errors = 0
if len(fname.strip()) == 0 or len(lname.strip()) == 0:
errors += 1
errmsgs.append(" <p>Name is either incomplete or was not entered</p>")
elif not fname.isalpha() or not lname.isalpha():
errors += 1
errmsgs.append(
" <p>The name that was entered should only contain letters</p>"
)
return errors
def valid_age(age):
"""
Checks if a valid age was entered
"""
errors = 0
if not age.isdigit():
errors += 1
errmsgs.append(" <p>Age was not entered</p>")
elif int(age) < 18 or int(age) > 120:
errors += 1
errmsgs.append(" <p>Age should be between 18 and 120</p>")
return errors
def valid_pol_affil(polaffil):
"""
Checks if a valid political affiliation was selected
"""
errors = 0
if len(polaffil.strip()) == 0:
errors += 1
errmsgs.append(" <p>No politcal party was selected</p>")
return errors
def valid_address(addr, cty, st, zip):
"""
Checks if a valid address, city, zip code was entered and checks if a valid state was selected
"""
errors = 0
# Regex Pattern for Addresses
addrformat = re.search(
"^([\d]{1,3}[\s][A-Z|a-z|\d](([\d|\s|A-Z|a-z])?){1,})$", addr
)
# Regex Pattern for Cities
ctyformat = re.search("^([A-Z|a-z]{1,}([\s][A-Z|a-z]{1,})?)$", cty)
if len(addr.strip()) == 0:
errors += 1
errmsgs.append(" <p>Address was not entered</p>")
else:
if not addrformat:
errors += 1
errmsgs.append(
' <div class="center">\n\t\t <p>Address is not in the correct format, it should looke something like this: 123 North Street</p>\n\t\t </div>'
)
if len(cty.strip()) == 0:
errors += 1
errmsgs.append(" <p>City was not entered</p>")
if not ctyformat:
errors += 1
errmsgs.append(" <p>City should only contain letters and spaces</p>")
if len(st.strip()) == 0:
errors += 1
errmsgs.append(" <p>No state was selected</p>")
if not zipcode.isdigit():
errors += 1
errmsgs.append(" <p>Zip code should only contain digits</p>")
elif len(zipcode) != 5:
errors += 1
errmsgs.append(" <p>Zip code should be 5 digits long</p>")
return errors
def valid_email(email):
"""
Checks if a valid email was entered
"""
errors = 0
# Regex Pattern for Emails
emailformat = re.search("^([\S]{1,}[@][\w]{4,}[\.][a-z]{2,4})$", email)
if len(email.strip()) == 0:
errors += 1
errmsgs.append(" <p>Email was not entered</p>")
elif not emailformat:
errors += 1
errmsgs.append(
' <div class="center">\n\t\t <p>Email is not in the correct format, it should look something like this: example@gmail.com</p>\n\t\t </div>'
)
return errors
def valid_account(uname, psw1, psw2):
"""
Checks if a valid username and password was entered and checks if the same password was
re-entered
"""
errors = 0 # keeps track of all errors
val_psws = True # determines if psw1 and psw2 should be checked for equality
# Username validation
if len(uname.strip()) == 0:
errors += 1
errmsgs.append(" <p>Username was not entered</p>")
elif len(uname.strip()) < 4:
errors += 1
errmsgs.append(" <p>Username should be at least 4 characters long</p>")
# Password validation
wschar = re.search("\s{1,}", psw1) # checks for any whitespace characters
digits = re.search("\d{1,}", psw1) # checks for 1 or more digits
wschar2 = re.search("\s{1,}", psw2) # checks for any whitespace characters
digits2 = re.search("\d{1,}", psw2) # checks for 1 or more digits
if len(psw1.strip()) == 0 or len(psw2.strip()) == 0:
errors += 1
errmsgs.append(
' <div class="center">\n\t\t <p>Password was either not entered at all or not re-entered</p>\n\t\t </div>'
)
val_psws = False
elif (
len(psw1.strip()) < 8
or wschar
or not digits
or len(psw2.strip()) < 8
or wschar2
or not digits2
):
errors += 1
errmsgs.append(
' <div class="center">\n\t\t <p>Password should be at least 8 characters long and contain no whitespace characters and at least 1 digit</p>\n\t\t </div>'
)
val_psws = False
if val_psws:
if psw1.strip() != psw2.strip():
errors += 1
errmsgs.append(
' <div class="center">\n\t\t <p>The password that was re-entered does not match the original password that was entered</p>\n\t\t </div>'
)
return errors
def select_account(uname, addr):
"""
Checks if an account that was entered is in the Accounts table
"""
errors = 0
try:
# Prepare SELECT statement
prep_select = "SELECT * FROM accounts WHERE uname = %s AND addr = %s"
# A tuple should always be used when binding placeholders (%s)
cursor.execute(prep_select, (uname, addr))
result = cursor.fetchall() # returns a list of tuples
if not result:
insert_account()
else:
errors += 1
errmsgs.append(" <p>Account already exists</p>")
except mysql.Error as e:
errors += 1
msg = " <p>" + str(e) + "</p>"
errmsgs.append(msg)
return errors
def insert_account():
"""
Stores a user's account into the Accounts table using the prepare statement
"""
# Generates a random number of bytes to be used to create a new hash
salt = os.urandom(64)
# Encrypts the password and email that was entered
enc_psw = enc.create_hash(psw1, salt)
enc_email = enc.create_hash(email, salt)
# Prepare INSERT Statement
prep_insert = "INSERT INTO accounts (uname, pwd, fname, lname, email, age, addr, city, state, zipCode, poliAffil) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"
values = (
uname,
enc_psw,
fname,
lname,
enc_email,
age,
addr,
cty,
st,
zipcode,
polaffil,
)
cursor.execute(prep_insert, values)
db.commit() # saves changes
# Stores salt in the database
store_salt(salt)
def store_salt(salt):
"""
Stores the salt used to encrypt data in the database using the prepare statement
"""
# Gets the ID
accid = find_accid()
# Prepare INSERT statement
prep_insert = "INSERT INTO salt (accId, salt) VALUES (%s, %s)"
cursor.execute(prep_insert, (accid, str(salt)))
db.commit() # saves changes
def find_accid():
"""
Finds the id of an account for the Salt table
"""
accid = 0
# Prepare SELECT statement
prep_select = "SELECT accId FROM accounts WHERE uname = %s"
# A tuple should always be used to bind placeholders
cursor.execute(prep_select, (uname,))
result = cursor.fetchall() # returns a list of tuples
if result:
# Should only return one row
(val_accid,) = result[0] # unpacks the tuple
accid = val_accid
return accid
cgitb.enable() # for debugging
# Connects to the database
db = connect_db()
cursor = db.cursor(prepared=True) # allows the prepare statement to be used
# Intializes an empty list of error messages
errmsgs = []
errctr = 0 # keeps track of all the errors that have occurred
form = cgi.FieldStorage()
# Name Validation
if "fname" in form:
fname = form.getvalue("fname")
else:
fname = ""
if "lname" in form:
lname = form.getvalue("lname")
else:
lname = ""
errctr += valid_name(fname, lname)
# Age Validation
if "age" in form:
age = form.getvalue("age")
else:
age = ""
errctr += valid_age(age)
# Political Affiliation Validation
if "polaffil" in form:
polaffil = form.getvalue("polaffil")
else:
polaffil = ""
errctr += valid_pol_affil(polaffil)
# Address, City, State, and Zip Code Validation
if "addr" in form:
addr = form.getvalue("addr")
else:
addr = ""
if "cty" in form:
cty = form.getvalue("cty")
else:
cty = ""
if "st" in form:
st = form.getvalue("st")
else:
st = ""
if "zip" in form:
zipcode = form.getvalue("zip")
else:
zipcode = ""
errctr += valid_address(addr, cty, st, zipcode)
# Email Validation
if "email" in form:
email = form.getvalue("email")
else:
email = ""
errctr += valid_email(email)
# Username and Password Validation
if "uname" in form:
uname = form.getvalue("uname")
else:
uname = ""
if "psw1" in form:
psw1 = form.getvalue("psw1")
else:
psw1 = ""
if "psw2" in form:
psw2 = form.getvalue("psw2")
else:
psw2 = ""
errctr += valid_account(uname, psw1, psw2)
# Determines if select_account() should be called
if errctr == 0:
# Checks if the account that was entered is already exists
errctr += select_account(uname, addr)
print("Content-Type: text/html")
# Checks if any errors occurred
if errctr == 0:
# Sets the new location (URL) to the login.html page
print("Location: http://localhost/vote-project/login.html\n")
# For when the page is still redirecting
print("<!DOCTYPE html>")
print('<html lang="en">')
print(" <head>")
print(" <title>Create Account</title>")
print(' <link rel="stylesheet" href="css/main-styles.css" />')
print(" </head>")
print(" <body>")
print(' <div id="container">')
print(' <div id="content">')
print(" <h1>Redirecting...</h1>")
print(
' <a href="login.html">Click here if you are still being redirected</a>'
)
else:
# Printed when invalid account information is entered
print() # adds a blank line since a blank line needs to follow the Content-Type
print("<!DOCTYPE html>")
print('<html lang="en">')
print(" <head>")
print(" <title>Create Account</title>")
print(' <link rel="stylesheet" href="css/main-styles.css" />')
print(" </head>")
print(" <body>")
print(' <div id="container">')
print(' <div id="content">')
print(" <h1>Error</h1>")
# Prints any error messages when errors occur
for i in range(errctr):
print(errmsgs[i])
print(' <a href="create.html">Click here to fix your mistakes</a>')
print(" </div>")
print(" </div>")
print(" </body>")
print("</html>")