def onStart(self): super().onStart() if self.ConfigManager.getSnipsConfiguration('project-alice', 'console_token'): self.logInfo('Snips console authorized') self._headers['Authorization'] = f"JWT {self.ConfigManager.getSnipsConfiguration('project-alice', 'console_token')}" self._user = SnipsConsoleUser({ 'id': self.ConfigManager.getSnipsConfiguration('project-alice', 'console_user_id'), 'email': self.ConfigManager.getSnipsConfiguration('project-alice', 'console_user_email') }) self._connected = True elif self.loginCredentialsAreConfigured(): self.logInfo('Snips console not authorized') self._login() else: self.logWarning('Snips console credentials not found') if not Path(self.Commons.rootDir(), '/assistant').exists(): self.logError('No assistant found, cannot start') self.ProjectAlice.onStop() else: self.logWarning('Assistant is existing, allowing to boot but functions will be restricted!') self.isActive = False
def _login(self): self._tries += 1 if self._tries > 3: self.logInfo(f'Tried to login {self._tries} times, giving up now') self._tries = 0 return self.logInfo( f"Connecting to Snips console using account {self.ConfigManager.getAliceConfigByName('snipsConsoleLogin')}" ) payload = { 'email': self.ConfigManager.getAliceConfigByName('snipsConsoleLogin'), 'password': self.ConfigManager.getAliceConfigByName('snipsConsolePassword') } req = self._req(url='/v1/user/auth', data=payload) if req.status_code == 200: self.logInfo( 'Connected to Snips console. Fetching and saving access token') try: token = req.headers['authorization'] self._user = SnipsConsoleUser(req.json()['user']) accessToken = self._getAccessToken(token) if accessToken: self.logInfo('Saving console access token') self.ConfigManager.updateSnipsConfiguration( parent='project-alice', key='console_token', value=accessToken['token']) self.ConfigManager.updateSnipsConfiguration( parent='project-alice', key='console_alias', value=accessToken['alias']) self.ConfigManager.updateSnipsConfiguration( parent='project-alice', key='console_user_id', value=self._user.userId) self.ConfigManager.updateSnipsConfiguration( parent='project-alice', key='console_user_email', value=self._user.userEmail) self._headers[ 'Authorization'] = f"JWT {accessToken['token']}" self._connected = True self._tries = 0 else: raise Exception('Error fetching JWT console token') except Exception as e: self.logError(f"Couldn't retrieve snips console token: {e}") self._connected = False return else: self.logError( f"Couldn't connect to Snips console: {req.status_code}") self._connected = False
def onStart(self): super().onStart() if self.ConfigManager.getSnipsConfiguration('project-alice', 'console_token'): self._logger.info(f'[{self.name}] Snips console authorized') self._headers['Authorization'] = f"JWT {self.ConfigManager.getSnipsConfiguration('project-alice', 'console_token')}" self._user = SnipsConsoleUser({ 'id': self.ConfigManager.getSnipsConfiguration('project-alice', 'console_user_id'), 'email': self.ConfigManager.getSnipsConfiguration('project-alice', 'console_user_email') }) self._connected = True elif self.loginCredentialsAreConfigured(): self._logger.info(f'[{self.name}] Snips console not authorized') self._login() else: self._logger.warning(f'[{self.name}] Snips console credentials not found') self.isActive = False
def _login(self): self._tries += 1 if self._tries > 3: self._logger.info('- Tried to login {} times, giving up now'.format(self._tries)) self._tries = 0 return self._logger.info('- Connecting to Snips console using account {}'.format(self.ConfigManager.getAliceConfigByName('snipsConsoleLogin'))) payload = { 'email' : self.ConfigManager.getAliceConfigByName('snipsConsoleLogin'), 'password': self.ConfigManager.getAliceConfigByName('snipsConsolePassword') } req = self._req(url='/v1/user/auth', data=payload) if req.status_code == 200: self._logger.info('[{}] Connected to Snips console. Fetching and saving access token'.format(self.NAME)) try: token = req.headers['authorization'] self._user = SnipsConsoleUser(json.loads(req.content)['user']) accessToken = self._getAccessToken(token) if accessToken: self._logger.info('- Saving console access token') self.ConfigManager.updateSnipsConfiguration(parent='project-alice', key='console_token', value=accessToken['token']) self.ConfigManager.updateSnipsConfiguration(parent='project-alice', key='console_alias', value=accessToken['alias']) self.ConfigManager.updateSnipsConfiguration(parent='project-alice', key='console_user_id', value=self._user.userId) self.ConfigManager.updateSnipsConfiguration(parent='project-alice', key='console_user_email', value=self._user.userEmail) self._connected = True self._tries = 0 else: raise Exception('- Error fetching JWT console token') except Exception as e: self._logger.error("- Couldn't retrieve snips console token: {}".format(e)) self._connected = False return else: self._logger.error("- Couldn't connect to Snips console: {}".format(req.status_code)) self._connected = False