source: mauRepo/dj_security_middleware/tags/v_0_0_7/dj_security_middleware/dj_security_middleware/middleware.py @ 8706

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/mauRepo/dj_security_middleware/tags/v_0_0_7/dj_security_middleware/dj_security_middleware/middleware.py@8706
Revision 8706, 7.6 KB checked in by mnagni, 7 years ago (diff)

Encodes base64 the GET's parameters

  • Property svn:mime-type set to text/plain
Line 
1'''
2BSD Licence
3Copyright (c) 2012, Science & Technology Facilities Council (STFC)
4All rights reserved.
5
6Redistribution and use in source and binary forms, with or without modification,
7are permitted provided that the following conditions are met:
8
9    * Redistributions of source code must retain the above copyright notice,
10        this list of conditions and the following disclaimer.
11    * Redistributions in binary form must reproduce the above copyright notice,
12        this list of conditions and the following disclaimer in the documentation
13        and/or other materials provided with the distribution.
14    * Neither the name of the Science & Technology Facilities Council (STFC)
15        nor the names of its contributors may be used to endorse or promote
16        products derived from this software without specific prior written permission.
17
18THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
22BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
23OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29Created on 2 Nov 2012
30
31@author: mnagni
32'''
33from paste.auth import auth_tkt
34from paste.auth.auth_tkt import BadTicket
35from django.conf import settings
36from dj_security_middleware.exception import DJMiddlewareException,\
37    MissingCookieException
38from django.utils.http import urlencode
39from django.http import HttpResponseRedirect
40import socket
41import logging
42import urlparse
43import re
44import base64
45
46# Get an instance of a logger
47LOGGER = logging.getLogger(__name__)
48
49LOGIN_SERVICE_ERROR = 'No LOGIN_SETTING parameter is defined in the \
50application settings.py file. Please define a proper URL to the \
51authenticating service'
52
53DJ_SECURITY_SHAREDSECRET_ERROR = 'No DJ_SECURITY_SHAREDSECRET parameter \
54is defined in the application settings.py file. \
55Please define it accordingly to the used LOGIN_SERVICE'
56
57AUTHENTICATION_COOKIE_MISSING = 'The expected cookie is missing. \
58Redirect to the authentication service'
59
60DJ_MIDDLEWARE_IP_ERROR = 'No DJ_MIDDLEWARE_IP parameter \
61is defined in the application settings.py file. \
62Please define it accordingly to the machine/proxy seen by the LOGIN_SERVICE'
63
64class DJ_Security_Middleware(object):
65    """
66        Validates if the actual user is authenticated agains a
67        given authentication service.
68        Actually the middleware intercepts all the requests submitted
69        to the underlying Django application and verifies if the presence
70        or not of a valid paste cookie in the request.
71    """   
72    def process_request(self, request):
73        url_fiters = getattr(settings, 'DJ_SECURITY_FILTER', None)
74        if url_fiters \
75            and security_url_filter(request.path, url_fiters):
76            return
77       
78        if not getattr(settings, 'DJ_SECURITY_LOGIN_SERVICE', None):
79            raise DJMiddlewareException(LOGIN_SERVICE_ERROR) 
80        if not getattr(settings, 'DJ_SECURITY_SHAREDSECRET', None):
81            raise DJMiddlewareException(DJ_SECURITY_SHAREDSECRET_ERROR)
82       
83        custom_auth = getattr(settings, 'DJ_SECURITY_AUTH_CHECK', None)
84        if custom_auth:
85            try:
86                if custom_auth(request):
87                    return
88            #Cannot specify the Exception type as don't know the
89            # exceptions type raised by custom_auth                 
90            except Exception:
91                pass
92       
93        #if not settings.DJ_MIDDLEWARE_IP:
94        #    raise DJMiddlewareException(DJ_MIDDLEWARE_IP_ERROR)       
95         
96        try:
97            timestamp, userid, tokens, user_data = _is_authenticated(request)
98            request.authenticated_user = {'timestamp': timestamp, \
99                                             'userid': userid, \
100                                             'tokens': tokens, \
101                                             'user_data': user_data}
102            LOGGER.debug("stored in request - userid:%s, user_data:%s" % (userid, user_data))
103        except (MissingCookieException, DJMiddlewareException):       
104            url = '%s?%s' % (settings.DJ_SECURITY_LOGIN_SERVICE, _build_ret_url(request))
105            LOGGER.debug("error in authentication. Redirecting to %s" % (url))           
106            return HttpResponseRedirect(url)
107           
108
109def _build_ret_url(request):
110    hostname = socket.getfqdn()
111    if request.META['SERVER_PORT'] != 80:
112        hostname = "%s:%s" % (hostname, request.META['SERVER_PORT'])
113    qs = {}
114    qs['r'] = base64.b64encode('http://%s%s?%s' % (hostname, request.path, request.GET.urlencode()))
115    return urlencode(qs)           
116
117def _is_authenticated(request):
118    """
119        Verifies the presence and validity of a paste cookie.
120        If the cookie is not present the request is redirected
121        to the url specified in LOGIN_SERVICE
122        ** Return ** a tuple containing (timestamp, userid, tokens, user_data)
123        ** raise ** a DJ_SecurityException if the ticket is not valid
124    """
125    if 'auth_tkt' in request.COOKIES:
126        LOGGER.debug("Found auth_tkt: %s in cookies" % (request.COOKIES.get('auth_tkt')))
127        try:
128           
129            return auth_tkt.parse_ticket(
130                    settings.DJ_SECURITY_SHAREDSECRET,
131                    request.COOKIES.get('auth_tkt', ''),
132                    _get_host_ip())
133        except BadTicket as ex:
134            raise DJMiddlewareException(ex)
135        finally:
136            request.COOKIES.pop('auth_tkt', None)
137    raise MissingCookieException(AUTHENTICATION_COOKIE_MISSING)
138   
139def _calculate_remote_ip(url_path):   
140    remote_url = urlparse.urlparse(url_path)
141    LOGGER.debug("calculating remote_ip for %s" % (str(remote_url)))
142    port = 80
143    host = None
144    if remote_url.netloc:
145        host = remote_url.netloc
146    elif remote_url.path:
147        host = remote_url.path
148       
149    if not host:
150        return None
151   
152    if ':' in host:
153        host, port = host.split(':')
154    addrinfo = socket.getaddrinfo(host, int(port))
155    LOGGER.debug("%s has remote_ip %s" % (url_path, addrinfo[0][-1][0]))
156    for remote_url in addrinfo:
157        if not remote_url[-1][0].startswith('127'):
158            return remote_url[-1][0]
159    return None
160   
161def _get_host_ip():
162    if getattr(settings, 'DJ_MIDDLEWARE_IP', None):
163        return settings.DJ_MIDDLEWARE_IP
164       
165    return _calculate_remote_ip(socket.getfqdn())
166
167def security_url_filter(string, filters):
168    """
169        Checks a given url request against a list of url filters.
170        ** string ** string a url
171        ** filters ** a list of strings
172        ** RETURN ** True if a match is found, False otherwise
173    """
174    try:       
175        result = urlparse.urlparse(string)
176        return _security_filter(result.path, filters)
177    except AttributeError:
178        return False
179
180def _security_filter(string, filters):
181    """
182        Checks a given strings against a list of strings.
183        ** string ** string a url
184        ** filters ** a list of strings
185    """   
186    if not filters or not string or len(string.strip()) == 0:
187        return False
188    if string in filters:
189        return True
190    for ifilter in filters:
191        if re.match(ifilter, string):
192            return True
Note: See TracBrowser for help on using the repository browser.