def accept(self): utils.addBoundlessRepository() if self.leLogin.text() == '' or self.lePassword.text() == '': QDialog.accept(self) return if self.authId == '': authConfig = QgsAuthMethodConfig('Basic') authId = QgsAuthManager.instance().uniqueConfigId() authConfig.setId(authId) authConfig.setConfig('username', self.leLogin.text()) authConfig.setConfig('password', self.lePassword.text()) authConfig.setName('Boundless Connect Portal') settings = QSettings('Boundless', 'BoundlessConnect') authConfig.setUri(settings.value('repoUrl', '', unicode)) if QgsAuthManager.instance().storeAuthenticationConfig(authConfig): utils.setRepositoryAuth(authId) else: QMessageBox.information(self, self.tr('Error!'), self.tr('Unable to save credentials')) else: authConfig = QgsAuthMethodConfig() QgsAuthManager.instance().loadAuthenticationConfig(self.authId, authConfig, True) authConfig.setConfig('username', self.leLogin.text()) authConfig.setConfig('password', self.lePassword.text()) QgsAuthManager.instance().updateAuthenticationConfig(authConfig) QDialog.accept(self)
def saveOrUpdateAuthId(self): if self.authId == '': authConfig = QgsAuthMethodConfig('Basic') self.authId = QgsAuthManager.instance().uniqueConfigId() authConfig.setId(self.authId) authConfig.setConfig('username', self.connectWidget.login().strip()) authConfig.setConfig('password', self.connectWidget.password().strip()) authConfig.setName('Boundless Connect Portal') authConfig.setUri(pluginSetting('repoUrl')) if QgsAuthManager.instance().storeAuthenticationConfig(authConfig): utils.setRepositoryAuth(self.authId) else: self._showMessage('Unable to save credentials.', QgsMessageBar.WARNING) else: authConfig = QgsAuthMethodConfig() QgsAuthManager.instance().loadAuthenticationConfig(self.authId, authConfig, True) authConfig.setConfig('username', self.connectWidget.login().strip()) authConfig.setConfig('password', self.connectWidget.password().strip()) QgsAuthManager.instance().updateAuthenticationConfig(authConfig) # also setup OAuth2 configuration if possible if oauth2_supported(): endpointUrl = "{}/token/oauth?version={}".format(pluginSetting("connectEndpoint"), pluginSetting("apiVersion")) setup_oauth(self.connectWidget.login().strip(), self.connectWidget.password().strip(), endpointUrl)
def setup_oauth(username, password, token_uri, refresh_token_uri='', authcfg_id='oauth-2', authcfg_name='OAuth2 test configuration'): """Setup oauth configuration to access OAuth API, return authcfg_id on success, None on failure """ cfgjson = { "accessMethod": 0, "apiKey": "", "clientId": "", "clientSecret": "", "configType": 1, "grantFlow": 2, "password": password, "persistToken": False, "redirectPort": '7070', "redirectUrl": "", "refreshTokenUrl": refresh_token_uri, "requestTimeout": '30', "requestUrl": "", "scope": "", "tokenUrl": token_uri, "username": username, "version": 1 } if authcfg_id not in QgsApplication.authManager( ).availableAuthMethodConfigs(): authConfig = QgsAuthMethodConfig('OAuth2') authConfig.setId(authcfg_id) authConfig.setName(authcfg_name) authConfig.setConfig('oauth2config', json.dumps(cfgjson)) if QgsApplication.authManager().storeAuthenticationConfig(authConfig): return authcfg_id else: authConfig = QgsAuthMethodConfig() QgsApplication.authManager().loadAuthenticationConfig( authcfg_id, authConfig, True) authConfig.setName(authcfg_name) authConfig.setConfig('oauth2config', json.dumps(cfgjson)) if QgsApplication.authManager().updateAuthenticationConfig(authConfig): return authcfg_id return None
def initAuthConfigId(): am = QgsApplication.authManager() if AUTHCFGID not in am.configIds(): conf = dict( URL=serverLocationBasicAuth()+'/rest', USER=GSUSER, PASSWORD=GSPASSWORD, AUTHCFG=AUTHCFGID) conf.update([(k, os.getenv('GS%s' % k)) for k in conf if 'GS%s' % k in os.environ]) cfg = QgsAuthMethodConfig() cfg.setId(AUTHCFGID) cfg.setName('Geoserver test') cfg.setMethod('Basic') cfg.setConfig('username', conf['USER']) cfg.setConfig('password', conf['PASSWORD']) self.am.storeAuthenticationConfig(cfg)
def initAuthConfigId(): am = QgsApplication.authManager() if AUTHCFGID not in am.configIds(): conf = dict( URL=serverLocationBasicAuth()+'/rest', USER=GSUSER, PASSWORD=GSPASSWORD, AUTHCFG=AUTHCFGID) conf.update([(k, os.getenv('GS%s' % k)) for k in conf if 'GS%s' % k in os.environ]) cfg = QgsAuthMethodConfig() cfg.setId(AUTHCFGID) cfg.setName('Geoserver test') cfg.setMethod('Basic') cfg.setConfig('username', conf['USER']) cfg.setConfig('password', conf['PASSWORD']) am.storeAuthenticationConfig(cfg)
def setup_oauth(username, password, token_uri, refresh_token_uri='', authcfg_id='oauth-2', authcfg_name='OAuth2 test configuration'): """Setup oauth configuration to access OAuth API, return authcfg_id on success, None on failure """ cfgjson = { "accessMethod": 0, "apiKey": "", "clientId": "", "clientSecret": "", "configType": 1, "grantFlow": 2, "password": password, "persistToken": False, "redirectPort": '7070', "redirectUrl": "", "refreshTokenUrl": refresh_token_uri, "requestTimeout": '30', "requestUrl": "", "scope": "", "state": "", "tokenUrl": token_uri, "username": username, "version": 1 } if authcfg_id not in QgsApplication.authManager().availableAuthMethodConfigs(): authConfig = QgsAuthMethodConfig('OAuth2') authConfig.setId(authcfg_id) authConfig.setName(authcfg_name) authConfig.setConfig('oauth2config', json.dumps(cfgjson)) if QgsApplication.authManager().storeAuthenticationConfig(authConfig): return authcfg_id else: authConfig = QgsAuthMethodConfig() QgsApplication.authManager().loadAuthenticationConfig(authcfg_id, authConfig, True) authConfig.setName(authcfg_name) authConfig.setConfig('oauth2config', json.dumps(cfgjson)) if QgsApplication.authManager().updateAuthenticationConfig(authConfig): return authcfg_id return None
from qgis.PyQt.QtCore import QSettings from qgis.core import QgsAuthManager, QgsAuthMethodConfig, QgsMessageLog, Qgis AUTHDB_MASTERPWD = 'password' QgsMessageLog.logMessage("Init script: %s" % __file__, tag="Init script", level=Qgis.Info) # Do not run twice! if not QSettings().value("InitScript/AuthCfgCreated", type=bool): QSettings().setValue("InitScript/AuthCfgCreated", True) # Check if authdb master password is set am = QgsAuthManager.instance() if not am.masterPasswordHashInDb(): # Set it! am.setMasterPassword(AUTHDB_MASTERPWD, True) # Create config am.authenticationDbPath() am.masterPasswordIsSet() cfg = QgsAuthMethodConfig() cfg.setId('myauth1') # Optional, useful for plugins to retrieve an authcfg cfg.setName('Example Auth Config HTTP Basic') cfg.setMethod('Basic') cfg.setConfig('username', 'username') cfg.setConfig('password', 'password') am.storeAuthenticationConfig(cfg) else: QgsMessageLog.logMessage("Master password was already set: aborting", tag="Init script", level=Qgis.Info) else: QgsMessageLog.logMessage("AuthCfg was already created: aborting", tag="Init script", level=Qgis.Info)
from qgis.PyQt.QtCore import QSettings from qgis.core import QgsAuthManager, QgsAuthMethodConfig, QgsMessageLog AUTHDB_MASTERPWD = 'password' QgsMessageLog.logMessage("Init script: %s" % __file__, tag="Init script", level=Qgis.Info) # Do not run twice! if not QSettings().value("InitScript/AuthCfgCreated", type=bool): QSettings().setValue("InitScript/AuthCfgCreated", True) # Check if authdb master password is set am = QgsAuthManager.instance() if not am.masterPasswordHashInDb(): # Set it! am.setMasterPassword(AUTHDB_MASTERPWD, True) # Create config am.authenticationDbPath() am.masterPasswordIsSet() cfg = QgsAuthMethodConfig() cfg.setId('myauth1') # Optional, useful for plugins to retrieve an authcfg cfg.setName('Example Auth Config HTTP Basic') cfg.setMethod('Basic') cfg.setConfig('username', 'username') cfg.setConfig('password', 'password') am.storeAuthenticationConfig(cfg) else: QgsMessageLog.logMessage("Master password was already set: aborting", tag="Init script", level=Qgis.Info) else: QgsMessageLog.logMessage("AuthCfg was already created: aborting", tag="Init script", level=Qgis.Info)