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

Add CAS 3.0 attribute support, and style cleanups #1

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
71 changes: 55 additions & 16 deletions mitx_cas_mapper/__init__.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,70 @@
"""
This is used as an attribute mapper
callable for django-cas per the README at
callable for django-cas per the README at
https://github.com/mitocw/django-cas.
"""
"""

from django.conf import settings

VERSION = "0.0.1"
VERSION = "0.1.0"

CAS_URI = 'http://www.yale.edu/tp/cas'
NSMAP = {'cas': CAS_URI}
CAS = '{%s}' % CAS_URI


def populate_user(user, authentication_response):
if authentication_response.find(CAS + 'authenticationSuccess/' + CAS + 'attributes' , namespaces=NSMAP) is not None:
attr = authentication_response.find(CAS + 'authenticationSuccess/' + CAS + 'attributes' , namespaces=NSMAP)
"""
This is passed a django user object to be modified
and the `authentication_response` from the CAS server.

It allows you to convert attributes from CAS into local
Django user attributes
"""
if settings.CAS_VERSION == '2':
if authentication_response.find(
CAS + 'authenticationSuccess/' + CAS + 'attributes',
namespaces=NSMAP
) is not None:
attr = authentication_response.find(
CAS + 'authenticationSuccess/' + CAS + 'attributes',
namespaces=NSMAP
)

if attr.find(CAS + 'is_staff', NSMAP) is not None:
user.is_staff = attr.find(
CAS + 'is_staff',
NSMAP
).text.upper() == 'TRUE'

if attr.find(CAS + 'is_superuser', NSMAP) is not None:
user.is_superuser = attr.find(
CAS + 'is_superuser',
NSMAP
).text.upper() == 'TRUE'

if attr.find(CAS + 'givenName', NSMAP) is not None:
user.first_name = attr.find(CAS + 'givenName', NSMAP).text

if attr.find(CAS + 'sn', NSMAP) is not None:
user.last_name = attr.find(CAS + 'sn', NSMAP).text

if attr.find(CAS + 'email', NSMAP) is not None:
user.email = attr.find(CAS + 'email', NSMAP).text

if attr.find(CAS + 'is_staff', NSMAP) is not None:
user.is_staff = attr.find(CAS + 'is_staff', NSMAP).text.upper() == 'TRUE'
if settings.CAS_VERSION == '3':
if authentication_response is not None:
if 'is_superuser' in authentication_response:
user.is_superuser = authentication_response['is_superuser']

if attr.find(CAS + 'is_superuser', NSMAP) is not None:
user.is_superuser = attr.find(CAS + 'is_superuser', NSMAP).text.upper() == 'TRUE'
if 'is_staff' in authentication_response:
user.is_staff = authentication_response['is_staff']

if attr.find(CAS + 'givenName', NSMAP) is not None:
user.first_name = attr.find(CAS + 'givenName', NSMAP).text
if 'givenName' in authentication_response:
user.first_name = authentication_response['givenName']

if attr.find(CAS + 'sn', NSMAP) is not None:
user.last_name = attr.find(CAS + 'sn', NSMAP).text
if 'sn' in authentication_response:
user.last_name = authentication_response['sn']

if attr.find(CAS + 'email', NSMAP) is not None:
user.email = attr.find(CAS + 'email', NSMAP).text
pass
if 'email' in authentication_response:
user.email = authentication_response['email']