Skip to content

Commit

Permalink
add support for django 1.6-1.9
Browse files Browse the repository at this point in the history
  • Loading branch information
fang.li committed May 4, 2016
1 parent 29d3556 commit f3f1340
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 19 deletions.
3 changes: 2 additions & 1 deletion AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ Pysaml2
Contributors
------------

- `Gene Wood <http://github.com/gene1wood/>`_
- `Gene Wood <http://github.com/gene1wood/>`_
- `Terry <https://github.com/tpeng>`_
17 changes: 16 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Django SAML2 Authentication Made Easy
=====================================

:Author: Fang Li
:Version: 1.0.4
:Version: 1.1.0

.. image:: https://img.shields.io/pypi/pyversions/django-saml2-auth.svg
:target: https://pypi.python.org/pypi/django-saml2-auth
Expand Down Expand Up @@ -48,6 +48,13 @@ or from source:
# cd django-saml2-auth
# python setup.py install
xmlsec is also required by pysaml2:

.. code-block:: bash
# yum install xmlsec1
// or
# apt-get install xmlsec1
What does this plugin do?
Expand Down Expand Up @@ -205,3 +212,11 @@ How to Contribute

.. _`the repository`: http://github.com/fangli/django-saml2-auth
.. _AUTHORS: https://github.com/fangli/django-saml2-auth/blob/master/AUTHORS.rst


Release Log
===========

1.1.0: Added support for Django 1.6/1.7/1.8/1.9

1.0.4: Fixed English grammar mistakes
46 changes: 30 additions & 16 deletions django_saml2_auth/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
from saml2.config import Config as Saml2Config

from django import get_version
from pkg_resources import parse_version
from django.conf import settings
from django.core.urlresolvers import reverse
from django.contrib.auth.models import (User, Group)
from django.contrib.auth.decorators import login_required
from django.contrib.auth import login, logout
Expand All @@ -22,29 +22,43 @@
from django.template import TemplateDoesNotExist
from django.http import HttpResponseRedirect

if get_version() >= "1.7":
if parse_version(get_version()) >= parse_version('1.7'):
from django.utils.module_loading import import_string
else:
from django.utils.module_loading import import_by_path as import_string


def get_current_domain(r):
return '{scheme}://{host}'.format(
scheme=r.META['wsgi.url_scheme'],
scheme='https' if r.is_secure() else 'http',
host=r.get_host(),
)


def get_reverse(objs):
'''In order to support different django version, I have to do this '''
from django.core.urlresolvers import reverse
if objs.__class__.__name__ not in ['list', 'tuple']:
objs = [objs]

for obj in objs:
try:
return reverse(obj)
except:
pass
raise Exception('We got a URL reverse issue: %s. This is a known issue but please still submit a ticket at https://github.com/fangli/django-saml2-auth/issues/new' % str(objs))


def _get_saml_client(domain):
acs_url = domain + reverse('acs')
acs_url = domain + get_reverse([acs, 'acs', 'django_saml2_auth:acs'])
import tempfile
tmp = tempfile.NamedTemporaryFile()
f = open(tmp.name, 'w')
f.write(urllib2.urlopen(settings.SAML2_AUTH['METADATA_AUTO_CONF_URL']).read())
f.close()
saml_settings = {
'metadata': {
"local": [tmp.name],
'local': [tmp.name],
},
'service': {
'sp': {
Expand Down Expand Up @@ -74,9 +88,9 @@ def _get_saml_client(domain):
@login_required
def welcome(r):
try:
return render(r, 'django_saml2_auth/welcome.html', context={'user': r.user})
return render(r, 'django_saml2_auth/welcome.html', {'user': r.user})
except TemplateDoesNotExist:
return HttpResponseRedirect(reverse('admin:index'))
return HttpResponseRedirect(get_reverse('admin:index'))


def denied(r):
Expand All @@ -99,19 +113,19 @@ def _create_new_user(username, email, firstname, lastname):
def acs(r):
saml_client = _get_saml_client(get_current_domain(r))
resp = r.POST.get('SAMLResponse', None)
next_url = r.session.get('login_next_url', reverse('admin:index'))
next_url = r.session.get('login_next_url', get_reverse('admin:index'))

if not resp:
return HttpResponseRedirect(reverse('denied'))
return HttpResponseRedirect(get_reverse([denied, 'denied', 'django_saml2_auth:denied']))

authn_response = saml_client.parse_authn_request_response(
resp, entity.BINDING_HTTP_POST)
if authn_response is None:
return HttpResponseRedirect(reverse('denied'))
return HttpResponseRedirect(get_reverse([denied, 'denied', 'django_saml2_auth:denied']))

user_identity = authn_response.get_identity()
if user_identity is None:
return HttpResponseRedirect(reverse('denied'))
return HttpResponseRedirect(get_reverse([denied, 'denied', 'django_saml2_auth:denied']))

user_email = user_identity[settings.SAML2_AUTH.get('ATTRIBUTES_MAP', {}).get('email', 'Email')][0]
user_name = user_identity[settings.SAML2_AUTH.get('ATTRIBUTES_MAP', {}).get('username', 'UserName')][0]
Expand All @@ -137,11 +151,11 @@ def acs(r):
target_user.backend = 'django.contrib.auth.backends.ModelBackend'
login(r, target_user)
else:
return HttpResponseRedirect(reverse('denied'))
return HttpResponseRedirect(get_reverse([denied, 'denied', 'django_saml2_auth:denied']))

if is_new_user:
try:
return render(r, 'django_saml2_auth/welcome.html', context={'user': r.user})
return render(r, 'django_saml2_auth/welcome.html', {'user': r.user})
except TemplateDoesNotExist:
return HttpResponseRedirect(next_url)
else:
Expand All @@ -151,13 +165,13 @@ def acs(r):
def signin(r):
import urlparse
from urllib import unquote
next_url = r.GET.get('next', reverse('admin:index'))
next_url = r.GET.get('next', get_reverse('admin:index'))

try:
if "next=" in unquote(next_url):
if 'next=' in unquote(next_url):
next_url = urlparse.parse_qs(urlparse.urlparse(unquote(next_url)).query)['next'][0]
except:
next_url = r.GET.get('next', reverse('admin:index'))
next_url = r.GET.get('next', get_reverse('admin:index'))

r.session['login_next_url'] = next_url

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
setup(
name='django_saml2_auth',

version='1.0.4',
version='1.1.0',

description='Django SAML2 Authentication Made Easy. Easily integrate with SAML2 SSO identity providers like Okta',
long_description=long_description,
Expand Down

0 comments on commit f3f1340

Please sign in to comment.