Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / im / target / shibboleth.py @ 672d445a

History | View | Annotate | Download (4.8 kB)

1 aba1e498 Antony Chazapis
# Copyright 2011-2012 GRNET S.A. All rights reserved.
2 64cd4730 Antony Chazapis
# 
3 64cd4730 Antony Chazapis
# Redistribution and use in source and binary forms, with or
4 64cd4730 Antony Chazapis
# without modification, are permitted provided that the following
5 64cd4730 Antony Chazapis
# conditions are met:
6 64cd4730 Antony Chazapis
# 
7 64cd4730 Antony Chazapis
#   1. Redistributions of source code must retain the above
8 64cd4730 Antony Chazapis
#      copyright notice, this list of conditions and the following
9 64cd4730 Antony Chazapis
#      disclaimer.
10 64cd4730 Antony Chazapis
# 
11 64cd4730 Antony Chazapis
#   2. Redistributions in binary form must reproduce the above
12 64cd4730 Antony Chazapis
#      copyright notice, this list of conditions and the following
13 64cd4730 Antony Chazapis
#      disclaimer in the documentation and/or other materials
14 64cd4730 Antony Chazapis
#      provided with the distribution.
15 64cd4730 Antony Chazapis
# 
16 64cd4730 Antony Chazapis
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 64cd4730 Antony Chazapis
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 64cd4730 Antony Chazapis
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 64cd4730 Antony Chazapis
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 64cd4730 Antony Chazapis
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 64cd4730 Antony Chazapis
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 64cd4730 Antony Chazapis
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 64cd4730 Antony Chazapis
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 64cd4730 Antony Chazapis
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 64cd4730 Antony Chazapis
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 64cd4730 Antony Chazapis
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 64cd4730 Antony Chazapis
# POSSIBILITY OF SUCH DAMAGE.
28 64cd4730 Antony Chazapis
# 
29 64cd4730 Antony Chazapis
# The views and conclusions contained in the software and
30 64cd4730 Antony Chazapis
# documentation are those of the authors and should not be
31 64cd4730 Antony Chazapis
# interpreted as representing official policies, either expressed
32 64cd4730 Antony Chazapis
# or implied, of GRNET S.A.
33 64cd4730 Antony Chazapis
34 64cd4730 Antony Chazapis
from django.http import HttpResponseBadRequest
35 18ffbee1 Sofia Papagiannaki
from django.utils.translation import ugettext as _
36 18ffbee1 Sofia Papagiannaki
from django.contrib import messages
37 18ffbee1 Sofia Papagiannaki
from django.template import RequestContext
38 64cd4730 Antony Chazapis
39 8f5a3a06 Sofia Papagiannaki
from astakos.im.util import get_or_create_user, prepare_response, get_context, get_invitation
40 8f5a3a06 Sofia Papagiannaki
from astakos.im.views import requires_anonymous, render_response
41 8f5a3a06 Sofia Papagiannaki
from astakos.im.settings import DEFAULT_USER_LEVEL
42 4e30244e Sofia Papagiannaki
from astakos.im.models import AstakosUser, Invitation
43 4e30244e Sofia Papagiannaki
from astakos.im.forms import LoginForm
44 4e30244e Sofia Papagiannaki
from astakos.im.activation_backends import get_backend, SimpleBackend
45 64cd4730 Antony Chazapis
46 64cd4730 Antony Chazapis
class Tokens:
47 64cd4730 Antony Chazapis
    # these are mapped by the Shibboleth SP software
48 64cd4730 Antony Chazapis
    SHIB_EPPN = "HTTP_EPPN" # eduPersonPrincipalName
49 64cd4730 Antony Chazapis
    SHIB_NAME = "HTTP_SHIB_INETORGPERSON_GIVENNAME"
50 64cd4730 Antony Chazapis
    SHIB_SURNAME = "HTTP_SHIB_PERSON_SURNAME"
51 64cd4730 Antony Chazapis
    SHIB_CN = "HTTP_SHIB_PERSON_COMMONNAME"
52 64cd4730 Antony Chazapis
    SHIB_DISPLAYNAME = "HTTP_SHIB_INETORGPERSON_DISPLAYNAME"
53 64cd4730 Antony Chazapis
    SHIB_EP_AFFILIATION = "HTTP_SHIB_EP_AFFILIATION"
54 64cd4730 Antony Chazapis
    SHIB_SESSION_ID = "HTTP_SHIB_SESSION_ID"
55 64cd4730 Antony Chazapis
56 63ecdd20 Sofia Papagiannaki
@requires_anonymous
57 18ffbee1 Sofia Papagiannaki
def login(request,  backend=None, on_login_template='im/login.html', on_creation_template='im/signup.html', extra_context={}):
58 4e30244e Sofia Papagiannaki
    tokens = request.META
59 4e30244e Sofia Papagiannaki
    
60 4e30244e Sofia Papagiannaki
    try:
61 4e30244e Sofia Papagiannaki
        eppn = tokens[Tokens.SHIB_EPPN]
62 4e30244e Sofia Papagiannaki
    except KeyError:
63 4e30244e Sofia Papagiannaki
        return HttpResponseBadRequest("Missing unique token in request")
64 4e30244e Sofia Papagiannaki
    
65 4e30244e Sofia Papagiannaki
    if Tokens.SHIB_DISPLAYNAME in tokens:
66 4e30244e Sofia Papagiannaki
        realname = tokens[Tokens.SHIB_DISPLAYNAME]
67 4e30244e Sofia Papagiannaki
    elif Tokens.SHIB_CN in tokens:
68 4e30244e Sofia Papagiannaki
        realname = tokens[Tokens.SHIB_CN]
69 4e30244e Sofia Papagiannaki
    elif Tokens.SHIB_NAME in tokens and Tokens.SHIB_SURNAME in tokens:
70 4e30244e Sofia Papagiannaki
        realname = tokens[Tokens.SHIB_NAME] + ' ' + tokens[Tokens.SHIB_SURNAME]
71 4e30244e Sofia Papagiannaki
    else:
72 4e30244e Sofia Papagiannaki
        return HttpResponseBadRequest("Missing user name in request")
73 4e30244e Sofia Papagiannaki
    
74 4e30244e Sofia Papagiannaki
    affiliation = tokens.get(Tokens.SHIB_EP_AFFILIATION, '')
75 64cd4730 Antony Chazapis
    
76 64cd4730 Antony Chazapis
    try:
77 8f5a3a06 Sofia Papagiannaki
        user = AstakosUser.objects.get(provider='shibboleth', third_party_identifier=eppn)
78 18ffbee1 Sofia Papagiannaki
        if user.is_active:
79 18ffbee1 Sofia Papagiannaki
            return prepare_response(request,
80 18ffbee1 Sofia Papagiannaki
                                    user,
81 18ffbee1 Sofia Papagiannaki
                                    request.GET.get('next'),
82 18ffbee1 Sofia Papagiannaki
                                    'renew' in request.GET)
83 18ffbee1 Sofia Papagiannaki
        else:
84 18ffbee1 Sofia Papagiannaki
            message = _('Inactive account')
85 18ffbee1 Sofia Papagiannaki
            messages.add_message(request, messages.ERROR, message)
86 18ffbee1 Sofia Papagiannaki
            return render_response(on_login_template,
87 672d445a Sofia Papagiannaki
                                   login_form = LoginForm(request=request),
88 18ffbee1 Sofia Papagiannaki
                                   context_instance=RequestContext(request))
89 8f5a3a06 Sofia Papagiannaki
    except AstakosUser.DoesNotExist, e:
90 8f5a3a06 Sofia Papagiannaki
        user = AstakosUser(third_party_identifier=eppn, realname=realname,
91 4e30244e Sofia Papagiannaki
                           affiliation=affiliation, provider='shibboleth')
92 4e30244e Sofia Papagiannaki
        try:
93 4e30244e Sofia Papagiannaki
            if not backend:
94 4e30244e Sofia Papagiannaki
                backend = get_backend(request)
95 4e30244e Sofia Papagiannaki
            form = backend.get_signup_form(provider='shibboleth', instance=user)
96 4e30244e Sofia Papagiannaki
        except (Invitation.DoesNotExist, ValueError), e:
97 4e30244e Sofia Papagiannaki
            form = SimpleBackend(request).get_signup_form(provider='shibboleth', instance=user)
98 4e30244e Sofia Papagiannaki
            messages.add_message(request, messages.ERROR, e)
99 4e30244e Sofia Papagiannaki
        form.data.update({'third_party_identifier':eppn, 'realname':realname,
100 4e30244e Sofia Papagiannaki
                          'affiliation':affiliation})
101 18ffbee1 Sofia Papagiannaki
        return render_response(on_creation_template,
102 4e30244e Sofia Papagiannaki
                               signup_form = form,
103 18ffbee1 Sofia Papagiannaki
                               context_instance=get_context(request, extra_context))