def __init__(self): config = utils.get_config() self.token_time = 0 self.username = self.load_username_from_config(config) self.password = self.load_password_from_config(config) self.pnc_config = psc.PncServerConfig(config) self.keycloak_config = kc.KeycloakConfig(config) self.token = self.retrieve_keycloak_token() self.apiclient = self.create_api_client()
def __setstate__(self, state): # here we need to read the config file again, to check that values for URLs haven't changed. the way to # change the username / password will be a login function the user calls. ideally the config file won't need # to be changed much once it's in place newtoken = False config = utils.get_config() saved_kc_config, saved_username, self.token, self.token_time = state self.pnc_config = psc.PncServerConfig(config) # check for changes in keycloak usrname; if so, we'll need to get a new token regardless of time self.username = self.load_username_from_config(config) if self.username is not None and self.username != saved_username: sys.stderr.write( "Keycloak username has been changed. Retrieving new token...\n" ) newtoken = True else: self.username = saved_username # check for changes in keycloak configuration; if so, we'll need to get a new token regardless of time current_keycloak_config = kc.KeycloakConfig(config) if not current_keycloak_config == saved_kc_config: logging.info( "Keycloak server has been reconfigured. Retrieving new token...\n" ) self.keycloak_config = current_keycloak_config newtoken = True else: self.keycloak_config = saved_kc_config # if more than a day has passed since we saved the token, and keycloak server configuration is not modified, # get a new one if not newtoken and utils.current_time_millis( ) - self.token_time > 86400000: # input the password again since we no longer cache it logging.info( "Keycloak token has expired for user {}. Retrieving new token...\n" .format(self.username)) newtoken = True if newtoken: # if using client auth, we simply get a new token. if (self.keycloak_config.client_mode in ['True', 'true', '1']): self.token = self.retrieve_keycloak_token() else: # enter password to get new token, but only if the user has not entered a password in pnc-cli.conf password = self.load_password_from_config(config) if password: self.password = password else: self.password = self.input_password() self.token = self.retrieve_keycloak_token() self.apiclient = self.create_api_client()