Better fix for last commit.. AuthManager will now raise a BadLoginError if the username or password

does not match
This commit is contained in:
Andrew Resch 2009-07-06 21:09:22 +00:00
commit 2b789b501b
3 changed files with 28 additions and 14 deletions

View file

@ -39,6 +39,7 @@ import stat
import deluge.component as component import deluge.component as component
import deluge.configmanager as configmanager import deluge.configmanager as configmanager
import deluge.error
from deluge.log import LOG as log from deluge.log import LOG as log
@ -49,6 +50,9 @@ AUTH_LEVEL_ADMIN = 10
AUTH_LEVEL_DEFAULT = AUTH_LEVEL_NORMAL AUTH_LEVEL_DEFAULT = AUTH_LEVEL_NORMAL
class BadLoginError(deluge.error.DelugeError):
pass
class AuthManager(component.Component): class AuthManager(component.Component):
def __init__(self): def __init__(self):
component.Component.__init__(self, "AuthManager") component.Component.__init__(self, "AuthManager")
@ -69,22 +73,24 @@ class AuthManager(component.Component):
:param username: str, username :param username: str, username
:param password: str, password :param password: str, password
:returns: int, the auth level for this user or 0 if not able to authenticate :returns: int, the auth level for this user
:rtype: int :rtype: int
:raises BadLoginError: if the username does not exist or password does not match
""" """
if username not in self.__auth: if username not in self.__auth:
# Let's try to re-load the file.. Maybe it's been updated # Let's try to re-load the file.. Maybe it's been updated
self.__load_auth_file() self.__load_auth_file()
if username not in self.__auth: if username not in self.__auth:
return 0 raise BadLoginError("Username does not exist")
if self.__auth[username][0] == password: if self.__auth[username][0] == password:
# Return the users auth level # Return the users auth level
return int(self.__auth[username][1]) return int(self.__auth[username][1])
else:
return 0 raise BadLoginError("Password does not match")
def __create_localclient_account(self): def __create_localclient_account(self):
""" """

View file

@ -373,6 +373,7 @@ class DaemonSSLProxy(DaemonProxy):
msg += "\n" + error.traceback + "\n" + error.exception_type + ": " + error.exception_msg msg += "\n" + error.traceback + "\n" + error.exception_type + ": " + error.exception_msg
msg += "\n" + "-" * 80 msg += "\n" + "-" * 80
log.error(msg) log.error(msg)
return error_data
def __on_connect(self, result, username, password): def __on_connect(self, result, username, password):
self.__login_deferred = self.call("daemon.login", username, password) self.__login_deferred = self.call("daemon.login", username, password)
@ -381,14 +382,9 @@ class DaemonSSLProxy(DaemonProxy):
def __on_connect_fail(self, reason): def __on_connect_fail(self, reason):
log.debug("connect_fail: %s", reason) log.debug("connect_fail: %s", reason)
self.login_deferred.callback(False) self.login_deferred.errback(reason)
def __on_login(self, result, username): def __on_login(self, result, username):
if not result:
# We received a 0 auth level from the server which means it failed
self.login_deferred.errback(result)
return
self.username = username self.username = username
# We need to tell the daemon what events we're interested in receiving # We need to tell the daemon what events we're interested in receiving
if self.__factory.event_handlers: if self.__factory.event_handlers:
@ -396,7 +392,8 @@ class DaemonSSLProxy(DaemonProxy):
self.login_deferred.callback(result) self.login_deferred.callback(result)
def __on_login_fail(self, result): def __on_login_fail(self, result):
self.login_deferred.callback(False) log.debug("_on_login_fail(): %s", result)
self.login_deferred.errback(result)
def set_disconnect_callback(self, cb): def set_disconnect_callback(self, cb):
""" """
@ -518,6 +515,12 @@ class Client(object):
self._daemon_proxy = DaemonSSLProxy(dict(self.__event_handlers)) self._daemon_proxy = DaemonSSLProxy(dict(self.__event_handlers))
self._daemon_proxy.set_disconnect_callback(self.__on_disconnect) self._daemon_proxy.set_disconnect_callback(self.__on_disconnect)
d = self._daemon_proxy.connect(host, port, username, password) d = self._daemon_proxy.connect(host, port, username, password)
def on_connect_fail(result):
log.debug("on_connect_fail: %s", result)
self.disconnect()
return result
d.addErrback(on_connect_fail)
return d return d
def disconnect(self): def disconnect(self):

View file

@ -52,17 +52,22 @@ class Command(BaseCommand):
else: else:
port = int(port) port = int(port)
def on_disconnect(result): def do_connect():
d = client.connect(host, port, username, password) d = client.connect(host, port, username, password)
def on_connect(result): def on_connect(result):
self.console.write("{!success!}Connected to %s:%s!" % (host, port)) self.console.write("{!success!}Connected to %s:%s!" % (host, port))
component.start() component.start()
def on_connect_fail(result): def on_connect_fail(result):
self.console.write("{!error!}Failed to connect to %s:%s!" % (host, port)) self.console.write("{!error!}Failed to connect to %s:%s with reason: %s" % (host, port, result.value.exception_msg))
d.addCallback(on_connect) d.addCallback(on_connect)
d.addErrback(on_connect_fail) d.addErrback(on_connect_fail)
return d return d
client.disconnect().addCallback(on_disconnect) if client.connected():
def on_disconnect(result):
do_connect()
client.disconnect().addCallback(on_disconnect)
else:
do_connect()