Skip to content

Commit

Permalink
feat(pydantic): Rework Bitwarden Client with pydantic classes + Usage
Browse files Browse the repository at this point in the history
  • Loading branch information
Lowaiz committed Feb 1, 2024
1 parent 2768fc4 commit c8fb56a
Show file tree
Hide file tree
Showing 15 changed files with 1,384 additions and 800 deletions.
70 changes: 62 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,74 @@ Contributions welcomed!
There are 2 types of clients:

- One for the vaultwarden admin API, that needs to be authenticated with an admin token.
- One for the bitwarden API, that needs to be authenticated with the user api keys or user's mail and password.
- One for the bitwarden API, that needs to be authenticated with the user api keys or user's mail and password. An Owner or Admin user is required to perform admin operations.

The `reset_account` and `transfer_account_rights` from the Admin client needs a valid Bitwarden client to re-invite the
target user.

## Usage

### Admin client

```python
from vaultwarden.clients.vaultwarden import VaultwardenAdminClient

client = VaultwardenAdminClient(url="https://vaultwarden.example.com", admin_secret_token="admin_token")

client.invite("john.doe@example.com")

all_users = client.get_all_users()

client.delete(all_users[0].id)

```

### Bitwarden client

```python
from vaultwarden.clients.bitwarden import BitwardenAPIClient
from vaultwarden.models.bitwarden import Organization, OrganizationCollection, get_organization

bitwarden_client = BitwardenAPIClient(url="https://vaultwarden.example.com", email="admin@example", password="admin_password", client_id="client_id", client_secret="client_secret")

org_uuid = "550e8400-e29b-41d4-a716-446655440000"

orga= get_organization(bitwarden_client, org_uuid)

collection_id_list = ["666e8400-e29b-41d4-a716-446655440000", "888e8400-e29b-41d4-a716-446655440000", "770e8400-e29b-41d4-a716-446655440000" ]
orga.invite(email="new@example.com", collections=collection_id_list, default_readonly=True, default_hide_passwords=True)
org_users = orga.users()
org_collections: list[OrganizationCollection] = orga.collections()
org_collections_by_name: dict[str: OrganizationCollection] = orga.collections(as_dict=True)
new_coll = orga.create_collection("new_collection")
orga.delete_collection(new_coll.Id)

my_coll = orga.collection("my_collection")
if new_coll:
users_coll = my_coll.users()

my_coll_2 = org_collections_by_name["my_coll_2"]

my_user = orga.users(search="john.doe@example.com")
if my_user:
my_user = my_user[0]
print(my_user.Collections)
my_user.add_collections([my_coll_2.Id])

```

## TODO
- [ ] Add tests form Vaultwarden admin client
- [ ] Rewrite crypto part to remove dependency on bitwardentools and add argon2id support
- [ ] Support email + password authentication
- [ ] Support end user operations
- [ ] Ciphers management support
- [ ] Many other things I didn't think of yet


## Credits

The cryptographic part is handled by the [bitwardentools library](https://github.com/corpusops/bitwardentools).
The [crypto part](src/vaultwarden/utils/crypto.py) originates from [bitwardentools](https://github.com/corpusops/bitwardentools).


<!-- Badges -->
Expand All @@ -41,12 +101,6 @@ The cryptographic part is handled by the [bitwardentools library](https://github
[GHAction-link]: https://github.com/numberly/python-vaultwarden/actions?query=event%3Apush+branch%3Amain
<!-- Links -->

[Issue]: https://github.com/numberly/python-vaultwarden/issues

[Discussions]: https://github.com/numberly/python-vaultwarden/discussions

[PyPA Code of Conduct]: https://www.pypa.io/en/latest/code-of-conduct/

## License

Python-vaultwarden is distributed under the terms of the [Apache-2.0](https://spdx.org/licenses/Apache-2.0.html) license.
7 changes: 6 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ classifiers = [
]
requires-python = ">=3.10"
dependencies = [
"bitwardentools >=1.0.55",
"hkdf >=0.0.3",
"pycryptodome >=3.17.0",
"pydantic >=2.5.0",
"httpx >=0.24.1",
]

Expand Down Expand Up @@ -108,8 +110,10 @@ format = [
[tool.ruff]
# Add "Q" to the list of enabled codes.
select = ["B", "E", "F", "I", "N", "Q", "RUF", "SIM", "TCH"]
ignore = ["N815"]
fixable = ["ALL"]
src = ["src/vaultwarden", "tests"]
exclude = ["src/vaultwarden/utils/crypto.py"]
target-version = "py310"
line-length = 79

Expand Down Expand Up @@ -141,5 +145,6 @@ version = "0.7.0"
tag_format = "$version"
update_changelog_on_bump = true
version_files = [
"pyproject.toml",
"src/vaultwarden/__version__.py",
]

0 comments on commit c8fb56a

Please sign in to comment.