Commit 40b4b48f authored by Robin Sonnabend's avatar Robin Sonnabend

Implemented logging in and out

import ldap
import hmac, hashlib
class User:
def __init__(self, username, groups):
self.username = username
self.groups = groups
def summarize(self):
return "{}:{}".format(self.username, ",".join(self.groups))
def from_summary(summary):
name, groupstring = summary.split(":", 1)
groups = groupstring.split(",")
return User(name, groups)
def from_hashstring(secure_string):
summary, hash = secure_string.split("=", 1)
return User.from_summary(summary)
class LdapManager:
def __init__(self, url, base):
self.connection = ldap.initialize(url)
self.base = base
def login(self, username, password):
if not self.authenticate(username, password):
return None
groups = list(map(lambda g: g.decode("utf-8"), self.groups(username)))
return User(username, groups)
def authenticate(self, username, password):
self.connection.simple_bind_s("uid={},ou=users,{}".format(username, self.base), password)
return True
return False
return False
def groups(self, username):
result = []
for _, result_dict in self.connection.search_s(self.base, ldap.SCOPE_SUBTREE, "(memberUid={})".format(username), ["cn"]):
return result
class SecurityManager:
def __init__(self, key):
self.maccer ="utf-8"), digestmod=hashlib.sha512)
def hash_user(self, user):
maccer = self.maccer.copy()
summary = user.summarize()
return "{}={}".format(summary, maccer.hexdigest())
def check_user(self, string):
parts = string.split("=", 1)
if len(parts) != 2:
# wrong format, expecting summary:hash
return False
summary, hash = map(lambda s: s.encode("utf-8"), parts)
maccer = self.maccer.copy()
return hmac.compare_digest(maccer.hexdigest().encode("utf-8"), hash)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment