Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def clean_confirm_new_password(self):
if 'new_password' not in self.cleaned_data:
return None
if not self.cleaned_data['confirm_new_password'] and self.cleaned_data['new_password']:
raise forms.ValidationError(_("Confirm your new password by entering it here"))
if self.cleaned_data['new_password'] != self.cleaned_data['confirm_new_password']:
raise forms.ValidationError(_("Mismatch between your new password and confirmation, try again"))
password = self.cleaned_data['new_password']
if password and not is_password_complex(password):
raise forms.ValidationError(_("Passwords must have at least 8 characters, including one uppercase, "
"one lowercase and one number"))
if password and PasswordHistory.is_password_repeat(self.instance, password):
raise forms.ValidationError(_("You have used this password before in the past year, "
"please use a new password."))
return self.cleaned_data['new_password']
def post_save(self, obj):
obj = super(UserCRUDL.Newpassword, self).post_save(obj)
PasswordHistory.objects.create(user=obj, password=obj.password)
return obj
def process_view(self, request, view, *args, **kwargs):
newpassword_path = reverse('users.user_newpassword', args=[0])
logout_path = reverse('users.user_logout')
if (self.password_expire < 0 or not request.user.is_authenticated or view == django.views.static.serve or request.path == newpassword_path or request.path == logout_path): # noqa
return
if PasswordHistory.is_password_expired(request.user):
return HttpResponseRedirect(reverse('users.user_newpassword', args=['0']))
def is_password_repeat(cls, user, password):
password_window = getattr(settings, 'USER_PASSWORD_REPEAT_WINDOW', -1)
if password_window <= 0:
return False
# check their current password
if check_password(password, user.password):
return True
# get all the passwords in the past year
window_ago = timezone.now() - timedelta(days=password_window)
previous_passwords = PasswordHistory.objects.filter(user=user, set_on__gte=window_ago)
for previous in previous_passwords:
if check_password(password, previous.password):
return True
return False