I found the solution after observing the QGIS Plugin "Discovery" from Lutraconsulting. See here.
The code is :
def get_postgres_conn_info(selected):
""" Read PostgreSQL connection details from QSettings stored by QGIS
"""
settings = QSettings()
settings.beginGroup(u"/PostgreSQL/connections/" + selected)
if not settings.contains("database"): # non-existent entry?
return {}
conn_info = dict()
conn_info["host"] = settings.value("host", "", type=str)
# password and username
username = ''
password = ''
authconf = settings.value('authcfg', '')
if authconf :
# password encrypted in AuthManager
auth_manager = QgsApplication.authManager()
conf = QgsAuthMethodConfig()
auth_manager.loadAuthenticationConfig(authconf, conf, True)
if conf.id():
username = conf.config('username', '')
password = conf.config('password', '')
else:
# basic (plain-text) settings
username = settings.value('username', '', type=str)
password = settings.value('password', '', type=str)
return username, password
###############
myname, mypass = get_postgres_conn_info("your_connection_name")
print(myname)
And as one can see they test for the two password storing possibilities and retrieve the login and password from the one that is used, after searching inside the QSettings
.