views.py 5.48 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
from django.http import HttpResponse, HttpResponseRedirect
from django.views.decorators.http import require_http_methods, require_GET
31
from django.views.decorators.csrf import csrf_exempt
32 33
from django.shortcuts import get_object_or_404, render_to_response
from django.template import RequestContext
34
from django.contrib import messages
35 36 37
from django.contrib.auth.decorators import login_required
from django.core.exceptions import PermissionDenied
from django.core.urlresolvers import reverse
38
from django.utils.http import is_safe_url
39 40 41
from django_sshkey import settings
from django_sshkey.models import UserKey
from django_sshkey.forms import UserKeyForm
42

43 44
@require_http_methods(['GET', 'POST'])
@csrf_exempt
45
def lookup(request):
46 47 48 49
  if request.method == 'POST':
    payload = request.read()
    key = UserKey.objects.get(id=int(payload))
    key.touch()
50
    return HttpResponse(str(key.last_used), content_type='text/plain')
51 52 53 54 55 56 57 58 59 60 61
  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:
62 63
    if settings.SSHKEY_AUTHORIZED_KEYS_OPTIONS:
      options = settings.SSHKEY_AUTHORIZED_KEYS_OPTIONS.format(
64 65 66
        username=key.user.username,
        key_id=key.id,
      ) + ' '
67
    else:
68 69
      options = ''
    response += options + key.key + '\n'
70
  return HttpResponse(response, content_type='text/plain')
71 72 73 74 75 76 77

@login_required
@require_GET
def userkey_list(request):
  userkey_list = UserKey.objects.filter(user=request.user)
  return render_to_response(
    'sshkey/userkey_list.html',
78
    { 'userkey_list': userkey_list, 'allow_edit': settings.SSHKEY_ALLOW_EDIT },
79 80 81 82 83 84 85 86
    context_instance = RequestContext(request),
  )

@login_required
@require_http_methods(['GET', 'POST'])
def userkey_add(request):
  if request.method == 'POST':
    userkey = UserKey(user=request.user)
87
    userkey.request = request
88 89 90
    form = UserKeyForm(request.POST, instance=userkey)
    if form.is_valid():
      form.save()
91
      default_redirect = reverse('django_sshkey.views.userkey_list')
92 93 94
      url = request.GET.get('next', default_redirect)
      if not is_safe_url(url=url, host=request.get_host()):
        url = default_redirect
95
      message = 'SSH public key %s was added.' % userkey.name
96
      messages.success(request, message, fail_silently=True)
97
      return HttpResponseRedirect(url)
98 99 100 101 102 103 104 105 106 107 108
  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):
109 110
  if not settings.SSHKEY_ALLOW_EDIT:
    raise PermissionDenied
111 112 113 114 115 116 117
  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()
118
      default_redirect = reverse('django_sshkey.views.userkey_list')
119 120 121
      url = request.GET.get('next', default_redirect)
      if not is_safe_url(url=url, host=request.get_host()):
        url = default_redirect
122
      message = 'SSH public key %s was saved.' % userkey.name
123
      messages.success(request, message, fail_silently=True)
124
      return HttpResponseRedirect(url)
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
  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()
140
  message = 'SSH public key %s was deleted.' % userkey.name
141
  messages.success(request, message, fail_silently=True)
142
  return HttpResponseRedirect(reverse('django_sshkey.views.userkey_list'))