如何使用 PyQGIS 在 auth-manager 中获取加密密码?

Byg*_*yga 4 python passwords postgis qgis

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

被询问的参数 QgsProcessingParameterAuthConfig 返回一个字符串,其中包含身份验证对象的标识密钥。

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

小智 7

因此,您似乎对字符串 (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()
Run Code Online (Sandbox Code Playgroud)

我从文档的各个地方得到了这个:

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

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

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