def test_15_hash_passwords(self): p_hash = hash_password("pass0rd", "phpass") PH = PasswordHash() self.assertTrue(PH.check_password("pass0rd", p_hash)) self.assertFalse(PH.check_password("passord", p_hash)) # {SHA} p_hash = hash_password("passw0rd", "sha") self.assertTrue(check_sha(p_hash, "passw0rd")) self.assertFalse(check_sha(p_hash, "password")) # OTRS p_hash = hash_password("passw0rd", "otrs") self.assertTrue(otrs_sha256(p_hash, "passw0rd")) self.assertFalse(otrs_sha256(p_hash, "password")) # {SSHA} p_hash = hash_password("passw0rd", "ssha") self.assertTrue(check_ssha(p_hash, "passw0rd", hashlib.sha1, 20)) self.assertFalse(check_ssha(p_hash, "password", hashlib.sha1, 20)) # {SSHA256} p_hash = hash_password("passw0rd", "ssha256") self.assertTrue(check_ssha(p_hash, "passw0rd", hashlib.sha256, 32)) self.assertFalse(check_ssha(p_hash, "password", hashlib.sha256, 32)) # {SSHA512} p_hash = hash_password("passw0rd", "ssha512") self.assertTrue(check_ssha(p_hash, "passw0rd", hashlib.sha512, 64)) self.assertFalse(check_ssha(p_hash, "password", hashlib.sha512, 64))
def prepare_attributes_for_db(self, attributes): """ Given a dictionary of attributes, return a dictionary mapping columns to values. If the attributes contain a password, hash the password according to the configured password hash type. :param attributes: attributes dictionary :return: dictionary with column name as keys """ attributes = attributes.copy() if "password" in attributes: attributes["password"] = hash_password(attributes["password"], self.password_hash_type) columns = {} for fieldname in attributes: if fieldname in self.map: columns[self.map[fieldname]] = attributes[fieldname] return columns
def add_user(self, attributes=None): """ Add a new user to the SQL database. attributes are these "username", "surname", "givenname", "email", "mobile", "phone", "password" :param attributes: Attributes according to the attribute mapping :return: The new UID of the user. The UserIdResolver needs to determine the way how to create the UID. """ attributes = attributes or {} if "password" in attributes and self.password_hash_type: attributes["password"] = hash_password(attributes["password"], self.password_hash_type) kwargs = self._attributes_to_db_columns(attributes) log.debug("Insert new user with attributes {0!s}".format(kwargs)) r = self.TABLE.insert(**kwargs) self.db.commit() # Return the UID of the new object return getattr(r, self.map.get("userid"))