如何使用PyQGIS在authmanager中获取加密密码?

2024-04-25 22:36:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在制作一个QGIS插件,其中我请求PostgreSQL连接的authentication对象(使用QgsProcessingParameterAuthConfig)(它已经在用户的连接列表中设置)。 我的目标是使用PyQGIS获取登录名和密码,并使用它们连接psycopg2。你知道吗

谢谢你!你知道吗

asked参数QgsProcessingParameterAuthConfig返回一个带有身份验证对象标识键的字符串。你知道吗

  • 我可以用这个键获取QgsAuthMethodConfig对象,但是 密码为空。你知道吗
  • 我没有找到一个方法来访问密码,也没有其他插件这样做。你知道吗
  • 可以知道保存密码的SQLite数据库,但是密码是加密的,我不知道解密它们的方法。你知道吗

Tags: 对象方法用户插件密码目标列表authentication
1条回答
网友
1楼 · 发布于 2024-04-25 22:36:52

因此,您似乎对字符串(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

相关问题 更多 >