我如何通过PyQGIS在auth-manager中获得加密的密码?

问题描述 投票:0回答:1

我正在制作一个QGIS插件,我向PostgreSQL连接(要求已在用户的连接列表中设置)请求身份验证对象(使用QgsProcessingParameterAuthConfig)。我的目标是使用PyQGIS获取登录名和密码,并使用它们与psycopg2连接。

谢谢!

询问的参数QgsProcessingParameterAuthConfig返回带有认证对象的标识密钥的字符串。

  • 我可以使用此键获取QgsAuthMethodConfig对象,但是密码为空。
  • 我没有找到访问密码的方法,也没有其他插件可以这样做。
  • 可以知道保存密码的SQLite数据库,但是密码是加密的,我不知道解密密码的方法。
python passwords postgis qgis
1个回答
0
投票

因此,似乎您要对字符串(id)执行以下操作:

# get the config id as a string
auth_method_id = self.parameterAsString(
        parameters,
        self.AUTH_CONFIG,
        context
    )

# get the application's authenticaion manager
auth_mgr = QgsApplication.authManager()

# create an empty authmethodconfig object
auth_cfg = QgsAuthMethodConfig()

# load config from manager to the new config instance and decrypt sensitive data
auth_mgr.loadAuthenticationConfig(auth_method_id, auth_cfg, True)

# get the configuration information (including username and password)
auth_cfg.configMap()

我从文档的不同地方得到了此:

https://qgis.org/pyqgis/master/core/QgsAuthManager.html

https://qgis.org/pyqgis/master/core/QgsAuthMethodConfig.html

https://qgis.org/pyqgis/master/core/QgsProcessingParameterAuthConfig.html

© www.soinside.com 2019 - 2024. All rights reserved.