Permalink
Browse files

Added key listing (private and public), key creation views and the re…

…spective API call to the django_gpg app
  • Loading branch information...
1 parent bad6f31 commit 93ab976c814c208b9b42640574032d4a6f93f464 @rosarior rosarior committed Nov 14, 2011
@@ -87,7 +87,14 @@
<ul class="nav">
<li><a href="{% url home_view %}">{% trans 'Home' %}</a></li>
-
+ <li class="dropdown" data-dropdown="dropdown">
+ <a class="dropdown-toggle" href="#">{% trans 'Key management' %}</a>
+ <ul class="dropdown-menu">
+ <li><a href="{% url key_create %}">{% trans 'Create new key pair' %}</a></li>
+ <li><a href="{% url key_private_list %}">{% trans 'Private key list' %}</a></li>
+ <li><a href="{% url key_public_list %}">{% trans 'Public key list' %}</a></li>
+ </ul>
+ </li>
<li class="dropdown" data-dropdown="dropdown">
<a class="dropdown-toggle" href="#">{% trans 'Resources' %}</a>
<ul class="dropdown-menu">
@@ -34,6 +34,10 @@ def get_all(cls, gpg, secret=False):
@classmethod
def get(cls, gpg, key_id, secret=False):
+ if len(key_id) > 16:
+ # key_id is a fingerpint
+ key_id = Key.get_key_id(key_id)
+
keys = gpg.gpg.list_keys(secret=secret)
key = next((key for key in keys if key['keyid'] == key_id), None)
key_instance = Key(fingerprint=key['fingerprint'], uids=key['uids'], type=key['type'])
@@ -50,8 +54,12 @@ def __init__(self, fingerprint, uids, type):
def key_id(self):
return Key.get_key_id(self.fingerprint)
+ @property
+ def user_ids(self):
+ return u', '.join(self.uids)
+
def __str__(self):
- return '%s "%s" (%s)' % (self.key_id, self.uids[0], KEY_TYPES.get(self.type, _(u'unknown')))
+ return '%s "%s" (%s)' % (self.key_id, self.user_ids, KEY_TYPES.get(self.type, _(u'unknown')))
def __unicode__(self):
return unicode(self.__str__())
@@ -161,3 +169,9 @@ def decrypt_file(self, file_input):
raise GPGDecryptionError('Unable to decrypt file')
return result
+
+ def create_key(self, *args, **kwargs):
+ input_data = self.gpg.gen_key_input(**kwargs)
+ fingerprint = self.gpg.gen_key(input_data)
+ return Key.get(self.gpg, fingerprint)
+
@@ -0,0 +1,24 @@
+from django import forms
+from django.utils.translation import ugettext_lazy as _
+
+#from django_gpg import GPG, Key
+
+
+class NewKeyForm(forms.Form):
+ name = forms.CharField(
+ label=_(u'Real name'),
+ #help_text=_(u'A name that uniquely identifies this resource, if left blank the filename is used instead.'),
+ #required=Truee,
+ )
+
+ comment = forms.CharField(
+ label=_(u'Comment'),
+ widget=forms.widgets.Textarea()
+ #help_text=_(u'The private key that will be used to sign the file.'),
+ )
+
+ email = forms.CharField(
+ label=_('Email'),
+ #help_text=_(u'Automatically convert relative references to images, links, CSS and Javascript.'),
+ #initial=True
+ )
@@ -0,0 +1,30 @@
+{% extends 'base.html' %}
+{% load i18n %}
+
+{% block title %}{{ title }}{% endblock %}
+
+{% block content %}
+ <table class="bordered-table zebra-striped">
+ <thead>
+ <tr>
+ <th>{% trans 'ID' %}</th>
+ <th>{% trans 'User IDs' %}</th>
+ <th>{% trans 'Fingerprint' %}</th>
+ </tr>
+ </thead>
+ <tbody>
+ {% for object in object_list %}
+ <tr>
+ <td>{{ object.key_id }}</td>
+ <td>{{ object.user_ids }}</td>
+ <td>{{ object.fingerprint }}</td>
+ </tr>
+ {% empty %}
+ <tr>
+ <td colspan="99">{% trans 'There are no keys available.' %}</td>
+ </tr>
+ {% endfor %}
+ </tbody>
+ </table>
+{% endblock %}
+
@@ -0,0 +1,11 @@
+from django.conf.urls.defaults import patterns, url
+
+urlpatterns = patterns('django_gpg.views',
+ #url(r'^serve/(?P<uuid>.+)/(?P<time_stamp>\d+)/$', 'resource_serve', (), 'resource_serve'),
+ #url(r'^serve/(?P<uuid>.+)/$', 'resource_serve', (), 'resource_serve'),
+ #url(r'^serve/$', 'resource_serve', (), 'resource_serve'),
+ #url(r'^upload/$', 'resource_upload', (), 'resource_upload'),
+ url(r'^create/$', 'key_create', (), 'key_create'),
+ url(r'^list/private/$', 'key_list', {'secret': True}, 'key_private_list'),
+ url(r'^list/public/$', 'key_list', {'secret': False}, 'key_public_list'),
+)
@@ -1 +1,52 @@
-# Create your views here.
+from django.utils.translation import ugettext_lazy as _
+from django.http import HttpResponseRedirect, HttpResponse, Http404
+from django.shortcuts import render_to_response, get_object_or_404
+from django.template import RequestContext
+from django.contrib import messages
+from django.views.generic.list_detail import object_list
+from django.core.urlresolvers import reverse
+
+from django_gpg import Key
+from django_gpg.forms import NewKeyForm
+
+from core.runtime import gpg
+
+
+def key_list(request, secret=True):
+ if secret:
+ object_list = Key.get_all(gpg, secret=True)
+ title = _(u'Private key list')
+ else:
+ object_list = Key.get_all(gpg)
+ title = _(u'Public key list')
+
+ return render_to_response('key_list.html', {
+ 'object_list': object_list,
+ 'title': title,
+ }, context_instance=RequestContext(request))
+
+
+def key_create(request):
+ if request.method == 'POST':
+ form = NewKeyForm(request.POST)
+ if form.is_valid():
+ try:
+ key = gpg.create_key(
+ name_real = form.cleaned_data['name'],
+ name_comment = form.cleaned_data['comment'],
+ name_email = form.cleaned_data['email'],
+ )
+
+
+ messages.success(request, _(u'Key: %s, created successfully.') % key)
+ return HttpResponseRedirect(reverse('key_create'))
+ except Exception, msg:
+ messages.error(request, msg)
+ return HttpResponseRedirect(reverse('key_create'))
+ else:
+ form = NewKeyForm()
+
+ return render_to_response('generic_form.html', {
+ 'form': form,
+ 'title': _(u'Create a new key'),
+ }, context_instance=RequestContext(request))
View
@@ -10,6 +10,7 @@
url(r'^common/', include('common.urls')),
url(r'^bittorrent/', include('bittorrent.urls')),
url(r'^resources/', include('openrelay_resources.urls')),
+ url(r'^gpg/', include('django_gpg.urls')),
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
url(r'^admin/', include(admin.site.urls)),

0 comments on commit 93ab976

Please sign in to comment.