views.py 5.23 KB
Newer Older
1 2
# Copyright (c) 2014, Clemson University
# All rights reserved.
Scott Duckworth committed
3
#
4 5
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
Scott Duckworth committed
6
#
7 8
# * Redistributions of source code must retain the above copyright notice, this
#   list of conditions and the following disclaimer.
Scott Duckworth committed
9
#
10 11 12
# * 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.
Scott Duckworth committed
13
#
14 15 16 17 18 19 20 21 22 23 24 25 26 27
# * 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.
Scott Duckworth committed
28

29 30 31 32
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
33
from django.contrib import messages
34 35 36
from django.contrib.auth.decorators import login_required
from django.core.exceptions import PermissionDenied
from django.core.urlresolvers import reverse
37
from django.utils.http import is_safe_url
38 39 40
from django_sshkey import settings
from django_sshkey.models import UserKey
from django_sshkey.forms import UserKeyForm
41

42
@require_GET
43 44 45 46 47 48 49 50 51 52 53 54
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:
55 56 57 58
    if settings.SSHKEY_AUTHORIZED_KEYS_OPTIONS:
      options = settings.SSHKEY_AUTHORIZED_KEYS_OPTIONS.format(
        username=key.user.username) + ' '
    elif settings.SSHKEY_AUTHORIZED_KEYS_COMMAND:
59 60 61 62 63
      options = 'command="%s" ' % (
        settings.SSHKEY_AUTHORIZED_KEYS_COMMAND
          .format(username=key.user.username)
          .replace('"', r'\"')
      )
64
    else:
65 66 67
      options = ''
    response += options + key.key + '\n'
  return HttpResponse(response, mimetype='text/plain')
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86

@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()
87
      default_redirect = reverse('django_sshkey.views.userkey_list')
88 89 90
      url = request.GET.get('next', default_redirect)
      if not is_safe_url(url=url, host=request.get_host()):
        url = default_redirect
91
      message = 'SSH key %s was saved.' % userkey.name
92
      messages.success(request, message, fail_silently=True)
93
      return HttpResponseRedirect(url)
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
  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()
112
      default_redirect = reverse('django_sshkey.views.userkey_list')
113 114 115
      url = request.GET.get('next', default_redirect)
      if not is_safe_url(url=url, host=request.get_host()):
        url = default_redirect
116
      message = 'SSH key %s was saved.' % userkey.name
117
      messages.success(request, message, fail_silently=True)
118
      return HttpResponseRedirect(url)
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
  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()
134 135
  message = 'SSH key %s was deleted.' % userkey.name
  messages.success(request, message, fail_silently=True)
136
  return HttpResponseRedirect(reverse('django_sshkey.views.userkey_list'))