Edgewall Software

Ticket #791: trac_auth.patch

File trac_auth.patch, 4.8 KB (added by Matthew Good <matt-good.net>, 7 years ago)

partial implementation

  • trac/core.py

     
    291291 
    292292    def end_headers(self): 
    293293        raise RuntimeError, 'Virtual method not implemented' 
     294         
     295    def send_cookie(self, cookie): 
     296        cookie = cookie.output(header='') 
     297        if len(cookie): 
     298            self.send_header('Set-Cookie', cookie) 
     299             
     300    def send_cache_headers(self): 
     301        self.send_header('Pragma', 'no-cache') 
     302        self.send_header('Cache-control', 'no-cache') 
     303        self.send_header('Expires', 'Fri, 01 Jan 1999 00:00:00 GMT') 
     304             
     305    def reauthorize(self, url): 
     306        self.send_response(401) 
     307        self.send_header('WWW-Authenticate', 'Basic realm="Trac"') 
     308        self.send_cookie(self.outcookie) 
     309        self.end_headers() 
     310        self.write('Reauthorizing...') 
    294311 
    295312    def redirect(self, url): 
    296313        self.send_response(302) 
    297314        self.send_header('Location', url) 
    298315        self.send_header('Content-Type', 'text/plain') 
    299         self.send_header('Pragma', 'no-cache') 
    300         self.send_header('Cache-control', 'no-cache') 
    301         self.send_header('Expires', 'Fri, 01 Jan 1999 00:00:00 GMT') 
    302         cookie = self.outcookie.output(header='') 
    303         if len(cookie): 
    304             self.send_header('Set-Cookie', cookie) 
     316        self.send_cache_headers() 
     317        self.send_cookie(self.outcookie) 
    305318        self.end_headers() 
    306319        self.write('Redirecting...') 
    307320        raise RedirectException() 
     
    319332            cs.parseFile(filename) 
    320333        data = cs.render() 
    321334        self.send_response(response) 
    322         self.send_header('Cache-control', 'no-cache') 
    323         self.send_header('Expires', 'Fri, 01 Jan 1999 00:00:00 GMT') 
     335        self.send_cache_headers() 
    324336        self.send_header('Content-Type', content_type + ';charset=utf-8') 
    325337        self.send_header('Content-Length', len(data)) 
    326         cookie = self.outcookie.output(header='') 
    327         if len(cookie): 
    328             self.send_header('Set-Cookie', cookie) 
     338        self.send_cookie(self.outcookie) 
    329339        self.end_headers() 
    330340        if self.command != 'HEAD': 
    331341            self.write(data) 
     
    399409    Wiki.populate_page_dict(database, env) 
    400410 
    401411    authenticator = auth.Authenticator(database, req) 
     412    logged_out = False 
    402413    if path_info == '/logout': 
    403         authenticator.logout() 
     414        authenticator.logout(req) 
    404415        referer = req.get_header('Referer') 
    405416        if referer[0:len(req.base_url)] != req.base_url: 
    406417            # only redirect to referer if the latter is from the same instance 
     
    410421        except RedirectException: 
    411422            pass 
    412423    elif req.remote_user and authenticator.authname == 'anonymous': 
    413         auth_cookie = authenticator.login(req) 
     424        logged_out = authenticator.login(req) 
    414425    if path_info == '/login': 
    415         referer = req.get_header('Referer') 
    416         if referer[0:len(req.base_url)] != req.base_url: 
    417             # only redirect to referer if the latter is from the same instance 
    418             referer = None 
    419         try: 
    420             req.redirect(referer or env.href.wiki()) 
    421         except RedirectException: 
    422             pass 
     426        if logged_out: 
     427            req.reauthorize() 
     428            return 
     429        else: 
     430            referer = req.get_header('Referer') 
     431            if not referer.startswith(req.base_url): 
     432                # only redirect to referer if the latter is from the same instance 
     433                referer = env.href.wiki() 
     434            try: 
     435                req.redirect(referer) 
     436            except RedirectException: 
     437                pass 
    423438    req.authname = authenticator.authname 
    424439 
    425440    newsession = args.has_key('newsession') and args['newsession'] 
  • trac/auth.py

     
    3737                self.authname = cursor.fetchone()[0] 
    3838 
    3939    def login(self, req): 
     40        try: 
     41            if req.incookie['trac_auth'].value == 'logout': 
     42                req.outcookie['trac_auth'] = 'ok' 
     43                req.outcookie['trac_auth']['path'] = req.cgi_location 
     44                return True 
     45        except: 
     46            pass 
     47         
    4048        cursor = self.db.cursor () 
    4149        cookie = util.hex_entropy() 
    4250        cursor.execute ("INSERT INTO auth_cookie (cookie, name, ipnr, time)" + 
     
    4856        req.outcookie['trac_auth'] = cookie 
    4957        req.outcookie['trac_auth']['path'] = req.cgi_location 
    5058 
    51     def logout(self): 
     59    def logout(self, req): 
    5260        cursor = self.db.cursor () 
    5361        cursor.execute ("DELETE FROM auth_cookie WHERE name=%s", 
    5462                        self.authname) 
    5563        self.db.commit () 
     64        req.outcookie['trac_auth'] = 'logout' 
     65        req.outcookie['trac_auth']['path'] = req.cgi_location 
     66