• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

docs/H24-Mar-2019-6744

keyring/H24-Mar-2019-2,3711,792

keyring.egg-info/H03-May-2022-414310

tests/H24-Mar-2019-1613

.flake8H A D24-Mar-2019178 76

.gitignoreH A D24-Mar-2019737 6048

.readthedocs.ymlH A D24-Mar-201974 65

.travis-macosH A D24-Mar-2019164 54

.travis.ymlH A D24-Mar-2019931 4134

CHANGES.rstH A D24-Mar-201929.4 KiB1,084782

LICENSEH A D24-Mar-20191 KiB84

PKG-INFOH A D24-Mar-201917.7 KiB414310

README.rstH A D24-Mar-201913.8 KiB388285

appveyor.ymlH A D24-Mar-2019454 2517

conftest.pyH A D24-Mar-2019213 117

hook-keyring.backend.pyH A D24-Mar-2019168 105

pyproject.tomlH A D24-Mar-2019120 43

pytest.iniH A D24-Mar-2019456 1211

setup.cfgH A D24-Mar-20191.6 KiB6457

setup.pyH A D24-Mar-2019112 73

skeleton.mdH A D24-Mar-20197.8 KiB12982

tox.iniH A D24-Mar-2019553 3228

README.rst

1.. image:: https://img.shields.io/pypi/v/keyring.svg
2   :target: https://pypi.org/project/keyring
3
4.. image:: https://img.shields.io/pypi/pyversions/keyring.svg
5
6.. image:: https://img.shields.io/travis/jaraco/keyring/master.svg
7   :target: https://travis-ci.org/jaraco/keyring
8
9.. image:: https://img.shields.io/appveyor/ci/jaraco/keyring/master.svg
10   :target: https://ci.appveyor.com/project/jaraco/keyring/branch/master
11
12.. image:: https://readthedocs.org/projects/keyring/badge/?version=latest
13   :target: https://keyring.readthedocs.io/en/latest/?badge=latest
14
15.. image:: https://tidelift.com/badges/github/jaraco/keyring
16   :target: https://tidelift.com/subscription/pkg/pypi-keyring?utm_source=pypi-keyring&utm_medium=readme
17
18=======================================
19Installing and Using Python Keyring Lib
20=======================================
21
22.. contents:: **Table of Contents**
23
24---------------------------
25What is Python keyring lib?
26---------------------------
27
28The Python keyring lib provides an easy way to access the system keyring service
29from python. It can be used in any application that needs safe password storage.
30
31The keyring library is licensed under both the `MIT license
32<http://opensource.org/licenses/MIT>`_ and the PSF license.
33
34These recommended keyring backends are supported by the Python keyring lib:
35
36* macOS `Keychain
37  <https://en.wikipedia.org/wiki/Keychain_%28software%29>`_
38* Freedesktop `Secret Service
39  <http://standards.freedesktop.org/secret-service/>`_ supports many DE including
40  GNOME (requires `secretstorage <https://pypi.python.org/pypi/secretstorage>`_)
41* KDE4 & KDE5 `KWallet <https://en.wikipedia.org/wiki/KWallet>`_
42  (requires `dbus <https://pypi.python.org/pypi/dbus-python>`_)
43* `Windows Credential Locker
44  <https://docs.microsoft.com/en-us/windows/uwp/security/credential-locker>`_
45
46Other keyring implementations are available through `Third-Party Backends`_.
47
48-------------------------
49Installation Instructions
50-------------------------
51
52Install from Index
53==================
54
55Install using your favorite installer. For example:
56
57    $ pip install keyring
58
59Linux
60-----
61
62On Linux, the KWallet backend relies on dbus-python_, which does not always
63install correctly when using pip (compilation is needed). So we recommend
64that dbus-python is installed as a system package. The same also applies to
65the Secret Storage backend under Python 2 (under Python 3 a different D-Bus
66implementation is used).
67
68.. _dbus-python: https://gitlab.freedesktop.org/dbus/dbus-python
69
70-------------
71Using Keyring
72-------------
73
74The basic usage of keyring is pretty simple: just call `keyring.set_password`
75and `keyring.get_password`:
76
77    >>> import keyring
78    >>> keyring.set_password("system", "username", "password")
79    >>> keyring.get_password("system", "username")
80    'password'
81
82Command-line Utility
83====================
84
85Keyring supplies a ``keyring`` command which is installed with the
86package. After installing keyring in most environments, the
87command should be available for setting, getting, and deleting
88passwords. For more information on usage, invoke with no arguments
89or with ``--help`` as so::
90
91    $ keyring --help
92    $ keyring set system username
93    Password for 'username' in 'system':
94    $ keyring get system username
95    password
96
97The command-line functionality is also exposed as an executable
98package, suitable for invoking from Python like so::
99
100    $ python -m keyring --help
101    $ python -m keyring set system username
102    Password for 'username' in 'system':
103    $ python -m keyring get system username
104    password
105
106--------------------------
107Configure your keyring lib
108--------------------------
109
110The python keyring lib contains implementations for several backends. The
111library will
112automatically choose the keyring that is most suitable for your current
113environment. You can also specify the keyring you like to be used in the
114config file or by calling the ``set_keyring()`` function.
115
116Customize your keyring by config file
117=====================================
118
119This section describes how to change your option in the config file.
120
121Config file path
122----------------
123
124The configuration of the lib is stored in a file named "keyringrc.cfg". This
125file must be found in a platform-specific location. To determine
126where the config file is stored, run the following::
127
128    python -c "import keyring.util.platform_; print(keyring.util.platform_.config_root())"
129
130Some keyrings also store the keyring data in the file system. To determine
131where the data files are stored, run this command::
132
133    python -c "import keyring.util.platform_; print(keyring.util.platform_.data_root())"
134
135
136Config file content
137-------------------
138
139To specify a keyring backend, set the **default-keyring** option to the
140full path of the class for that backend, such as
141``keyring.backends.OS_X.Keyring``.
142
143If **keyring-path** is indicated, keyring will add that path to the Python
144module search path before loading the backend.
145
146For example, this config might be used to load the
147``SimpleKeyring`` from the ``simplekeyring`` module in
148the ``./demo`` directory (not implemented)::
149
150    [backend]
151    default-keyring=simplekeyring.SimpleKeyring
152    keyring-path=demo
153
154Third-Party Backends
155====================
156
157In addition to the backends provided by the core keyring package for
158the most common and secure use cases, there
159are additional keyring backend implementations available for other
160use-cases. Simply install them to make them available:
161
162- `keyrings.cryptfile <https://pypi.org/project/keyrings.cryptfile>`_
163  - Encrypted text file storage.
164- `keyring_jeepney <https://pypi.org/project/keyring_jeepney>`__ - a
165  pure Python backend using the secret service DBus API for desktop
166  Linux.
167- `keyrings.alt <https://pypi.org/project/keyrings.alt>`_ - "alternate",
168  possibly-insecure backends, originally part of the core package, but
169  available for opt-in.
170- `gsheet-keyring <https://pypi.org/project/gsheet-keyring>`_
171  - a backend that stores secrets in a Google Sheet. For use with
172  `ipython-secrets <https://pypi.org/project/ipython-secrets>`_.
173- `bitwarden-keyring <https://pypi.org/project/bitwarden-keyring/0.1.0/>`_
174  - a backend that stores secrets in the `BitWarden <https://bitwarden.com/>`_
175  password manager.
176
177
178Write your own keyring backend
179==============================
180
181The interface for the backend is defined by ``keyring.backend.KeyringBackend``.
182Every backend should derive from that base class and define a ``priority``
183attribute and three functions: ``get_password()``, ``set_password()``, and
184``delete_password()``. The ``get_credential()`` function may be defined if
185desired.
186
187See the ``backend`` module for more detail on the interface of this class.
188
189Keyring employs entry points to allow any third-party package to implement
190backends without any modification to the keyring itself. Those interested in
191creating new backends are encouraged to create new, third-party packages
192in the ``keyrings`` namespace, in a manner modeled by the `keyrings.alt
193package <https://github.com/jaraco/keyrings.alt>`_. See the ``setup.py`` file
194in that project for a hint on how to create the requisite entry points.
195Backends that prove essential may be considered for inclusion in the core
196library, although the ease of installing these third-party packages should
197mean that extensions may be readily available.
198
199If you've created an extension for Keyring, please submit a pull request to
200have your extension mentioned as an available extension.
201
202Set the keyring in runtime
203==========================
204
205Keyring additionally allows programmatic configuration of the
206backend calling the api ``set_keyring()``. The indicated backend
207will subsequently be used to store and retrieve passwords.
208
209Here's an example demonstrating how to invoke ``set_keyring``::
210
211    # define a new keyring class which extends the KeyringBackend
212    import keyring.backend
213
214    class TestKeyring(keyring.backend.KeyringBackend):
215        """A test keyring which always outputs same password
216        """
217        priority = 1
218
219        def set_password(self, servicename, username, password):
220            pass
221
222        def get_password(self, servicename, username):
223            return "password from TestKeyring"
224
225        def delete_password(self, servicename, username, password):
226            pass
227
228    # set the keyring for keyring lib
229    keyring.set_keyring(TestKeyring())
230
231    # invoke the keyring lib
232    try:
233        keyring.set_password("demo-service", "tarek", "passexample")
234        print("password stored successfully")
235    except keyring.errors.PasswordSetError:
236        print("failed to store password")
237    print("password", keyring.get_password("demo-service", "tarek"))
238
239
240Using Keyring on Ubuntu 16.04
241=============================
242
243The following is a complete transcript for installing keyring in a
244virtual environment on Ubuntu 16.04.  No config file was used.::
245
246  $ sudo apt install python3-venv libdbus-glib-1-dev
247  $ cd /tmp
248  $ pyvenv py3
249  $ source py3/bin/activate
250  $ pip install -U pip
251  $ pip install secretstorage dbus-python
252  $ pip install keyring
253  $ python
254  >>> import keyring
255  >>> keyring.get_keyring()
256  <keyring.backends.SecretService.Keyring object at 0x7f9b9c971ba8>
257  >>> keyring.set_password("system", "username", "password")
258  >>> keyring.get_password("system", "username")
259  'password'
260
261
262Using Keyring on headless Linux systems
263=======================================
264
265It is possible to use the SecretService backend on Linux systems without
266X11 server available (only D-Bus is required). To do that, you need the
267following:
268
269* Install the `GNOME Keyring`_ daemon.
270* Start a D-Bus session, e.g. run ``dbus-run-session -- sh`` and run
271  the following commands inside that shell.
272* Run ``gnome-keyring-daemon`` with ``--unlock`` option. The description of
273  that option says:
274
275      Read a password from stdin, and use it to unlock the login keyring
276      or create it if the login keyring does not exist.
277
278  When that command is started, enter your password into stdin and
279  press Ctrl+D (end of data). After that the daemon will fork into
280  background (use ``--foreground`` option to prevent that).
281* Now you can use the SecretService backend of Keyring. Remember to
282  run your application in the same D-Bus session as the daemon.
283
284.. _GNOME Keyring: https://wiki.gnome.org/Projects/GnomeKeyring
285
286-----------------------------------------------
287Integrate the keyring lib with your application
288-----------------------------------------------
289
290API interface
291=============
292
293The keyring lib has a few functions:
294
295* ``get_keyring()``: Return the currently-loaded keyring implementation.
296* ``get_password(service, username)``: Returns the password stored in the
297  active keyring. If the password does not exist, it will return None.
298* ``get_credential(service, username)``: Return a credential object stored
299  in the active keyring. This object contains at least ``username`` and
300  ``password`` attributes for the specified service, where the returned
301  ``username`` may be different from the argument.
302* ``set_password(service, username, password)``: Store the password in the
303  keyring.
304* ``delete_password(service, username)``: Delete the password stored in
305  keyring. If the password does not exist, it will raise an exception.
306
307In all cases, the parameters (``service``, ``username``, ``password``)
308should be Unicode text. On Python 2, these parameters are accepted as
309simple ``str`` in the default encoding as they will be implicitly
310decoded to text. Some backends may accept ``bytes`` for these parameters,
311but such usage is discouraged.
312
313
314Exceptions
315==========
316
317The keyring lib raises following exceptions:
318
319* ``keyring.errors.KeyringError``: Base Error class for all exceptions in keyring lib.
320* ``keyring.errors.InitError``: Raised when the keyring can't be initialized.
321* ``keyring.errors.PasswordSetError``: Raise when password can't be set in the keyring.
322* ``keyring.errors.PasswordDeleteError``: Raised when the password can't be deleted in the keyring.
323
324------------
325Get involved
326------------
327
328Python keyring lib is an open community project and highly welcomes new
329contributors.
330
331* Repository: https://github.com/jaraco/keyring/
332* Bug Tracker: https://github.com/jaraco/keyring/issues/
333* Mailing list: http://groups.google.com/group/python-keyring
334
335Security Contact
336================
337
338If you wish to report a security vulnerability, the public disclosure
339of which may exacerbate the risk, please
340`Contact Tidelift security <https://tidelift.com/security>`_,
341which will coordinate the fix and disclosure privately.
342
343Making Releases
344===============
345
346This project makes use of automated releases via Travis-CI. The
347simple workflow is to tag a commit and push it to Github. If it
348passes tests on a late Python version, it will be automatically
349deployed to PyPI.
350
351Other things to consider when making a release:
352
353 - first ensure that tests pass (preferably on Windows and Linux)
354 - check that the changelog is current for the intended release
355
356Running Tests
357=============
358
359Tests are `continuously run <https://travis-ci.org/#!/jaraco/keyring>`_ using
360Travis-CI.
361
362To run the tests yourself, you'll want keyring installed to some environment
363in which it can be tested. Recommended technique is described below.
364
365Using tox
366---------
367
368Keyring prefers use of `tox <https://pypi.org/project/tox>`_ to run tests.
369Simply install and invoke ``tox``.
370
371This technique is the one used by the Travis-CI script.
372
373----------
374Background
375----------
376
377The project was based on Tarek Ziade's idea in `this post`_. Kang Zhang
378initially carried it out as a `Google Summer of Code`_ project, and Tarek
379mentored Kang on this project.
380
381.. _this post: http://tarekziade.wordpress.com/2009/03/27/pycon-hallway-session-1-a-keyring-library-for-python/
382.. _Google Summer of Code: http://socghop.appspot.com/
383
384
385.. image:: https://badges.gitter.im/jaraco/keyring.svg
386   :alt: Join the chat at https://gitter.im/jaraco/keyring
387   :target: https://gitter.im/jaraco/keyring?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge
388