I have a Mercurial keyring on my Windows 7 machine. I am using the Python keyring
library to get user credentials from the Mercurial keyring.
I can retrieve the password for a given username with:
keyring.get_password('Mercurial', 'user@@etc')
Is there a similar function to retrieve the username?
While keyring
was only designed to store passwords, you can abuse get_password
to store the username separately.
import keyring
# store username & password
keyring.set_password("name_of_app", "username", "user123")
keyring.set_password("name_of_app", "password", "pass123")
# retrieve username & password
username = keyring.get_password("name_of_app", "username")
password = keyring.get_password("name_of_app", "password")
Alternatively, if you want to keep the username paired with the password:
import keyring
service_id = "name_of_app"
username = "user123"
# store username & password
keyring.set_password(service_id, "username", username)
keyring.set_password(service_id, username, "pass123")
# retrieve username & password
username = keyring.get_password(service_id, "username")
password = keyring.get_password(service_id, username)
Credit to Dustin Wyatt & Alex Chan for this solution.