crypto: simplify api/forms key validation
This commit is contained in:
parent
7ff7398aff
commit
ffee86fcf3
|
@ -22,16 +22,15 @@ class CertificateKeyPairSerializer(ModelSerializer):
|
||||||
def validate_key_data(self, value):
|
def validate_key_data(self, value):
|
||||||
"""Verify that input is a valid PEM RSA Key"""
|
"""Verify that input is a valid PEM RSA Key"""
|
||||||
# Since this field is optional, data can be empty.
|
# Since this field is optional, data can be empty.
|
||||||
if value == "":
|
if value != "":
|
||||||
return value
|
try:
|
||||||
try:
|
load_pem_private_key(
|
||||||
load_pem_private_key(
|
str.encode("\n".join([x.strip() for x in value.split("\n")])),
|
||||||
str.encode("\n".join([x.strip() for x in value.split("\n")])),
|
password=None,
|
||||||
password=None,
|
backend=default_backend(),
|
||||||
backend=default_backend(),
|
)
|
||||||
)
|
except ValueError:
|
||||||
except ValueError:
|
raise ValidationError("Unable to load private key.")
|
||||||
raise ValidationError("Unable to load private key.")
|
|
||||||
return value
|
return value
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
|
|
|
@ -26,16 +26,15 @@ class CertificateKeyPairForm(forms.ModelForm):
|
||||||
"""Verify that input is a valid PEM RSA Key"""
|
"""Verify that input is a valid PEM RSA Key"""
|
||||||
key_data = self.cleaned_data["key_data"]
|
key_data = self.cleaned_data["key_data"]
|
||||||
# Since this field is optional, data can be empty.
|
# Since this field is optional, data can be empty.
|
||||||
if key_data == "":
|
if key_data != "":
|
||||||
return key_data
|
try:
|
||||||
try:
|
load_pem_private_key(
|
||||||
load_pem_private_key(
|
str.encode("\n".join([x.strip() for x in key_data.split("\n")])),
|
||||||
str.encode("\n".join([x.strip() for x in key_data.split("\n")])),
|
password=None,
|
||||||
password=None,
|
backend=default_backend(),
|
||||||
backend=default_backend(),
|
)
|
||||||
)
|
except ValueError:
|
||||||
except ValueError:
|
raise forms.ValidationError("Unable to load private key.")
|
||||||
raise forms.ValidationError("Unable to load private key.")
|
|
||||||
return key_data
|
return key_data
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
|
|
Reference in New Issue