root / aai / middleware.py @ d01e6404
History | View | Annotate | Download (3.5 kB)
1 |
from django.conf import settings |
---|---|
2 |
from django.http import HttpResponse, HttpResponseRedirect |
3 |
from synnefo.db.models import SynnefoUser |
4 |
from synnefo.aai.shibboleth import Tokens, register_shibboleth_user |
5 |
import time |
6 |
|
7 |
class SynnefoAuthMiddleware(object): |
8 |
def process_request(self, request): |
9 |
if request.path.startswith('/api/') : |
10 |
return
|
11 |
|
12 |
if request.path.startswith('/invitations/login') : |
13 |
return
|
14 |
|
15 |
# Special case for testing purposes, delivers the cookie for the
|
16 |
# test user on first access
|
17 |
if settings.BYPASS_AUTHENTICATION and \ |
18 |
request.GET.get('test') is not None: |
19 |
u = SynnefoUser.objects.get( |
20 |
auth_token='46e427d657b20defe352804f0eb6f8a2')
|
21 |
return self._redirect_shib_auth_user(user = u) |
22 |
|
23 |
token = None
|
24 |
|
25 |
# Try to find token in a cookie
|
26 |
token = request.COOKIES.get('X-Auth-Token', None) |
27 |
|
28 |
# Try to find token in request header
|
29 |
if not token: |
30 |
token = request.META.get('HTTP_X_AUTH_TOKEN', None) |
31 |
|
32 |
if token:
|
33 |
# token was found, retrieve user from backing store
|
34 |
try:
|
35 |
user = SynnefoUser.objects.get(auth_token=token) |
36 |
except SynnefoUser.DoesNotExist:
|
37 |
return HttpResponseRedirect(settings.APP_INSTALL_URL + settings.LOGIN_PATH)
|
38 |
|
39 |
# check user's auth token validity
|
40 |
if (time.time() -
|
41 |
time.mktime(user.auth_token_expires.timetuple())) > 0:
|
42 |
# the user's token has expired, prompt to re-login
|
43 |
return HttpResponseRedirect(settings.APP_INSTALL_URL + settings.LOGIN_PATH)
|
44 |
|
45 |
request.user = user |
46 |
return
|
47 |
|
48 |
# token was not found but user authenticated by Shibboleth
|
49 |
if Tokens.SHIB_EPPN in request.META and \ |
50 |
Tokens.SHIB_SESSION_ID in request.META:
|
51 |
try:
|
52 |
user = SynnefoUser.objects.get(uniq=request.META[Tokens.SHIB_EPPN]) |
53 |
return self._redirect_shib_auth_user(user) |
54 |
except SynnefoUser.DoesNotExist:
|
55 |
# attempt to create a new user object
|
56 |
if register_shibboleth_user(request.META):
|
57 |
user = SynnefoUser.objects.get(uniq=request.META[Tokens.SHIB_EPPN]) |
58 |
return self._redirect_shib_auth_user(user) |
59 |
else:
|
60 |
return HttpResponseRedirect(settings.APP_INSTALL_URL + settings.LOGIN_PATH)
|
61 |
|
62 |
if settings.TEST and 'TEST-AAI' in request.META: |
63 |
return HttpResponseRedirect(settings.APP_INSTALL_URL + settings.LOGIN_PATH)
|
64 |
|
65 |
if request.path.endswith(settings.LOGIN_PATH):
|
66 |
# avoid redirect loops
|
67 |
return
|
68 |
else:
|
69 |
# no authentication info found in headers, redirect back
|
70 |
return HttpResponseRedirect(settings.APP_INSTALL_URL + settings.LOGIN_PATH)
|
71 |
|
72 |
def process_response(self, request, response): |
73 |
# Tell proxies and other interested parties that the request varies
|
74 |
# based on X-Auth-Token, to avoid caching of results
|
75 |
response['Vary'] = 'X-Auth-Token' |
76 |
return response
|
77 |
|
78 |
def _redirect_shib_auth_user(self, user): |
79 |
expire_fmt = user.auth_token_expires.strftime('%a, %d-%b-%Y %H:%M:%S %Z')
|
80 |
|
81 |
response = HttpResponse() |
82 |
response.set_cookie('X-Auth-Token', value=user.auth_token, expires=expire_fmt, path='/') |
83 |
response['X-Auth-Token'] = user.auth_token
|
84 |
response['Location'] = settings.APP_INSTALL_URL
|
85 |
response.status_code = 302
|
86 |
return response
|