Search code examples
pythonpython-keyring

get keyring password with blank username


I have existing windows credentials in the credential manager with a blank username. It doesn't appear that the keyring package can read these:

import keyring
keyring.set_password("test", "", "mypassword")
keyring.get_password("test", "")  # returns None

Running the same commands with a username works fine. I can view the created credential in the windows credential manager utility. It's there. I just can't read it via the keyring library. Is there any workaround for this or am I doing something wrong (besides having creds with no username)? Thanks


Solution

  • I can use your code "as is" and still retrieve the information:

    >>> import keyring
    >>> keyring.set_password("test", "", "mypassword")
    >>> keyring.get_password("test", "")
    'mypassword'
    

    However, this is not the only way to "retrieve" the credentials. Using the keyring.get_credential method, we can get both username and password in one command:

    >>> x = keyring.get_credential(service_name="test", username=None)
    >>> x.username
    ''
    >>> x.password
    'mypassword'
    

    You have to specify a username in the call, but you can just pass None to it, and it will fetch the first one it matches.