Skip to main content

A click option decorator to store and retrieve credentials using keyring.

Project description

click-keyring

click-keyring provides a customized click password option decorator to store and retrieve credentials using keyring.

When a command is decorated with click-keyring:

  • click-keyring generates a keyring service name using the command name by default (this can be customized).
  • click-keyring uses the service name to look up an existing password using keyring.
  • If an existing password is found, it is used as the param value.
  • If not found, the user is prompted to enter a password.
  • The new password is then saved to the keyring backend.

Installation

pip install click-keyring

Example

See the examples folder for additional examples.

import click
from click_keyring import keyring_option


@keyring_option('-p', '--password')
@click.option('-u', '--username', prompt='Username')
@click.command()
def simple_cmd(username, password):
    """
    Example of click_keyring using defaults.

    The password will be saved to keyring with service name
    matching the click command name (in this case "simple_cmd").

    This can be overridden by setting `prefix` and/or `keyring_option`
     on the decorator.
    """
    click.echo()
    click.echo('** Command Params. User: {}, Password: {}'.format(username, password))


if __name__ == '__main__':
    simple_cmd()

When executed the first time, both username and password will be prompted.

~/g/c/examples python ./simple.py
Username: testuser
Password:

** Command Params. User: testuser, Password: testpw
~/g/c/examples

Subsequent executions using the same username will retrieve the password from the keyring backend.

~/g/c/examples python ./simple.py
Username: testuser

** Command Params. User: testuser, Password: testpw
~/g/c/examples

Service Names

By default, the value of the click.Command.name attribute is used as the service name.
The name is based on the function name or, if provided, the name argument on the @click.command decorator.

@keyring_option('-p', '--password')
@click.option('-u', '--username', prompt='Username')
@click.command()
def simple_cmd(username, password):
    # service name will be the value of `simple_cmd.name`
    # This will likely be "simple-cmd" as click replaces underscores with hyphens.
    pass

A custom service name can be specified using the prefix argument.

@keyring_option('-p', '--password', prefix='customnservice')
@click.option('-u', '--username', prompt='Username')
@click.command()
def simple_cmd(username, password):
    # service name will be "customnservice"
    pass

Other options on the command can be included in the service name using the other_options argument. This is an iterable of option names. The values provided for those options is appended to the service name.

@keyring_option('-p', '--password', prefix='customnservice', other_options=('hostname',))
@click.option('-n', '--hostname')
@click.option('-u', '--username', prompt='Username')
@click.command()
def simple_cmd(username, hostname, password):
    # service name will be "customnservice_[value provided for hostname]"
    pass

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

click-keyring-0.2.1.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

click_keyring-0.2.1-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file click-keyring-0.2.1.tar.gz.

File metadata

  • Download URL: click-keyring-0.2.1.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/3.7.4 Darwin/19.6.0

File hashes

Hashes for click-keyring-0.2.1.tar.gz
Algorithm Hash digest
SHA256 a4bf3f75b13db9e9b1dee22a3c3d420db8ff3d998b94ea3bdea7b3a0bfe72e1f
MD5 3c4f127128e1481e21d5021b1ac2ac3b
BLAKE2b-256 88abe0ef37852d65fb32b8b9a05e5bcdd55fca2f43fa26529b23d1081a188898

See more details on using hashes here.

File details

Details for the file click_keyring-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: click_keyring-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 4.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/3.7.4 Darwin/19.6.0

File hashes

Hashes for click_keyring-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1f9b02fec7213a3f7477c32d4b545f711d586ce907e382b3c1251b21e8eeeaa7
MD5 20679d7b4d8c9ef15cd46478b4f0310a
BLAKE2b-256 e26a0228525a2b23847989120cb5ce57048215097a1b9f046482738a0a1aae9a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page