|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding:utf-8 -*- |
| 3 | + |
| 4 | + |
| 5 | +import urllib2 |
| 6 | +from saml2 import ( |
| 7 | + BINDING_HTTP_POST, |
| 8 | + BINDING_HTTP_REDIRECT, |
| 9 | + entity, |
| 10 | +) |
| 11 | +from saml2.client import Saml2Client |
| 12 | +from saml2.config import Config as Saml2Config |
| 13 | + |
| 14 | +from django.conf import settings |
| 15 | +from django.core.urlresolvers import reverse |
| 16 | +from django.contrib.auth.models import (User, Group) |
| 17 | +from django.contrib.auth.decorators import login_required |
| 18 | +from django.contrib.auth import login, logout |
| 19 | +from django.shortcuts import render |
| 20 | +from django.views.decorators.csrf import csrf_exempt |
| 21 | +from django.template import TemplateDoesNotExist |
| 22 | +from django.http import HttpResponseRedirect |
| 23 | + |
| 24 | + |
| 25 | +def get_current_domain(r): |
| 26 | + return '{scheme}://{host}'.format( |
| 27 | + scheme=r.scheme, |
| 28 | + host=r.get_host(), |
| 29 | + ) |
| 30 | + |
| 31 | + |
| 32 | +def _get_saml_client(domain): |
| 33 | + acs_url = domain + reverse('acs') |
| 34 | + import tempfile |
| 35 | + tmp = tempfile.NamedTemporaryFile() |
| 36 | + f = open(tmp.name, 'w') |
| 37 | + f.write(urllib2.urlopen(settings.SAML2_AUTH['METADATA_AUTO_CONF_URL']).read()) |
| 38 | + f.close() |
| 39 | + saml_settings = { |
| 40 | + 'metadata': { |
| 41 | + "local": [tmp.name], |
| 42 | + }, |
| 43 | + 'service': { |
| 44 | + 'sp': { |
| 45 | + 'endpoints': { |
| 46 | + 'assertion_consumer_service': [ |
| 47 | + (acs_url, BINDING_HTTP_REDIRECT), |
| 48 | + (acs_url, BINDING_HTTP_POST) |
| 49 | + ], |
| 50 | + }, |
| 51 | + 'allow_unsolicited': True, |
| 52 | + 'authn_requests_signed': False, |
| 53 | + 'logout_requests_signed': True, |
| 54 | + 'want_assertions_signed': True, |
| 55 | + 'want_response_signed': False, |
| 56 | + }, |
| 57 | + }, |
| 58 | + } |
| 59 | + |
| 60 | + spConfig = Saml2Config() |
| 61 | + spConfig.load(saml_settings) |
| 62 | + spConfig.allow_unknown_attributes = True |
| 63 | + saml_client = Saml2Client(config=spConfig) |
| 64 | + tmp.close() |
| 65 | + return saml_client |
| 66 | + |
| 67 | + |
| 68 | +@login_required |
| 69 | +def welcome(r): |
| 70 | + try: |
| 71 | + return render(r, 'django_saml2_auth/welcome.html', context={'user': r.user}) |
| 72 | + except TemplateDoesNotExist: |
| 73 | + return HttpResponseRedirect(reverse('admin:index')) |
| 74 | + |
| 75 | + |
| 76 | +def denied(r): |
| 77 | + return render(r, 'django_saml2_auth/denied.html') |
| 78 | + |
| 79 | + |
| 80 | +def _create_new_user(username, email, firstname, lastname): |
| 81 | + user = User.objects.create_user(username, email) |
| 82 | + user.first_name = firstname |
| 83 | + user.last_name = lastname |
| 84 | + user.groups = [Group.objects.get(name=x) for x in settings.SAML2_AUTH.get('NEW_USER_PROFILE', {}).get('USER_GROUPS', [])] |
| 85 | + user.is_active = settings.SAML2_AUTH.get('NEW_USER_PROFILE', {}).get('ACTIVE_STATUS', True) |
| 86 | + user.is_staff = settings.SAML2_AUTH.get('NEW_USER_PROFILE', {}).get('STAFF_STATUS', True) |
| 87 | + user.is_superuser = settings.SAML2_AUTH.get('NEW_USER_PROFILE', {}).get('SUPERUSER_STATUS', False) |
| 88 | + user.save() |
| 89 | + return user |
| 90 | + |
| 91 | + |
| 92 | +@csrf_exempt |
| 93 | +def acs(r): |
| 94 | + saml_client = _get_saml_client(get_current_domain(r)) |
| 95 | + resp = r.POST.get('SAMLResponse', None) |
| 96 | + next_url = r.session.get('login_next_url', reverse('admin:index')) |
| 97 | + |
| 98 | + if not resp: |
| 99 | + return HttpResponseRedirect(reverse('denied')) |
| 100 | + |
| 101 | + authn_response = saml_client.parse_authn_request_response( |
| 102 | + resp, entity.BINDING_HTTP_POST) |
| 103 | + if authn_response is None: |
| 104 | + return HttpResponseRedirect(reverse('denied')) |
| 105 | + |
| 106 | + user_identity = authn_response.get_identity() |
| 107 | + if user_identity is None: |
| 108 | + return HttpResponseRedirect(reverse('denied')) |
| 109 | + |
| 110 | + user_email = user_identity[settings.SAML2_AUTH.get('ATTRIBUTES_MAP', {}).get('email', 'Email')][0] |
| 111 | + user_name = user_identity[settings.SAML2_AUTH.get('ATTRIBUTES_MAP', {}).get('username', 'UserName')][0] |
| 112 | + user_first_name = user_identity[settings.SAML2_AUTH.get('ATTRIBUTES_MAP', {}).get('first_name', 'FirstName')][0] |
| 113 | + user_last_name = user_identity[settings.SAML2_AUTH.get('ATTRIBUTES_MAP', {}).get('last_name', 'LastName')][0] |
| 114 | + |
| 115 | + target_user = None |
| 116 | + is_new_user = False |
| 117 | + |
| 118 | + try: |
| 119 | + target_user = User.objects.get(username=user_name) |
| 120 | + except User.DoesNotExist: |
| 121 | + target_user = _create_new_user(user_name, user_email, user_first_name, user_last_name) |
| 122 | + is_new_user = True |
| 123 | + |
| 124 | + r.session.flush() |
| 125 | + |
| 126 | + if target_user.is_active: |
| 127 | + target_user.backend = 'django.contrib.auth.backends.ModelBackend' |
| 128 | + login(r, target_user) |
| 129 | + else: |
| 130 | + return HttpResponseRedirect(reverse('denied')) |
| 131 | + |
| 132 | + if is_new_user: |
| 133 | + try: |
| 134 | + return render(r, 'django_saml2_auth/welcome.html', context={'user': r.user}) |
| 135 | + except TemplateDoesNotExist: |
| 136 | + return HttpResponseRedirect(next_url) |
| 137 | + else: |
| 138 | + return HttpResponseRedirect(next_url) |
| 139 | + |
| 140 | + |
| 141 | +def signin(r): |
| 142 | + import urlparse |
| 143 | + from urllib import unquote |
| 144 | + next_url = r.GET.get('next', reverse('admin:index')) |
| 145 | + |
| 146 | + try: |
| 147 | + if "next=" in unquote(next_url): |
| 148 | + next_url = urlparse.parse_qs(urlparse.urlparse(unquote(next_url)).query)['next'][0] |
| 149 | + except: |
| 150 | + next_url = r.GET.get('next', reverse('admin:index')) |
| 151 | + |
| 152 | + r.session['login_next_url'] = next_url |
| 153 | + |
| 154 | + saml_client = _get_saml_client(get_current_domain(r)) |
| 155 | + _, info = saml_client.prepare_for_authenticate() |
| 156 | + |
| 157 | + redirect_url = None |
| 158 | + |
| 159 | + for key, value in info['headers']: |
| 160 | + if key == 'Location': |
| 161 | + redirect_url = value |
| 162 | + break |
| 163 | + |
| 164 | + return HttpResponseRedirect(redirect_url) |
| 165 | + |
| 166 | + |
| 167 | +def signout(r): |
| 168 | + logout(r) |
| 169 | + return render(r, 'django_saml2_auth/signout.html') |
0 commit comments