X-Git-Url: https://git.mdrn.pl/cas.git/blobdiff_plain/e236585868829946444ac537adbbf481782ecb8f..fb8d40c9ddc4534e8209649edf6eb8e03b242899:/provider/cas_provider/views.py
diff --git a/provider/cas_provider/views.py b/provider/cas_provider/views.py
index 2733eb3..e2466b8 100644
--- a/provider/cas_provider/views.py
+++ b/provider/cas_provider/views.py
@@ -4,6 +4,7 @@ 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 cas_provider.forms import LoginForm
from cas_provider.models import ServiceTicket, LoginTicket, auth_success_response
@@ -15,14 +16,13 @@ try:
from urlparse import parse_qs as url_parse_qs
except ImportError:
from cgi import parse_qs as url_parse_qs
-
import logging
-logger = logging.getLogger("fnp.cas.provider")
+logger = logging.getLogger("cas.provider")
__all__ = ['login', 'validate', 'service_validate', 'logout']
-def _add_query_param(url, param, value):
+def _add_query_param(url, param, value):
parsed = urlparse.urlparse(url)
query = url_parse_qs(parsed.query)
query[param] = [unicode(value, 'utf-8')]
@@ -33,12 +33,10 @@ def _add_query_param(url, param, value):
return parsed.geturl()
-def login(request, template_name = 'cas/login.html', success_redirect = '/accounts/'):
+def login(request, template_name='cas/login.html', success_redirect='/accounts/'):
service = request.GET.get('service', None)
-
+
if request.user.is_authenticated():
- logger.info("User %s passed auth, service is %s", request.user, service)
-
if service is not None:
ticket = create_service_ticket(request.user, service)
target = _add_query_param(service, 'ticket', ticket.ticket)
@@ -47,53 +45,46 @@ def login(request, template_name = 'cas/login.html', success_redirect = '/accoun
else:
logger.info("Redirecting to default: %s", success_redirect)
return HttpResponseRedirect(success_redirect)
-
+
errors = []
if request.method == 'POST':
username = request.POST.get('username', None)
password = request.POST.get('password', None)
service = request.POST.get('service', None)
lt = request.POST.get('lt', None)
-
+
logger.debug("User %s logging in", username)
- logger.info("Login submit: serivce = %s, Lticket=%s",service, lt)
try:
- login_ticket = LoginTicket.objects.get(ticket = lt)
+ login_ticket = LoginTicket.objects.get(ticket=lt)
except:
- errors.append('Login ticket expired. Please try again.')
+ errors.append(_(u'Login ticket expired. Please try again.'))
else:
login_ticket.delete()
- logger.debug("Auth")
- user = authenticate(username = username, password = password)
+ user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
- logger.debug("AuthLogin")
auth_login(request, user)
- if service is not None:
+ if service is not None:
ticket = create_service_ticket(user, service)
- logger.info("Service=%s, ticket=%s", service, ticket)
target = _add_query_param(service, 'ticket', ticket.ticket)
- logger.info("Redirecting to %s", target)
return HttpResponseRedirect(target)
else:
- logger.info("Redirecting to default: %s", success_redirect)
return HttpResponseRedirect(success_redirect)
else:
- errors.append('This account is disabled.')
+ errors.append(_(u'This account is disabled.'))
else:
- errors.append('Incorrect username and/or password.')
-
- logger.debug("LOGIN GET, service = %s", service)
+ errors.append(_(u'Incorrect username and/or password.'))
+
form = LoginForm(service)
- return render_to_response(template_name, {'form': form, 'errors': errors}, context_instance = RequestContext(request))
+ return render_to_response(template_name, {'form': form, 'errors': errors}, context_instance=RequestContext(request))
def validate(request):
service = request.GET.get('service', None)
ticket_string = request.GET.get('ticket', None)
if service is not None and ticket_string is not None:
try:
- ticket = ServiceTicket.objects.get(ticket = ticket_string)
+ ticket = ServiceTicket.objects.get(ticket=ticket_string)
username = ticket.user.username
ticket.delete()
return HttpResponse("yes\n%s\n" % username)
@@ -105,24 +96,24 @@ def service_validate(request):
service = request.GET.get('service', None)
ticket_string = request.GET.get('ticket', None)
if service is None or ticket_string is None:
- return HttpResponse('''
+ return HttpResponse(r'''
Not all required parameters were sent.
- ''', mimetype = 'text/xml')
+ ''', mimetype='application/xml')
try:
- ticket = ServiceTicket.objects.get(ticket = ticket_string)
+ ticket = ServiceTicket.objects.get(ticket=ticket_string)
ticket.delete()
- return HttpResponse(auth_success_response(ticket.user), mimetype = 'text/xml')
+ return HttpResponse(auth_success_response(ticket.user), mimetype='text/xml')
except ServiceTicket.DoesNotExist:
- return HttpResponse('''
+ return HttpResponse(r'''
The provided ticket is invalid.
- ''', mimetype = 'text/xml')
+ ''', mimetype='application/xml')
-def logout(request, template_name = 'cas/logout.html'):
+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))
+ return render_to_response(template_name, {'url': url}, context_instance=RequestContext(request))