from django import forms from django.core.exceptions import ValidationError from cryptography.hazmat.backends import default_backend from cryptography.hazmat.primitives import serialization from .models import File, RuleAttribute, AttributeType, TrustedAuthority class FileUploadForm(forms.ModelForm): class Meta: model = File fields = ['name', 'file'] class UploadCertificateForm(forms.Form): signature = forms.FileField(label='Signature File') certificate = forms.FileField() class RuleAttributeForm(forms.ModelForm): class Meta: model = RuleAttribute fields = ['attribute_type', 'operator', 'value'] 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