django-cas-server/cas_server/views.py

546 lines
21 KiB
Python
Raw Normal View History

2015-05-16 21:43:46 +00:00
# ⁻*- coding: utf-8 -*-
2015-05-27 20:10:06 +00:00
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License version 3 for
# more details.
#
# You should have received a copy of the GNU General Public License version 3
# along with this program; if not, write to the Free Software Foundation, Inc., 51
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# (c) 2015 Valentin Samir
2015-05-27 19:56:39 +00:00
"""views for the app"""
2015-05-27 20:18:01 +00:00
from . import 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-29 14:11:10 +00:00
from django.views.generic import View
2015-05-16 21:43:46 +00:00
import requests
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
2015-05-29 14:11:10 +00:00
class LogoutView(View):
"""destroy CAS session (logout) view"""
request = None
service = None
def clean_session_variables(self):
"""Clean sessions variables"""
try:
del self.request.session["authenticated"]
except KeyError:
pass
try:
del self.request.session["username"]
except KeyError:
pass
try:
del self.request.session["warn"]
except KeyError:
pass
def logout(self):
"""effectively destroy CAS session"""
try:
user = models.User.objects.get(username=self.request.session.get("username"))
user.logout(self.request)
user.delete()
except models.User.DoesNotExist:
pass
finally:
self.clean_session_variables()
def get(self, request, *args, **kwargs):
"""methode called on GET request on this view"""
self.request = request
self.service = request.GET.get('service')
self.logout()
# if service is set, redirect to service after logout
if self.service:
list(messages.get_messages(request)) # clean messages before leaving the django app
return HttpResponseRedirect(self.service)
# else redirect to login page
else:
messages.add_message(request, messages.SUCCESS, _(u'Successfully logout'))
return redirect("cas_server:login")
class LoginView(View, LogoutView):
2015-05-27 19:56:39 +00:00
"""credential requestor / acceptor"""
2015-05-29 14:11:10 +00:00
# pylint: disable=too-many-instance-attributes
# Nine is reasonable in this case.
2015-05-16 21:43:46 +00:00
user = None
form = None
2015-05-29 14:11:10 +00:00
request = None
service = None
renew = None
gateway = None
method = None
2015-05-16 21:43:46 +00:00
renewed = False
warned = False
2015-05-29 14:11:10 +00:00
def post(self, request, *args, **kwargs):
"""methode called on POST request on this view"""
self.request = request
self.service = request.POST.get('service')
self.renew = True if request.POST.get('renew') else False
self.gateway = request.POST.get('gateway')
self.method = request.POST.get('method')
if not request.session.get("authenticated") or self.renew:
self.form = forms.UserCredential(
2015-05-27 19:56:39 +00:00
request.POST,
2015-05-29 14:11:10 +00:00
initial={
'service':self.service,
'method':self.method,
'warn':request.session.get("warn")
}
2015-05-27 19:56:39 +00:00
)
2015-05-29 14:11:10 +00:00
if self.form.is_valid():
self.user = models.User.objects.get(username=self.form.cleaned_data['username'])
2015-05-19 16:33:56 +00:00
request.session.set_expiry(0)
2015-05-29 14:11:10 +00:00
request.session["username"] = self.form.cleaned_data['username']
request.session["warn"] = True if self.form.cleaned_data.get("warn") else False
2015-05-16 21:43:46 +00:00
request.session["authenticated"] = True
2015-05-29 14:11:10 +00:00
self.renewed = True
self.warned = True
2015-05-16 21:43:46 +00:00
else:
2015-05-29 14:11:10 +00:00
self.logout()
return self.common()
def get(self, request, *args, **kwargs):
self.request = request
self.service = request.GET.get('service')
self.renew = True if request.GET.get('renew') else False
self.gateway = request.GET.get('gateway')
self.method = request.GET.get('method')
if not request.session.get("authenticated") or self.renew:
self.form = forms.UserCredential(
initial={
'service':self.service,
'method':self.method,
'warn':request.session.get("warn")
}
2015-05-27 19:56:39 +00:00
)
2015-05-29 14:11:10 +00:00
return self.common()
2015-05-16 21:43:46 +00:00
2015-05-29 14:11:10 +00:00
def service_login(self):
"""Perform login agains a service"""
try:
# is the service allowed
service_pattern = models.ServicePattern.validate(self.service)
# is the current user allowed on this service
service_pattern.check_user(self.user)
# if the user has asked to be warned before any login to a service
if self.request.session.get("warn", True) and not self.warned:
2015-05-27 19:56:39 +00:00
messages.add_message(
2015-05-29 14:11:10 +00:00
self.request,
messages.WARNING,
_(u"Authentication has been required by service %(name)s (%(url)s)") % \
{'name':service_pattern.name, 'url':self.service}
2015-05-27 19:56:39 +00:00
)
2015-05-29 14:11:10 +00:00
return render(
self.request,
settings.CAS_WARN_TEMPLATE,
{'service_ticket_url':self.user.get_service_url(
self.service,
service_pattern,
renew=self.renew
)}
2015-05-27 19:56:39 +00:00
)
2015-05-29 14:11:10 +00:00
else:
# redirect, using method ?
return HttpResponseRedirect(
self.user.get_service_url(self.service, service_pattern, renew=self.renew)
2015-05-27 19:56:39 +00:00
)
2015-05-29 14:11:10 +00:00
except models.ServicePattern.DoesNotExist:
messages.add_message(
self.request,
messages.ERROR,
_(u'Service %(url)s non allowed.') % {'url' : self.service}
)
except models.BadUsername:
messages.add_message(
self.request,
messages.ERROR,
_(u"Username non allowed")
)
except models.BadFilter:
messages.add_message(
self.request,
messages.ERROR,
_(u"User charateristics non allowed")
)
except models.UserFieldNotDefined:
messages.add_message(
self.request,
messages.ERROR,
_(u"The attribut %(field)s is needed to use" \
" that service") % {'field':service_pattern.user_field}
)
# if gateway is set and auth failed redirect to the service without authentication
if self.gateway:
list(messages.get_messages(self.request)) # clean messages before leaving django
return HttpResponseRedirect(self.service)
2015-05-16 21:43:46 +00:00
2015-05-29 14:11:10 +00:00
return render(self.request, settings.CAS_LOGGED_TEMPLATE, {'session':self.request.session})
2015-05-16 21:43:46 +00:00
2015-05-29 14:11:10 +00:00
def authenticated(self):
"""Processing authenticated users"""
try:
self.user = models.User.objects.get(username=self.request.session.get("username"))
except models.User.DoesNotExist:
self.logout()
return utils.redirect_params("cas_server:login", params=dict(self.request.GET))
# if login agains a service is self.requestest
if self.service:
return self.service_login()
else:
return render(
self.request,
settings.CAS_LOGGED_TEMPLATE,
{'session':self.request.session}
)
def not_authenticated(self):
"""Processing non authenticated users"""
if self.service:
2015-05-18 18:30:00 +00:00
try:
2015-05-29 14:11:10 +00:00
service_pattern = models.ServicePattern.validate(self.service)
if self.gateway:
list(messages.get_messages(self.request))# clean messages before leaving django
return HttpResponseRedirect(self.service)
if self.request.session.get("authenticated") and self.renew:
2015-05-27 19:56:39 +00:00
messages.add_message(
2015-05-29 14:11:10 +00:00
self.request,
2015-05-27 19:56:39 +00:00
messages.WARNING,
2015-05-29 14:11:10 +00:00
_(u"Authentication renewal required by service %(name)s (%(url)s).") %
{'name':service_pattern.name, 'url':self.service}
2015-05-27 19:56:39 +00:00
)
2015-05-18 18:30:00 +00:00
else:
2015-05-27 19:56:39 +00:00
messages.add_message(
2015-05-29 14:11:10 +00:00
self.request,
2015-05-27 19:56:39 +00:00
messages.WARNING,
2015-05-29 14:11:10 +00:00
_(u"Authentication required by service %(name)s (%(url)s).") %
{'name':service_pattern.name, 'url':self.service}
2015-05-27 19:56:39 +00:00
)
2015-05-18 18:30:00 +00:00
except models.ServicePattern.DoesNotExist:
2015-05-27 19:56:39 +00:00
messages.add_message(
2015-05-29 14:11:10 +00:00
self.request,
2015-05-27 19:56:39 +00:00
messages.ERROR,
2015-05-29 14:11:10 +00:00
_(u'Service %s non allowed') % self.service
2015-05-27 19:56:39 +00:00
)
2015-05-29 14:11:10 +00:00
return render(self.request, settings.CAS_LOGIN_TEMPLATE, {'form':self.form})
2015-05-16 21:43:46 +00:00
2015-05-29 14:11:10 +00:00
def common(self):
"""Part execute uppon GET and POST request"""
# if authenticated and successfully renewed authentication if needed
if self.request.session.get("authenticated") and (not self.renew or self.renewed):
return self.authenticated()
else:
return self.not_authenticated()
2015-05-16 21:43:46 +00:00
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 _validate_error(request, code, msg=""):
"""render the serviceValidateError.xml template using `code` and `msg`"""
return render(
request,
"cas_server/serviceValidateError.xml",
{'code':code, 'msg':msg},
content_type="text/xml; charset=utf-8"
)
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:
for elt in ticket_type:
if ticket.startswith(elt):
2015-05-16 21:43:46 +00:00
break
else:
return _validate_error(
2015-05-27 19:56:39 +00:00
request,
'INVALID_TICKET',
'tickets should begin with %s' % ' or '.join(ticket_type)
2015-05-27 19:56:39 +00:00
)
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,
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,
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()
if ticket.service != service:
return _validate_error(request, 'INVALID_SERVICE')
2015-05-16 21:43:46 +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-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)
if pattern.proxy_callback:
proxyid = utils.gen_pgtiou()
2015-05-27 19:56:39 +00:00
pticket = models.ProxyGrantingTicket.objects.create(
user=ticket.user,
service=pgt_url,
service_pattern=pattern,
single_log_out=pattern.single_log_out
2015-05-27 19:56:39 +00:00
)
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"
)
except requests.exceptions.SSLError as error:
error = utils.unpack_nested_exception(error)
return _validate_error(request, 'INVALID_PROXY_CALLBACK', str(error))
2015-05-16 21:43:46 +00:00
else:
return _validate_error(
2015-05-27 19:56:39 +00:00
request,
'INVALID_PROXY_CALLBACK',
"callback url not allowed by configuration"
2015-05-27 19:56:39 +00:00
)
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 _validate_error(request, 'INVALID_TICKET', 'ticket not found')
2015-05-27 19:56:39 +00:00
except models.ServicePattern.DoesNotExist:
return _validate_error(
2015-05-27 19:56:39 +00:00
request,
'INVALID_PROXY_CALLBACK',
'callback url not allowed by configuration'
2015-05-27 19:56:39 +00:00
)
2015-05-16 21:43:46 +00:00
else:
return _validate_error(
2015-05-27 19:56:39 +00:00
request,
'INVALID_REQUEST',
"you must specify a service and a ticket"
2015-05-27 19:56:39 +00:00
)
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)
if not pattern.proxy:
return _validate_error(
request,
'UNAUTHORIZED_SERVICE',
'the service do not allow proxy ticket'
)
2015-05-27 19:56:39 +00:00
# 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 _validate_error(request, 'INVALID_TICKET', 'PGT not found')
2015-05-27 19:56:39 +00:00
except models.ServicePattern.DoesNotExist:
return _validate_error(request, 'UNAUTHORIZED_SERVICE')
except (models.BadUsername, models.BadFilter, models.UserFieldNotDefined):
return _validate_error(
2015-05-27 19:56:39 +00:00
request,
'UNAUTHORIZED_USER',
'%s not allowed on %s' % (ticket.user, target_service)
2015-05-27 19:56:39 +00:00
)
2015-05-16 21:43:46 +00:00
else:
return _validate_error(
2015-05-27 19:56:39 +00:00
request,
'INVALID_REQUEST',
"you must specify and pgt and targetService"
2015-05-27 19:56:39 +00:00
)
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
def _saml_validate_error(request, code, msg=""):
"""render the samlValidateError.xml templace using `code` and `msg`"""
return render(
request,
"cas_server/samlValidateError.xml",
{
'code':code,
'msg':msg,
'IssueInstant':timezone.now().isoformat(),
'ResponseID':utils.gen_saml_id()
},
content_type="text/xml; charset=utf-8"
)
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
if ticket.startswith("ST"):
ticket = models.ServiceTicket.objects.get(
value=ticket,
validate=False,
creation__gt=(timezone.now() - timedelta(seconds=settings.CAS_TICKET_VALIDITY))
)
elif ticket.startswith("PT"):
ticket = models.ProxyTicket.objects.get(
value=ticket,
validate=False,
creation__gt=(timezone.now() - timedelta(seconds=settings.CAS_TICKET_VALIDITY))
)
else:
return _saml_validate_error(
request,
'AuthnFailed',
'ticket should begin with PT- or ST-'
)
2015-05-22 17:31:50 +00:00
ticket.validate = True
ticket.save()
if ticket.service != target:
return _saml_validate_error(
request,
'AuthnFailed',
'TARGET do not match ticket service'
)
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':utils.gen_saml_id(),
2015-05-27 19:56:39 +00:00
'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"
)
except (IndexError, KeyError):
return _saml_validate_error(request, 'VersionMismatch')
except (models.ServiceTicket.DoesNotExist, models.ProxyTicket.DoesNotExist):
return _saml_validate_error(request, 'AuthnFailed', 'ticket not found')
else:
return _saml_validate_error(
request,
'VersionMismatch',
'request should be send using POST'
)