-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.py
140 lines (112 loc) · 4.23 KB
/
database.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
import sqlite3
from sqlite3.dbapi2 import Cursor
import requests
import json
class Database:
__instance = None
conn = None
cursor = None
@classmethod
def get_instance(cls):
if not cls.__instance:
cls.__instance = cls
return cls.__instance
@classmethod
def connect(cls, file):
if not cls.conn:
print('Creating db connection...')
cls.conn = sqlite3.connect(file, check_same_thread=False)
if not cls.cursor:
cls.cursor = cls.conn.cursor()
return cls.conn
@classmethod
def disconnect(cls):
return cls.conn.close()
@classmethod
def validateUser(cls, username):
sql = "SELECT * FROM users WHERE username = '{}'".format(username)
result = cls.cursor.execute(sql).fetchone()
if result:
return {'status': True, 'public_key': result[2]}
else:
return {'status': False}
@classmethod
def validatePassword(cls, credentials):
sql = "SELECT * FROM users WHERE username = '{}'".format(
credentials['username'])
user = cls.cursor.execute(sql).fetchone()
if not user:
return {'success': False, 'message': 'Invalid credentials'}
if (credentials['password'] == user[1]):
return {'success': True, 'message': 'Valid credentials'}
else:
return {'success': False, 'message': 'Invalid credentials'}
@classmethod
def registerKeys(cls, username, keys):
# Check if we received a username
if username:
# Check if the user already exists before trying to create it.
registered = cls.validateUser(username)
print('Is user registered? ' + str(registered))
# If not registered:
if not (registered['status']):
print('not registered, registering.')
sql = "INSERT INTO users VALUES('{}','null','{}', '{}')".format(
username, keys['public_key'], keys['private_key'])
cls.cursor.execute(sql)
result = cls.cursor.execute(
"SELECT * FROM users WHERE username ='{}'".format(
username)).fetchone()
print('Database registerKeys public_key recvd:')
print(keys['public_key'].encode('utf-8'))
if result:
cls.conn.commit()
return json.dumps({
'success': True,
'registered': True,
'message': 'Succeeded on creating this user.',
'public_key': keys['public_key']
})
else:
return json.dumps({
'success': False,
'registered': False,
'message': 'Could not create user.'
})
else:
return json.dumps({
'success': False,
'registered': True,
'message': 'Username already exists.'
})
else:
return json.dumps({
'success': False,
'registered': False,
'message': 'Not enough parameters received.'
})
@classmethod
def registerPassword(cls, credentials):
sql = "UPDATE users SET password='{}' WHERE username='{}'".format(
credentials['password'], credentials['username'])
print('Credentials in registerPassword')
print(credentials)
result = cls.cursor.execute(sql)
validationSQL = 'SELECT * FROM users WHERE username="{}"'.format(
credentials['username'])
validation = cls.cursor.execute(validationSQL).fetchone()
print('validation')
print(validation)
if validation[1]:
cls.conn.commit()
return True
else:
return False
@classmethod
def loadPrivateKey(cls, public_key):
sql = "SELECT * FROM users WHERE public_key = '{}'".format(public_key)
result = cls.cursor.execute(sql)
if result:
return (result.fetchone()[3])
else:
return False