Edgewall Software

Ticket #2310: web.auth.py.diff

File web.auth.py.diff, 4.4 KB (added by anonymous, 3 years ago)

patch for web/auth.py

  • \\rio-fs\p$\Python23\Lib\site-packages\trac\web\auth.py

    old new  
    2020 
    2121from trac.core import * 
    2222from trac.web.api import IAuthenticator, IRequestHandler 
    2323from trac.web.chrome import INavigationContributor 
    2424from trac.util import escape, hex_entropy, TRUE 
    2525 
     26LOWER = ['l', 'lo', 'lower'] 
     27UPPER = ['u', 'up', 'upper'] 
    2628 
    2729class LoginModule(Component): 
    2830    """Implements user authentication based on HTTP authentication provided by 
    2931    the web-server, combined with cookies for communicating the login 
    3032    information across the whole site. 
    3133 
     
    4850        elif req.incookie.has_key('trac_auth'): 
    4951            authname = self._get_name_for_cookie(req, req.incookie['trac_auth']) 
    5052 
    5153        if not authname: 
    5254            return None 
    5355 
    54         ignore_case = self.env.config.get('trac', 'ignore_auth_case') 
    55         ignore_case = ignore_case.strip().lower() in TRUE 
    56         if ignore_case: 
    57             authname = authname.lower() 
    58         return authname 
     56        return self._fix_auth_id_case(req, authname) 
    5957 
    6058    # INavigationContributor methods 
    6159 
    6260    def get_active_navigation_item(self, req): 
    6361        return 'login' 
    6462 
     
    8280        elif req.path_info.startswith('/logout'): 
    8381            self._do_logout(req) 
    8482        self._redirect_back(req) 
    8583 
    8684    # Internal methods 
    8785 
     86    def _fix_auth_id_case(self, req, auth_id): 
     87        fixed_id = auth_id 
     88 
     89        ignore_case = self.env.config.get('trac', 'ignore_auth_case') 
     90        ignore_case = ignore_case.strip().lower() in TRUE 
     91        if ignore_case: 
     92            fixed_id = fixed_id.lower() 
     93 
     94        auth_domain_case = self.env.config.get('trac', 'auth_domain_case') 
     95        lower_auth_domain_case = auth_domain_case.strip().lower() in LOWER 
     96        upper_auth_domain_case = auth_domain_case.strip().lower() in UPPER 
     97        change_domain_case = lower_auth_domain_case or upper_auth_domain_case 
     98 
     99        auth_username_case = self.env.config.get('trac', 'auth_username_case') 
     100        lower_auth_username_case = auth_username_case.strip().lower() in LOWER 
     101        upper_auth_username_case = auth_username_case.strip().lower() in UPPER 
     102        change_username_case = lower_auth_username_case or upper_auth_username_case 
     103 
     104        change_auth_case = change_domain_case or change_username_case 
     105        if change_domain_case: 
     106            expected_pair = fixed_id.split('\\') 
     107            has_domain_in_id = (len(expected_pair) == 2) 
     108            if has_domain_in_id: 
     109                domain_part = expected_pair[0] 
     110                username_part = expected_pair[1] 
     111 
     112                if lower_auth_domain_case: 
     113                    domain_part = domain_part.lower() 
     114                if upper_auth_domain_case: 
     115                    domain_part = domain_part.upper() 
     116 
     117                if lower_auth_username_case: 
     118                    username_part = username_part.lower() 
     119                if upper_auth_username_case: 
     120                    username_part = username_part.upper() 
     121 
     122                fixed_id = '%s\\%s' % (domain_part, username_part) 
     123 
     124        return fixed_id 
     125 
    88126    def _do_login(self, req): 
    89127        """Log the remote user in. 
    90128 
    91129        This function expects to be called when the remote user name is 
    92130        available. The user name is inserted into the `auth_cookie` table and a 
    93131        cookie identifying the user on subsequent requests is sent back to the 
     
    98136        will be converted to lower case before being used. This is to avoid 
    99137        problems on installations authenticating against Windows which is not 
    100138        case sensitive regarding user names and domain names 
    101139        """ 
    102140        assert req.remote_user, 'Authentication information not available.' 
    103141 
    104         remote_user = req.remote_user 
    105         ignore_case = self.env.config.get('trac', 'ignore_auth_case') 
    106         ignore_case = ignore_case.strip().lower() in TRUE 
    107         if ignore_case: 
    108             remote_user = remote_user.lower() 
     142        remote_user = self._fix_auth_id_case(req, req.remote_user) 
    109143 
    110144        assert req.authname in ('anonymous', remote_user), \ 
    111145               'Already logged in as %s.' % req.authname 
    112146 
    113147        cookie = hex_entropy() 
    114148        db = self.env.get_db_cnx()