add set admin password

This commit is contained in:
2024-09-21 22:22:13 +02:00
parent 985ae8f90d
commit 14c52afc37

View File

@@ -16,7 +16,7 @@ urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
class SatiAPI:
"""A API wrapper for Satisfactory dedicated server"""
__AUTHOR__ = 'anima'
__VERSION__ = '0.7.3'
__VERSION__ = '0.8.6'
def __init__(self, host: str = None, port: int = 7777, token: str = None, conffile: str = 'conf.yml', logfile: str = 'SatiAPI.log', loglevel: int = 20) -> None:
"""create a wrapper for satisfactory dedicated server
@@ -143,6 +143,19 @@ class SatiAPI:
if 'token' in self._config['auth']['Administrator'] and self.token is None:
self.token = self._config['auth']['Administrator']['token']
def _get_config_password(self, privilegeLevel: str = 'Administrator') -> str | None:
"""internal helper method to get password from config file
:param privilegeLevel: level of needed privilege
:type privilegeLevel: string
:return: password if exists
:rtype: string | None
"""
if 'auth' in self._config:
if privilegeLevel in self._config['auth']:
if 'password' in self._config['auth'][privilegeLevel]:
return self._config['auth'][privilegeLevel]['password']
def __query(self, query: str, data: dict = None, auth: bool = False) -> dict | bool | None:
"""run a query to satisfacoty dedicated server
@@ -191,6 +204,21 @@ class SatiAPI:
self._log.debug(f'{response.request.body=}')
return False
def _save_token(self, response: dict) -> bool:
"""internal helper method to save token from response
:param response: response after query
:type response: dict
:return: true if token successfull saved
:rtype: bool
"""
if response:
if 'authenticationToken' in response['data']:
self.token = response['data']['authenticationToken']
return True
return False
def _passwordless_login(self, privilegeLevel: str = 'InitialAdmin') -> bool:
"""Attempts to perform a passwordless login to the Dedicated Server as a player. Passwordless login is possible if the Dedicated Server is not claimed, or if Client Protection Password is not set for the Dedicated Server. This function requires no Authentication.
@@ -203,11 +231,7 @@ class SatiAPI:
data['MinimumPrivilegeLevel'] = privilegeLevel
response = self.__query('PasswordlessLogin', data)
if response:
self.token = response['data']['authenticationToken']
return True
else:
return response
return self._save_token(response)
def claim_server(self, servername: str, password: str = None) -> bool:
"""Claims this Dedicated Server if it is not claimed. Requires InitialAdmin privilege level, which can only be acquired by attempting passwordless login while the server does not have an Admin Password set, e.g. it is not claimed yet. Function does not return any data in case of success, and the server is claimed. The client should drop InitialAdmin privileges after that and use returned AuthenticationToken instead, and update it's cached server game state by calling QueryServerState.
@@ -224,24 +248,40 @@ class SatiAPI:
if self._passwordless_login():
data = dict()
if password is None:
if 'auth' in self._config:
if 'Administrator' in self._config['auth']:
if 'token' in self._config['auth']['Administrator'] and self.token is None:
password = self._config['auth']['Administrator']['password']
else:
password = self._get_config_password()
self._log.debug('password used from conf file')
if password is None:
self._log.error('no password for server claim set')
return False
data['ServerName'] = servername
data['AdminPassword'] = password
response = self.__query('ClaimServer', data, True)
if response:
self.token = response['data']['authenticationToken']
return True
else:
return False
return self._save_token(response)
else:
self._log.error('server can not be clamed because no authentication possable')
return False
def set_admin_password(self, password: str = None):
"""Updates the currently set Admin Password. This will invalidate all previously issued Client and Admin authentication tokens. Requires Admin privileges. Function does not return any data on success.
:param password: the new passwort to set will be read from config file it not set
:type password: string
:return: true if new password successfull set
:rtype: bool
"""
if password is None:
password = self._get_config_password()
if password is None:
self._log.error('can not set new admin password because no passwort is set')
return False
data = dict()
data['Password'] = password
response = self.__query('SetAdminPassword', data, True)
return self._save_token(response)
def get_token(self, password: str = None, privilegeLevel: str = 'Administrator') -> bool:
"""get token from satisfacory dedicated server, password is needed!
@@ -259,28 +299,20 @@ class SatiAPI:
if isinstance(password, str):
self._log.debug('password used from parameter')
else:
if self._config is not None and 'auth' in self._config:
if privilegeLevel in self._config['auth']:
if 'password' in self._config['auth'][privilegeLevel]:
password = self._config['auth'][privilegeLevel]['password']
self._log.debug('password used from conf file')
else:
self._log('missing information in config file under auth')
else:
password = self._get_config_password()
if password is not None:
self._log.debug('password used from conf file')
else:
self._log.error(f'no password for auth given!')
return False
data = dict()
data['password'] = password
data['MinimumPrivilegeLevel'] = privilegeLevel
response = self.__query('PasswordLogin', data)
if response:
self.token = response['data']['authenticationToken']
return True
else:
return response
return self._save_token(response)
def get_status(self) -> dict:
"""Retrieves the current state of the Dedicated Server. Does not require any input parameters.
@@ -324,7 +356,6 @@ class SatiAPI:
## todo
#RenameServer
#SetClientPassword
#SetAdminPassword
#SetAutoLoadSessionName
#ApplyServerOptions
#ApplyAdvancedGameSettings
@@ -342,5 +373,5 @@ class SatiAPI:
if __name__ == "__main__":
sati = SatiAPI(loglevel=10)
# print('Verbundene Spieler: ', sati.get_status()['serverGameState']['numConnectedPlayers'])
print(sati.claim_server('SDGame01'))
print(sati.get_status())
print(sati.get_status())
print(sati.token)