-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuserAccountManagement.py
More file actions
55 lines (45 loc) · 1.68 KB
/
Copy pathuserAccountManagement.py
File metadata and controls
55 lines (45 loc) · 1.68 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
import hashlib #to hash passwords
def register(username, password, db):
'''Allow users to create a new account'''
#check if the chosen username is already in use
if username == 'Guest':
return False #as this is the guest account
#endif
cursor = db.cursor()
cursor.execute('SELECT username FROM user_details')
for names in cursor:
for name in names:
if name == username:
return False #so the program doesn't continue
#endif
#endif
#endfor
#hash the user's password using SHA256 so it is unrecognisable
password = str(hashlib.sha256(password.encode('utf-8')).hexdigest())
#add a record to the database with None in the high score fields
sql = 'INSERT INTO user_details (username, password) VALUES (%s, %s)'
values = (username, password)
cursor.execute(sql, values)
db.commit()
return True
#endsub
def signIn(username, password, db):
'''Allow the user to sign in to their existing account'''
#lookup the password hash in the database
cursor = db.cursor()
sql = 'SELECT password FROM user_details WHERE username = %s'
value = (username, )
cursor.execute(sql, value)
result = cursor.fetchone()
if result:
for oldhash in result:
#calculate the hash of the newly typed password and check against this hash
newhash = str(hashlib.sha256(password.encode('utf-8')).hexdigest())
if oldhash == newhash:
return True
#endif
#endfor
else:
return False #the username was not in the database
#endif
#endsub