root / helpdesk / middleware.py @ 88443f66
History | View | Annotate | Download (4.5 kB)
1 |
# vim: set fileencoding=utf-8 :
|
---|---|
2 |
# Copyright 2011 GRNET S.A. All rights reserved.
|
3 |
#
|
4 |
# Redistribution and use in source and binary forms, with or without
|
5 |
# modification, are permitted provided that the following conditions
|
6 |
# are met:
|
7 |
#
|
8 |
# 1. Redistributions of source code must retain the above copyright
|
9 |
# notice, this list of conditions and the following disclaimer.
|
10 |
#
|
11 |
# 2. Redistributions in binary form must reproduce the above copyright
|
12 |
# notice, this list of conditions and the following disclaimer in the
|
13 |
# documentation and/or other materials provided with the distribution.
|
14 |
#
|
15 |
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
16 |
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
17 |
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
18 |
# ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
19 |
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
20 |
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
21 |
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
22 |
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
23 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
24 |
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
25 |
# SUCH DAMAGE.
|
26 |
#
|
27 |
# The views and conclusions contained in the software and documentation are
|
28 |
# those of the authors and should not be interpreted as representing official
|
29 |
# policies, either expressed or implied, of GRNET S.A.
|
30 |
|
31 |
from synnefo.aai import middleware |
32 |
from synnefo.db.models import SynnefoUser |
33 |
from django.conf import settings |
34 |
from django.http import HttpResponse |
35 |
import time |
36 |
|
37 |
class HelpdeskMiddleware(object): |
38 |
|
39 |
auth_tmp_token = "X-Auth-Tmp-Token"
|
40 |
install_path = "/helpdesk"
|
41 |
|
42 |
def __init__(self): |
43 |
middleware.add_url_exception(self.install_path)
|
44 |
|
45 |
def process_request(self, request): |
46 |
|
47 |
if not request.path.startswith('/helpdesk'): |
48 |
if not 'X-Auth-Tmp-Token' in request.COOKIES: |
49 |
return
|
50 |
|
51 |
# Check the request's IP address
|
52 |
allowed = settings.HELPDESK_ALLOWED_IPS |
53 |
if not check_ip(request.META['REMOTE_ADDR'], allowed): |
54 |
try:
|
55 |
proxy_ip = request.META['HTTP_X_FORWARDED_FOR']
|
56 |
except Exception: |
57 |
return HttpResponse(status=403, |
58 |
content="IP Address not allowed")
|
59 |
if not check_ip(proxy_ip, allowed): |
60 |
return HttpResponse(status=403, |
61 |
content="IP Address not allowed")
|
62 |
|
63 |
# Helpdesk application request, search for a valid helpdesk user
|
64 |
try:
|
65 |
hd_user_token = request.COOKIES['X-Auth-Token']
|
66 |
if hd_user_token:
|
67 |
try:
|
68 |
hd_user = SynnefoUser.objects.get(auth_token=hd_user_token) |
69 |
except Exception: |
70 |
return HttpResponse(status=401, |
71 |
content="Not a valid helpdesk user")
|
72 |
|
73 |
if not hd_user.type == 'HELPDESK': |
74 |
return HttpResponse(status=401, |
75 |
content="Not a valid helpdesk user")
|
76 |
else:
|
77 |
return HttpResponse(status=401, |
78 |
content="Not a valid helpdesk user")
|
79 |
except KeyError: |
80 |
return
|
81 |
|
82 |
# Helpdesk application request, search for a valid tmp token
|
83 |
if not 'X-Auth-Tmp-Token' in request.COOKIES: |
84 |
return
|
85 |
|
86 |
tmp_token = request.COOKIES['X-Auth-Tmp-Token']
|
87 |
|
88 |
try:
|
89 |
tmp_user = SynnefoUser.objects.get(tmp_auth_token=tmp_token) |
90 |
except Exception: |
91 |
return HttpResponse(status=401, content="Not a valid helpdesk user") |
92 |
|
93 |
if (time.time() -
|
94 |
time.mktime(tmp_user.tmp_auth_token_expires.timetuple())) > 0:
|
95 |
# The impersonated user's token has expired, re-login
|
96 |
return
|
97 |
|
98 |
# Impersonate the request user: Perform requests from the helpdesk
|
99 |
# account on behalf of the impersonated user
|
100 |
request.user = tmp_user |
101 |
request.readonly = True
|
102 |
|
103 |
def check_ip(ip, allowed): |
104 |
for addr in allowed: |
105 |
# Check exact match
|
106 |
if ip == addr:
|
107 |
return True; |
108 |
# Check range match
|
109 |
if addr.endswith('.0'): |
110 |
iprange = ip[0:ip.rfind(".")] |
111 |
if addr.startswith(iprange):
|
112 |
return True |
113 |
else:
|
114 |
continue
|
115 |
|
116 |
return False |