-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql.py
More file actions
108 lines (84 loc) · 3.21 KB
/
Copy pathsql.py
File metadata and controls
108 lines (84 loc) · 3.21 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
import sqlite3
import os
import hashlib
# This class is a simple handler for all of our SQL database actions
# Practicing a good separation of concerns, we should only ever call
# These functions from our models
# If you notice anything out of place here, consider it to your advantage and don't spoil the surprise
class SQLDatabase():
'''
Our SQL Database
'''
# Get the database running
def __init__(self, database_arg=":memory:"):
self.conn = sqlite3.connect(database_arg)
self.cur = self.conn.cursor()
self.setup = False
# SQLite 3 does not natively support multiple commands in a single statement
# Using this handler restores this functionality
# This only returns the output of the last command
def execute(self, sql_string):
out = self.cur.execute(sql_string)
# Commit changes to the database
def commit(self):
self.conn.commit()
#-----------------------------------------------------------------------------
# Sets up the database
# Default admin password
def database_setup(self, admin_password='admin'):
# Clear the database if needed
if not self.setup:
self.execute("DROP TABLE IF EXISTS Users")
self.commit()
# Create the users table
self.execute("""CREATE TABLE Users(
username TEXT UNIQUE,
salt TEXT,
password TEXT,
admin INTEGER DEFAULT 0
)""")
self.commit()
# Add our admin user
self.add_user('admin', admin_password, 1)
self.setup = True
#-----------------------------------------------------------------------------
# User handling
#-----------------------------------------------------------------------------
# Add a user to the database
def add_user(self, username1, password, admin1=0):
rand_salt = os.urandom(32)
key = hashlib.pbkdf2_hmac(
'sha256',
password.encode('utf-8'),
rand_salt,
100000
)
sql_cmd = "INSERT INTO Users VALUES('{username}', '{salt}', '{password}', {admin})"
sql_cmd = sql_cmd.format(username=username1, salt=rand_salt.hex(), password=key.hex(), admin=admin1)
self.execute(sql_cmd)
self.commit()
return True
#-----------------------------------------------------------------------------
# Check login credentials
def check_credentials(self, username1, password):
get_query = "SELECT * FROM Users WHERE username = '{username}'"
get_query = get_query.format(username=username1)
self.execute(get_query)
result = self.cur.fetchone()
pw = ""
salt = ""
if result:
salt = bytes.fromhex(result[1])
pw = result[2]
hashed_pw = hashlib.pbkdf2_hmac('sha256', password.encode('utf-8'), salt, 100000)
hashed_pw = hashed_pw.hex()
print(pw)
print(hashed_pw)
# see if they match
return (pw == hashed_pw)
return False
# QUICK TEST (WORKING)
#db = SQLDatabase()
#db.database_setup()
#db.add_user('bob', 'booop', 0)
#print(db.check_credentials('bob', 'booop'))