Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CAS 3.0 support #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion django_cas/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,38 @@ def verify_proxy_ticket(ticket, service):
page.close()


_PROTOCOLS = {'1': _verify_cas1, '2': _verify_cas2}
def _verify_cas3(ticket, service):
"""Verifies CAS 3.0+ XML-based authentication ticket and returns extended attributes.
Returns username on success and None on failure.
"""

try:
from xml.etree import ElementTree
except ImportError:
from elementtree import ElementTree

params = {'ticket': ticket, 'service': service}
url = (urljoin(settings.CAS_SERVER_URL, 'proxyValidate') + '?' +
urlencode(params))
page = urlopen(url)
try:
user = None
attributes = {}
response = page.read()
tree = ElementTree.fromstring(response)
if tree[0].tag.endswith('authenticationSuccess'):
for element in tree[0]:
if element.tag.endswith('user'):
user = element.text
elif element.tag.endswith('attributes'):
for attribute in element:
attributes[attribute.tag.split("}").pop()] = attribute.text
return user, attributes
finally:
page.close()


_PROTOCOLS = {'1': _verify_cas1, '2': _verify_cas2, '3': _verify_cas3}

if settings.CAS_VERSION not in _PROTOCOLS:
raise ValueError('Unsupported CAS_VERSION %r' % settings.CAS_VERSION)
Expand Down