1 """CAS login/logout replacement views"""
3 from urllib import urlencode
4 from urlparse import urljoin
6 from django.http import get_host, HttpResponseRedirect, HttpResponseForbidden
7 from django.conf import settings
8 from django.contrib.auth import REDIRECT_FIELD_NAME
10 __all__ = ['login', 'logout']
12 def _service_url(request, redirect_to = None):
13 """Generates application service URL for CAS"""
15 protocol = ('http://', 'https://')[request.is_secure()]
16 host = get_host(request)
17 service = protocol + host + request.path
23 service += urlencode({REDIRECT_FIELD_NAME: redirect_to.encode('utf-8')})
27 def _redirect_url(request):
28 """Redirects to referring page, or CAS_REDIRECT_URL if no referrer is
32 next = request.GET.get(REDIRECT_FIELD_NAME)
34 if settings.CAS_IGNORE_REFERER:
35 next = settings.CAS_REDIRECT_URL
37 next = request.META.get('HTTP_REFERER', settings.CAS_REDIRECT_URL)
38 prefix = (('http://', 'https://')[request.is_secure()] +
40 if next.startswith(prefix):
41 next = next[len(prefix):]
45 def _login_url(service):
46 """Generates CAS login URL"""
48 params = {'service': service}
49 if settings.CAS_EXTRA_LOGIN_PARAMS:
50 params.update(settings.CAS_EXTRA_LOGIN_PARAMS)
51 return urljoin(settings.CAS_SERVER_URL, 'login') + '?' + urlencode(params)
54 def _logout_url(request, next_page = None):
55 """Generates CAS logout URL"""
57 url = urljoin(settings.CAS_SERVER_URL, 'logout')
59 protocol = ('http://', 'https://')[request.is_secure()]
60 host = get_host(request)
61 url += '?' + urlencode({'url': protocol + host + next_page})
65 def login(request, next_page = None, required = False):
66 """Forwards to CAS login URL or verifies CAS ticket"""
68 print "LOGIN original NEXT_PAGE:", next_page
71 next_page = _redirect_url(request)
72 print "LOGIN redirect NEXT_PAGE:", next_page
74 if request.user.is_authenticated():
75 message = "You are logged in as %s." % request.user.username
76 request.user.message_set.create(message = message)
77 return HttpResponseRedirect(next_page)
78 ticket = request.GET.get('ticket')
79 service = _service_url(request, next_page)
80 print "TICKET: ", ticket
81 print "SERVICE: ", service
83 from django.contrib import auth
84 user = auth.authenticate(ticket = ticket, service = service)
86 auth.login(request, user)
87 name = user.first_name or user.username
88 message = "Login succeeded. Welcome, %s." % name
89 user.message_set.create(message = message)
90 return HttpResponseRedirect(next_page)
91 elif settings.CAS_RETRY_LOGIN or required:
92 return HttpResponseRedirect(_login_url(service))
94 error = "<h1>Forbidden</h1><p>Login failed.</p>"
95 return HttpResponseForbidden(error)
98 return HttpResponseRedirect(_login_url(service))
101 def logout(request, next_page = None):
102 """Redirects to CAS logout page"""
104 from django.contrib.auth import logout
107 next_page = _redirect_url(request)
108 if settings.CAS_LOGOUT_COMPLETELY:
109 return HttpResponseRedirect(_logout_url(request, next_page))
111 return HttpResponseRedirect(next_page)