Revision ef39e7ee api/middleware.py

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

  
......
8 10
    auth_key   = "X-Auth-Key"
9 11

  
10 12
    def process_request(self, request):
13

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

  
18 22
        #An authentication request
19 23
        if self.auth_user in request.META and 'X-Auth-Key' in request.META \
20 24
           and '/v1.0' == request.path and 'GET' == request.method:
21
            #Do authenticate or redirect
22
            return
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 HttpResponseAuthenticationRequired(content='Athentication Required')
23 34

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

Also available in: Unified diff