Revision 5ce3ce4f snf-astakos-app/astakos/im/target/shibboleth.py

b/snf-astakos-app/astakos/im/target/shibboleth.py
1 1
# Copyright 2011-2012 GRNET S.A. All rights reserved.
2
# 
2
#
3 3
# Redistribution and use in source and binary forms, with or
4 4
# without modification, are permitted provided that the following
5 5
# conditions are met:
6
# 
6
#
7 7
#   1. Redistributions of source code must retain the above
8 8
#      copyright notice, this list of conditions and the following
9 9
#      disclaimer.
10
# 
10
#
11 11
#   2. Redistributions in binary form must reproduce the above
12 12
#      copyright notice, this list of conditions and the following
13 13
#      disclaimer in the documentation and/or other materials
14 14
#      provided with the distribution.
15
# 
15
#
16 16
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 17
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 18
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
......
25 25
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 26
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 27
# POSSIBILITY OF SUCH DAMAGE.
28
# 
28
#
29 29
# The views and conclusions contained in the software and
30 30
# documentation are those of the authors and should not be
31 31
# interpreted as representing official policies, either expressed
......
42 42
from astakos.im.forms import LoginForm
43 43
from astakos.im.activation_backends import get_backend, SimpleBackend
44 44

  
45

  
45 46
class Tokens:
46 47
    # these are mapped by the Shibboleth SP software
47
    SHIB_EPPN = "HTTP_EPPN" # eduPersonPrincipalName
48
    SHIB_EPPN = "HTTP_EPPN"  # eduPersonPrincipalName
48 49
    SHIB_NAME = "HTTP_SHIB_INETORGPERSON_GIVENNAME"
49 50
    SHIB_SURNAME = "HTTP_SHIB_PERSON_SURNAME"
50 51
    SHIB_CN = "HTTP_SHIB_PERSON_COMMONNAME"
......
53 54
    SHIB_SESSION_ID = "HTTP_SHIB_SESSION_ID"
54 55
    SHIB_MAIL = "HTTP_SHIB_MAIL"
55 56

  
57

  
56 58
@requires_anonymous
57 59
def login(request, backend=None, on_login_template='im/login.html',
58
    on_creation_template='im/third_party_registration.html',
59
    extra_context=None
60
):
60
          on_creation_template='im/third_party_registration.html',
61
          extra_context=None
62
          ):
61 63
    tokens = request.META
62
    
64

  
63 65
    try:
64 66
        eppn = tokens[Tokens.SHIB_EPPN]
65 67
    except KeyError:
66 68
        return HttpResponseBadRequest("Missing unique token in request")
67
    
69

  
68 70
    if Tokens.SHIB_DISPLAYNAME in tokens:
69 71
        realname = tokens[Tokens.SHIB_DISPLAYNAME]
70 72
    elif Tokens.SHIB_CN in tokens:
......
73 75
        realname = tokens[Tokens.SHIB_NAME] + ' ' + tokens[Tokens.SHIB_SURNAME]
74 76
    else:
75 77
        return HttpResponseBadRequest("Missing user name in request")
76
    
78

  
77 79
    affiliation = tokens.get(Tokens.SHIB_EP_AFFILIATION, '')
78 80
    email = tokens.get(Tokens.SHIB_MAIL, None)
79
    
81

  
80 82
    try:
81 83
        user = AstakosUser.objects.get(provider='shibboleth',
82
            third_party_identifier=eppn
83
        )
84
                                       third_party_identifier=eppn
85
                                       )
84 86
        if user.is_active:
85 87
            return prepare_response(request,
86 88
                                    user,
......
90 92
            message = _('Inactive account')
91 93
            messages.error(request, message)
92 94
            return render_response(on_login_template,
93
                                   login_form = LoginForm(request=request),
95
                                   login_form=LoginForm(request=request),
94 96
                                   context_instance=RequestContext(request))
95 97
    except AstakosUser.DoesNotExist, e:
96 98
        user = AstakosUser(third_party_identifier=eppn, realname=realname,
......
99 101
        try:
100 102
            if not backend:
101 103
                backend = get_backend(request)
102
            form = backend.get_signup_form(provider='shibboleth', instance=user)
104
            form = backend.get_signup_form(
105
                provider='shibboleth', instance=user)
103 106
        except Exception, e:
104 107
            form = SimpleBackend(request).get_signup_form(
105 108
                provider='shibboleth',
......
107 110
            )
108 111
            messages.error(request, e)
109 112
        return render_response(on_creation_template,
110
            signup_form = form,
111
            provider = 'shibboleth',
112
            context_instance=get_context(
113
                request,
114
                extra_context
115
            )
116
        )
113
                               signup_form=form,
114
                               provider='shibboleth',
115
                               context_instance=get_context(
116
                               request,
117
                               extra_context
118
                               )
119
                               )

Also available in: Unified diff