/
views.py
executable file
·74 lines (62 loc) · 2.5 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
from django.conf import settings
from django.contrib import auth
from django.contrib.auth.decorators import login_required
from django.core.exceptions import ImproperlyConfigured
from django.http import HttpResponse
from django.shortcuts import redirect
from django.utils.decorators import method_decorator
from django.views.generic import TemplateView
from urllib.parse import quote
#Logout settings.
from shibboleth.app_settings import LOGOUT_URL, LOGOUT_REDIRECT_URL
class ShibbolethView(TemplateView):
"""
This is here to offer a Shib protected page that we can
route users through to login.
"""
template_name = 'shibboleth/user_info.html'
@method_decorator(login_required)
def dispatch(self, request, *args, **kwargs):
"""
Django docs say to decorate the dispatch method for
class based views.
https://docs.djangoproject.com/en/dev/topics/auth/
"""
return super(ShibbolethView, self).dispatch(request, *args, **kwargs)
def get(self, request, **kwargs):
"""Process the request."""
next = self.request.GET.get('next', None)
if next is not None:
return redirect(next)
return super(ShibbolethView, self).get(request)
def get_context_data(self, **kwargs):
context = super(ShibbolethView, self).get_context_data(**kwargs)
context['user'] = self.request.user
return context
class ShibbolethLoginView(TemplateView):
"""
Pass the user to the Shibboleth login page.
Some code borrowed from:
https://github.com/stefanfoulis/django-class-based-auth-views.
"""
redirect_field_name = "target"
def get(self, *args, **kwargs):
#Remove session value that is forcing Shibboleth reauthentication.
login = settings.LOGIN_URL + '?target=%s' % quote(self.request.GET.get(self.redirect_field_name, ''))
return redirect(login)
class ShibbolethLogoutView(TemplateView):
"""
Pass the user to the Shibboleth logout page.
Some code borrowed from:
https://github.com/stefanfoulis/django-class-based-auth-views.
"""
redirect_field_name = "target"
def get(self, request, *args, **kwargs):
#Log the user out.
auth.logout(self.request)
#Get target url in order of preference.
target = LOGOUT_REDIRECT_URL or\
quote(self.request.GET.get(self.redirect_field_name, '')) or\
quote(request.build_absolute_uri())
logout = LOGOUT_URL % target
return redirect(logout)