[WebUI] Refactor out AuthError for NotAuthorizedError

This commit is contained in:
Calum Lind 2017-06-23 12:19:39 +01:00
parent a7c4228ce7
commit 0cc0882ac9
2 changed files with 9 additions and 24 deletions

View File

@ -19,21 +19,12 @@ from email.utils import formatdate
from twisted.internet.task import LoopingCall
from deluge.common import AUTH_LEVEL_ADMIN, AUTH_LEVEL_NONE
from deluge.error import NotAuthorizedError
from deluge.ui.web.json_api import JSONComponent, export
log = logging.getLogger(__name__)
class AuthError(Exception):
"""
An exception that might be raised when checking a request for
authentication.
"""
pass
# Import after as json_api imports the above AuthError
from deluge.ui.web.json_api import export, JSONComponent # NOQA, isort:skip pylint: disable=wrong-import-position
def make_checksum(session_id):
checksum = 0
for value in [ord(char) for char in session_id]:
@ -226,7 +217,7 @@ class Auth(JSONComponent):
request.session_id = session_id
if auth_level < level:
raise AuthError('Not authenticated')
raise NotAuthorizedError(auth_level, level)
def _change_password(self, new_password):
"""

View File

@ -21,8 +21,10 @@ from twisted.internet import defer, reactor
from twisted.internet.defer import Deferred, DeferredList
from twisted.web import http, resource, server
from deluge import common, component, httpdownloader
from deluge import component, httpdownloader
from deluge.common import AUTH_LEVEL_DEFAULT, get_magnet_info, is_magnet
from deluge.configmanager import get_config_dir
from deluge.error import NotAuthorizedError
from deluge.ui.client import Client, client
from deluge.ui.common import FileTree2, TorrentInfo
from deluge.ui.coreconfig import CoreConfig
@ -33,9 +35,6 @@ from deluge.ui.web.common import _, compress
log = logging.getLogger(__name__)
AUTH_LEVEL_DEFAULT = None
AuthError = None
class JSONComponent(component.Component):
def __init__(self, name, interval=1, depend=None):
@ -55,11 +54,6 @@ def export(auth_level=AUTH_LEVEL_DEFAULT):
:type auth_level: int
"""
global AUTH_LEVEL_DEFAULT, AuthError
if AUTH_LEVEL_DEFAULT is None:
from deluge.common import AUTH_LEVEL_DEFAULT
from deluge.ui.web.auth import AuthError # NOQA pylint: disable=redefined-outer-name
def wrap(func, *args, **kwargs):
func._json_export = True
func._json_auth_level = auth_level
@ -161,7 +155,7 @@ class JSON(resource.Resource, component.Component):
result = self._exec_remote(method, params, request)
else:
error = {'message': 'Unknown method', 'code': 2}
except AuthError:
except NotAuthorizedError:
error = {'message': 'Not authenticated', 'code': 1}
except Exception as ex:
log.error('Error calling method `%s`: %s', method, ex)
@ -650,7 +644,7 @@ class WebApi(JSONComponent):
@export
def get_magnet_info(self, uri):
return common.get_magnet_info(uri)
return get_magnet_info(uri)
@export
def add_torrents(self, torrents):
@ -672,7 +666,7 @@ class WebApi(JSONComponent):
deferreds = []
for torrent in torrents:
if common.is_magnet(torrent['path']):
if is_magnet(torrent['path']):
log.info('Adding torrent from magnet uri `%s` with options `%r`',
torrent['path'], torrent['options'])
d = client.core.add_torrent_magnet(torrent['path'], torrent['options'])