Ejemplo n.º 1
0
    def password_changed(self, password, user=None):

        if not self._user_ok(user):
            return

        user_config = UserPasswordHistoryConfig.objects.filter(
            user=user,
            iterations=get_password_hasher().iterations
        ).first()

        if not user_config:
            user_config = UserPasswordHistoryConfig()
            user_config.user = user
            user_config.save()

        password_hash = user_config.make_password_hash(password)

        # We are looking hash password in the database
        try:
            PasswordHistory.objects.get(
                user_config=user_config,
                password=password_hash
            )
        except PasswordHistory.DoesNotExist:
            ols_password = PasswordHistory()
            ols_password.user_config = user_config
            ols_password.password = password_hash
            ols_password.save()

        # We make sure there are no old passwords in the database.
        self.delete_old_passwords(user)
    def password_changed(self, password, user=None):

        if not user:
            return

        user_config = UserPasswordHistoryConfig.objects.filter(
            user=user,
            iterations=get_password_hasher().iterations
        ).first()

        if not user_config:
            user_config = UserPasswordHistoryConfig()
            user_config.user = user
            user_config.save()

        password_hash = user_config.make_password_hash(password)

        # We are looking hash password in the database
        try:
            PasswordHistory.objects.get(
                user_config=user_config,
                password=password_hash
            )
        except PasswordHistory.DoesNotExist:
            ols_password = PasswordHistory()
            ols_password.user_config = user_config
            ols_password.password = password_hash
            ols_password.save()
Ejemplo n.º 3
0
 def save(self, *args, **kwargs):
     # When there is no salt as defined for a given user,
     # then we create the salt.
     if not self.salt:
         self._gen_password_history_salt()
     # We take iterations from the default Hasher
     if not self.iterations:
         self.iterations = get_password_hasher().iterations
     return super(UserPasswordHistoryConfig, self).save(*args, **kwargs)
Ejemplo n.º 4
0
    def make_password_hash(self, password):
        """
        Generates a password  hash for the given password.

        Args:
            passaword - the password is not encrypted form
        """
        hasher = get_password_hasher()()
        return hasher.encode(password, self.salt, self.iterations)
Ejemplo n.º 5
0
 def save(self, *args, **kwargs):
     # When there is no salt as defined for a given user,
     # then we create the salt.
     if not self.salt:
         self._gen_password_history_salt()
     # We take iterations from the default Hasher
     if not self.iterations:
         self.iterations = get_password_hasher().iterations
     return super(UserPasswordHistoryConfig, self).save(*args, **kwargs)
Ejemplo n.º 6
0
    def make_password_hash(self, password):
        """
        Generates a password  hash for the given password.

        Args:
            passaword - the password is not encrypted form
        """
        hasher = get_password_hasher()()
        return hasher.encode(password, self.salt, self.iterations)