Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
# -*- coding: utf-8 -*- # # Copyright (C) 2009 Andrew Resch <andrewresch@gmail.com> # Copyright (C) 2011 Pedro Algarvio <pedro@algarvio.me> # # This file is part of Deluge and is licensed under GNU General Public License 3.0, or later, with # the additional special exception to link portions of this program with the OpenSSL library. # See LICENSE for more details. #
AUTH_LEVEL_READONLY, create_localclient_account)
'NONE': AUTH_LEVEL_NONE, 'READONLY': AUTH_LEVEL_READONLY, 'DEFAULT': AUTH_LEVEL_NORMAL, 'NORMAL': AUTH_LEVEL_DEFAULT, 'ADMIN': AUTH_LEVEL_ADMIN }
return { 'username': self.username, 'password': self.password, 'authlevel': AUTH_LEVELS_MAPPING_REVERSE[self.authlevel], 'authlevel_int': self.authlevel }
return ('<Account username="%(username)s" authlevel=%(authlevel)s>' % self.__dict__)
# Check for auth file and create if necessary log.info("Authfile not found, recreating it.") self.__load_auth_file() return
log.info("Auth file changed, reloading it!") self.__load_auth_file()
"""Authorizes users based on username and password.
Args: username (str): Username password (str): Password
Returns: int: The auth level for this user.
Raises: AuthenticationRequired: If aditional details are required to authenticate. BadLoginError: If the username does not exist or password does not match.
""" raise AuthenticationRequired( "Username and Password are required.", username )
# Let's try to re-load the file.. Maybe it's been updated raise BadLoginError("Username does not exist", username)
# Return the users auth level elif not password and self.__auth[username].password: raise AuthenticationRequired("Password is required", username) else: raise BadLoginError("Password does not match", username)
"""Returns a list of known deluge usernames.""" self.__load_auth_file() return [account.data() for account in self.__auth.values()]
if username in self.__auth: raise AuthManagerError("Username in use.", username) try: self.__auth[username] = Account(username, password, AUTH_LEVELS_MAPPING[authlevel]) self.write_auth_file() return True except Exception as ex: log.exception(ex) raise ex
if username not in self.__auth: raise AuthManagerError("Username not known", username) try: self.__auth[username].username = username self.__auth[username].password = password self.__auth[username].authlevel = AUTH_LEVELS_MAPPING[authlevel] self.write_auth_file() return True except Exception as ex: log.exception(ex) raise ex
if username not in self.__auth: raise AuthManagerError("Username not known", username) elif username == component.get("RPCServer").get_session_user(): raise AuthManagerError( "You cannot delete your own account while logged in!", username )
del self.__auth[username] self.write_auth_file() return True
filename = "auth" filepath = os.path.join(configmanager.get_config_dir(), filename) filepath_bak = filepath + ".bak" filepath_tmp = filepath + ".tmp"
try: if os.path.isfile(filepath): log.debug("Creating backup of %s at: %s", filename, filepath_bak) shutil.copy2(filepath, filepath_bak) except IOError as ex: log.error("Unable to backup %s to %s: %s", filepath, filepath_bak, ex) else: log.info("Saving the %s at: %s", filename, filepath) try: with open(filepath_tmp, "wb") as _file: for account in self.__auth.values(): _file.write("%(username)s:%(password)s:%(authlevel_int)s\n" % account.data()) _file.flush() os.fsync(_file.fileno()) shutil.move(filepath_tmp, filepath) except IOError as ex: log.error("Unable to save %s: %s", filename, ex) if os.path.isfile(filepath_bak): log.info("Restoring backup of %s from: %s", filename, filepath_bak) shutil.move(filepath_bak, filepath)
self.__load_auth_file()
# Check for auth file and create if necessary
elif self.__auth_modification_time == auth_file_modification_time: # File didn't change, no need for re-parsing's return
except IOError as ex: log.warning("Unable to load %s: %s", _filepath, ex) file_data = [] else:
# Load the auth file into a dictionary: {username: Account(...)} # This line is a comment or empty continue username, password = lsplit log.warning("Your auth entry for %s contains no auth level, " "using AUTH_LEVEL_DEFAULT(%s)..", username, AUTH_LEVEL_DEFAULT) if username == 'localclient': authlevel = AUTH_LEVEL_ADMIN else: authlevel = AUTH_LEVEL_DEFAULT # This is probably an old auth file save_and_reload = True else: log.error("Your auth file is malformed: Incorrect number of fields!") continue
except ValueError: try: authlevel = AUTH_LEVELS_MAPPING[authlevel] except KeyError: log.error("Your auth file is malformed: %r is not a valid auth level", authlevel) continue
create_localclient_account(True) return self.__load_auth_file()
log.info("Re-writing auth file (upgrade)") self.write_auth_file() |