root / api / middleware.py @ 5509b599
History | View | Annotate | Download (2.7 kB)
1 | 22aee0fb | Georgios Gousios | from django.conf import settings |
---|---|---|---|
2 | 22aee0fb | Georgios Gousios | from django.http import HttpResponse, HttpResponseRedirect |
3 | 22aee0fb | Georgios Gousios | from synnefo.db.models import SynnefoUser |
4 | 22aee0fb | Georgios Gousios | from synnefo.aai.shibboleth import Tokens, register_shibboleth_user |
5 | 22aee0fb | Georgios Gousios | import time |
6 | 22aee0fb | Georgios Gousios | import datetime |
7 | 22aee0fb | Georgios Gousios | |
8 | 22aee0fb | Georgios Gousios | class ApiAuthMiddleware(object): |
9 | 22aee0fb | Georgios Gousios | |
10 | 22aee0fb | Georgios Gousios | auth_token = "X-Auth-Token"
|
11 | 22aee0fb | Georgios Gousios | auth_user = "X-Auth-User"
|
12 | 22aee0fb | Georgios Gousios | auth_key = "X-Auth-Key"
|
13 | 22aee0fb | Georgios Gousios | |
14 | 22aee0fb | Georgios Gousios | def process_request(self, request): |
15 | 22aee0fb | Georgios Gousios | if not request.path.startswith('/api/') : |
16 | 22aee0fb | Georgios Gousios | return
|
17 | 22aee0fb | Georgios Gousios | |
18 | 22aee0fb | Georgios Gousios | token = None
|
19 | 22aee0fb | Georgios Gousios | |
20 | 22aee0fb | Georgios Gousios | #Try to find token in a cookie
|
21 | 22aee0fb | Georgios Gousios | try:
|
22 | 22aee0fb | Georgios Gousios | token = request.COOKIES['X-Auth-Token']
|
23 | 22aee0fb | Georgios Gousios | except Exception: |
24 | 22aee0fb | Georgios Gousios | pass
|
25 | 22aee0fb | Georgios Gousios | |
26 | 22aee0fb | Georgios Gousios | #Try to find token in request header
|
27 | 22aee0fb | Georgios Gousios | if not token: |
28 | 22aee0fb | Georgios Gousios | token = request.META.get('HTTP_X_AUTH_TOKEN', None) |
29 | 22aee0fb | Georgios Gousios | |
30 | 22aee0fb | Georgios Gousios | if token:
|
31 | 22aee0fb | Georgios Gousios | user = None
|
32 | 22aee0fb | Georgios Gousios | #Retrieve user from DB or other caching mechanism
|
33 | 22aee0fb | Georgios Gousios | try:
|
34 | 22aee0fb | Georgios Gousios | user = SynnefoUser.objects.get(auth_token=token) |
35 | 22aee0fb | Georgios Gousios | except SynnefoUser.DoesNotExist:
|
36 | 22aee0fb | Georgios Gousios | user = None
|
37 | 22aee0fb | Georgios Gousios | |
38 | 22aee0fb | Georgios Gousios | #Check user's auth token
|
39 | 22aee0fb | Georgios Gousios | if (time.time() -
|
40 | 22aee0fb | Georgios Gousios | time.mktime(user.auth_token_created.timetuple()) - |
41 | 22aee0fb | Georgios Gousios | settings.AUTH_TOKEN_DURATION * 3600) > 0: |
42 | 22aee0fb | Georgios Gousios | #The user's token has expired, re-login
|
43 | 22aee0fb | Georgios Gousios | user = None
|
44 | 22aee0fb | Georgios Gousios | |
45 | 22aee0fb | Georgios Gousios | request.user = user |
46 | 22aee0fb | Georgios Gousios | return
|
47 | 22aee0fb | Georgios Gousios | |
48 | 22aee0fb | Georgios Gousios | #A Rackspace API authentication request
|
49 | 22aee0fb | Georgios Gousios | if self.auth_user in request.META and self.auth_key in request.META and 'GET' == request.method: |
50 | 22aee0fb | Georgios Gousios | # This is here merely for compatibility with the Openstack API.
|
51 | 22aee0fb | Georgios Gousios | # All normal users should authenticate through Sibbolleth. Admin
|
52 | 22aee0fb | Georgios Gousios | # users or other selected users could use this as a bypass
|
53 | 22aee0fb | Georgios Gousios | # mechanism
|
54 | 22aee0fb | Georgios Gousios | user = SynnefoUser.objects\ |
55 | 22aee0fb | Georgios Gousios | .filter(name = request.META[self.auth_user]) \
|
56 | 22aee0fb | Georgios Gousios | .filter(uniq = request.META[self.auth_key])
|
57 | 22aee0fb | Georgios Gousios | |
58 | 22aee0fb | Georgios Gousios | response = HttpResponse() |
59 | 22aee0fb | Georgios Gousios | if user.count() <= 0: |
60 | 22aee0fb | Georgios Gousios | response.status_code = 401
|
61 | 22aee0fb | Georgios Gousios | else:
|
62 | 22aee0fb | Georgios Gousios | response.status_code = 204
|
63 | 22aee0fb | Georgios Gousios | response['X-Auth-Token'] = user[0].auth_token |
64 | 22aee0fb | Georgios Gousios | #TODO: set the following fields when we do have this info
|
65 | 22aee0fb | Georgios Gousios | response['X-Server-Management-Url'] = "" |
66 | 22aee0fb | Georgios Gousios | response['X-Storage-Url'] = "" |
67 | 22aee0fb | Georgios Gousios | response['X-CDN-Management-Url'] = "" |
68 | 22aee0fb | Georgios Gousios | return response
|
69 | 22aee0fb | Georgios Gousios | |
70 | 22aee0fb | Georgios Gousios | request.user = None
|
71 | 22aee0fb | Georgios Gousios | |
72 | 22aee0fb | Georgios Gousios | def process_response(self, request, response): |
73 | 22aee0fb | Georgios Gousios | #Tell proxies and other interested parties that the
|
74 | 22aee0fb | Georgios Gousios | #request varies based on the auth token, to avoid
|
75 | 22aee0fb | Georgios Gousios | #caching of results
|
76 | 22aee0fb | Georgios Gousios | response['Vary'] = self.auth_token |
77 | 22aee0fb | Georgios Gousios | return response
|