|
|
@ -92,35 +92,41 @@ from tornado.ioloop import IOLoop |
|
|
|
|
|
|
|
req_headers = None |
|
|
|
|
|
|
|
def require_basic_auth(handler_class): |
|
|
|
def basicauth(handler_class): |
|
|
|
def wrap_execute(handler_execute): |
|
|
|
def require_basic_auth(handler, kwargs): |
|
|
|
def get_auth(): |
|
|
|
def basicauth(handler, transforms, *args, **kwargs): |
|
|
|
def _request_basic_auth(handler): |
|
|
|
handler.set_status(401) |
|
|
|
handler.set_header('WWW-Authenticate', 'Basic realm=Restricted') |
|
|
|
handler._transforms = [] |
|
|
|
handler.finish() |
|
|
|
return False |
|
|
|
|
|
|
|
if not sickbeard.WEB_USERNAME and not sickbeard.WEB_PASSWORD: |
|
|
|
if not handler.get_secure_cookie("user"): |
|
|
|
handler.set_secure_cookie("user", str(time.time())) |
|
|
|
return True |
|
|
|
try: |
|
|
|
auth_hdr = handler.request.headers.get('Authorization') |
|
|
|
|
|
|
|
if auth_hdr == None: |
|
|
|
return _request_basic_auth(handler) |
|
|
|
if not auth_hdr.startswith('Basic '): |
|
|
|
return _request_basic_auth(handler) |
|
|
|
|
|
|
|
auth_decoded = base64.decodestring(auth_hdr[6:]) |
|
|
|
username, password = auth_decoded.split(':', 2) |
|
|
|
|
|
|
|
auth_header = handler.request.headers.get('Authorization') |
|
|
|
if auth_header and auth_header.startswith('Basic '): |
|
|
|
auth_decoded = base64.decodestring(auth_header[6:]) |
|
|
|
basicauth_user, basicauth_pass = auth_decoded.split(':', 2) |
|
|
|
if basicauth_user == sickbeard.WEB_USERNAME and basicauth_pass == sickbeard.WEB_PASSWORD: |
|
|
|
if username == sickbeard.WEB_USERNAME and password == sickbeard.WEB_PASSWORD: |
|
|
|
#logger.log('authenticated user successfully', logger.DEBUG) |
|
|
|
if not handler.get_secure_cookie("user"): |
|
|
|
handler.set_secure_cookie("user", str(time.time())) |
|
|
|
return True |
|
|
|
|
|
|
|
handler.clear_cookie("user") |
|
|
|
get_auth() |
|
|
|
else: |
|
|
|
if handler.get_secure_cookie("user"): |
|
|
|
handler.clear_cookie("user") |
|
|
|
return _request_basic_auth(handler) |
|
|
|
except Exception, e: |
|
|
|
handler.clear_cookie("user") |
|
|
|
return _request_basic_auth(handler) |
|
|
|
return True |
|
|
|
|
|
|
|
def _execute(self, transforms, *args, **kwargs): |
|
|
|
if not require_basic_auth(self, kwargs): |
|
|
|
if not basicauth(self, transforms, *args, **kwargs): |
|
|
|
return False |
|
|
|
return handler_execute(self, transforms, *args, **kwargs) |
|
|
|
|
|
|
@ -129,12 +135,12 @@ def require_basic_auth(handler_class): |
|
|
|
handler_class._execute = wrap_execute(handler_class._execute) |
|
|
|
return handler_class |
|
|
|
|
|
|
|
@require_basic_auth |
|
|
|
class RedirectHandler(RequestHandler): |
|
|
|
|
|
|
|
def get(self, path, **kwargs): |
|
|
|
self.redirect(path, permanent=True) |
|
|
|
|
|
|
|
@basicauth |
|
|
|
class IndexHandler(RedirectHandler): |
|
|
|
def __init__(self, application, request, **kwargs): |
|
|
|
super(IndexHandler, self).__init__(application, request, **kwargs) |
|
|
@ -155,7 +161,7 @@ class IndexHandler(RedirectHandler): |
|
|
|
args[arg] = value[0] |
|
|
|
return args |
|
|
|
|
|
|
|
def _dispatch(self): |
|
|
|
def _dispatch(self, callback): |
|
|
|
|
|
|
|
args = None |
|
|
|
path = self.request.uri.split('?')[0] |
|
|
@ -194,21 +200,22 @@ class IndexHandler(RedirectHandler): |
|
|
|
|
|
|
|
if func: |
|
|
|
if args: |
|
|
|
return func(**args) |
|
|
|
callback(func(**args)) |
|
|
|
else: |
|
|
|
return func() |
|
|
|
callback(func()) |
|
|
|
|
|
|
|
if self.request.uri != ('/'): |
|
|
|
raise HTTPError(404) |
|
|
|
callback(HTTPError(404)) |
|
|
|
|
|
|
|
def get_current_user(self): |
|
|
|
return self.get_secure_cookie("user") |
|
|
|
|
|
|
|
@authenticated |
|
|
|
@asynchronous |
|
|
|
@gen.engine |
|
|
|
def get(self, *args, **kwargs): |
|
|
|
try: |
|
|
|
self.finish(self._dispatch()) |
|
|
|
result = yield gen.Task(self._dispatch) |
|
|
|
self.finish(result) |
|
|
|
except Exception as e: |
|
|
|
logger.log(ex(e), logger.ERROR) |
|
|
|
logger.log(u"Traceback: " + traceback.format_exc(), logger.DEBUG) |
|
|
@ -216,8 +223,8 @@ class IndexHandler(RedirectHandler): |
|
|
|
|
|
|
|
def post(self, *args, **kwargs): |
|
|
|
try: |
|
|
|
resp = self._dispatch() |
|
|
|
self.finish(resp) |
|
|
|
result = yield gen.Task(self._dispatch) |
|
|
|
self.finish(result) |
|
|
|
except Exception as e: |
|
|
|
logger.log(ex(e), logger.ERROR) |
|
|
|
logger.log(u"Traceback: " + traceback.format_exc(), logger.DEBUG) |
|
|
|