def clean(self): cleaned_data = super().clean() crt_file = self.cleaned_data.get('crt_file') key_file = self.cleaned_data.get('key_file') crt_text = self.cleaned_data.get('crt_text') key_text = self.cleaned_data.get('key_text') if ((crt_file or key_file) and (crt_text or key_text)) or ( not (crt_file or key_file or crt_text or key_text)): raise ValidationError( 'Please fill 2 field to choose from(File or Text)') if self.errors: return if crt_file: crt = crt_file.read() key = key_file.read() crt_file.seek(0) key_file.seek(0) else: crt = crt_text key = key_text crypto.load_certificate(crypto.FILETYPE_PEM, crt) ca = Ca() if not ca.check_crt_and_key(crt, key): raise ValidationError('You upload a different key and certificate') return cleaned_data
def form_valid(self, form): self.object = models.SiteCrt.objects.get(pk=self.kwargs['pk']) ca = Ca() ca.generate_site_crt(self.object.cn, form.cleaned_data['validity_period'], self.kwargs['pk']) messages.success(self.request, 'Recreation success') return super().form_valid(form)
def form_valid(self, form): ca = Ca() if ca.get_type_alt_names(form.cleaned_data['cn']): self.object = ca.generate_site_crt( form.cleaned_data['cn'], form.cleaned_data['validity_period'], alt_name='IP') else: self.object = ca.generate_site_crt( form.cleaned_data['cn'], form.cleaned_data['validity_period']) return super().form_valid(form)
def get_or_create(self): instance = models.SiteCrt.objects.filter(cn=self.validated_data["cn"]) if not instance: validity_period = ( datetime.datetime.now() + datetime.timedelta(days=settings.VALIDITY_PERIOD_CRT) ).date() ca = Ca() if ca.get_type_alt_names(self.validated_data["cn"]): instance = ca.generate_site_crt( self.validated_data["cn"], validity_period, alt_name="IP" ) else: instance = ca.generate_site_crt( self.validated_data["cn"], validity_period ) else: instance = instance.first() return instance
def clean(self): cleaned_data = super().clean() cert_data = cleaned_data.get('crt').read() key_data = cleaned_data.get('key').read() cleaned_data.get('crt').seek(0) cleaned_data.get('key').seek(0) try: cert = crypto.load_certificate(crypto.FILETYPE_PEM, cert_data).get_subject() ca = Ca() if not ca.check_crt_and_key(cert_data.decode(), key_data.decode()): raise ValidationError( 'You upload a different key and certificate') if not cert.C or not cert.ST or not cert.L or not cert.O: msg = 'Please enter required field in certificate: Country, State, Location, Organization' self.add_error('crt', msg) except crypto.Error: raise ValidationError('Please load valid certificate and key') return cleaned_data
def save(self): ca = Ca() if ca.get_type_alt_names(self.validated_data["cn"]): ca.generate_site_crt( self.validated_data["cn"], self.validated_data["validity_period"], alt_name="IP", ) else: ca.generate_site_crt( self.validated_data["cn"], self.validated_data["validity_period"] )
def form_valid(self, form): ca = Ca() ca.generate_root_crt(form.cleaned_data, recreation=True) messages.success(self.request, 'Recreation success') return super().form_valid(form)
def form_valid(self, form): ca = Ca() ca.generate_root_crt(form.cleaned_data) return super(GenerateNew, self).form_valid(form)