Statistics
| Branch: | Tag: | Revision:

root / aai / middleware.py @ 40777cc8

History | View | Annotate | Download (4.1 kB)

1
from time import time
2
from django.conf import settings
3
from django.http import HttpResponse, HttpResponseRedirect
4
from synnefo.db.models import SynnefoUser
5
from synnefo.aai.shibboleth import Tokens, register_shibboleth_user
6
import time
7

    
8
class SynnefoAuthMiddleware(object):
9

    
10
    auth_token = "X-Auth-Token"
11
    auth_user  = "X-Auth-User"
12
    auth_key   = "X-Auth-Key"
13

    
14
    def process_request(self, request):
15
        token = request.META.get('HTTP_X_AUTH_TOKEN', None)
16
        if token:
17
            user = None
18
            #Retrieve user from DB or other caching mechanism
19
            try:
20
                user = SynnefoUser.objects.get(auth_token=token)
21
            except SynnefoUser.DoesNotExist:
22
                return HttpResponseRedirect(settings.APP_INSTALL_URL + settings.LOGIN_PATH)
23

    
24
            #Check user's auth token
25
            if (time.time() -
26
                time.mktime(user.auth_token_created.timetuple()) -
27
                settings.AUTH_TOKEN_DURATION * 3600) > 0:
28
                #The user's token has expired, re-login
29
                return HttpResponseRedirect(settings.APP_INSTALL_URL + settings.LOGIN_PATH)
30

    
31
            request.user = user
32
            return
33

    
34
        #A user authenticated by Shibboleth, must include a uniq id
35
        if Tokens.SIB_EPPN in request.META and Tokens.SIB_SESSION_ID in request.META:
36
            user = None
37
            try:
38
                user = SynnefoUser.objects.get(
39
                    uniq = request.META[Tokens.SIB_EPPN])
40
            except SynnefoUser.DoesNotExist:
41
                pass
42

    
43
            #No user with this id could be found in the database
44
            if user is None:
45
                #Attempt to register the incoming user
46
                if register_shibboleth_user(request.META):
47
                    user = SynnefoUser.objects.get(
48
                        uniq = request.META[Tokens.SIB_EPPN])
49
                    return self._redirect_shib_auth_user(user)
50
                else:
51
                    return HttpResponseRedirect(settings.APP_INSTALL_URL + settings.LOGIN_PATH)
52

    
53
            #User and authentication token valid, user allowed to proceed
54
            return self._redirect_shib_auth_user(user)
55

    
56
        #An API authentication request
57
        if self.auth_user in request.META and self.auth_key in request.META and 'GET' == request.method:
58
            # This is here merely for compatibility with the Openstack API.
59
            # All normal users should authenticate through Sibbolleth. Admin
60
            # users or other selected users could use this as a bypass
61
            # mechanism
62
            user = SynnefoUser.objects\
63
                    .filter(name = request.META[self.auth_user]) \
64
                    .filter(uniq = request.META[self.auth_key])
65

    
66
            response = HttpResponse()
67
            if user.count() <= 0:
68
                response.status_code = 401
69
            else:
70
                response.status_code = 204
71
                response['X-Auth-Token'] = user[0].auth_token
72
                #TODO: set the following fields when we do have this info
73
                response['X-Server-Management-Url'] = ""
74
                response['X-Storage-Url'] = ""
75
                response['X-CDN-Management-Url'] = ""
76
            return response
77

    
78
        if settings.TEST:
79
            if 'TEST-AAI' in request.META:
80
                return HttpResponseRedirect(settings.APP_INSTALL_URL + settings.LOGIN_PATH)
81
        else:
82
            #Avoid redirect loops
83
            if request.path.endswith(settings.LOGIN_PATH): 
84
                return
85
            else :
86
                #No authentication info found in headers, redirect to Shibboleth
87
                return HttpResponseRedirect(settings.APP_INSTALL_URL + settings.LOGIN_PATH)
88

    
89
    def process_response(self, request, response):
90
        #Tell proxies and other interested parties that the
91
        #request varies based on the auth token, to avoid
92
        #caching of results
93
        response['Vary'] = self.auth_token
94
        return response
95

    
96

    
97
    def _redirect_shib_auth_user(self, user):
98
        response = HttpResponse()
99
        response[self.auth_token] = user.auth_token
100
        response['Location'] = settings.APP_INSTALL_URL
101
        response.status_code = 302
102
        return response