Send password_manager as a general application setting instead of per handler

This commit is contained in:
Guillem Anguera 2016-02-12 23:30:42 +01:00
parent 5306523446
commit 195ea8e15a

View File

@ -48,23 +48,20 @@ class BasicAuth(object):
self._transforms = [] self._transforms = []
self.finish() self.finish()
def initialize(self, **kwargs):
self.wauthenticator = kwargs.get("wauthenticator")
def prepare(self): def prepare(self):
if self.wauthenticator: wauthenticator = self.application.settings['wauthenticator']
if wauthenticator:
auth_header = self.request.headers.get('Authorization') auth_header = self.request.headers.get('Authorization')
if auth_header is None or not auth_header.startswith('Basic '): if auth_header is None or not auth_header.startswith('Basic '):
self.set_auth_headers() self.set_auth_headers()
else: else:
self.auth_decoded = base64.decodestring(auth_header[6:]) self.auth_decoded = base64.decodestring(auth_header[6:])
self.username, self.password = self.auth_decoded.split(':', 2) self.username, self.password = self.auth_decoded.split(':', 2)
if not self.wauthenticator.test(self.username, self.password): if not wauthenticator.test(self.username, self.password):
self.set_auth_headers() self.set_auth_headers()
raise APIError(401, "Invalid username or password.") raise APIError(401, "Invalid username or password.")
class RequestHandler(BasicAuth, tornado.web.RequestHandler): class RequestHandler(BasicAuth, tornado.web.RequestHandler):
def set_default_headers(self): def set_default_headers(self):
@ -311,9 +308,6 @@ class Application(tornado.web.Application):
def __init__(self, master, debug, wauthenticator): def __init__(self, master, debug, wauthenticator):
self.master = master self.master = master
self.additional_args = dict(
wauthenticator=wauthenticator,
)
handlers = [ handlers = [
(r"/", IndexHandler), (r"/", IndexHandler),
(r"/filter-help", FiltHelp), (r"/filter-help", FiltHelp),
@ -330,14 +324,12 @@ class Application(tornado.web.Application):
(r"/settings", Settings), (r"/settings", Settings),
(r"/clear", ClearAll), (r"/clear", ClearAll),
] ]
for i, handler in enumerate(handlers):
handlers[i] += (self.additional_args,)
settings = dict( settings = dict(
template_path=os.path.join(os.path.dirname(__file__), "templates"), template_path=os.path.join(os.path.dirname(__file__), "templates"),
static_path=os.path.join(os.path.dirname(__file__), "static"), static_path=os.path.join(os.path.dirname(__file__), "static"),
xsrf_cookies=True, xsrf_cookies=True,
cookie_secret=os.urandom(256), cookie_secret=os.urandom(256),
debug=debug, debug=debug,
wauthenticator=wauthenticator,
) )
super(Application, self).__init__(handlers, **settings) super(Application, self).__init__(handlers, **settings)