1 """CAS authentication backend"""
3 from urllib import urlencode, urlopen
4 from urlparse import urljoin
5 from django.conf import settings
6 from django_cas.models import User
8 __all__ = ['CASBackend']
11 def _verify_cas1(ticket, service):
12 """Verifies CAS 1.0 authentication ticket.
14 Returns (username, None) on success and (None, None) on failure.
17 params = {'ticket': ticket, 'service': service}
18 url = (urljoin(settings.CAS_SERVER_URL, 'validate') + '?' +
22 verified = page.readline().strip()
24 return page.readline().strip(), None
31 def _verify_cas2(ticket, service):
32 """Verifies CAS 2.0+ XML-based authentication ticket.
34 Returns (username, attr_dict) on success and (None, None) on failure.
38 from lxml import etree as ElementTree
40 from elementtree import ElementTree
42 params = {'ticket': ticket, 'service': service}
43 url = (urljoin(settings.CAS_SERVER_URL, 'serviceValidate') + '?' +
47 response = page.read()
48 tree = ElementTree.fromstring(response)
49 if tree[0].tag.endswith('authenticationSuccess'):
51 for tag in tree[0][1:]:
52 attrs[tag.tag] = tag.text
53 return tree[0][0].text, attrs
66 _PROTOCOLS = {'1': _verify_cas1, '2': _verify_cas2}
68 if settings.CAS_VERSION not in _PROTOCOLS:
69 raise ValueError('Unsupported CAS_VERSION %r' % settings.CAS_VERSION)
71 _verify = _PROTOCOLS[settings.CAS_VERSION]
74 class CASBackend(object):
75 """CAS authentication backend"""
77 def authenticate(self, ticket, service):
78 """Verifies CAS ticket and gets or creates User object"""
80 username, attrs = _verify(ticket, service)
85 if hasattr(settings, 'CAS_USER_ATTRS_MAP'):
86 attr_map = settings.CAS_USER_ATTRS_MAP
87 for k, v in attrs.items():
89 user_attrs[attr_map[k]] = v # unicode(v, 'utf-8')
92 user = User.objects.get(username__iexact=username)
95 for k, v in user_attrs.items():
96 if getattr(user, k) != v:
101 except User.DoesNotExist:
102 # user will have an "unusable" password
103 user = User.objects.create_user(username, '')
104 for k, v in user_attrs.items():
106 user.first_name = attrs.get('firstname', '')
107 user.last_name = attrs.get('lastname', '')
111 def get_user(self, user_id):
112 """Retrieve the user's entry in the User model if it exists"""
115 return User.objects.get(pk=user_id)
116 except User.DoesNotExist: