root / aai / middleware.py @ 8f377cd6
History | View | Annotate | Download (4.1 kB)
1 |
from time import time |
---|---|
2 |
from django.conf import settings |
3 |
from django.http import HttpResponse, HttpResponseRedirect |
4 |
from synnefo.db.models import SynnefoUser |
5 |
from synnefo.aai.shibboleth import Tokens, register_shibboleth_user |
6 |
import time |
7 |
|
8 |
class SynnefoAuthMiddleware(object): |
9 |
|
10 |
auth_token = "X-Auth-Token"
|
11 |
auth_user = "X-Auth-User"
|
12 |
auth_key = "X-Auth-Key"
|
13 |
|
14 |
def process_request(self, request): |
15 |
|
16 |
if self.auth_token in request.META: |
17 |
user = None
|
18 |
#Retrieve user from DB or other caching mechanism
|
19 |
try:
|
20 |
user = SynnefoUser.objects.get(auth_token = request.META[self.auth_token])
|
21 |
except SynnefoUser.DoesNotExist:
|
22 |
return HttpResponseRedirect(settings.SHIBBOLETH_HOST)
|
23 |
|
24 |
#Check user's auth token
|
25 |
if (time.time() -
|
26 |
time.mktime(user.auth_token_created.timetuple()) + |
27 |
settings.AUTH_TOKEN_DURATION * 3600) > 0: |
28 |
#The user's token has expired, re-login
|
29 |
return HttpResponseRedirect(settings.SHIBBOLETH_HOST)
|
30 |
|
31 |
request.user = user |
32 |
return
|
33 |
|
34 |
#A user authenticated by Shibboleth, must include a uniq id
|
35 |
if Tokens.SIB_EDU_PERSON_PRINCIPAL_NAME in request.META: |
36 |
#We must somehow make sure that we only process
|
37 |
#SIB headers when coming from a URL whitelist,
|
38 |
#or a similar form of restriction
|
39 |
if request.get_host() not in settings.SHIBBOLETH_WHITELIST.keys(): |
40 |
return HttpResponseRedirect(settings.SHIBBOLETH_HOST)
|
41 |
|
42 |
user = None
|
43 |
try:
|
44 |
user = SynnefoUser.objects.get( |
45 |
uniq = request.META[Tokens.SIB_EDU_PERSON_PRINCIPAL_NAME]) |
46 |
except SynnefoUser.DoesNotExist:
|
47 |
pass
|
48 |
|
49 |
#No user with this id could be found in the database
|
50 |
if user is None: |
51 |
#Attempt to register the incoming user
|
52 |
if register_shibboleth_user(request.META):
|
53 |
user = SynnefoUser.objects.get( |
54 |
uniq = request.META[Tokens.SIB_EDU_PERSON_PRINCIPAL_NAME]) |
55 |
response = HttpResponse() |
56 |
response[self.auth_token] = user.auth_token
|
57 |
response['Location'] = "/" |
58 |
response.status_code = 302
|
59 |
return response
|
60 |
else:
|
61 |
return HttpResponseRedirect(settings.SHIBBOLETH_HOST)
|
62 |
|
63 |
#User and authentication token valid, user allowed to proceed
|
64 |
return
|
65 |
|
66 |
#An API authentication request
|
67 |
if self.auth_user in request.META and self.auth_key in request.META and 'GET' == request.method: |
68 |
# This is here merely for compatibility with the Openstack API.
|
69 |
# All normal users should authenticate through Sibbolleth. Admin
|
70 |
# users or other selected users could use this as a bypass
|
71 |
# mechanism
|
72 |
user = SynnefoUser.objects\ |
73 |
.filter(name = request.META[self.auth_user]) \
|
74 |
.filter(uniq = request.META[self.auth_key])
|
75 |
|
76 |
response = HttpResponse() |
77 |
if user.count() <= 0: |
78 |
response.status_code = 401
|
79 |
else:
|
80 |
response.status_code = 204
|
81 |
response['X-Auth-Token'] = user[0].auth_token |
82 |
#TODO: set the following fields when we do have this info
|
83 |
response['X-Server-Management-Url'] = "" |
84 |
response['X-Storage-Url'] = "" |
85 |
response['X-CDN-Management-Url'] = "" |
86 |
return response
|
87 |
|
88 |
if settings.TESTING:
|
89 |
if 'TEST-AAI' in request.META: |
90 |
return HttpResponseRedirect(settings.SHIBBOLETH_HOST)
|
91 |
else:
|
92 |
#No authentication info found in headers, redirect to Shibboleth
|
93 |
return HttpResponseRedirect(settings.SHIBBOLETH_HOST)
|
94 |
|
95 |
def process_response(self, request, response): |
96 |
#Tell proxies and other interested parties that the
|
97 |
#request varies based on the auth token, to avoid
|
98 |
#caching of results
|
99 |
response['Vary'] = self.auth_token |
100 |
return response
|