Skip to main content

access the Bitwarden/Vaultwarden CLI ("bw") conveniently

Project description

bwinterface

A Python wrapper around the "bw" (Bitwarden) CLI tool

The "bw" command line utility is used for accessing Bitwarden vaults or Vaultwarden vaults. At time of writing, there is no stable official module for accessing such vaults using Python. Therefore this module provides a low-level Python wrapper around the "bw" CLI utility. A bunch of higher-level methods and facilities are provided for increased convenience, too.


Features

  • Provide a low-level wrapper around the "bw" CLI utility.
  • Parse the "bw" tool output into Python data structures.
  • Provide certain results in more convenient data structures.
  • Caches certain results for increased performance in practical applications.
  • Provide convenience methods to access the "bw" functionality in a simple manner.
  • No other external libraries are needed, i.e. no dependencies except the "bw" utility.

Installation

Install using PyPi:

pip3 install bwinterface

Quickstart

Accessing the bw CLI using bwinterface

The following code snippets provide examples on how bwinterface is used:

import bwinterface

# Instantiate a wrapper object while providing the path to the "bw" CLI
bw = bwinterface.BWInterface(bw_cli='/opt/bw')

# Execute any bw command using "execute"
result = bw.execute('--help')
print(result)

# Login using API key
if not bw.check_login():
    print(bw.set_config_server('https://vaultwarden.mydomain.local'))
    result = bw.login_apikey(clientid='user.aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee', clientsecret='ffffffffffffffffffffffffffffff')
    print(result)

# Unlock the local vault
result = bw.unlock('MyMasterPassword')
print(result)

# There are useful properties
print(bw.organizations_asdictbyid)
print(bw.organizations_asdictbyname)

# There are get methods with filtering capabilities. Examples:
print(bw.get_collections('aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee').data)
print(bw.get_collections('MyCollection').data)
print(bw.get_collections_asdictbyid('MyCollection'))
print(bw.get_collections_asdictbyname('MyCollection'))
print(bw.get_collectionid('MyCollection'))
print(bw.get_item('aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee'))
print(bw.get_item('"MyItem"'))
print(bw.get_item('aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee').data.get('notes'))
print(bw.get_item_notes('aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee'))
print(bw.get_items_asdictbyid(organization=''))  # default organization
print(bw.get_items_asdictbyname(organization='MyOrganization'))

# You may create, change and delete objects. Examples:
print(bw.create_item('MyNewItem', 'myuser', 'mypassword', notes='This is a note'))
print(bw.edit_item('aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee', password='MyNewPassword'))
print(bw.delete_item('aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee', permanent=True))

# And there are many other convenience methods. Examples:
print(bw.sync())
print(bw.get_status())
print(bw.generate_password(uppercase=True, lowercase=True, number=True, special=True, passphrase=None, length=20, words=None, min_number=None, min_special=None, separator=None, capitalize=None, include_number=None, avoid_ambiguous=True))

Please see the section below for full API documentation.


Detailed API description

Properties

organizations

Returns a cached list of organizations as returned by "bw"

organizations_asdictbyid

Returns a dictionary of (cached) organizations with organization identifiers as keys

Notes:

  • This just differently formats the data provided by the organizationproperty.

organizations_asdictbyname

Returns a dictionary of (cached) organizations with organization names as keys

Notes:

  • This just differently formats the data provided by the organizationproperty.

Methods for interaction (in alphabetical order)

__init__(bw_cli='./bw', show_bwcommands=True, sparse_output=False, suppressoutput=True, suppresserrors=False)

Initializes the instance

Parameters:

  • "bw_cli" (str, optional, default: "/opt/bitwarden/bw"): Path to the "bw" command line utility The bwinterface module depends on the "bw" CLI utility for its operation. Provide the full path to the tool that shall be used.
  • "show_bwcommands" (boolean, optional, default: True): Echo "bw" commands If True, each "bw" command executed is printed to stdout to show what is going on.
  • "sparse_output" (boolean, optional, default: False): Reduce amount of output If True, "--raw" output of "bw" ise provided only.
  • "suppressoutput" (boolean, optional, default: True): Don't show output of "bw" utility If True, the full output to stdout of the "bw" utility is printed.
  • "suppresserrors" (boolean, optional, default: False): Don't show error output of "bw" utility If True, the full output to stderr of the "bw" utility is printed.

Examples:

  • bw = bwinterface.BWInterface(bw_cli='/opt/bw')
  • bw = bwinterface.BWInterface(bw_cli='/opt/bw', suppressoutput=False)

check_login()

Checks whether we are logged in

create_collection(name, organization=None, external_id=None, otherfields=None)

Create a collection with the given data

create_item(name, username, password, organization=None, collection=None, folder=None, totp=None, uris=None, type=1, notes=None, favorite=False, fields=None, otherfields=None)

Create an item with the given data

delete_collection(collection, organization, permanent=False)

Delete collection with the provided identifier ('permanent' does not use trash)

delete_item(itemid, permanent=False)

Delete item with the provided identifier ('permanent' does not use trash)

dict2base64(datadict)

Encodes a dictionary into a base64-encoded JSON notation

edit_item(itemid, name=None, username=None, password=None, organization=None, collection=None, folder=None, totp=None, uris=None, type=None, notes=None, favorite=None, fields=None, otherfields=None, create_if_not_exists=False, use_cache=True)

Update an item with the given data

execute(command, env=None, datadict=None, nojson=False, sparse_output=None, pretty=None)

Execute a bw command and return result

generate(uppercase=None, lowercase=None, number=None, special=None, passphrase=None, length=None, words=None, min_number=None, min_special=None, separator=None, capitalize=None, include_number=None, avoid_ambiguous=None)

Generates a new password/passphrase

generate_password(uppercase=None, lowercase=None, number=None, special=None, passphrase=None, length=None, words=None, min_number=None, min_special=None, separator=None, capitalize=None, include_number=None, avoid_ambiguous=None)

Returns a new password/passphrase

get_collectionid(collection, organization=None, use_cache=True)

Converts a string identifying a collection into the collection's UUID

get_collections(organization=None)

Gets a list of collections, optionally filtered

get_collections_aslist(organization=None)

Gets a list of collections, optionally filtered; always returns a list (empty list on invalid filters)

get_collections_asdictbyid(organization=None, use_cache=True)

Dictionary of collections with identifiers as keys, optionally filtered

get_collections_asdictbyname(organization=None, use_cache=True)

Dictionary of collections with names as keys, optionally filtered

get_folderid(folder)

Converts a string identifying a folder into the folder's UUID

get_item(itemid)

Get item with the provided identifier

Note: it is possible to enter a search term (use double quotes) instead of an item id

get_item_notes(itemid)

Get notes of item with the provided identifier (result is provided in 'out')

get_items(organization=None, collection=None, folder=None)

Gets a list of items, optionally filtered

get_items_asdict(organization=None, collection=None, folder=None, byname=False)

Dictionary of items, uncached, optionally filtered

get_items_asdictbyid(organization=None, collection=None, folder=None, use_cache=True)

Dictionary of items with identifiers as keys, optionally filtered

get_items_asdictbyname(organization=None, collection=None, folder=None, use_cache=True)

Dictionary of items with names as keys, optionally filtered

get_items_aslist(organization=None, collection=None, folder=None)

Gets a list of items, optionally filtered; always returns a list (empty list on invalid filters)

get_organizationid(organization)

Converts a string identifying an organization into the organization's UUID

get_organizations(self)

Gets a list of organizations

get_status(self)

Gets status information

invalidate_collection_cache()

Clears the collection cache

invalidate_item_cache()

Clears the item cache

invalidate_organization_cache()

Clears the organization cache

is_uuid(s)

Checks whether the given string is a valid UUID

login_apikey(clientid, clientsecret)

Logs in using the provided API credentials

logout(self)

Logout from vault

run_process(command, env=None)

Execute a command and return result

set_config_server(server)

Configures the server to use

sync(self)

Gets updates from the remote vault

unlock(pwd)

Unlocks the vault with the provided password

update_collection_cache(data, organization)

Updates the collection cache with the new or updated collection in case organization matches

update_item_cache(data, organization)

Updates the item cache with the new or updated item in case organization matches


Reporting bugs

In case you encounter any bugs, please report the expected behavior and the actual behavior so that the issue can be reproduced and fixed.


Developers

Clone repository

Clone this repo to your local machine using https://github.com/towalink/bwinterface.git

Install the module temporarily to make it available in your Python installation:

pip3 install -e <path to root of "src" directory>

License

License

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

bwinterface-0.1.0.tar.gz (12.2 kB view details)

Uploaded Source

Built Distribution

bwinterface-0.1.0-py3-none-any.whl (10.3 kB view details)

Uploaded Python 3

File details

Details for the file bwinterface-0.1.0.tar.gz.

File metadata

  • Download URL: bwinterface-0.1.0.tar.gz
  • Upload date:
  • Size: 12.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for bwinterface-0.1.0.tar.gz
Algorithm Hash digest
SHA256 fda357f6f75cbd5b03308d94485a4040e47b637950108dd6aeb3cd8db02cf6b5
MD5 c6731c436632682f0cfe6befdcb62619
BLAKE2b-256 be2dab1e9527397e818b4e1181800ca3cb5acef04fa650de7a0c3cc599a47ec2

See more details on using hashes here.

File details

Details for the file bwinterface-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: bwinterface-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 10.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for bwinterface-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 acf5e2acaa91fe46492da3181b34f39407a5429334c73a96ef75fde85943689e
MD5 d891136679b5a62a5ae50be67d06905a
BLAKE2b-256 fc1b9fe1145ada9d69b17e6013535db57053da987d240e8f50a7cb8b7c16edf4

See more details on using hashes here.

Supported by

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