Commit b46a13cb by Scott Duckworth

use UserKey.clean() instead of pre_save signal to set and verify fingerprint

parent 04e475dd
from django.db import models from django.db import models
from django.dispatch import receiver
from django.db.models.signals import pre_save
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.core.exceptions import ValidationError
from sshkey.util import sshkey_fingerprint from sshkey.util import sshkey_fingerprint
class UserKey(models.Model): class UserKey(models.Model):
...@@ -18,6 +17,8 @@ class UserKey(models.Model): ...@@ -18,6 +17,8 @@ class UserKey(models.Model):
def __unicode__(self): def __unicode__(self):
return unicode(self.user) + u': ' + self.name return unicode(self.user) + u': ' + self.name
@receiver(pre_save, sender=UserKey, dispatch_uid=__name__ + '.set_fingerprint') def clean(self):
def set_fingerprint(sender, instance, **kwargs): try:
instance.fingerprint = sshkey_fingerprint(instance.key) self.fingerprint = sshkey_fingerprint(self.key)
except Exception, e:
raise ValidationError('Not a valid SSH key: ' + str(e))
...@@ -7,7 +7,7 @@ sshkey_re = re.compile(r'\s*(?:(?P<options>.*?)\s+)?(?P<type>ssh-\w+)\s+(?P<key> ...@@ -7,7 +7,7 @@ sshkey_re = re.compile(r'\s*(?:(?P<options>.*?)\s+)?(?P<type>ssh-\w+)\s+(?P<key>
def sshkey_fingerprint(key_line): def sshkey_fingerprint(key_line):
match = sshkey_re.match(key_line) match = sshkey_re.match(key_line)
if not match: if not match:
return None raise Exception('Key is not in OpenSSH authorized_keys format')
key = base64.b64decode(match.group('key')) key = base64.b64decode(match.group('key'))
fp_plain = hashlib.md5(key).hexdigest() fp_plain = hashlib.md5(key).hexdigest()
return ':'.join(a+b for a,b in zip(fp_plain[::2], fp_plain[1::2])) return ':'.join(a+b for a,b in zip(fp_plain[::2], fp_plain[1::2]))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment