Commit aaa45a30 by Paul Kilgo

allow clients to register key formatters

parent 8ab1eb6c
# Copyright (c) 2014, Clemson University
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# * Neither the name of the {organization} nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
from django_sshkey.models import UserKey
from django_sshkey import settings
def userkey_formatter(key):
userkey = UserKey.objects.select_related('user').get(basekey=key)
if settings.SSHKEY_AUTHORIZED_KEYS_OPTIONS:
options = settings.SSHKEY_AUTHORIZED_KEYS_OPTIONS.format(
username=userkey.user.username,
key_id=key.id,
) + ' '
else:
options = ''
return options + key.key
...@@ -36,3 +36,13 @@ SSHKEY_EMAIL_ADD_KEY_SUBJECT = getattr(settings, 'SSHKEY_EMAIL_ADD_KEY_SUBJECT', ...@@ -36,3 +36,13 @@ SSHKEY_EMAIL_ADD_KEY_SUBJECT = getattr(settings, 'SSHKEY_EMAIL_ADD_KEY_SUBJECT',
) )
SSHKEY_FROM_EMAIL = getattr(settings, 'SSHKEY_FROM_EMAIL', settings.DEFAULT_FROM_EMAIL) SSHKEY_FROM_EMAIL = getattr(settings, 'SSHKEY_FROM_EMAIL', settings.DEFAULT_FROM_EMAIL)
SSHKEY_SEND_HTML_EMAIL = getattr(settings, 'SSHKEY_SEND_HTML_EMAIL', False) SSHKEY_SEND_HTML_EMAIL = getattr(settings, 'SSHKEY_SEND_HTML_EMAIL', False)
SSHKEY_KEY_FORMATTERS = getattr(settings, 'SSHKEY_KEY_FORMATTERS', {})
SSHKEY_KEY_FORMATTERS.setdefault('django_sshkey.userkey', 'django_sshkey.defaults.userkey_formatter')
def get_formatter(key):
module_name, object_name = SSHKEY_KEY_FORMATTERS[key].rsplit('.', 1)
obj = __import__(module_name)
for p in module_name.split('.')[1:]:
obj = getattr(obj, p)
return getattr(obj, object_name)
...@@ -37,7 +37,7 @@ from django.core.exceptions import PermissionDenied ...@@ -37,7 +37,7 @@ from django.core.exceptions import PermissionDenied
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from django.utils.http import is_safe_url from django.utils.http import is_safe_url
from django_sshkey import settings from django_sshkey import settings
from django_sshkey.models import UserKey from django_sshkey.models import UserKey, Key
from django_sshkey.forms import UserKeyForm from django_sshkey.forms import UserKeyForm
@require_http_methods(['GET', 'POST']) @require_http_methods(['GET', 'POST'])
...@@ -45,28 +45,28 @@ from django_sshkey.forms import UserKeyForm ...@@ -45,28 +45,28 @@ from django_sshkey.forms import UserKeyForm
def lookup(request): def lookup(request):
if request.method == 'POST': if request.method == 'POST':
payload = request.read() payload = request.read()
key = UserKey.objects.get(id=int(payload)) key = Key.objects.get(id=int(payload))
key.touch() key.touch()
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(basekey__fingerprint=fingerprint) keys = Key.objects.filter(fingerprint=fingerprint)
except KeyError: except KeyError:
try: try:
username = request.GET['username'] username = request.GET['username']
keys = UserKey.objects.filter(user__username=username) keys = Key.objects.filter(
content_type__app_label='django_sshkey',
content_type__model='userkey',
userkey__user__username=username
)
except KeyError: except KeyError:
keys = UserKey.objects.iterator() keys = Key.objects.iterator()
response = '' response = ''
for key in keys: for key in keys:
if settings.SSHKEY_AUTHORIZED_KEYS_OPTIONS: content_type = key.content_type
options = settings.SSHKEY_AUTHORIZED_KEYS_OPTIONS.format( formatter_key = '%s.%s' % (content_type.app_label, content_type.model)
username=key.user.username, formatter = settings.get_formatter(formatter_key)
key_id=key.id, response += formatter(key) + '\n'
) + ' '
else:
options = ''
response += options + key.key + '\n'
return HttpResponse(response, content_type='text/plain') return HttpResponse(response, content_type='text/plain')
@login_required @login_required
......
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