Changeset 8669


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

Improved the middleware hostname/IP retrivial feature

Location:
mauRepo/dj_security_middleware/trunk/dj_security_middleware
Files:
4 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/README

    r8655 r8669  
    1515service to encrypt the 'auth_tkt' coookie (say 'sharedsecret') 
    1616 
    17 4) DJ_MIDDLEWARE_IP to specify the client machine where the middleware is installed  
     174) DJ_MIDDLEWARE_IP (optional) to specify the client machine where the middleware is installed  
    1818(say '123.456.7.8'). The reason for this is that the client machine could be behind  
    1919a proxy and in this case the authentication service uses the remote machine IP,  
  • mauRepo/dj_security_middleware/trunk/dj_security_middleware/dj_security_middleware/__init__.py

    r8655 r8669  
    1 __version__ = '0.0.1' 
     1__version__ = '0.0.2' 
  • 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.