|
|
@@ -15,9 +15,10 @@ def hash_pw(password, salt=None):
|
|
|
if salt is None:
|
|
|
salt = os.urandom(16)
|
|
|
h = hmac.new(salt, password.encode('utf-8'), hashlib.sha256)
|
|
|
- hashed = h.hexdigest()
|
|
|
+ hashed = hashlib.pbkdf2_hmac('sha512', password.encode('utf-8'), salt, 100000)
|
|
|
+ hashed_hex = binascii.hexlify(hashed).decode()
|
|
|
salt_hex = binascii.hexlify(salt).decode()
|
|
|
- return hashed, salt_hex
|
|
|
+ return hashed_hex, salt_hex
|
|
|
|
|
|
class MomokoDB:
|
|
|
db = momoko.Pool(dsn='dbname=%s user=%s' % (config.database, config.db_user), size=2)
|
|
|
@@ -28,20 +29,20 @@ class MomokoDB:
|
|
|
return result
|
|
|
|
|
|
@tornado.gen.coroutine
|
|
|
- def create_user(self, username, password):
|
|
|
- hashed_password, salt = hash_pw(password)
|
|
|
- sql = 'INSERT INTO users (username, password, salt) VALUES (%s, %s, %s) RETURNING id;'
|
|
|
- cursor = yield self.execute(sql, username, hashed_password, salt)
|
|
|
+ def create_user(self, email, password):
|
|
|
+ hashed, salt = hash_pw(password)
|
|
|
+ sql = 'INSERT INTO users (email, password, salt) VALUES (%s, %s, %s) RETURNING id;'
|
|
|
+ cursor = yield self.execute(sql, email, hashed, salt)
|
|
|
return cursor.fetchone()['id']
|
|
|
|
|
|
@tornado.gen.coroutine
|
|
|
- def check_user(self, username, password):
|
|
|
- sql = 'SELECT id, username, password, salt FROM users WHERE username=%s;'
|
|
|
- cursor = yield self.execute(sql, username)
|
|
|
+ def check_user(self, email, password):
|
|
|
+ sql = 'SELECT id, email, password, salt FROM users WHERE email=%s;'
|
|
|
+ cursor = yield self.execute(sql, email)
|
|
|
user = cursor.fetchone()
|
|
|
if not user:
|
|
|
return
|
|
|
- salt = binascii.unhexlify(bytes(user['salt'], 'ascii'))
|
|
|
+ salt = binascii.unhexlify(user['salt'].encode())
|
|
|
hashed, _ = hash_pw(password, salt)
|
|
|
if hashed == user['password']:
|
|
|
return user
|
|
|
@@ -57,8 +58,8 @@ class MomokoDB:
|
|
|
return group_id
|
|
|
|
|
|
@tornado.gen.coroutine
|
|
|
- def invite_user_group(self, username, group_id):
|
|
|
- cursor = yield self.execute('SELECT id FROM users WHERE username = %s;', username)
|
|
|
+ def invite_user_group(self, email, group_id):
|
|
|
+ cursor = yield self.execute('SELECT id FROM users WHERE email = %s;', email)
|
|
|
user_id = cursor.fetchone()['id']
|
|
|
yield self.execute('INSERT INTO user_groups (user_id, group_id) VALUES(%s, %s);', user_id, group_id)
|
|
|
|