class Authentication(): def __init__(self, db_name='db.json'): self.db = Database(name=db_name) def _gen_salt(self, length=6, chars=string.ascii_letters): return ''.join([random.choice(chars) for _ in range(length)]) def _gen_password(self): return '12345' def create_user(self, kind, username, **kwargs): # Create client in the database if kind == 'client': user_id = self.db.new_client(**kwargs) elif kind == 'employee': user_id = self.db.new_employee(**kwargs) else: raise Exception('Invalid user.') # Create new user in the database salt = self._gen_salt() m = hashlib.sha256() m.update(self._gen_password().encode('utf-8')) m.update(salt.encode('utf-8')) self.db.new_user(username, m.hexdigest(), salt, user_id) return user_id # Client needs to know the password def login(self, username, password): # retrieve the data from db info = self.db.get_login_data(username) if not info: # wrong username return False # generate the password m = hashlib.sha256() m.update(password.encode('utf-8')) m.update(info['salt'].encode('utf-8')) return info['password'] == m.hexdigest()
class Controller(): def __init__(self, db_name='db.json'): self.db = Database(name=db_name) self.a = Authentication(db_name=db_name) def login(self, username, password): return self.a.login(username, password) def create_client(self, username, **kwargs): return self.a.create_user('client', username, **kwargs) def create_client_req(self, **kwargs): event_id = self.db.new_event(**kwargs) clients_events = self.db.get_client('id',kwargs['client_id'], all_data=False)[0]['events'] clients_events.append(event_id) self.db.update_client_events(kwargs['client_id'], clients_events) return event_id def create_employee(self, username, **kwargs): return self.a.create_user('employee', username, **kwargs) def update_event(self, **kwargs): self.db.update_event(kwargs) def create_task(self, **kwargs): return self.db.new_task(**kwargs) def update_task(self, **kwargs): self.db.update_task(kwargs) def create_financial_req(self, **kwargs): return self.db.new_financial_req(**kwargs) def create_recruitment_req(self, **kwargs): return self.db.new_recruitment_req(**kwargs) def update_recruitment_req(self, **kwargs): return self.db.update_recruitment_req(kwargs) def update_financial_req(self, **kwargs): self.db.update_financial_req(kwargs) def get_user_id(self, username): return self.db.get_login_data(username)['user_id'] def update_client_events(self, cl_id, events): self.db.update_client_events(cl_id, events) def get_client(self, col_name='', criteria='', all_data=True): return self._get_data('client', col_name, criteria, all_data) def get_employee(self, col_name='', criteria='', all_data=True): return self._get_data('employee', col_name, criteria, all_data) def get_task(self, col_name='', criteria='', all_data=True): return self._get_data('task', col_name, criteria, all_data) def get_event(self, col_name='', criteria='', all_data=True): return self._get_data('event', col_name, criteria, all_data) def get_financial_req(self, col_name='', criteria='', all_data=True): return self._get_data('financial_req', col_name, criteria, all_data) def get_recruitment_req(self, col_name='', criteria='', all_data=True): return self._get_data('recruitment_req', col_name, criteria, all_data) def _get_data(self, table, col_name, criteria, all_data): return getattr(self.db, "get_%s" % table)(col_name, criteria, all_data)