root / snf-astakos-app / astakos / im / target / shibboleth.py @ f432088a
History | View | Annotate | Download (7.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 |
from django.http import HttpResponseBadRequest |
35 |
from django.utils.translation import ugettext as _ |
36 |
from django.contrib import messages |
37 |
from django.template import RequestContext |
38 |
from django.views.decorators.http import require_http_methods |
39 |
from django.http import HttpResponseRedirect |
40 |
from django.core.urlresolvers import reverse |
41 |
from django.core.exceptions import ImproperlyConfigured |
42 |
from django.shortcuts import get_object_or_404 |
43 |
|
44 |
from urlparse import urlunsplit, urlsplit |
45 |
|
46 |
from astakos.im.util import prepare_response, get_context |
47 |
from astakos.im.views import requires_anonymous, render_response, \ |
48 |
requires_auth_provider
|
49 |
from astakos.im.settings import ENABLE_LOCAL_ACCOUNT_MIGRATION, BASEURL |
50 |
from astakos.im.models import AstakosUser, PendingThirdPartyUser |
51 |
from astakos.im.forms import LoginForm |
52 |
from astakos.im.activation_backends import get_backend, SimpleBackend |
53 |
from astakos.im import settings |
54 |
|
55 |
import astakos.im.messages as astakos_messages |
56 |
|
57 |
import logging |
58 |
|
59 |
logger = logging.getLogger(__name__) |
60 |
|
61 |
class Tokens: |
62 |
# these are mapped by the Shibboleth SP software
|
63 |
SHIB_EPPN = "HTTP_EPPN" # eduPersonPrincipalName |
64 |
SHIB_NAME = "HTTP_SHIB_INETORGPERSON_GIVENNAME"
|
65 |
SHIB_SURNAME = "HTTP_SHIB_PERSON_SURNAME"
|
66 |
SHIB_CN = "HTTP_SHIB_PERSON_COMMONNAME"
|
67 |
SHIB_DISPLAYNAME = "HTTP_SHIB_INETORGPERSON_DISPLAYNAME"
|
68 |
SHIB_EP_AFFILIATION = "HTTP_SHIB_EP_AFFILIATION"
|
69 |
SHIB_SESSION_ID = "HTTP_SHIB_SESSION_ID"
|
70 |
SHIB_MAIL = "HTTP_SHIB_MAIL"
|
71 |
|
72 |
@requires_auth_provider('local', login=True) |
73 |
@require_http_methods(["GET", "POST"]) |
74 |
def login( |
75 |
request, |
76 |
template='im/third_party_check_local.html',
|
77 |
extra_context=None
|
78 |
): |
79 |
extra_context = extra_context or {}
|
80 |
|
81 |
tokens = request.META |
82 |
|
83 |
try:
|
84 |
eppn = tokens.get(Tokens.SHIB_EPPN) |
85 |
if not eppn: |
86 |
raise KeyError(_(astakos_messages.SHIBBOLETH_MISSING_EPPN)) |
87 |
if Tokens.SHIB_DISPLAYNAME in tokens: |
88 |
realname = tokens[Tokens.SHIB_DISPLAYNAME] |
89 |
elif Tokens.SHIB_CN in tokens: |
90 |
realname = tokens[Tokens.SHIB_CN] |
91 |
elif Tokens.SHIB_NAME in tokens and Tokens.SHIB_SURNAME in tokens: |
92 |
realname = tokens[Tokens.SHIB_NAME] + ' ' + tokens[Tokens.SHIB_SURNAME]
|
93 |
else:
|
94 |
raise KeyError(_(astakos_messages.SHIBBOLETH_MISSING_NAME)) |
95 |
except KeyError, e: |
96 |
# invalid shibboleth headers, redirect to login, display message
|
97 |
messages.error(request, e.message) |
98 |
return HttpResponseRedirect(reverse('login')) |
99 |
|
100 |
affiliation = tokens.get(Tokens.SHIB_EP_AFFILIATION, '')
|
101 |
email = tokens.get(Tokens.SHIB_MAIL, '')
|
102 |
|
103 |
# an existing user accessed the view
|
104 |
if request.user.is_authenticated():
|
105 |
if request.user.has_auth_provider('shibboleth', identifier=eppn): |
106 |
return HttpResponseRedirect(reverse('edit_profile')) |
107 |
|
108 |
# automatically add eppn provider to user
|
109 |
user = request.user |
110 |
if not request.user.can_add_auth_provider('shibboleth', |
111 |
identifier=eppn): |
112 |
messages.error(request, 'Account already exists.')
|
113 |
return HttpResponseRedirect(reverse('edit_profile')) |
114 |
|
115 |
user.add_auth_provider('shibboleth', identifier=eppn)
|
116 |
return HttpResponseRedirect(reverse('edit_profile')) |
117 |
|
118 |
try:
|
119 |
# astakos user exists ?
|
120 |
user = AstakosUser.objects.get_auth_provider_user( |
121 |
'shibboleth',
|
122 |
identifier=eppn |
123 |
) |
124 |
if user.is_active:
|
125 |
# authenticate user
|
126 |
return prepare_response(request,
|
127 |
user, |
128 |
request.GET.get('next'),
|
129 |
'renew' in request.GET) |
130 |
elif not user.activation_sent: |
131 |
message = _('Your request is pending activation')
|
132 |
#TODO: use astakos_messages
|
133 |
if not settings.MODERATION_ENABLED: |
134 |
url = user.get_resend_activation_url() |
135 |
msg_extra = _('<a href="%s">Resend activation email?</a>') % url
|
136 |
message = message + u' ' + msg_extra
|
137 |
|
138 |
messages.error(request, message) |
139 |
return HttpResponseRedirect(reverse('login')) |
140 |
|
141 |
else:
|
142 |
#TODO: use astakos_messages
|
143 |
message = _(u'Account disabled. Please contact support')
|
144 |
messages.error(request, message) |
145 |
return HttpResponseRedirect(reverse('login')) |
146 |
|
147 |
except AstakosUser.DoesNotExist, e:
|
148 |
#TODO: use astakos_messages
|
149 |
# eppn not stored in astakos models, create pending profile
|
150 |
user, created = PendingThirdPartyUser.objects.get_or_create( |
151 |
third_party_identifier=eppn, |
152 |
provider='shibboleth',
|
153 |
) |
154 |
# update pending user
|
155 |
user.realname = realname |
156 |
user.affiliation = affiliation |
157 |
user.email = email |
158 |
user.generate_token() |
159 |
user.save() |
160 |
|
161 |
extra_context['provider'] = 'shibboleth' |
162 |
extra_context['token'] = user.token
|
163 |
|
164 |
return render_response(
|
165 |
template, |
166 |
context_instance=get_context(request, extra_context) |
167 |
) |
168 |
|
169 |
|
170 |
@requires_auth_provider('local', login=True, create=True) |
171 |
@require_http_methods(["GET"]) |
172 |
@requires_anonymous
|
173 |
def signup( |
174 |
request, |
175 |
token, |
176 |
backend=None,
|
177 |
on_creation_template='im/third_party_registration.html',
|
178 |
extra_context=None):
|
179 |
|
180 |
extra_context = extra_context or {}
|
181 |
if not token: |
182 |
#TODO: use astakos_messages
|
183 |
return HttpResponseBadRequest(_('Missing key parameter.')) |
184 |
|
185 |
pending = get_object_or_404(PendingThirdPartyUser, token=token) |
186 |
d = pending.__dict__ |
187 |
d.pop('_state', None) |
188 |
d.pop('id', None) |
189 |
d.pop('token', None) |
190 |
d.pop('created', None) |
191 |
user = AstakosUser(**d) |
192 |
|
193 |
try:
|
194 |
backend = backend or get_backend(request)
|
195 |
except ImproperlyConfigured, e:
|
196 |
messages.error(request, e) |
197 |
else:
|
198 |
extra_context['form'] = backend.get_signup_form(
|
199 |
provider='shibboleth',
|
200 |
instance=user |
201 |
) |
202 |
|
203 |
extra_context['provider'] = 'shibboleth' |
204 |
extra_context['third_party_token'] = token
|
205 |
return render_response(
|
206 |
on_creation_template, |
207 |
context_instance=get_context(request, extra_context) |
208 |
) |