Statistics
| Branch: | Tag: | Revision:

root / helpdesk / middleware.py @ 5ac53b64

History | View | Annotate | Download (4.1 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(self.install_path) :
48
            return
49

    
50
        # Check the request's IP address
51
        allowed = settings.HELPDESK_ALLOWED_IPS
52
        if not check_ip(request.META['REMOTE_ADDR'], allowed):
53
            try:
54
                proxy_ip = request.META['HTTP_X_FORWARDED_FOR']
55
            except Exception:
56
                return HttpResponse(status=403,
57
                                    content="IP Address not allowed")
58
            if not check_ip(proxy_ip, allowed):
59
                return HttpResponse(status=403,
60
                                    content="IP Address not allowed")
61

    
62
        # Helpdesk application request, search for a valid helpdesk user
63
        try:
64
            hd_user_token = request.COOKIES['X-Auth-Token']
65
            if hd_user_token:
66
                hd_user = SynnefoUser.objects.get(auth_token=hd_user_token)
67
                if not hd_user.type == 'HELPDESK':
68
                    return HttpResponse(status=401,
69
                                    content="Not a valid helpdesk user")
70
            else:
71
                return HttpResponse(status=401,
72
                                    content="Not a valid helpdesk user")
73
        except KeyError:
74
            return
75

    
76
        # Helpdesk application request, search for a valid tmp token
77
        tmp_token = None
78
        try:
79
            tmp_token = request.COOKIES['X-Auth-Tmp-Token']
80
        except KeyError:
81
            return
82

    
83
        tmp_user = SynnefoUser.objects.get(tmp_auth_token=tmp_token)
84

    
85
        if (time.time() -
86
            time.mktime(tmp_user.tmp_auth_token_expires.timetuple())) > 0:
87
            # The impersonated user's token has expired, re-login
88
            return HttpResponse(status=403, content="Temporary token expired")
89

    
90
        # Impersonate the request user: Perform requests from the helpdesk
91
        # account on behalf of the impersonated user
92
        request.user = tmp_user
93

    
94
def check_ip(ip, allowed):
95
    for addr in allowed:
96
        # Check exact match
97
        if ip == addr:
98
            return True;
99
        # Check range match
100
        if addr.endswith('.0'):
101
            iprange = ip[0:ip.rfind(".")]
102
            if addr.startswith(iprange):
103
                return True
104
        else:
105
            continue
106

    
107
        return False