-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_session.py
70 lines (59 loc) · 2.74 KB
/
db_session.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
# -*- coding: utf-8 -*-
import db_utils as dbu
import AESCipher
class Session:
def __init__(self, master, run_as_flask=False):
self.master = master
self.enkryptor = AESCipher.AESCipher(self.master)
if not run_as_flask:
assert(self.verify_password())
def remove_from_table(self, key, username=""):
if username == "":
query = "DELETE FROM %s WHERE key=?;" % (dbu.PROJECT_TABLE_NAME)
dbu.execute(query, (key,))
else:
query = "DELETE FROM %s WHERE key=? AND username=?;" % (dbu.PROJECT_TABLE_NAME)
dbu.execute(query, (key, username))
def insert_into_table(self, key, username, password):
existing = self.get_password_by_key(key, username)
if len(existing) > 0:
return
query = "INSERT INTO %s (key, username, hash) VALUES (? , ? , ?);"
query = query % (dbu.PROJECT_TABLE_NAME,)
dbu.execute(query, (key, username, self.enkryptor.encrypt(password)))
def get_password_by_key(self, key, username=""):
rows = []
if username == "":
query = "SELECT key, username, hash FROM %s WHERE key=?;" % (dbu.PROJECT_TABLE_NAME,)
rows = dbu.execute(query, (key,))
else:
query = "SELECT key, username, hash FROM %s WHERE key=? AND username=?;" % (dbu.PROJECT_TABLE_NAME,)
rows = dbu.execute(query, (key, username))
return [ ( row[0], row[1], self.enkryptor.decrypt(row[2]) ) for row in rows ]
def get_keys(self):
query = "SELECT DISTINCT key FROM %s;" % (dbu.PROJECT_TABLE_NAME,)
rows = dbu.execute(query)
return rows
def get_all(self):
query = "SELECT key, username, hash FROM %s;" % (dbu.PROJECT_TABLE_NAME,)
rows = dbu.execute(query)
return [ ( row[0], row[1], self.enkryptor.decrypt(row[2]) ) for row in rows ]
def get_config_value(self, value):
query = "SELECT value FROM %s WHERE key='%s';"
query = query % (dbu.CONFIG_TABLE_NAME, dbu.CONFIG_KEYS[value])
# print "EXECUTING QUERY:", repr(query)
rows = dbu.execute(query)
# print rows
return rows[0][0] if rows else None
def verify_password(self):
# print self.get_config_value("auth-key")
if len(self.get_keys()) == 0 or not self.get_config_value("auth-key"):
query = "INSERT INTO %s (key, value) VALUES (? , ?);"
query = query % (dbu.CONFIG_TABLE_NAME,)
dbu.execute(query, (dbu.CONFIG_KEYS["auth-key"], self.enkryptor.encrypt(self.master)))
return True
else:
return self.enkryptor.decrypt(self.get_config_value("auth-key")) == self.master
def __del__(self):
del self.master
del self.enkryptor