-
Notifications
You must be signed in to change notification settings - Fork 1
/
UserDBModule.py
154 lines (124 loc) · 5.17 KB
/
UserDBModule.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
# -*- coding: utf-8 -*-
"""
Created on Wed Sep 30 05:19:51 2015
@author: Dan
"""
import sqlite3
import cfg
from Cryptography import hashed, encryption, decryption, sanitize
UserDBFile = cfg.UserDBFile
"""
if form.LogAttempt == "Create":
# protocol to create new account and fill it with session data
tried = True
FailureMessage = MyUsers.CreateUser(form.Username, form.Password, form.eMail, FormID)
if form.LogAttempt == "Login":
# Protocol to authenticate account and log in
# If the account exists and authenticated, merge input data with old data
# If does not exist, send problem message, offer to create account
# if Attempt succeed, set status to loggedin
tried = True
PriorID = MyUsers.FindUser(form.Username, form.Password)
if (PriorID != ""):
FormID = PriorID
# TODO: Merging code may come here
else:
FailureMessage = "No account matches username / password combination"
"""
class UserDatabase():
def __init__(self):
UserDB = sqlite3.connect(UserDBFile)
cur = UserDB.cursor()
script = \
"""
CREATE TABLE IF NOT EXISTS users ( id INTEGER PRIMARY KEY,
user TEXT UNIQUE NOT NULL,
pass TEXT NOT NULL,
email TEXT UNIQUE NOT NULL,
sessionid TEXT UNIQUE NOT NULL);
"""
cur.execute(script)
UserDB.commit()
UserDB.close()
def CreateUser(self, username, password, email, sessionID):
# Check for non-complying username
HashedUsername = hashed(username.lower(), "USERNAME")
if (HashedUsername == ""):
return "Invalid username, may only use alphanumeric characters and _ "
# Check for non-complying password
HashedPassword = hashed(password, "PASSWORD")
if (HashedPassword == ""):
return "Invalid password, may not use white spaces"
UserDB = sqlite3.connect(UserDBFile)
cur = UserDB.cursor()
# Check if already logged in (sessionID exists)
cur.execute('SELECT COUNT(*) FROM users WHERE sessionid = "%s";' % sessionID)
c = cur.fetchone()
if c[0]>0:
UserDB.commit()
UserDB.close()
return "Already Logged In c=" + str(c)
# Check if account with matching password already exists
cur.execute('SELECT COUNT(*) FROM users WHERE user = "%s" AND pass = "%s";' \
% (HashedUsername, HashedPassword) )
c = cur.fetchone()
if c[0]>0:
UserDB.commit()
UserDB.close()
return "LogMe"
# Check if username is already taken
cur.execute('SELECT COUNT(*) FROM users WHERE user = "%s";' % HashedUsername)
c = cur.fetchone()
if c[0]>0:
UserDB.commit()
UserDB.close()
return "Username already taken, please select another"
SanitizedEmail = sanitize (email, "EMAIL")
Key = HashedPassword.decode('hex')
BLOCK_SIZE = 32
EncryptedEmail = encryption(SanitizedEmail, Key, BLOCK_SIZE)
script = '''INSERT OR IGNORE INTO users
(user, pass, email, sessionid) VALUES ("%s", "%s", "%s", "%s");''' \
% (HashedUsername, HashedPassword, EncryptedEmail, sessionID)
cur.execute(script)
UserDB.commit()
UserDB.close()
return ""
def FindUser(self, username, password):
# Will search user database for a user with matching Username and Password
# and will return the sessionID if found (or "" if not)
HashedUsername = hashed(username.lower(), "USERNAME")
HashedPassword = hashed(password, "PASSWORD")
if (HashedUsername == "") or (HashedPassword == ""):
return ""
UserDB = sqlite3.connect(UserDBFile)
cur = UserDB.cursor()
cur.execute('SELECT sessionid FROM users WHERE user = "%s" AND pass = "%s";' \
% (HashedUsername, HashedPassword) )
allrows = cur.fetchall()
UserDB.commit()
UserDB.close()
if len(allrows) == 0:
return ""
else:
return allrows[0][0]
def PrintDB(self):
# Show table count, columns, and top 5 rows
UserDB = sqlite3.connect(UserDBFile)
cur = UserDB.cursor()
cur.execute("SELECT COUNT(*) FROM users;")
c = cur.fetchone()
cur.execute("SELECT * FROM users LIMIT 5;")
allrows = cur.fetchall()
names = [description[0] for description in cur.description]
print c, names
for row in allrows:
print(row)
UserDB.commit()
UserDB.close()
MyUsers = UserDatabase()
if False:
Newbase = UserDatabase()
Newbase.CreateUser("MyName","MyPass","MyEmail@Domain.com")
Newbase.CreateUser("Name2","Pass2","P2@Domain.com")
Newbase.PrintDB()