1

I want to create a new authentication configuration and a new connection to the Oracle database programmatically. I successfully create both of them; however, in the end, the username and password are still saved as plain text rather than being stored using authcfg. What am I missing?

# https://docs.qgis.org/3.34/en/docs/pyqgis_developer_cookbook/authentication.html#populate-authdb-with-a-new-authentication-configuration-entry
auth_mgr = QgsApplication.authManager()
auth_config = QgsAuthMethodConfig()
auth_config.setName('myconfig')
auth_config.setMethod('Basic')
auth_config.setConfigMap({
    'username': login,
    'password': password
})
assert auth_config.isValid()
auth_mgr.storeAuthenticationConfig(auth_config)
auth_config_id = auth_config.id()
assert auth_config_id

# https://gis.stackexchange.com/a/461589/173206
uri = QgsDataSourceUri()
uri.setConnection('myhost', '1521', 'mydb', None, None, QgsDataSourceUri.SslPrefer, auth_config_id)
uri.setSchema('myschema')

conn_config = {
  "saveUsername": True,
  "savePassword": True,
  "estimatedMetadata": True,
  "metadataInDatabase": True,
  "onlyExistingTypes": False,
}

metadata = QgsProviderRegistry.instance().providerMetadata('oracle')
connection = metadata.createConnection(uri.uri(False), conn_config)  # tried both True and False inside uri()
connection: QgsAbstractDatabaseProviderConnection
connection.store('My Connection')
iface.browserModel().reload()

Then I drag and drop a layer from this connection to the map. In the end, the source of the layer from this connection looks like this:

iface.activeLayer().source()
'dbname=\'mydb\' host=myhost port=1521 user=\'username\' password=\'topsecret\' srid=4326 type=Polygon table="myschema"."mylayer" (SHAPE)'

I want it to look like this (this is from a connection created using the GUI):

iface.activeLayer().source()
'dbname=\'mydb\' host=myhost port=1521 authcfg=vnh3ijp estimatedmetadata=true srid=4326 type=Polygon table="myschema"."mylayer" (SHAPE)'

The problem is that properties of the connection created by this code looks like this:

db connection properties

There is no authentication method selected despite creating it - but it appears in the list when expanded, and it's also visible in Settings -> Options -> Authentication. Also, why is there no schema set, despite using uri.setSchema('myschema')?

7
  • The default value of the expandAuthConfig parameter in the uri() method is True, so try connection = metadata.createConnection(uri.uri(False), conn_config). See docs.qgis.org/3.40/en/docs/pyqgis_developer_cookbook/…, where it says "The False argument passed to uri.uri(False) prevents the expansion of the authentication configuration parameters, if you are not using any authentication configuration this argument does not make any difference". Commented Oct 16 at 13:34
  • Unfortunately passing False argument to the uri() method didn't help. Commented Oct 17 at 6:29
  • Does it produce the same output? Commented Oct 17 at 9:31
  • Yes, the output is the same. The source() of the layer consists of the username and password as plain text. The code runs without any errors. Commented Oct 17 at 9:43
  • Since QGIS 3.34, there is also another method called publicSource(), that could theoretically help Commented Oct 17 at 9:52

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.