def validate_password(password, user=None): """ EdX's custom password validator for passwords. This function performs the following functions: 1) Normalizes the password according to NFKC unicode standard 2) Calls Django's validate_password method. This calls the validate function in all validators specified in AUTH_PASSWORD_VALIDATORS configuration. Parameters: password (str or unicode): the user's password to be validated user (django.contrib.auth.models.User): The user object to use for validating the given password against the username and/or email. Returns: None Raises: ValidationError if any of the password validators fail. """ if not isinstance(password, text_type): try: # some checks rely on unicode semantics (e.g. length) password = text_type(password, encoding='utf8') except UnicodeDecodeError: # no reason to get into weeds raise ValidationError([_('Invalid password.')]) if PASSWORD_UNICODE_NORMALIZE_FLAG.is_enabled(): password = normalize_password(password) django_validate_password(password, user)
def validate_password(password, user=None): """ EdX's custom password validator for passwords. This function performs the following functions: 1) Normalizes the password according to NFKC unicode standard 2) Calls Django's validate_password method. This calls the validate function in all validators specified in AUTH_PASSWORD_VALIDATORS configuration. Parameters: password (str or unicode): the user's password to be validated user (django.contrib.auth.models.User): The user object to use for validating the given password against the username and/or email. Returns: None Raises: ValidationError if any of the password validators fail. """ password = normalize_password(password) django_validate_password(password, user)
def validate(self, data): if data.get('password'): if not data.get('password2'): raise serializers.ValidationError( "Password confirmation not provided.") if data.get('password') != data.get('password2'): raise serializers.ValidationError("Passwords must match.") errors = dict() try: # validate the password and catch the exception django_validate_password(password=data.get('password'), user=self.context.get('user')) # the exception raised here is different than serializers.ValidationError except exceptions.ValidationError as e: errors['password'] = list(e.messages) if errors: raise serializers.ValidationError(errors) return data
def validate_password(self, value): django_validate_password(value) return value