diff --git a/authentik/crypto/api.py b/authentik/crypto/api.py index 1cbe0b728..17b1bbe16 100644 --- a/authentik/crypto/api.py +++ b/authentik/crypto/api.py @@ -22,16 +22,15 @@ class CertificateKeyPairSerializer(ModelSerializer): def validate_key_data(self, value): """Verify that input is a valid PEM RSA Key""" # Since this field is optional, data can be empty. - if value == "": - return value - try: - load_pem_private_key( - str.encode("\n".join([x.strip() for x in value.split("\n")])), - password=None, - backend=default_backend(), - ) - except ValueError: - raise ValidationError("Unable to load private key.") + if value != "": + try: + load_pem_private_key( + str.encode("\n".join([x.strip() for x in value.split("\n")])), + password=None, + backend=default_backend(), + ) + except ValueError: + raise ValidationError("Unable to load private key.") return value class Meta: diff --git a/authentik/crypto/forms.py b/authentik/crypto/forms.py index 6ab8dafc4..61d8cd594 100644 --- a/authentik/crypto/forms.py +++ b/authentik/crypto/forms.py @@ -26,16 +26,15 @@ class CertificateKeyPairForm(forms.ModelForm): """Verify that input is a valid PEM RSA Key""" key_data = self.cleaned_data["key_data"] # Since this field is optional, data can be empty. - if key_data == "": - return key_data - try: - load_pem_private_key( - str.encode("\n".join([x.strip() for x in key_data.split("\n")])), - password=None, - backend=default_backend(), - ) - except ValueError: - raise forms.ValidationError("Unable to load private key.") + if key_data != "": + try: + load_pem_private_key( + str.encode("\n".join([x.strip() for x in key_data.split("\n")])), + password=None, + backend=default_backend(), + ) + except ValueError: + raise forms.ValidationError("Unable to load private key.") return key_data class Meta: