+++ /dev/null
-"""CAS authentication backend"""
-
-from urllib import urlencode, urlopen
-from urlparse import urljoin
-from django.conf import settings
-from django_cas.models import User
-
-__all__ = ['CASBackend']
-
-def _verify_cas1(ticket, service):
- """Verifies CAS 1.0 authentication ticket.
-
- Returns username on success and None on failure.
- """
-
- params = {'ticket': ticket, 'service': service}
- url = (urljoin(settings.CAS_SERVER_URL, 'validate') + '?' +
- urlencode(params))
- page = urlopen(url)
- try:
- verified = page.readline().strip()
- if verified == 'yes':
- return page.readline().strip()
- else:
- return None
- finally:
- page.close()
-
-
-def _verify_cas2(ticket, service):
- """Verifies CAS 2.0+ XML-based authentication ticket.
-
- Returns username on success and None on failure.
- """
-
- try:
- from lxml import etree as ElementTree
- except ImportError:
- from elementtree import ElementTree
-
- params = {'ticket': ticket, 'service': service}
- url = (urljoin(settings.CAS_SERVER_URL, 'serviceValidate') + '?' +
- urlencode(params))
- page = urlopen(url)
- try:
- response = page.read()
- tree = ElementTree.fromstring(response)
- if tree[0].tag.endswith('authenticationSuccess'):
- return tree[0][0].text
- else:
- return None
- except:
- import traceback
- traceback.print_exc()
- print "****"
- print response
- print "****"
- finally:
- page.close()
-
-
-_PROTOCOLS = {'1': _verify_cas1, '2': _verify_cas2}
-
-if settings.CAS_VERSION not in _PROTOCOLS:
- raise ValueError('Unsupported CAS_VERSION %r' % settings.CAS_VERSION)
-
-_verify = _PROTOCOLS[settings.CAS_VERSION]
-
-
-class CASBackend(object):
- """CAS authentication backend"""
-
- def authenticate(self, ticket, service):
- """Verifies CAS ticket and gets or creates User object"""
-
- username = _verify(ticket, service)
- if not username:
- return None
- try:
- user = User.objects.get(username__iexact = username)
- except User.DoesNotExist:
- # user will have an "unusable" password
- user = User.objects.create_user(username, '')
- user.save()
- return user
-
- def get_user(self, user_id):
- """Retrieve the user's entry in the User model if it exists"""
-
- try:
- return User.objects.get(pk = user_id)
- except User.DoesNotExist:
- return None