1 from django.conf import settings
2 from django.db import models
3 from django.utils.translation import ugettext_lazy as _
4 from .utils import get_key_details
7 class SSHKey(models.Model):
8 user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, verbose_name=_('user'))
9 key = models.TextField(_('key'))
10 comment = models.CharField(_('comment'), max_length=255, editable=False)
11 algorithm = models.CharField(_('algorithm'), max_length=32, editable=False)
12 bit_length = models.IntegerField(_('bit length'), null=True, editable=False)
13 sha256_hash = models.CharField(_('SHA256 hash'), max_length=128, editable=False)
14 md5_hash = models.CharField(_('MD5 hash'), max_length=128, editable=False)
15 created_at = models.DateTimeField(_('created at'), auto_now_add=True)
16 last_seen_at = models.DateTimeField(_('last seen at'), null=True, editable=False)
19 ordering = ['created_at']
20 verbose_name = _('SSH key')
21 verbose_name_plural = _('SSH keys')
22 unique_together = [('algorithm', 'bit_length', 'md5_hash')]
27 def save(self, *args, **kwargs):
28 det = get_key_details(self.key)
29 self.comment = det['comment'][:255]
30 self.algorithm = det['algo']
31 self.bit_length = det['bits']
32 self.md5_hash = det['md5']
33 self.sha256_hash = det['sha256']
34 return super().save(*args, **kwargs)