root / snf-cyclades-app / synnefo / aai / middleware.py @ 9c0ac5af
History | View | Annotate | Download (5.3 kB)
1 |
# Copyright 2011 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or without
|
4 |
# modification, are permitted provided that the following conditions
|
5 |
# are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above copyright
|
8 |
# notice, this list of conditions and the following disclaimer.
|
9 |
#
|
10 |
# 2. Redistributions in binary form must reproduce the above copyright
|
11 |
# notice, this list of conditions and the following disclaimer in the
|
12 |
# documentation and/or other materials provided with the distribution.
|
13 |
#
|
14 |
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
15 |
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
16 |
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
17 |
# ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
18 |
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
19 |
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
20 |
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
21 |
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
22 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
23 |
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
24 |
# SUCH DAMAGE.
|
25 |
#
|
26 |
# The views and conclusions contained in the software and documentation are
|
27 |
# those of the authors and should not be interpreted as representing official
|
28 |
# policies, either expressed or implied, of GRNET S.A.
|
29 |
|
30 |
from django.conf import settings |
31 |
from django.http import HttpResponse, HttpResponseRedirect |
32 |
from django.utils.cache import patch_vary_headers |
33 |
from synnefo.db.models import SynnefoUser |
34 |
from synnefo.aai.shibboleth import Tokens, register_shibboleth_user |
35 |
import time |
36 |
|
37 |
DONT_CHECK = getattr(settings, "AAI_SKIP_AUTH_URLS", ['/api', '/invitations/login']) |
38 |
|
39 |
class SynnefoAuthMiddleware(object): |
40 |
|
41 |
def process_request(self, request): |
42 |
|
43 |
for path in DONT_CHECK: |
44 |
if request.path.startswith(path):
|
45 |
return
|
46 |
|
47 |
# Special case for testing purposes, delivers the cookie for the
|
48 |
# test user on first access
|
49 |
if settings.BYPASS_AUTHENTICATION and \ |
50 |
request.GET.get('test') is not None: |
51 |
try:
|
52 |
u = SynnefoUser.objects.get( |
53 |
auth_token=settings.BYPASS_AUTHENTICATION_SECRET_TOKEN) |
54 |
except SynnefoUser.DoesNotExist:
|
55 |
raise Exception("No user found with token matching " |
56 |
"BYPASS_AUTHENTICATION_SECRET_TOKEN.")
|
57 |
return self._redirect_shib_auth_user(user = u) |
58 |
|
59 |
token = None
|
60 |
|
61 |
# Try to find token in a cookie
|
62 |
token = request.COOKIES.get('X-Auth-Token', None) |
63 |
|
64 |
# Try to find token in request header
|
65 |
if not token: |
66 |
token = request.META.get('HTTP_X_AUTH_TOKEN', None) |
67 |
|
68 |
if token:
|
69 |
# token was found, retrieve user from backing store
|
70 |
try:
|
71 |
user = SynnefoUser.objects.get(auth_token=token) |
72 |
|
73 |
except SynnefoUser.DoesNotExist:
|
74 |
return HttpResponseRedirect(settings.LOGIN_URL)
|
75 |
# check user's auth token validity
|
76 |
if (time.time() -
|
77 |
time.mktime(user.auth_token_expires.timetuple())) > 0:
|
78 |
# the user's token has expired, prompt to re-login
|
79 |
return HttpResponseRedirect(settings.LOGIN_URL)
|
80 |
|
81 |
request.user = user |
82 |
return
|
83 |
|
84 |
# token was not found but user authenticated by Shibboleth
|
85 |
if Tokens.SHIB_EPPN in request.META and \ |
86 |
Tokens.SHIB_SESSION_ID in request.META:
|
87 |
try:
|
88 |
user = SynnefoUser.objects.get(uniq=request.META[Tokens.SHIB_EPPN]) |
89 |
return self._redirect_shib_auth_user(user) |
90 |
except SynnefoUser.DoesNotExist:
|
91 |
if register_shibboleth_user(request.META):
|
92 |
user = SynnefoUser.objects.get(uniq=request.META[Tokens.SHIB_EPPN]) |
93 |
return self._redirect_shib_auth_user(user) |
94 |
else:
|
95 |
return HttpResponseRedirect(settings.LOGIN_URL)
|
96 |
|
97 |
if settings.TEST and 'TEST-AAI' in request.META: |
98 |
return HttpResponseRedirect(settings.LOGIN_URL)
|
99 |
|
100 |
if request.path.endswith(settings.LOGIN_URL):
|
101 |
# avoid redirect loops
|
102 |
return
|
103 |
else:
|
104 |
# no authentication info found in headers, redirect back
|
105 |
return HttpResponseRedirect(settings.LOGIN_URL)
|
106 |
|
107 |
def process_response(self, request, response): |
108 |
# Tell proxies and other interested parties that the request varies
|
109 |
# based on X-Auth-Token, to avoid caching of results
|
110 |
patch_vary_headers(response, ('X-Auth-Token',))
|
111 |
return response
|
112 |
|
113 |
def _redirect_shib_auth_user(self, user): |
114 |
expire_fmt = user.auth_token_expires.strftime('%a, %d-%b-%Y %H:%M:%S %Z')
|
115 |
|
116 |
response = HttpResponse() |
117 |
response.set_cookie('X-Auth-Token', value=user.auth_token,
|
118 |
expires=expire_fmt, path='/')
|
119 |
response['X-Auth-Token'] = user.auth_token
|
120 |
response['Location'] = settings.APP_INSTALL_URL
|
121 |
response.status_code = 302
|
122 |
return response
|
123 |
|
124 |
|
125 |
def add_url_exception(url): |
126 |
if not url in DONT_CHECK: |
127 |
DONT_CHECK.append(url) |