views.py 3.68 KB
Newer Older
1 2 3 4
from django.http import HttpResponse, HttpResponseRedirect
from django.views.decorators.http import require_http_methods, require_GET
from django.shortcuts import get_object_or_404, render_to_response
from django.template import RequestContext
5
from django.contrib import messages
6 7 8
from django.contrib.auth.decorators import login_required
from django.core.exceptions import PermissionDenied
from django.core.urlresolvers import reverse
9
from django.utils.http import is_safe_url
10
from sshkey import settings
11
from sshkey.models import UserKey
12
from sshkey.forms import UserKeyForm
13

14
@require_GET
15 16 17 18 19 20 21 22 23 24 25 26
def lookup(request):
  try:
    fingerprint = request.GET['fingerprint']
    keys = UserKey.objects.filter(fingerprint=fingerprint)
  except KeyError:
    try:
      username = request.GET['username']
      keys = UserKey.objects.filter(user__username=username)
    except KeyError:
      keys = UserKey.objects.iterator()
  response = ''
  for key in keys:
27 28 29 30
    if settings.SSHKEY_AUTHORIZED_KEYS_OPTIONS:
      options = settings.SSHKEY_AUTHORIZED_KEYS_OPTIONS.format(
        username=key.user.username) + ' '
    elif settings.SSHKEY_AUTHORIZED_KEYS_COMMAND:
31 32 33 34 35
      options = 'command="%s" ' % (
        settings.SSHKEY_AUTHORIZED_KEYS_COMMAND
          .format(username=key.user.username)
          .replace('"', r'\"')
      )
36
    else:
37 38 39
      options = ''
    response += options + key.key + '\n'
  return HttpResponse(response, mimetype='text/plain')
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58

@login_required
@require_GET
def userkey_list(request):
  userkey_list = UserKey.objects.filter(user=request.user)
  return render_to_response(
    'sshkey/userkey_list.html',
    { 'userkey_list': userkey_list },
    context_instance = RequestContext(request),
  )

@login_required
@require_http_methods(['GET', 'POST'])
def userkey_add(request):
  if request.method == 'POST':
    userkey = UserKey(user=request.user)
    form = UserKeyForm(request.POST, instance=userkey)
    if form.is_valid():
      form.save()
59 60 61 62
      default_redirect = reverse('sshkey.views.userkey_list')
      url = request.GET.get('next', default_redirect)
      if not is_safe_url(url=url, host=request.get_host()):
        url = default_redirect
63
      message = 'SSH key %s was saved.' % userkey.name
64
      messages.success(request, message, fail_silently=True)
65
      return HttpResponseRedirect(url)
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
  else:
    form = UserKeyForm()
  return render_to_response(
    'sshkey/userkey_detail.html',
    { 'form': form, 'action': 'add' },
    context_instance = RequestContext(request),
  )

@login_required
@require_http_methods(['GET', 'POST'])
def userkey_edit(request, pk):
  userkey = get_object_or_404(UserKey, pk=pk)
  if userkey.user != request.user:
    raise PermissionDenied
  if request.method == 'POST':
    form = UserKeyForm(request.POST, instance=userkey)
    if form.is_valid():
      form.save()
84 85 86 87
      default_redirect = reverse('sshkey.views.userkey_list')
      url = request.GET.get('next', default_redirect)
      if not is_safe_url(url=url, host=request.get_host()):
        url = default_redirect
88
      message = 'SSH key %s was saved.' % userkey.name
89
      messages.success(request, message, fail_silently=True)
90
      return HttpResponseRedirect(url)
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
  else:
    form = UserKeyForm(instance=userkey)
  return render_to_response(
    'sshkey/userkey_detail.html',
    { 'form': form, 'action': 'edit' },
    context_instance = RequestContext(request),
  )

@login_required
@require_GET
def userkey_delete(request, pk):
  userkey = get_object_or_404(UserKey, pk=pk)
  if userkey.user != request.user:
    raise PermissionDenied
  userkey.delete()
106 107
  message = 'SSH key %s was deleted.' % userkey.name
  messages.success(request, message, fail_silently=True)
108
  return HttpResponseRedirect(reverse('sshkey.views.userkey_list'))