server/process/epasswd.py

64 lines
1.7 KiB
Python
Raw Normal View History

2016-02-10 13:42:53 +01:00
#!/usr/bin/python
from string import split
from string import strip
from string import lower
import bcrypt
import sqlite3
2016-02-10 13:42:53 +01:00
class EPasswd:
def __init__(self):
self.data = {}
def set_data(self, no, email, passwd):
lc_id = lower(no)
self.data[lc_id] = {}
self.data[lc_id]["id"] = no
self.data[lc_id]["email"] = email
self.data[lc_id]["passwd"] = passwd
def load_database(self, file):
conn = sqlite3.connect(file)
c = conn.cursor()
for row in c.execute('SELECT `no`, `email`, `password` FROM `faction`'):
(no, email, passwd) = row
self.set_data(no, email, passwd)
conn.close()
def load_file(self, file):
try:
fp = open(file,"r")
except:
fp = None
if fp != None:
while True:
line = fp.readline()
if not line: break
line = strip(line)
[id, email, passwd] = split(line, ":")[0:3]
self.set_data(id, email, passwd)
fp.close()
def check(self, id, passwd):
pw = self.get_passwd(id)
if pw[0:4]=='$2a$' or pw[0:4]=='$2y$':
2019-02-28 10:32:08 +01:00
try:
2019-03-01 19:14:09 +01:00
uhash = pw.encode('utf8')
upass = passwd.encode('utf8')
return bcrypt.checkpw(upass, uhash)
2019-02-28 10:32:08 +01:00
except:
return False
return pw == passwd
def get_passwd(self, id):
return self.data[lower(id)]["passwd"]
def get_email(self, id):
return self.data[lower(id)]["email"]
def get_canon_id(self, id):
return self.data[lower(id)]["id"]
def fac_exists(self, id):
return self.data.has_key(lower(id))