Ticket #791: trac_auth.patch
| File trac_auth.patch, 4.8 KB (added by Matthew Good <matt-good.net>, 7 years ago) |
|---|
-
trac/core.py
291 291 292 292 def end_headers(self): 293 293 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...') 294 311 295 312 def redirect(self, url): 296 313 self.send_response(302) 297 314 self.send_header('Location', url) 298 315 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) 305 318 self.end_headers() 306 319 self.write('Redirecting...') 307 320 raise RedirectException() … … 319 332 cs.parseFile(filename) 320 333 data = cs.render() 321 334 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() 324 336 self.send_header('Content-Type', content_type + ';charset=utf-8') 325 337 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) 329 339 self.end_headers() 330 340 if self.command != 'HEAD': 331 341 self.write(data) … … 399 409 Wiki.populate_page_dict(database, env) 400 410 401 411 authenticator = auth.Authenticator(database, req) 412 logged_out = False 402 413 if path_info == '/logout': 403 authenticator.logout( )414 authenticator.logout(req) 404 415 referer = req.get_header('Referer') 405 416 if referer[0:len(req.base_url)] != req.base_url: 406 417 # only redirect to referer if the latter is from the same instance … … 410 421 except RedirectException: 411 422 pass 412 423 elif req.remote_user and authenticator.authname == 'anonymous': 413 auth_cookie= authenticator.login(req)424 logged_out = authenticator.login(req) 414 425 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 423 438 req.authname = authenticator.authname 424 439 425 440 newsession = args.has_key('newsession') and args['newsession'] -
trac/auth.py
37 37 self.authname = cursor.fetchone()[0] 38 38 39 39 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 40 48 cursor = self.db.cursor () 41 49 cookie = util.hex_entropy() 42 50 cursor.execute ("INSERT INTO auth_cookie (cookie, name, ipnr, time)" + … … 48 56 req.outcookie['trac_auth'] = cookie 49 57 req.outcookie['trac_auth']['path'] = req.cgi_location 50 58 51 def logout(self ):59 def logout(self, req): 52 60 cursor = self.db.cursor () 53 61 cursor.execute ("DELETE FROM auth_cookie WHERE name=%s", 54 62 self.authname) 55 63 self.db.commit () 64 req.outcookie['trac_auth'] = 'logout' 65 req.outcookie['trac_auth']['path'] = req.cgi_location 66
