Fixed auth. Some cleanup

This commit is contained in:
Ferdinand Thiessen 2020-09-02 13:07:21 +02:00
parent b4505de253
commit 5bfa305c41
7 changed files with 22 additions and 21 deletions

View File

@ -50,8 +50,8 @@ def _login():
token = access_controller.create(user, user_agent=request.user_agent) token = access_controller.create(user, user_agent=request.user_agent)
logger.debug("access token is {{ {} }}".format(token)) logger.debug("access token is {{ {} }}".format(token))
logger.debug("validate access token") logger.debug("validate access token")
dic = user.toJSON() dic = user.default()
dic["accessToken"] = token dic["accessToken"] = token.token
logger.info("User {{ {} }} success login.".format(username)) logger.info("User {{ {} }} success login.".format(username))
logger.debug("return login {{ {} }}".format(dic)) logger.debug("return login {{ {} }}".format(dic))
return jsonify(dic) return jsonify(dic)

View File

@ -110,17 +110,17 @@ class AuthLDAP(modules.Auth):
attributes=['uid', 'givenName', 'sn', 'mail']) attributes=['uid', 'givenName', 'sn', 'mail'])
r = self.ldap.connection.response[0]['attributes'] r = self.ldap.connection.response[0]['attributes']
if r['uid'][0] == user.uid: if r['uid'][0] == user.uid:
user.setAttribute('DN', self.ldap.connection.response[0]['dn']) user.set_attribute('DN', self.ldap.connection.response[0]['dn'])
user.firstname = r['givenName'][0] user.firstname = r['givenName'][0]
user.lastname = r['sn'][0] user.lastname = r['sn'][0]
if r['mail']: if r['mail']:
user.mail = r['mail'][0] user.mail = r['mail'][0]
if 'displayName' in r: if 'displayName' in r:
user.displayname = r['displayName'][0] user.display_name = r['displayName'][0]
for group in self._getGroups(user.uid): for group in self._get_groups(user.uid):
user.addGroup(group) user.add_group(group)
def _getGroups(self, uid): def _get_groups(self, uid):
try: try:
groups = [] groups = []
@ -141,7 +141,7 @@ class AuthLDAP(modules.Auth):
groups.append(data['attributes']['cn'][0]) groups.append(data['attributes']['cn'][0])
return groups return groups
except Exception as err: except Exception as err:
debug.warning("exception in get groups from ldap", exc_info=True) logger.warning("exception in get groups from ldap", exc_info=True)
return [] return []
# def getAllUser(self): # def getAllUser(self):

View File

@ -10,18 +10,18 @@ class AuthPlain(modules.Auth):
if not user: if not user:
return False return False
if 'password' in user.attributes: if 'password' in user.attributes:
return self.__verify_password(user.attributes['password'].value, password) return self._verify_password(user.attributes['password'].value, password)
return False return False
def __hash_password(self, password): def _hash_password(self, password):
salt = hashlib.sha256(os.urandom(60)).hexdigest().encode('ascii') salt = hashlib.sha256(os.urandom(60)).hexdigest().encode('ascii')
pwdhash = hashlib.pbkdf2_hmac('sha3-512', password.encode('utf-8'), salt, 100000) pass_hash = hashlib.pbkdf2_hmac('sha3-512', password.encode('utf-8'), salt, 100000)
pwdhash = binascii.hexlify(pwdhash) pass_hash = binascii.hexlify(pass_hash)
return (salt + pwdhash).decode('ascii') return (salt + pass_hash).decode('ascii')
def __verify_password(self, stored_password, provided_password): def _verify_password(self, stored_password, provided_password):
salt = stored_password[:64] salt = stored_password[:64]
stored_password = stored_password[64:] stored_password = stored_password[64:]
pwdhash = hashlib.pbkdf2_hmac('sha3-512', provided_password.encode('utf-8'), salt.encode('ascii'), 100000) pass_hash = hashlib.pbkdf2_hmac('sha3-512', provided_password.encode('utf-8'), salt.encode('ascii'), 100000)
pwdhash = binascii.hexlify(pwdhash).decode('ascii') pass_hash = binascii.hexlify(pass_hash).decode('ascii')
return pwdhash == stored_password return pass_hash == stored_password

View File

@ -65,7 +65,7 @@ class AccessTokenController(metaclass=Singleton):
return True return True
return False return False
def create(self, user, user_agent=None): def create(self, user, user_agent=None) -> AccessToken:
""" Create an AccessToken """ Create an AccessToken
Create an AccessToken for an User and add it to the tokenList. Create an AccessToken for an User and add it to the tokenList.
@ -75,7 +75,7 @@ class AccessTokenController(metaclass=Singleton):
user_agent: User agent to identify session user_agent: User agent to identify session
Returns: Returns:
A created Token for User AccessToken: A created Token for User
""" """
logger.debug("create access token") logger.debug("create access token")
token_str = secrets.token_hex(16) token_str = secrets.token_hex(16)

View File

@ -5,6 +5,7 @@ from flaschengeist.system.models.user import User
from flaschengeist.system.database import db from flaschengeist.system.database import db
from flaschengeist import logger from flaschengeist import logger
class Base: class Base:
def loginUser(self, username, password): def loginUser(self, username, password):
logger.info("login user {{ {} }}".format(username)) logger.info("login user {{ {} }}".format(username))

View File

@ -33,7 +33,7 @@ class AccessToken(db.Model):
logger.debug("update timestamp from access token {{ {} }}".format(self)) logger.debug("update timestamp from access token {{ {} }}".format(self))
self.timestamp = datetime.utcnow() self.timestamp = datetime.utcnow()
def toJSON(self): def default(self):
""" Create Dic to dump in JSON """ Create Dic to dump in JSON
Returns: Returns:

View File

@ -61,7 +61,7 @@ class User(db.Model):
if 'display_name' in data: if 'display_name' in data:
self.display_name = data['display_name'] self.display_name = data['display_name']
def toJSON(self): def default(self):
return { return {
# TODO: username should be UID? # TODO: username should be UID?
"username": self.uid, "username": self.uid,