# -*-mode: doctest; tab-width: 0; py-indent-offset: 4; coding: utf-8-unix -*-

== Local Keyrings ==

This is a Python doctest file that is executable documentation.

    >>> import keyring

=== Keyring Binary ===

/bin/keyctl

    >>> import os
    >>> os.system('/bin/keyctl --version') #doctest: +ELLIPSIS
    0
    >>> os.system('file /bin/keyctl') #doctest: +ELLIPSIS
    0
    
=== Keyring Config ===

    >>> import keyring.util.platform_

Was .../.local/share/python_keyring

    >>> print(keyring.util.platform_.config_root()) #doctest: +ELLIPSIS
    /.../.config/python_keyring

The config file is keyringrc.cfg

=== Kernel Keyring ===

    >>> from keyutils.keys import Key, Keyring, session_keyring
    >>> Keyring.join_session_keyring() #doctest: +ELLIPSIS
    <keyutils.keys.Keyring object at ...>
    
    >>> key = Key.create_or_update('user', 'some description', 'my payload', session_keyring)
    >>> key.serial >= 0
    True

    >>> b'my payload' == key.payload()
    True

    >>> from keyring.core import load_keyring
    >>> keyring = load_keyring('keyutils.backend.KeyutilsKeyringBackend')
    >>> keyring.set_password('service X', 'username foo', 'password bar')
    >>> keyring.get_password('service X', 'username foo')
    'password bar'
    
    >>> keyring.set_password('some service', 'foobar', 'secret')
    >>> keyring.get_password('some service', 'foobar')
    'secret'
     
    >>> keyring.delete_password('service X', 'username foo')

https://rtfm.co.ua/en/what-is-linux-keyring-gnome-keyring-secret-service-and-d-bus/
Let's see which keyrings are available for my current session:
    >>> from sh import keyctl
    >>> keyctl('show', '@s') #doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
    Keyring
      ... --alswrv ... keyring: _ses
      ... --alswrv ...  \_ user: some description
      ... --alswrv ...  \_ keyring: python-keyring-keyutils
      ... --alswrv ...      \_ user: some service:foobar
    <BLANKLINE>

Also, in the /proc/key-users you can get information about all
keyrings and their statistics by each user:
    >>> from sh import cat
    >>> cat('/proc/key-users') #doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
    0:    ...
    ...
    <BLANKLINE>

Or for my user:
    >>> keyctl('show', '@u') #doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
    Keyring
    ... --alswrv ...  keyring: _uid...

Or for my user session:
    >>> keyctl('show', '@us') #doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
    Keyring
    ... --alswrv ...  keyring: _uid...

=== Gnome Keyring ===

Check D-Bus services - look for the org.freedesktop.secrets:

    >>> from sh import dbus_send
    >>> dbus_send('--session', '--dest=org.freedesktop.DBus', '--type=method_call', '--print-reply', '/org/freedesktop/DBus', 'org.freedesktop.DBus.ListNames') #doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
    method return ...
    ...
    string "org.freedesktop.secrets"
    ...
    <BLANKLINE>

FixMe: maybe gnome-keyring is not installed and org.freedesktop.DBus is in the
answer instead.
