Statistics
| Branch: | Tag: | Revision:

root / api / middleware.py @ 838c404d

History | View | Annotate | Download (769 Bytes)

1 89f86fd3 Georgios Gousios
from synnefo.api.errors import Unauthorized
2 89f86fd3 Georgios Gousios
from synnefo.db.models import SynnefoUser
3 89f86fd3 Georgios Gousios
4 89f86fd3 Georgios Gousios
class SynnefoAuthMiddleware(object):
5 89f86fd3 Georgios Gousios
6 89f86fd3 Georgios Gousios
    auth_token = "X-Auth-Token"
7 89f86fd3 Georgios Gousios
    auth_user  = "X-Auth-User"
8 89f86fd3 Georgios Gousios
    auth_key   = "X-Auth-Key"
9 89f86fd3 Georgios Gousios
10 89f86fd3 Georgios Gousios
    def process_request(self, request):
11 89f86fd3 Georgios Gousios
        if self.auth_token in request.META:
12 89f86fd3 Georgios Gousios
            #Retrieve user from DB
13 89f86fd3 Georgios Gousios
            user = SynnefoUser.objects.get(request.META.get(self.auth_token))
14 89f86fd3 Georgios Gousios
            if user is None :
15 89f86fd3 Georgios Gousios
                return
16 89f86fd3 Georgios Gousios
            request.user = user
17 89f86fd3 Georgios Gousios
18 89f86fd3 Georgios Gousios
        #An authentication request
19 89f86fd3 Georgios Gousios
        if self.auth_user in request.META and 'X-Auth-Key' in request.META \
20 89f86fd3 Georgios Gousios
           and '/v1.0' == request.path and 'GET' == request.method:
21 89f86fd3 Georgios Gousios
            #Do authenticate or redirect
22 89f86fd3 Georgios Gousios
            return
23 89f86fd3 Georgios Gousios
24 89f86fd3 Georgios Gousios
        raise Unauthorized