Commit a1d8d854 by Paul Kilgo

minimum work necessary to make unit tests pass

parent eb0d50b5
...@@ -27,11 +27,13 @@ ...@@ -27,11 +27,13 @@
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
from django import forms from django import forms
from django_sshkey.models import UserKey from django_sshkey.models import Key
class UserKeyForm(forms.ModelForm): class UserKeyForm(forms.ModelForm):
name = forms.CharField(max_length=50, required=False)
class Meta: class Meta:
model = UserKey model = Key
fields = ['name', 'key'] fields = ['name', 'key']
widgets = { widgets = {
'name': forms.TextInput(attrs={ 'name': forms.TextInput(attrs={
......
...@@ -39,9 +39,7 @@ except ImportError: ...@@ -39,9 +39,7 @@ except ImportError:
from django_sshkey.util import PublicKeyParseError, pubkey_parse from django_sshkey.util import PublicKeyParseError, pubkey_parse
from django_sshkey import settings from django_sshkey import settings
class UserKey(models.Model): class Key(models.Model):
user = models.ForeignKey(User, db_index=True)
name = models.CharField(max_length=50, blank=True)
key = models.TextField(max_length=2000) key = models.TextField(max_length=2000)
fingerprint = models.CharField(max_length=47, blank=True, db_index=True) fingerprint = models.CharField(max_length=47, blank=True, db_index=True)
created = models.DateTimeField(auto_now_add=True, null=True) created = models.DateTimeField(auto_now_add=True, null=True)
...@@ -49,10 +47,7 @@ class UserKey(models.Model): ...@@ -49,10 +47,7 @@ class UserKey(models.Model):
last_used = models.DateTimeField(null=True) last_used = models.DateTimeField(null=True)
class Meta: class Meta:
db_table = 'sshkey_userkey' db_table = 'sshkey_key'
unique_together = [
('user', 'name'),
]
def __unicode__(self): def __unicode__(self):
return unicode(self.user) + u': ' + self.name return unicode(self.user) + u': ' + self.name
...@@ -73,27 +68,16 @@ class UserKey(models.Model): ...@@ -73,27 +68,16 @@ class UserKey(models.Model):
raise ValidationError(str(e)) raise ValidationError(str(e))
self.key = pubkey.format_openssh() self.key = pubkey.format_openssh()
self.fingerprint = pubkey.fingerprint() self.fingerprint = pubkey.fingerprint()
if not self.name:
if not pubkey.comment:
raise ValidationError('Name or key comment required')
self.name = pubkey.comment
def validate_unique(self, exclude=None): def validate_unique(self, exclude=None):
if self.pk is None: if self.pk is None:
objects = type(self).objects objects = type(self).objects
else: else:
objects = type(self).objects.exclude(pk=self.pk) objects = type(self).objects.exclude(pk=self.pk)
if exclude is None or 'name' not in exclude:
if objects.filter(user=self.user, name=self.name).count():
message = 'You already have a key with that name'
raise ValidationError({'name': [message]})
if exclude is None or 'key' not in exclude: if exclude is None or 'key' not in exclude:
try: try:
other = objects.get(fingerprint=self.fingerprint, key=self.key) other = objects.get(fingerprint=self.fingerprint, key=self.key)
if self.user == other.user: message = 'That key is already on file.'
message = 'You already have that key on file (%s)' % other.name
else:
message = 'Somebody else already has that key on file'
raise ValidationError({'key': [message]}) raise ValidationError({'key': [message]})
except type(self).DoesNotExist: except type(self).DoesNotExist:
pass pass
...@@ -110,12 +94,58 @@ class UserKey(models.Model): ...@@ -110,12 +94,58 @@ class UserKey(models.Model):
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
if kwargs.pop('update_last_modified', True): if kwargs.pop('update_last_modified', True):
self.last_modified = now() self.last_modified = now()
super(UserKey, self).save(*args, **kwargs) super(Key, self).save(*args, **kwargs)
def touch(self): def touch(self):
self.last_used = now() self.last_used = now()
self.save(update_last_modified=False) self.save(update_last_modified=False)
class ApplicationKey(models.Model):
basekey = models.ForeignKey(Key)
class Meta:
abstract = True
@property
def key(self):
return self.basekey.key
class NamedKey(ApplicationKey):
name = models.CharField(max_length=50, blank=True)
class Meta:
abstract = True
def clean(self):
if not self.name:
pubkey = pubkey_parse(self.key)
if not pubkey.comment:
raise ValidationError('Name or key comment required')
self.name = pubkey.comment
class UserKey(NamedKey):
user = models.ForeignKey(User, db_index=True)
class Meta:
db_table = 'sshkey_userkey'
unique_together = [
('user', 'name'),
]
def validate_unique(self, exclude=None):
cls = type(self)
if not self.pk is None:
qs = cls.objects.exclude(pk=pk)
else:
qs = cls.objects.all()
if exclude is None or 'name' not in exclude:
if qs.filter(user=self.user, name=self.name).count():
message = 'You already have a key with that name'
raise ValidationError({'name': [message]})
if exclude is None or 'basekey' not in exclude:
if qs.filter(basekey=self.basekey):
message = 'Cannot associate key with two users.'
raise ValidationError({'basekey': [message]})
@receiver(pre_save, sender=UserKey) @receiver(pre_save, sender=UserKey)
def send_email_add_key(sender, instance, **kwargs): def send_email_add_key(sender, instance, **kwargs):
if not settings.SSHKEY_EMAIL_ADD_KEY or instance.pk: if not settings.SSHKEY_EMAIL_ADD_KEY or instance.pk:
......
...@@ -50,7 +50,7 @@ def lookup(request): ...@@ -50,7 +50,7 @@ def lookup(request):
return HttpResponse(str(key.last_used), content_type='text/plain') return HttpResponse(str(key.last_used), content_type='text/plain')
try: try:
fingerprint = request.GET['fingerprint'] fingerprint = request.GET['fingerprint']
keys = UserKey.objects.filter(fingerprint=fingerprint) keys = UserKey.objects.filter(basekey__fingerprint=fingerprint)
except KeyError: except KeyError:
try: try:
username = request.GET['username'] username = request.GET['username']
......
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