Statistics
| Branch: | Tag: | Revision:

root / api / middleware.py @ 44193110

History | View | Annotate | Download (1.4 kB)

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

    
6
class SynnefoAuthMiddleware(object):
7

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

    
12
    def process_request(self, request):
13

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

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

    
31
            return HttpResponseRedirect(content= settings.SIBBOLLETH_HOST)
32

    
33
        return HttpResponseRedirect(content='Athentication Required')
34

    
35
#class HttpResponseAuthenticationRequired(HttpResponse):
36
#    status_code = 401