def insert_into_clients(username, password):
    insert_sql = '''INSERT INTO clients (username, password, salt)
                         VALUES (?, ?, ?)'''
    hashed_password, salt = hash_pass(password)

    cursor.execute(insert_sql, (username, hashed_password, salt))
    conn.commit()
def change_pass(new_pass, logged_user):
    update_sql = '''UPDATE clients
                    SET password = ?, salt = ?
                    WHERE id = ?'''
    pwd_hash, salt = hash_pass(new_pass)
    cursor.execute(update_sql, (pwd_hash, salt, logged_user.get_id(),))
    conn.commit()
def _login(username, password):
    salt_query = '''SELECT username, salt, password
                    FROM clients
                    WHERE username = ?
                    LIMIT 1'''
    cursor.execute(salt_query, (username,))
    auth_result = cursor.fetchone()

    if auth_result is None:
        return False

    pwd_hash, _ = hash_pass(password, salt=auth_result['salt'])

    select_query = '''SELECT id, username, balance, message
                      FROM clients
                      WHERE username = ?
                      AND password = ?
                      LIMIT 1'''

    cursor.execute(select_query, (username, pwd_hash))
    user = cursor.fetchone()

    if(user):
        return Client(user[0], user[1], user[2], user[3])
    else:
        return False
 def login(self, email, password):
     hashed_pass, salt = hash_pass(password)
     user = self.session.query(Client).\
             filter(Client.email == email,
                    Client.password == hashed_pass,
                    Client.salt == salt).first()
     if user:
         return Client(email=email, name=user.name,
                         password=hashed_pass, salt=salt)
     else:
         raise NoSuchCLient('There is no such client.')
    def change_password(self, new_password, logged_user):
        user = self.session.query(Client).\
                filter(Client.email == logged_user.email).first()

        if user is None:
            raise NoSuchCLient('There is no such client.')

        hashed_pass, salt = hash_pass(new_password)

        user.password = hashed_pass
        user.salt = salt
        self.__commit()
    def register(self, email, name, password):
        user = self.session.query(Client).\
                filter(Client.email == email).first()

        if user is not None:
            raise ClientAlreadyRegistered('Client already registered.')

        if not get_validator(email.split("@")[0]).is_valid(password):
            raise PasswordNotStrongError

        if not email_validation(email):
            raise EmailNotValidError

        hashed_pass, salt = hash_pass(password)

        client = Client(email=email, name=name, password=hashed_pass, salt=salt)
        self.__commit_object(client)