Changeset 8692


Ignore:
Timestamp:
21/11/12 13:33:59 (7 years ago)
Author:
mnagni
Message:

Corrects many network wrong assumptions and add more log for debug

Location:
mauRepo/dj_security_middleware/trunk/dj_security_middleware/dj_security_middleware
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • mauRepo/dj_security_middleware/trunk/dj_security_middleware/dj_security_middleware/__init__.py

    r8683 r8692  
    1 __version__ = '0.0.4' 
     1__version__ = '0.0.5' 
  • mauRepo/dj_security_middleware/trunk/dj_security_middleware/dj_security_middleware/middleware.py

    r8683 r8692  
    4040from django.http import HttpResponseRedirect 
    4141import socket 
    42  
    4342import logging 
     43import urlparse 
    4444 
    4545# Get an instance of a logger 
    46 logger = logging.getLogger(__name__) 
     46LOGGER = logging.getLogger(__name__) 
    4747 
    4848LOGIN_SERVICE_ERROR = 'No LOGIN_SETTING parameter is defined in the \ 
     
    9494                                             'tokens': tokens, \ 
    9595                                             'user_data': user_data} 
     96            LOGGER.debug("stored in request - userid:%s, user_data:%s" % (userid, user_data)) 
    9697        except (MissingCookieException, DJMiddlewareException):         
    9798            url = '%s?%s' % (settings.DJ_SECURITY_LOGIN_SERVICE, _build_ret_url(request)) 
     99            LOGGER.debug("error in authentication. Redirecting to %s" % (url))             
    98100            return HttpResponseRedirect(url) 
    99101             
    100102 
    101103def _build_ret_url(request): 
    102     hostname = _get_hostname() 
     104    hostname = socket.getfqdn() 
    103105    if request.META['SERVER_PORT'] != 80: 
    104106        hostname = "%s:%s" % (hostname, request.META['SERVER_PORT']) 
     
    114116        ** Return ** a tuple containing (timestamp, userid, tokens, user_data) 
    115117        ** raise ** a DJ_SecurityException if the ticket is not valid 
    116     """ 
     118    """  
    117119    if 'auth_tkt' in request.COOKIES: 
     120        LOGGER.debug("Found auth_tkt: %s in cookies" % (request.COOKIES.get('auth_tkt'))) 
    118121        try: 
     122             
    119123            return auth_tkt.parse_ticket( 
    120124                    settings.DJ_SECURITY_SHAREDSECRET,  
    121                     getattr(request.COOKIES, 'auth_tkt', ''),  
     125                    request.COOKIES.get('auth_tkt', ''),  
    122126                    _get_host_ip()) 
    123127        except BadTicket as ex: 
     
    126130            request.COOKIES.pop('auth_tkt', None)  
    127131    raise MissingCookieException(AUTHENTICATION_COOKIE_MISSING) 
    128  
    129 def _get_hostname(): 
    130     try: 
    131         hostname = socket.gethostname() 
    132         return socket.gethostbyname_ex(hostname)[0] 
    133     except Exception: 
    134         return 'localhost'     
     132    
     133def _calculate_remote_ip(url_path):    
     134    remote_url = urlparse.urlparse(url_path) 
     135    LOGGER.debug("calculating remote_ip for %s" % (str(remote_url))) 
     136    port = 80 
     137    host = None 
     138    if remote_url.netloc: 
     139        host = remote_url.netloc 
     140    elif remote_url.path: 
     141        host = remote_url.path 
     142         
     143    if not host: 
     144        return None 
     145     
     146    if ':' in host: 
     147        host, port = host.split(':') 
     148    addrinfo = socket.getaddrinfo(host, int(port)) 
     149    LOGGER.debug("%s has remote_ip %s" % (url_path, addrinfo[0][-1][0])) 
     150    for remote_url in addrinfo: 
     151        if not remote_url[-1][0].startswith('127'): 
     152            return remote_url[-1][0] 
     153    return None 
    135154     
    136155def _get_host_ip(): 
     
    138157        return settings.DJ_MIDDLEWARE_IP 
    139158         
    140     remote_urls = socket.getaddrinfo(socket.gethostname(), None) 
    141     for remote_url in remote_urls: 
    142         ret = remote_url[4][0] 
    143         if not ret.startswith('127'): 
    144             logger.debug("remote_url: %s" % (ret)) 
    145             return ret 
     159    return _calculate_remote_ip(socket.getfqdn()) 
Note: See TracChangeset for help on using the changeset viewer.