Revision dbf97ed2

b/api/middleware.py
1
from time import time
1 2
from django.conf import settings
2 3
from django.http import HttpResponse, HttpResponseRedirect
3 4
from synnefo.db.models import SynnefoUser
5
from synnefo.logic.shibboleth import Tokens, register_shibboleth_user
4 6

  
5 7
class SynnefoAuthMiddleware(object):
6 8

  
......
14 16
            #Retrieve user from DB or other caching mechanism
15 17
            user = SynnefoUser.objects.filter(auth_token = request.META[self.auth_token])
16 18
            if user is None :
17
                return HttpResponseRedirect(settings.SIBBOLLETH_HOST)
19
                return HttpResponseRedirect(settings.SHIBBOLETH_HOST)
18 20
            request.user = user
19 21
            return
20 22

  
21
        #An authentication request
23
        #A user authenticated by Shibboleth
24
        if Tokens.SIB_EDU_PERSON_PRINCIPAL_NAME in request.META:
25
            #TODO: We must somehow make sure that we only process
26
            #      SIB headers when coming from a URL whitelist,
27
            #      or a similar for of restriction
28
            if request.get_host() not in settings.SHIBBOLETH_HOST:
29
                return HttpResponseRedirect(settings.SHIBBOLETH_HOST)
30

  
31
            user = SynnefoUser.objects.filter(
32
                uniq = request.META[Tokens.SIB_EDU_PERSON_PRINCIPAL_NAME])
33

  
34
            #No user with this id could be found in the database
35
            if user is None:
36
                #Try to register incoming user
37
                if register_shibboleth_user(request.META):
38
                    #Registration succeded, user allowed to proceed
39
                    return
40
                #Registration failed, redirect to Shibboleth
41
                return HttpResponseRedirect(settings.SHIBBOLETH_HOST)
42

  
43
            #At this point, the user has been identified in our database
44
            #Check user's auth token
45
            if time() - user.auth_token_created > settings.AUTH_TOKEN_DURATION * 3600:
46
                #The user's token has expired, re-login
47
                return HttpResponseRedirect(settings.SHIBBOLETH_HOST)
48

  
49
            #User and authentication token valid, user allowed to proceed
50
            return
51
            
52
        #An API authentication request
22 53
        if self.auth_user in request.META and 'X-Auth-Key' in request.META \
23
           and '/v1.0' == request.path and 'GET' == request.method:
54
           and '/v1.1' == request.path and 'GET' == request.method:
24 55
            # This is here merely for compatibility with the Openstack API.
25 56
            # All normal users should authenticate through Sibbolleth. Admin
26 57
            # users or other selected users could use this as a bypass
27 58
            # mechanism
28 59
            user = SynnefoUser.objects.filter(username = request.META[self.auth_user])
60
            
61
            return HttpResponseRedirect(settings.SHIBBOLETH_HOST)
29 62

  
30
            return HttpResponseRedirect(settings.SIBBOLLETH_HOST)
31

  
32
        return HttpResponseRedirect(settings.SIBBOLLETH_HOST)
63
        #No authentication info found in headers, redirect to Shibboleth
64
        return HttpResponseRedirect(settings.SHIBBOLETH_HOST)
33 65

  
34 66
    def process_response(self, request, response):
67
        #Tell proxies and other interested parties that the
68
        #request varies based on the auth token, to avoid
69
        #caching of results
35 70
        response['Vary'] = self.auth_key
36 71
        return response
37 72

  
b/api/tests_auth.py
28 28
    def test_register_shibboleth_user(self):
29 29
        """ test registration of sibboleth user upon new incoming request
30 30
        """
31
        #TODO: Test request from wrong host
32
        #self.client
33
        #response = self.client.get(self.apibase + '/servers', {},
34
        #                           **{Tokens.SIB_GIVEN_NAME: 'Jimmy',
35
        #                              Tokens.SIB_EDU_PERSON_PRINCIPAL_NAME: 'jh@gmail.com',
36
        #                              Tokens.SIB_DISPLAY_NAME: 'Jimmy Hendrix'})
37

  
38

  
39
        #Test correct request
31 40
        response = self.client.get(self.apibase + '/servers', {},
32 41
                                   **{Tokens.SIB_GIVEN_NAME: 'Jimmy',
33 42
                                      Tokens.SIB_EDU_PERSON_PRINCIPAL_NAME: 'jh@gmail.com',
b/logic/shibboleth.py
39 39
        users.register_student(realname, '' ,unq)
40 40
    else :
41 41
        users.register_professor(realname, '' ,unq)
42

  
43
    return True
b/settings.py.dist
153 153

  
154 154
API_ROOT_URL = 'http://127.0.0.1:8000/api/'
155 155

  
156
SIBBOLLETH_HOST = "http://wayf.grnet.gr/"
156
SHIBBOLETH_HOST = "http://wayf.grnet.gr/"
157

  
158
SHIBBOLETH_WHITELIST = {
159
    'localhost' : '127.0.0.1'
160
}
161

  
162
#Number of hours during which a user token is active
163
AUTH_TOKEN_DURATION = 24

Also available in: Unified diff