root / snf-astakos-app / astakos / im / target / shibboleth.py @ a53ee093
History | View | Annotate | Download (5.6 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.http import HttpResponseBadRequest |
37 |
from django.utils.translation import ugettext as _ |
38 |
from django.contrib import messages |
39 |
from django.template import RequestContext |
40 |
from django.views.decorators.http import require_http_methods |
41 |
from django.http import HttpResponseRedirect |
42 |
from django.core.urlresolvers import reverse |
43 |
from django.core.exceptions import ImproperlyConfigured |
44 |
from django.shortcuts import get_object_or_404 |
45 |
|
46 |
from urlparse import urlunsplit, urlsplit |
47 |
|
48 |
from astakos.im.util import prepare_response, get_context, login_url |
49 |
from astakos.im.views import ( |
50 |
requires_anonymous, render_response, requires_auth_provider) |
51 |
from astakos.im.settings import ENABLE_LOCAL_ACCOUNT_MIGRATION, BASEURL |
52 |
from astakos.im.models import AstakosUser, PendingThirdPartyUser |
53 |
from astakos.im.forms import LoginForm |
54 |
from astakos.im.activation_backends import get_backend, SimpleBackend |
55 |
from astakos.im import auth_providers |
56 |
from astakos.im import settings |
57 |
from astakos.im.target import add_pending_auth_provider, get_pending_key, \ |
58 |
handle_third_party_signup, handle_third_party_login, init_third_party_session |
59 |
|
60 |
import astakos.im.messages as astakos_messages |
61 |
import logging |
62 |
|
63 |
logger = logging.getLogger(__name__) |
64 |
|
65 |
|
66 |
class Tokens: |
67 |
# these are mapped by the Shibboleth SP software
|
68 |
SHIB_EPPN = "HTTP_EPPN" # eduPersonPrincipalName |
69 |
SHIB_NAME = "HTTP_SHIB_INETORGPERSON_GIVENNAME"
|
70 |
SHIB_SURNAME = "HTTP_SHIB_PERSON_SURNAME"
|
71 |
SHIB_CN = "HTTP_SHIB_PERSON_COMMONNAME"
|
72 |
SHIB_DISPLAYNAME = "HTTP_SHIB_INETORGPERSON_DISPLAYNAME"
|
73 |
SHIB_EP_AFFILIATION = "HTTP_SHIB_EP_AFFILIATION"
|
74 |
SHIB_SESSION_ID = "HTTP_SHIB_SESSION_ID"
|
75 |
SHIB_MAIL = "HTTP_SHIB_MAIL"
|
76 |
|
77 |
|
78 |
@requires_auth_provider('shibboleth', login=True) |
79 |
@require_http_methods(["GET", "POST"]) |
80 |
def login( |
81 |
request, |
82 |
template='im/third_party_check_local.html',
|
83 |
extra_context=None):
|
84 |
|
85 |
init_third_party_session(request) |
86 |
extra_context = extra_context or {}
|
87 |
|
88 |
tokens = request.META |
89 |
third_party_key = get_pending_key(request) |
90 |
|
91 |
shibboleth_headers = {} |
92 |
for token in dir(Tokens): |
93 |
if token == token.upper():
|
94 |
shibboleth_headers[token] = tokens.get(token, 'NOT_SET')
|
95 |
# log shibboleth headers
|
96 |
# TODO: info -> debug
|
97 |
logger.info("shibboleth request: %r" % shibboleth_headers)
|
98 |
|
99 |
try:
|
100 |
eppn = tokens.get(Tokens.SHIB_EPPN) |
101 |
if not eppn: |
102 |
raise KeyError(_(astakos_messages.SHIBBOLETH_MISSING_EPPN) % { |
103 |
'domain': settings.BASEURL,
|
104 |
'contact_email': settings.DEFAULT_CONTACT_EMAIL
|
105 |
}) |
106 |
if Tokens.SHIB_DISPLAYNAME in tokens: |
107 |
realname = tokens[Tokens.SHIB_DISPLAYNAME] |
108 |
elif Tokens.SHIB_CN in tokens: |
109 |
realname = tokens[Tokens.SHIB_CN] |
110 |
elif Tokens.SHIB_NAME in tokens and Tokens.SHIB_SURNAME in tokens: |
111 |
realname = tokens[Tokens.SHIB_NAME] + ' ' + tokens[Tokens.SHIB_SURNAME]
|
112 |
else:
|
113 |
if settings.SHIBBOLETH_REQUIRE_NAME_INFO:
|
114 |
raise KeyError(_(astakos_messages.SHIBBOLETH_MISSING_NAME)) |
115 |
else:
|
116 |
realname = ''
|
117 |
|
118 |
except KeyError, e: |
119 |
# invalid shibboleth headers, redirect to login, display message
|
120 |
messages.error(request, e.message) |
121 |
return HttpResponseRedirect(login_url(request))
|
122 |
|
123 |
affiliation = tokens.get(Tokens.SHIB_EP_AFFILIATION, 'Shibboleth')
|
124 |
email = tokens.get(Tokens.SHIB_MAIL, '')
|
125 |
provider_info = {'eppn': eppn, 'email': email, 'name': realname} |
126 |
userid = eppn |
127 |
|
128 |
|
129 |
try:
|
130 |
return handle_third_party_login(request, 'shibboleth', |
131 |
eppn, provider_info, |
132 |
affiliation, third_party_key) |
133 |
except AstakosUser.DoesNotExist, e:
|
134 |
third_party_key = get_pending_key(request) |
135 |
user_info = {'affiliation': affiliation, 'realname': realname} |
136 |
return handle_third_party_signup(request, userid, 'shibboleth', |
137 |
third_party_key, |
138 |
provider_info, |
139 |
user_info, |
140 |
template, |
141 |
extra_context) |
142 |
|