root / snf-astakos-app / astakos / im / target / shibboleth.py @ 34a857c0
History | View | Annotate | Download (5.9 kB)
1 |
# Copyright 2011-2012 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or
|
4 |
# without modification, are permitted provided that the following
|
5 |
# conditions are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above
|
8 |
# copyright notice, this list of conditions and the following
|
9 |
# disclaimer.
|
10 |
#
|
11 |
# 2. Redistributions in binary form must reproduce the above
|
12 |
# copyright notice, this list of conditions and the following
|
13 |
# disclaimer in the documentation and/or other materials
|
14 |
# provided with the distribution.
|
15 |
#
|
16 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 |
# POSSIBILITY OF SUCH DAMAGE.
|
28 |
#
|
29 |
# The views and conclusions contained in the software and
|
30 |
# documentation are those of the authors and should not be
|
31 |
# interpreted as representing official policies, either expressed
|
32 |
# or implied, of GRNET S.A.
|
33 |
|
34 |
import json |
35 |
|
36 |
from django.conf import settings as global_settings |
37 |
from django.http import HttpResponseBadRequest |
38 |
from django.utils.translation import ugettext as _ |
39 |
from django.contrib import messages |
40 |
from django.template import RequestContext |
41 |
from django.views.decorators.http import require_http_methods |
42 |
from django.http import HttpResponseRedirect |
43 |
from django.core.urlresolvers import reverse |
44 |
from django.core.exceptions import ImproperlyConfigured |
45 |
from django.shortcuts import get_object_or_404 |
46 |
|
47 |
from urlparse import urlunsplit, urlsplit |
48 |
|
49 |
from astakos.im.util import prepare_response, get_context, login_url |
50 |
from astakos.im.views import ( |
51 |
requires_anonymous, render_response, requires_auth_provider) |
52 |
from astakos.im.settings import ENABLE_LOCAL_ACCOUNT_MIGRATION, BASEURL |
53 |
from astakos.im.models import AstakosUser, PendingThirdPartyUser |
54 |
from astakos.im.forms import LoginForm |
55 |
from astakos.im.activation_backends import get_backend, SimpleBackend |
56 |
from astakos.im import auth_providers |
57 |
from astakos.im import settings |
58 |
from astakos.im.target import add_pending_auth_provider, get_pending_key, \ |
59 |
handle_third_party_signup, handle_third_party_login, init_third_party_session |
60 |
|
61 |
import astakos.im.messages as astakos_messages |
62 |
import logging |
63 |
|
64 |
logger = logging.getLogger(__name__) |
65 |
|
66 |
|
67 |
class Tokens: |
68 |
# these are mapped by the Shibboleth SP software
|
69 |
SHIB_EPPN = "HTTP_EPPN" # eduPersonPrincipalName |
70 |
SHIB_NAME = "HTTP_SHIB_INETORGPERSON_GIVENNAME"
|
71 |
SHIB_SURNAME = "HTTP_SHIB_PERSON_SURNAME"
|
72 |
SHIB_CN = "HTTP_SHIB_PERSON_COMMONNAME"
|
73 |
SHIB_DISPLAYNAME = "HTTP_SHIB_INETORGPERSON_DISPLAYNAME"
|
74 |
SHIB_EP_AFFILIATION = "HTTP_SHIB_EP_AFFILIATION"
|
75 |
SHIB_SESSION_ID = "HTTP_SHIB_SESSION_ID"
|
76 |
SHIB_MAIL = "HTTP_SHIB_MAIL"
|
77 |
|
78 |
|
79 |
@requires_auth_provider('shibboleth') |
80 |
@require_http_methods(["GET", "POST"]) |
81 |
def login( |
82 |
request, |
83 |
template='im/third_party_check_local.html',
|
84 |
extra_context=None):
|
85 |
|
86 |
init_third_party_session(request) |
87 |
extra_context = extra_context or {}
|
88 |
|
89 |
tokens = request.META |
90 |
third_party_key = get_pending_key(request) |
91 |
|
92 |
shibboleth_headers = {} |
93 |
for token in dir(Tokens): |
94 |
if token == token.upper():
|
95 |
shibboleth_headers[token] = request.META.get(token, 'NOT_SET')
|
96 |
|
97 |
# log shibboleth headers
|
98 |
# TODO: info -> debug
|
99 |
logger.info("shibboleth request: %r" % shibboleth_headers)
|
100 |
|
101 |
try:
|
102 |
eppn = tokens.get(Tokens.SHIB_EPPN) |
103 |
if global_settings.DEBUG and not eppn: |
104 |
eppn = getattr(global_settings, 'SHIBBOLETH_TEST_EPPN', None) |
105 |
realname = getattr(global_settings, 'SHIBBOLETH_TEST_REALNAME', |
106 |
None)
|
107 |
|
108 |
if not eppn: |
109 |
raise KeyError(_(astakos_messages.SHIBBOLETH_MISSING_EPPN) % { |
110 |
'domain': settings.BASEURL,
|
111 |
'contact_email': settings.CONTACT_EMAIL
|
112 |
}) |
113 |
if Tokens.SHIB_DISPLAYNAME in tokens: |
114 |
realname = tokens[Tokens.SHIB_DISPLAYNAME] |
115 |
elif Tokens.SHIB_CN in tokens: |
116 |
realname = tokens[Tokens.SHIB_CN] |
117 |
elif Tokens.SHIB_NAME in tokens and Tokens.SHIB_SURNAME in tokens: |
118 |
realname = tokens[Tokens.SHIB_NAME] + ' ' + tokens[Tokens.SHIB_SURNAME]
|
119 |
else:
|
120 |
if settings.SHIBBOLETH_REQUIRE_NAME_INFO:
|
121 |
raise KeyError(_(astakos_messages.SHIBBOLETH_MISSING_NAME)) |
122 |
else:
|
123 |
realname = ''
|
124 |
|
125 |
except KeyError, e: |
126 |
# invalid shibboleth headers, redirect to login, display message
|
127 |
messages.error(request, e.message) |
128 |
return HttpResponseRedirect(login_url(request))
|
129 |
|
130 |
affiliation = tokens.get(Tokens.SHIB_EP_AFFILIATION, 'Shibboleth')
|
131 |
email = tokens.get(Tokens.SHIB_MAIL, '')
|
132 |
provider_info = {'eppn': eppn, 'email': email, 'name': realname} |
133 |
userid = eppn |
134 |
|
135 |
|
136 |
try:
|
137 |
return handle_third_party_login(request, 'shibboleth', |
138 |
eppn, provider_info, |
139 |
affiliation, third_party_key) |
140 |
except AstakosUser.DoesNotExist, e:
|
141 |
third_party_key = get_pending_key(request) |
142 |
user_info = {'affiliation': affiliation, 'realname': realname} |
143 |
return handle_third_party_signup(request, userid, 'shibboleth', |
144 |
third_party_key, |
145 |
provider_info, |
146 |
user_info, |
147 |
template, |
148 |
extra_context) |
149 |
|