Ignore:
Timestamp:
06/11/12 14:28:09 (8 years ago)
Author:
mnagni
Message:

Improved the middleware hostname/IP retrivial feature

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

Legend:

Unmodified
Added
Removed
  • mauRepo/dj_security_middleware/trunk/dj_security_middleware

    • Property svn:ignore set to
      dj_security_middleware.egg-info

      dist
  • mauRepo/dj_security_middleware/trunk/dj_security_middleware/dj_security_middleware/middleware.py

    r8655 r8669  
    5555Please define it accordingly to the machine/proxy seen by the LOGIN_SERVICE' 
    5656 
    57 APP_IP_ADDRESS = '127.0.0.1' 
    58  
    5957class DJ_Security_Middleware(object): 
    6058    """ 
     
    7775        except MissingCookieException:              
    7876            url = '%s?%s' % (settings.DJ_SECURITY_LOGIN_SERVICE, _build_ret_url(request)) 
    79             return HttpResponseRedirect(url)               
     77            return HttpResponseRedirect(url)         
    8078 
    8179def _build_ret_url(request): 
     80    hostname = _get_hostname() 
     81    if request.META['SERVER_PORT'] != 80: 
     82        hostname = "%s:%s" % (hostname, request.META['SERVER_PORT']) 
    8283    qs = {} 
    83     qs['r'] = escape('http://%s/%s' % (request.META['HTTP_HOST'], '')) 
     84    qs['r'] = escape('http://%s%s' % (hostname, request.path)) 
    8485    return urlencode(qs)            
    8586 
     
    9697            return auth_tkt.parse_ticket( 
    9798                    settings.DJ_SECURITY_SHAREDSECRET,  
    98                     request.COOKIES['auth_tkt'], settings.DJ_MIDDLEWARE_IP) 
     99                    request.COOKIES['auth_tkt'], _get_host_ip()) 
    99100        except BadTicket as ex: 
    100101            raise DJMiddlewareException(ex)     
    101102    raise MissingCookieException(AUTHENTICATION_COOKIE_MISSING) 
    102103 
    103  
     104def _get_hostname(): 
     105    import socket 
     106    ret = 'localhost' 
     107    try: 
     108        hostname = socket.gethostname() 
     109        ret = socket.gethostbyname_ex(hostname)[0] 
     110    except Exception: 
     111        pass     
     112    return ret 
    104113     
     114def _get_host_ip(): 
     115    if settings.DJ_MIDDLEWARE_IP: 
     116        return settings.DJ_MIDDLEWARE_IP 
     117         
     118    import socket 
     119    ret = 'localhost' 
     120    try: 
     121        hostname = socket.gethostname() 
     122        ret = socket.gethostbyname_ex(hostname)[2][0] 
     123    except Exception: 
     124        pass     
     125    return ret     
Note: See TracChangeset for help on using the changeset viewer.