X-Git-Url: https://git.mdrn.pl/django-cas-provider.git/blobdiff_plain/cc61c5969227c2d49544997ebbf074541029400b..3cbcd5e5abc9765cd329b09de514f2b5c47b7723:/cas_provider/views.py?ds=inline
diff --git a/cas_provider/views.py b/cas_provider/views.py
index 6fc19f6..3686d74 100644
--- a/cas_provider/views.py
+++ b/cas_provider/views.py
@@ -1,19 +1,19 @@
-from django.http import HttpResponse, HttpResponseForbidden, HttpResponseRedirect
-from django.shortcuts import get_object_or_404, render_to_response, get_list_or_404
-from django.core.urlresolvers import reverse
-from django.core.exceptions import SuspiciousOperation
+from django.conf import settings
+from django.contrib.auth import authenticate, login as auth_login, \
+ logout as auth_logout
+from django.http import HttpResponse, HttpResponseRedirect
+from django.shortcuts import render_to_response
from django.template import RequestContext
-from django.contrib.auth.models import User
-from django.contrib.auth import authenticate
-from django.contrib.auth import login as auth_login, logout as auth_logout
-
+from django.utils.translation import ugettext_lazy as _
from forms import LoginForm
from models import ServiceTicket, LoginTicket
from utils import create_service_ticket
-__all__ = ['login', 'validate', 'logout']
-def login(request, template_name='cas/login.html', success_redirect='/accounts/'):
+__all__ = ['login', 'validate', 'logout', 'service_validate']
+
+
+def login(request, template_name='cas/login.html', success_redirect=getattr(settings, 'LOGIN_REDIRECT_URL', '/accounts/')):
service = request.GET.get('service', None)
if request.user.is_authenticated():
if service is not None:
@@ -30,11 +30,11 @@ def login(request, template_name='cas/login.html', success_redirect='/accounts/'
password = request.POST.get('password', None)
service = request.POST.get('service', None)
lt = request.POST.get('lt', None)
-
+
try:
login_ticket = LoginTicket.objects.get(ticket=lt)
except:
- errors.append('Login ticket expired. Please try again.')
+ errors.append(_('Login ticket expired. Please try again.'))
else:
login_ticket.delete()
user = authenticate(username=username, password=password)
@@ -47,13 +47,15 @@ def login(request, template_name='cas/login.html', success_redirect='/accounts/'
else:
return HttpResponseRedirect(success_redirect)
else:
- errors.append('This account is disabled.')
+ errors.append(_('This account is disabled.'))
else:
- errors.append('Incorrect username and/or password.')
+ errors.append(_('Incorrect username and/or password.'))
form = LoginForm(service)
return render_to_response(template_name, {'form': form, 'errors': errors}, context_instance=RequestContext(request))
-
+
+
def validate(request):
+ """Validate ticket via CAS v.1 protocol"""
service = request.GET.get('service', None)
ticket_string = request.GET.get('ticket', None)
if service is not None and ticket_string is not None:
@@ -61,12 +63,46 @@ def validate(request):
ticket = ServiceTicket.objects.get(ticket=ticket_string)
username = ticket.user.username
ticket.delete()
- return HttpResponse("yes\n\r%s\n\r" % username)
+ return HttpResponse("yes\r\n%s\r\n" % username)
except:
pass
- return HttpResponse("no\n\r\n\r")
-
+ return HttpResponse("no\r\n\r\n")
+
+
def logout(request, template_name='cas/logout.html'):
url = request.GET.get('url', None)
auth_logout(request)
- return render_to_response(template_name, {'url': url}, context_instance=RequestContext(request))
\ No newline at end of file
+ return render_to_response(template_name, {'url': url}, context_instance=RequestContext(request))
+
+
+def service_validate(request):
+ """Validate ticket via CAS v.2 protocol"""
+ service = request.GET.get('service', None)
+ ticket_string = request.GET.get('ticket', None)
+ if service is None or ticket_string is None:
+ return _cas2_error_response(u'INVALID_REQUEST', u'Not all required parameters were sent.')
+
+ try:
+ ticket = ServiceTicket.objects.get(ticket=ticket_string)
+ except ServiceTicket.DoesNotExist:
+ return _cas2_error_response(u'INVALID_TICKET', u'The provided ticket is invalid.')
+
+ if settings.CAS_CHECK_SERVICE and ticket.service != service:
+ ticket.delete()
+ return _cas2_error_response('INVALID_SERVICE', u'Service is invalid')
+
+ username = ticket.user.username
+ ticket.delete()
+ return HttpResponse(u'''
+
+ %(username)s
+
+ ''' % {'username': username}, mimetype='text/xml')
+
+
+def _cas2_error_response(code, message):
+ return HttpResponse(u''''
+
+ %s
+
+ ''', mimetype='text/xml')