2015-05-16 21:43:46 +00:00
|
|
|
# ⁻*- coding: utf-8 -*-
|
2015-05-27 19:56:39 +00:00
|
|
|
"""views for the app"""
|
|
|
|
import cas_server.default_settings
|
2015-05-17 21:24:41 +00:00
|
|
|
|
2015-05-16 21:43:46 +00:00
|
|
|
from django.shortcuts import render, redirect
|
2015-05-27 19:56:39 +00:00
|
|
|
from django.http import HttpResponse, HttpResponseRedirect
|
2015-05-16 21:43:46 +00:00
|
|
|
from django.conf import settings
|
|
|
|
from django.contrib import messages
|
2015-05-22 17:31:50 +00:00
|
|
|
from django.views.decorators.csrf import csrf_exempt
|
2015-05-22 15:55:00 +00:00
|
|
|
from django.utils.translation import ugettext as _
|
2015-05-27 19:56:39 +00:00
|
|
|
from django.core.urlresolvers import reverse
|
|
|
|
from django.utils import timezone
|
2015-05-16 21:43:46 +00:00
|
|
|
|
|
|
|
import requests
|
2015-05-23 17:57:18 +00:00
|
|
|
import urllib
|
2015-05-22 17:31:50 +00:00
|
|
|
from lxml import etree
|
2015-05-27 19:56:39 +00:00
|
|
|
from datetime import timedelta
|
2015-05-16 21:43:46 +00:00
|
|
|
|
2015-05-27 19:56:39 +00:00
|
|
|
from . import utils
|
|
|
|
from . import forms
|
|
|
|
from . import models
|
2015-05-16 21:43:46 +00:00
|
|
|
|
|
|
|
def _logout(request):
|
2015-05-27 19:56:39 +00:00
|
|
|
"""Clean sessions variables"""
|
|
|
|
try:
|
|
|
|
del request.session["authenticated"]
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
try:
|
|
|
|
del request.session["username"]
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
try:
|
|
|
|
del request.session["warn"]
|
|
|
|
except KeyError:
|
|
|
|
pass
|
2015-05-16 21:43:46 +00:00
|
|
|
|
2015-05-23 17:57:18 +00:00
|
|
|
|
2015-05-27 19:56:39 +00:00
|
|
|
def redirect_params(url_name, params=None):
|
|
|
|
"""Redirect to `url_name` with `params` as querystring"""
|
|
|
|
url = reverse(url_name)
|
|
|
|
params = urllib.urlencode(params if params else {})
|
2015-05-23 17:57:18 +00:00
|
|
|
return HttpResponseRedirect(url + "?%s" % params)
|
|
|
|
|
2015-05-27 19:56:39 +00:00
|
|
|
|
2015-05-16 21:43:46 +00:00
|
|
|
def login(request):
|
2015-05-27 19:56:39 +00:00
|
|
|
"""credential requestor / acceptor"""
|
2015-05-16 21:43:46 +00:00
|
|
|
user = None
|
|
|
|
form = None
|
|
|
|
service_pattern = None
|
|
|
|
renewed = False
|
2015-05-16 21:50:25 +00:00
|
|
|
warned = False
|
2015-05-16 21:43:46 +00:00
|
|
|
if request.method == 'POST':
|
|
|
|
service = request.POST.get('service')
|
|
|
|
renew = True if request.POST.get('renew') else False
|
|
|
|
gateway = request.POST.get('gateway')
|
|
|
|
method = request.POST.get('method')
|
|
|
|
|
|
|
|
if not request.session.get("authenticated") or renew:
|
2015-05-27 19:56:39 +00:00
|
|
|
form = forms.UserCredential(
|
|
|
|
request.POST,
|
|
|
|
initial={'service':service, 'method':method, 'warn':request.session.get("warn")}
|
|
|
|
)
|
2015-05-16 21:43:46 +00:00
|
|
|
if form.is_valid():
|
|
|
|
user = models.User.objects.get(username=form.cleaned_data['username'])
|
2015-05-19 16:33:56 +00:00
|
|
|
request.session.set_expiry(0)
|
2015-05-16 21:43:46 +00:00
|
|
|
request.session["username"] = form.cleaned_data['username']
|
|
|
|
request.session["warn"] = True if form.cleaned_data.get("warn") else False
|
|
|
|
request.session["authenticated"] = True
|
|
|
|
renewed = True
|
2015-05-16 21:50:25 +00:00
|
|
|
warned = True
|
2015-05-16 21:43:46 +00:00
|
|
|
else:
|
|
|
|
_logout(request)
|
|
|
|
else:
|
|
|
|
service = request.GET.get('service')
|
|
|
|
renew = True if request.GET.get('renew') else False
|
|
|
|
gateway = request.GET.get('gateway')
|
|
|
|
method = request.GET.get('method')
|
|
|
|
|
|
|
|
if not request.session.get("authenticated") or renew:
|
2015-05-27 19:56:39 +00:00
|
|
|
form = forms.UserCredential(
|
|
|
|
initial={'service':service, 'method':method, 'warn':request.session.get("warn")}
|
|
|
|
)
|
|
|
|
|
2015-05-16 21:43:46 +00:00
|
|
|
# if authenticated and successfully renewed authentication if needed
|
2015-05-27 19:56:39 +00:00
|
|
|
if request.session.get("authenticated") and \
|
|
|
|
request.session.get("username") and (not renew or renewed):
|
2015-05-16 21:43:46 +00:00
|
|
|
try:
|
|
|
|
user = models.User.objects.get(username=request.session["username"])
|
|
|
|
except models.User.DoesNotExist:
|
|
|
|
_logout(request)
|
2015-05-23 17:57:18 +00:00
|
|
|
return redirect_params("login", params=dict(request.GET))
|
2015-05-16 21:43:46 +00:00
|
|
|
|
|
|
|
# if login agains a service is requestest
|
|
|
|
if service:
|
|
|
|
try:
|
|
|
|
# is the service allowed
|
|
|
|
service_pattern = models.ServicePattern.validate(service)
|
|
|
|
# is the current user allowed on this service
|
|
|
|
service_pattern.check_user(user)
|
2015-05-27 19:56:39 +00:00
|
|
|
# if the user has asked to be warned before any login to a service
|
2015-05-23 17:43:21 +00:00
|
|
|
if request.session.get("warn", True) and not warned:
|
2015-05-27 19:56:39 +00:00
|
|
|
messages.add_message(
|
|
|
|
request,
|
|
|
|
messages.WARNING,
|
|
|
|
_(u"Authentication has been required by service %(name)s (%(url)s)") % \
|
|
|
|
{'name':service_pattern.name, 'url':service}
|
|
|
|
)
|
|
|
|
return render(
|
|
|
|
request,
|
|
|
|
settings.CAS_WARN_TEMPLATE,
|
|
|
|
{'service_ticket_url':user.get_service_url(
|
|
|
|
service,
|
|
|
|
service_pattern,
|
|
|
|
renew=renew
|
|
|
|
)}
|
|
|
|
)
|
2015-05-16 21:43:46 +00:00
|
|
|
else:
|
2015-05-27 19:56:39 +00:00
|
|
|
# redirect, using method ?
|
|
|
|
return redirect(user.get_service_url(service, service_pattern, renew=renew))
|
2015-05-16 21:43:46 +00:00
|
|
|
except models.ServicePattern.DoesNotExist:
|
2015-05-27 19:56:39 +00:00
|
|
|
messages.add_message(
|
|
|
|
request,
|
|
|
|
messages.ERROR,
|
|
|
|
_(u'Service %(url)s non allowed.') % {'url' : service}
|
|
|
|
)
|
2015-05-16 21:43:46 +00:00
|
|
|
except models.BadUsername:
|
2015-05-27 19:56:39 +00:00
|
|
|
messages.add_message(
|
|
|
|
request,
|
|
|
|
messages.ERROR,
|
|
|
|
_(u"Username non allowed")
|
|
|
|
)
|
2015-05-16 21:43:46 +00:00
|
|
|
except models.BadFilter:
|
2015-05-27 19:56:39 +00:00
|
|
|
messages.add_message(
|
|
|
|
request,
|
|
|
|
messages.ERROR,
|
|
|
|
_(u"User charateristics non allowed")
|
|
|
|
)
|
2015-05-17 21:24:41 +00:00
|
|
|
except models.UserFieldNotDefined:
|
2015-05-27 19:56:39 +00:00
|
|
|
messages.add_message(
|
|
|
|
request,
|
|
|
|
messages.ERROR,
|
|
|
|
_(u"The attribut %(field)s is needed to use" \
|
|
|
|
" that service") % {'field':service_pattern.user_field}
|
|
|
|
)
|
2015-05-16 21:43:46 +00:00
|
|
|
|
|
|
|
# if gateway is set and auth failed redirect to the service without authentication
|
|
|
|
if gateway:
|
|
|
|
list(messages.get_messages(request)) # clean messages before leaving the django app
|
|
|
|
return redirect(service)
|
|
|
|
|
2015-05-22 15:55:00 +00:00
|
|
|
return render(request, settings.CAS_LOGGED_TEMPLATE, {'session':request.session})
|
2015-05-16 21:43:46 +00:00
|
|
|
else:
|
|
|
|
if service:
|
2015-05-18 18:30:00 +00:00
|
|
|
try:
|
|
|
|
service_pattern = models.ServicePattern.validate(service)
|
|
|
|
if gateway:
|
2015-05-27 19:56:39 +00:00
|
|
|
list(messages.get_messages(request)) # clean messages before leaving django
|
2015-05-18 18:30:00 +00:00
|
|
|
return redirect(service)
|
|
|
|
if request.session.get("authenticated") and renew:
|
2015-05-27 19:56:39 +00:00
|
|
|
messages.add_message(
|
|
|
|
request,
|
|
|
|
messages.WARNING,
|
|
|
|
_(u"Authentication renewal required by service" \
|
|
|
|
" %(name)s (%(url)s).") % {'name':service_pattern.name, 'url':service}
|
|
|
|
)
|
2015-05-18 18:30:00 +00:00
|
|
|
else:
|
2015-05-27 19:56:39 +00:00
|
|
|
messages.add_message(
|
|
|
|
request,
|
|
|
|
messages.WARNING,
|
|
|
|
_(u"Authentication required by service" \
|
|
|
|
" %(name)s (%(url)s).") % {'name':service_pattern.name, 'url':service}
|
|
|
|
)
|
2015-05-18 18:30:00 +00:00
|
|
|
except models.ServicePattern.DoesNotExist:
|
2015-05-27 19:56:39 +00:00
|
|
|
messages.add_message(
|
|
|
|
request,
|
|
|
|
messages.ERROR,
|
|
|
|
_(u'Service %s non allowed') % service
|
|
|
|
)
|
2015-05-16 21:43:46 +00:00
|
|
|
return render(request, settings.CAS_LOGIN_TEMPLATE, {'form':form})
|
|
|
|
|
|
|
|
def logout(request):
|
2015-05-27 19:56:39 +00:00
|
|
|
"""destroy CAS session (logout)"""
|
2015-05-16 21:43:46 +00:00
|
|
|
service = request.GET.get('service')
|
|
|
|
if request.session.get("authenticated"):
|
|
|
|
user = models.User.objects.get(username=request.session["username"])
|
|
|
|
user.logout(request)
|
|
|
|
user.delete()
|
|
|
|
_logout(request)
|
|
|
|
# if service is set, redirect to service after logout
|
|
|
|
if service:
|
|
|
|
list(messages.get_messages(request)) # clean messages before leaving the django app
|
|
|
|
return redirect(service)
|
|
|
|
# else redirect to login page
|
|
|
|
else:
|
2015-05-22 15:55:00 +00:00
|
|
|
messages.add_message(request, messages.SUCCESS, _(u'Successfully logout'))
|
2015-05-16 21:43:46 +00:00
|
|
|
return redirect("login")
|
|
|
|
|
|
|
|
def validate(request):
|
2015-05-27 19:56:39 +00:00
|
|
|
"""service ticket validation"""
|
2015-05-16 21:43:46 +00:00
|
|
|
service = request.GET.get('service')
|
|
|
|
ticket = request.GET.get('ticket')
|
|
|
|
renew = True if request.GET.get('renew') else False
|
|
|
|
if service and ticket:
|
|
|
|
try:
|
2015-05-27 19:56:39 +00:00
|
|
|
ticket = models.ServiceTicket.objects.get(
|
|
|
|
value=ticket,
|
|
|
|
service=service,
|
|
|
|
validate=False,
|
|
|
|
renew=renew,
|
|
|
|
creation__gt=(timezone.now() - timedelta(seconds=settings.CAS_TICKET_VALIDITY))
|
|
|
|
)
|
2015-05-16 21:43:46 +00:00
|
|
|
ticket.validate = True
|
|
|
|
ticket.save()
|
|
|
|
return HttpResponse("yes\n", content_type="text/plain")
|
|
|
|
except models.ServiceTicket.DoesNotExist:
|
|
|
|
return HttpResponse("no\n", content_type="text/plain")
|
|
|
|
else:
|
|
|
|
return HttpResponse("no\n", content_type="text/plain")
|
|
|
|
|
2015-05-27 19:56:39 +00:00
|
|
|
|
|
|
|
def ps_validate(request, ticket_type=None):
|
|
|
|
"""factorization for serviceValidate and proxyValidate"""
|
|
|
|
if ticket_type is None:
|
|
|
|
ticket_type = ['ST']
|
2015-05-16 21:43:46 +00:00
|
|
|
service = request.GET.get('service')
|
|
|
|
ticket = request.GET.get('ticket')
|
2015-05-27 19:56:39 +00:00
|
|
|
pgt_url = request.GET.get('pgtUrl')
|
2015-05-16 21:43:46 +00:00
|
|
|
renew = True if request.GET.get('renew') else False
|
|
|
|
if service and ticket:
|
2015-05-27 19:56:39 +00:00
|
|
|
for typ in ticket_type:
|
|
|
|
if ticket.startswith(typ):
|
2015-05-16 21:43:46 +00:00
|
|
|
break
|
|
|
|
else:
|
2015-05-27 19:56:39 +00:00
|
|
|
return render(
|
|
|
|
request,
|
|
|
|
"cas_server/serviceValidateError.xml",
|
|
|
|
{'code':'INVALID_TICKET'},
|
|
|
|
content_type="text/xml; charset=utf-8"
|
|
|
|
)
|
2015-05-16 21:43:46 +00:00
|
|
|
try:
|
|
|
|
proxies = []
|
|
|
|
if ticket.startswith("ST"):
|
2015-05-27 19:56:39 +00:00
|
|
|
ticket = models.ServiceTicket.objects.get(
|
|
|
|
value=ticket,
|
|
|
|
service=service,
|
|
|
|
validate=False,
|
|
|
|
renew=renew,
|
|
|
|
creation__gt=(timezone.now() - timedelta(seconds=settings.CAS_TICKET_VALIDITY))
|
|
|
|
)
|
2015-05-16 21:43:46 +00:00
|
|
|
elif ticket.startswith("PT"):
|
2015-05-27 19:56:39 +00:00
|
|
|
ticket = models.ProxyTicket.objects.get(
|
|
|
|
value=ticket,
|
|
|
|
service=service,
|
|
|
|
validate=False,
|
|
|
|
renew=renew,
|
|
|
|
creation__gt=(timezone.now() - timedelta(seconds=settings.CAS_TICKET_VALIDITY))
|
|
|
|
)
|
|
|
|
for prox in ticket.proxies.all():
|
|
|
|
proxies.append(prox.url)
|
2015-05-16 21:43:46 +00:00
|
|
|
ticket.validate = True
|
|
|
|
ticket.save()
|
|
|
|
attributes = []
|
|
|
|
for key, value in ticket.attributs.items():
|
|
|
|
if isinstance(value, list):
|
2015-05-27 19:56:39 +00:00
|
|
|
for elt in value:
|
|
|
|
attributes.append((key, elt))
|
2015-05-16 21:43:46 +00:00
|
|
|
else:
|
|
|
|
attributes.append((key, value))
|
|
|
|
params = {'username':ticket.user.username, 'attributes':attributes, 'proxies':proxies}
|
2015-05-27 19:56:39 +00:00
|
|
|
if ticket.service_pattern.user_field and \
|
|
|
|
ticket.user.attributs.get(ticket.service_pattern.user_field):
|
2015-05-17 21:24:41 +00:00
|
|
|
params['username'] = ticket.user.attributs.get(ticket.service_pattern.user_field)
|
2015-05-27 19:56:39 +00:00
|
|
|
if pgt_url and pgt_url.startswith("https://"):
|
|
|
|
pattern = models.ServicePattern.validate(pgt_url)
|
2015-05-16 21:43:46 +00:00
|
|
|
if pattern.proxy:
|
2015-05-27 19:56:39 +00:00
|
|
|
proxyid = models.gen_pgtiou()
|
|
|
|
pticket = models.ProxyGrantingTicket.objects.create(
|
|
|
|
user=ticket.user,
|
|
|
|
service=pgt_url,
|
|
|
|
service_pattern=pattern
|
|
|
|
)
|
|
|
|
url = utils.update_url(pgt_url, {'pgtIou':proxyid, 'pgtId':pticket.value})
|
2015-05-16 21:43:46 +00:00
|
|
|
try:
|
2015-05-27 19:56:39 +00:00
|
|
|
ret = requests.get(url, verify=settings.CAS_PROXY_CA_CERTIFICATE_PATH)
|
|
|
|
if ret.status_code == 200:
|
2015-05-16 21:43:46 +00:00
|
|
|
params['proxyGrantingTicket'] = proxyid
|
|
|
|
else:
|
|
|
|
pticket.delete()
|
2015-05-27 19:56:39 +00:00
|
|
|
return render(
|
|
|
|
request,
|
|
|
|
"cas_server/serviceValidate.xml",
|
|
|
|
params,
|
|
|
|
content_type="text/xml; charset=utf-8"
|
|
|
|
)
|
2015-05-16 21:43:46 +00:00
|
|
|
except requests.exceptions.SSLError:
|
2015-05-27 19:56:39 +00:00
|
|
|
return render(
|
|
|
|
request,
|
|
|
|
"cas_server/serviceValidateError.xml",
|
|
|
|
{'code':'INVALID_PROXY_CALLBACK'},
|
|
|
|
content_type="text/xml; charset=utf-8"
|
|
|
|
)
|
2015-05-16 21:43:46 +00:00
|
|
|
else:
|
2015-05-27 19:56:39 +00:00
|
|
|
return render(
|
|
|
|
request,
|
|
|
|
"cas_server/serviceValidateError.xml",
|
|
|
|
{'code':'INVALID_PROXY_CALLBACK'},
|
|
|
|
content_type="text/xml; charset=utf-8"
|
|
|
|
)
|
2015-05-16 21:43:46 +00:00
|
|
|
else:
|
2015-05-27 19:56:39 +00:00
|
|
|
return render(
|
|
|
|
request,
|
|
|
|
"cas_server/serviceValidate.xml",
|
|
|
|
params,
|
|
|
|
content_type="text/xml; charset=utf-8"
|
|
|
|
)
|
|
|
|
except (models.ServiceTicket.DoesNotExist, models.ProxyTicket.DoesNotExist):
|
|
|
|
return render(
|
|
|
|
request,
|
|
|
|
"cas_server/serviceValidateError.xml",
|
|
|
|
{'code':'INVALID_TICKET'},
|
|
|
|
content_type="text/xml; charset=utf-8"
|
|
|
|
)
|
|
|
|
except models.ServicePattern.DoesNotExist:
|
|
|
|
return render(
|
|
|
|
request,
|
|
|
|
"cas_server/serviceValidateError.xml",
|
|
|
|
{'code':'INVALID_TICKET'},
|
|
|
|
content_type="text/xml; charset=utf-8"
|
|
|
|
)
|
2015-05-16 21:43:46 +00:00
|
|
|
else:
|
2015-05-27 19:56:39 +00:00
|
|
|
return render(
|
|
|
|
request,
|
|
|
|
"cas_server/serviceValidateError.xml",
|
|
|
|
{'code':'INVALID_REQUEST'},
|
|
|
|
content_type="text/xml; charset=utf-8"
|
|
|
|
)
|
2015-05-16 21:43:46 +00:00
|
|
|
|
2015-05-27 19:56:39 +00:00
|
|
|
def service_validate(request):
|
|
|
|
"""service ticket validation CAS 2.0 (also work for CAS 3.0)"""
|
|
|
|
return ps_validate(request)
|
|
|
|
def proxy_validate(request):
|
|
|
|
"""service/proxy ticket validation CAS 2.0 (also work for CAS 3.0)"""
|
|
|
|
return ps_validate(request, ["ST", "PT"])
|
2015-05-16 21:43:46 +00:00
|
|
|
|
|
|
|
def proxy(request):
|
2015-05-27 19:56:39 +00:00
|
|
|
"""proxy ticket service"""
|
2015-05-16 21:43:46 +00:00
|
|
|
pgt = request.GET.get('pgt')
|
2015-05-27 19:56:39 +00:00
|
|
|
target_service = request.GET.get('targetService')
|
|
|
|
if pgt and target_service:
|
2015-05-16 21:43:46 +00:00
|
|
|
try:
|
2015-05-27 19:56:39 +00:00
|
|
|
# is the target service allowed
|
|
|
|
pattern = models.ServicePattern.validate(target_service)
|
|
|
|
# is the proxy granting ticket valid
|
|
|
|
ticket = models.ProxyGrantingTicket.objects.get(
|
|
|
|
value=pgt,
|
|
|
|
creation__gt=(timezone.now() - timedelta(seconds=settings.CAS_TICKET_VALIDITY))
|
|
|
|
)
|
|
|
|
# is the pgt user allowed on the target service
|
2015-05-17 21:24:41 +00:00
|
|
|
pattern.check_user(ticket.user)
|
2015-05-27 19:56:39 +00:00
|
|
|
pticket = ticket.user.get_ticket(models.ProxyTicket, target_service, pattern, False)
|
2015-05-16 21:43:46 +00:00
|
|
|
pticket.proxies.create(url=ticket.service)
|
2015-05-27 19:56:39 +00:00
|
|
|
return render(
|
|
|
|
request,
|
|
|
|
"cas_server/proxy.xml",
|
|
|
|
{'ticket':pticket.value},
|
|
|
|
content_type="text/xml; charset=utf-8"
|
|
|
|
)
|
|
|
|
except models.ProxyGrantingTicket.DoesNotExist:
|
|
|
|
return render(
|
|
|
|
request,
|
|
|
|
"cas_server/serviceValidateError.xml",
|
|
|
|
{'code':'INVALID_TICKET'},
|
|
|
|
content_type="text/xml; charset=utf-8"
|
|
|
|
)
|
|
|
|
except models.ServicePattern.DoesNotExist:
|
|
|
|
return render(
|
|
|
|
request,
|
|
|
|
"cas_server/serviceValidateError.xml",
|
|
|
|
{'code':'INVALID_TICKET'},
|
|
|
|
content_type="text/xml; charset=utf-8"
|
|
|
|
)
|
|
|
|
except models.BadUsername:
|
|
|
|
return render(
|
|
|
|
request,
|
|
|
|
"cas_server/serviceValidateError.xml",
|
|
|
|
{'code':'INVALID_TICKET'},
|
|
|
|
content_type="text/xml; charset=utf-8"
|
|
|
|
)
|
|
|
|
except models.BadFilter:
|
|
|
|
return render(
|
|
|
|
request,
|
|
|
|
"cas_server/serviceValidateError.xml",
|
|
|
|
{'code':'INVALID_TICKET'},
|
|
|
|
content_type="text/xml; charset=utf-8"
|
|
|
|
)
|
|
|
|
except models.UserFieldNotDefined:
|
|
|
|
return render(
|
|
|
|
request,
|
|
|
|
"cas_server/serviceValidateError.xml",
|
|
|
|
{'code':'INVALID_TICKET'},
|
|
|
|
content_type="text/xml; charset=utf-8"
|
|
|
|
)
|
2015-05-16 21:43:46 +00:00
|
|
|
else:
|
2015-05-27 19:56:39 +00:00
|
|
|
return render(
|
|
|
|
request,
|
|
|
|
"cas_server/serviceValidateError.xml",
|
|
|
|
{'code':'INVALID_REQUEST'},
|
|
|
|
content_type="text/xml; charset=utf-8"
|
|
|
|
)
|
2015-05-16 21:43:46 +00:00
|
|
|
|
2015-05-27 19:56:39 +00:00
|
|
|
def p3_service_validate(request):
|
|
|
|
"""service ticket validation CAS 3.0"""
|
|
|
|
return service_validate(request)
|
2015-05-16 21:43:46 +00:00
|
|
|
|
2015-05-27 19:56:39 +00:00
|
|
|
def p3_proxy_validate(request):
|
|
|
|
"""service/proxy ticket validation CAS 3.0"""
|
|
|
|
return proxy_validate(request)
|
2015-05-22 17:31:50 +00:00
|
|
|
|
|
|
|
@csrf_exempt
|
2015-05-27 19:56:39 +00:00
|
|
|
def saml_validate(request):
|
|
|
|
"""checks the validity of a Service Ticket by a SAML 1.1 request"""
|
2015-05-22 17:31:50 +00:00
|
|
|
if request.method == 'POST':
|
|
|
|
target = request.GET.get('TARGET')
|
|
|
|
root = etree.fromstring(request.body)
|
|
|
|
try:
|
|
|
|
auth_req = root.getchildren()[1].getchildren()[0]
|
2015-05-27 19:56:39 +00:00
|
|
|
issue_instant = auth_req.attrib['IssueInstant']
|
|
|
|
request_id = auth_req.attrib['RequestID']
|
2015-05-22 17:31:50 +00:00
|
|
|
ticket = auth_req.getchildren()[0].text
|
2015-05-27 19:56:39 +00:00
|
|
|
ticket = models.ServiceTicket.objects.get(
|
|
|
|
value=ticket,
|
|
|
|
service=target,
|
|
|
|
validate=False,
|
|
|
|
creation__gt=(timezone.now() - timedelta(seconds=settings.CAS_TICKET_VALIDITY))
|
|
|
|
)
|
2015-05-22 17:31:50 +00:00
|
|
|
ticket.validate = True
|
|
|
|
ticket.save()
|
2015-05-27 19:56:39 +00:00
|
|
|
expire_instant = (ticket.creation + \
|
|
|
|
timedelta(seconds=settings.CAS_TICKET_VALIDITY)).isoformat()
|
2015-05-22 17:31:50 +00:00
|
|
|
attributes = []
|
|
|
|
for key, value in ticket.attributs.items():
|
|
|
|
if isinstance(value, list):
|
2015-05-27 19:56:39 +00:00
|
|
|
for elt in value:
|
|
|
|
attributes.append((key, elt))
|
2015-05-22 17:31:50 +00:00
|
|
|
else:
|
|
|
|
attributes.append((key, value))
|
2015-05-27 19:56:39 +00:00
|
|
|
params = {
|
|
|
|
'IssueInstant':issue_instant,
|
|
|
|
'expireInstant':expire_instant,
|
|
|
|
'Recipient':target,
|
|
|
|
'ResponseID':request_id,
|
|
|
|
'username':ticket.user.username,
|
|
|
|
'attributes':attributes
|
|
|
|
}
|
|
|
|
if ticket.service_pattern.user_field and \
|
|
|
|
ticket.user.attributs.get(ticket.service_pattern.user_field):
|
2015-05-22 17:31:50 +00:00
|
|
|
params['username'] = ticket.user.attributs.get(ticket.service_pattern.user_field)
|
2015-05-27 19:56:39 +00:00
|
|
|
return render(
|
|
|
|
request,
|
|
|
|
"cas_server/samlValidate.xml",
|
|
|
|
params,
|
|
|
|
content_type="text/xml; charset=utf-8"
|
|
|
|
)
|
2015-05-22 17:31:50 +00:00
|
|
|
except IndexError:
|
2015-05-27 19:56:39 +00:00
|
|
|
return render(
|
|
|
|
request,
|
|
|
|
"cas_server/samlValidateError.xml",
|
|
|
|
{'code':'VersionMismatch'},
|
|
|
|
content_type="text/xml; charset=utf-8"
|
|
|
|
)
|
2015-05-22 17:31:50 +00:00
|
|
|
except KeyError:
|
2015-05-27 19:56:39 +00:00
|
|
|
return render(
|
|
|
|
request,
|
|
|
|
"cas_server/samlValidateError.xml",
|
|
|
|
{'code':'VersionMismatch'},
|
|
|
|
content_type="text/xml; charset=utf-8"
|
|
|
|
)
|
2015-05-22 17:31:50 +00:00
|
|
|
except models.ServiceTicket.DoesNotExist:
|
2015-05-27 19:56:39 +00:00
|
|
|
return render(
|
|
|
|
request,
|
|
|
|
"cas_server/samlValidateError.xml",
|
|
|
|
{'code':'AuthnFailed'},
|
|
|
|
content_type="text/xml; charset=utf-8"
|
|
|
|
)
|
2015-05-22 17:39:51 +00:00
|
|
|
else:
|
|
|
|
return redirect("login")
|