You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
1.2 KiB
Python

1 year ago
from django import forms
1 year ago
from django.core.exceptions import ValidationError
1 year ago
1 year ago
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import serialization
from .models import File, RuleAttribute, AttributeType, TrustedAuthority
1 year ago
class FileUploadForm(forms.ModelForm):
class Meta:
model = File
fields = ['name', 'file']
class UploadCertificateForm(forms.Form):
1 year ago
signature = forms.FileField(label='Signature File')
1 year ago
certificate = forms.FileField()
class RuleAttributeForm(forms.ModelForm):
class Meta:
model = RuleAttribute
fields = ['attribute_type', 'operator', 'value']
1 year ago
attribute_type = forms.ModelChoiceField(queryset=AttributeType.objects.all())
class TrustedAuthorityForm(forms.ModelForm):
class Meta:
model = TrustedAuthority
fields = ['name', 'public_key']
def clean_public_key(self):
data = self.cleaned_data['public_key']
try:
# Try to load the RSA public key. If it's invalid, it'll raise an error
serialization.load_pem_public_key(data.encode(), backend=default_backend())
except ValueError:
raise ValidationError("Invalid RSA public key.")
return data