Statistics
| Branch: | Tag: | Revision:

root / api / middleware.py @ dd53338a

History | View | Annotate | Download (1.5 kB)

1
from django.conf import settings
2
from django.http import HttpResponse, HttpResponseRedirect
3
from synnefo.db.models import SynnefoUser
4

    
5
class SynnefoAuthMiddleware(object):
6

    
7
    auth_token = "X-Auth-Token"
8
    auth_user  = "X-Auth-User"
9
    auth_key   = "X-Auth-Key"
10

    
11
    def process_request(self, request):
12

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

    
21
        #An authentication request
22
        if self.auth_user in request.META and 'X-Auth-Key' in request.META \
23
           and '/v1.0' == request.path and 'GET' == request.method:
24
            # This is here merely for compatibility with the Openstack API.
25
            # All normal users should authenticate through Sibbolleth. Admin
26
            # users or other selected users could use this as a bypass
27
            # mechanism
28
            user = SynnefoUser.objects.filter(username = request.META[self.auth_user])
29

    
30
            return HttpResponseRedirect(settings.SIBBOLLETH_HOST)
31

    
32
        return HttpResponseRedirect(settings.SIBBOLLETH_HOST)
33

    
34
    def process_response(self, request, response):
35
        response['Vary'] = self.auth_key
36
        return response
37

    
38
#class HttpResponseAuthenticationRequired(HttpResponse):
39
#    status_code = 401