python-vaultwarden2 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,130 @@
1
+ """The public Vaultwarden client."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import functools
6
+ from typing import TYPE_CHECKING
7
+
8
+ from vaultwarden2 import _offload, dump
9
+
10
+ if TYPE_CHECKING:
11
+ import os
12
+
13
+ from vaultwarden2.dump import Argon2Params, Compressor
14
+ from vaultwarden2.models import OrganizationRef
15
+
16
+ from . import organizations as _organizations_mod
17
+
18
+
19
+ class AsyncVaultwardenClient(_organizations_mod.AsyncOrganizationsMixin):
20
+ """Client for a Vaultwarden server: everything a user can do in the web vault.
21
+
22
+ All encryption happens on this machine; the server only sees ciphertext and the login hash. Create the client,
23
+ then log in with one of the two methods; both also unlock the vault, so decrypted items are available right away:
24
+
25
+ * [login_password][vaultwarden2.AsyncVaultwardenClient.login_password]``(email, password)``: email and master
26
+ password. Accounts with two-step login raise [TwoFactorRequiredError][vaultwarden2.TwoFactorRequiredError];
27
+ call again with ``two_factor_token=<code>``.
28
+ * [login_api_key][vaultwarden2.AsyncVaultwardenClient.login_api_key]``(client_id, client_secret, password)``:
29
+ the personal API key (from the web vault, or
30
+ [get_api_key][vaultwarden2.AsyncVaultwardenClient.get_api_key]) plus the master password, which is still
31
+ needed to decrypt the vault. Suited for automation.
32
+
33
+ Use the client as a context manager, or call ``close()`` when done.
34
+
35
+ Args:
36
+ base_url: Address of the server, for example ``https://vault.example.org``. Plain ``http://`` to anything
37
+ but localhost triggers an [InsecureTransportWarning][vaultwarden2.InsecureTransportWarning].
38
+ timeout: Timeout for every HTTP request, in seconds.
39
+ verify: Verify the server's TLS certificate. Only disable for local testing.
40
+ device: How this client appears in the account's device list. Pass the same
41
+ [Device][vaultwarden2.Device] on every run so repeated runs count as one device and do not trigger
42
+ "new device" notifications. Defaults to a new random device.
43
+ http_client: A preconfigured httpx2 client (proxies, custom transports). Its ``base_url`` must point at the
44
+ server; it is closed together with this client.
45
+
46
+ Examples:
47
+ Email and master password, with the two-step login fallback:
48
+
49
+ ```python
50
+ import os
51
+ from vaultwarden2 import AsyncVaultwardenClient, TwoFactorRequiredError
52
+
53
+ async with AsyncVaultwardenClient('https://vault.example.org') as vw:
54
+ try:
55
+ await vw.login_password('me@example.org', os.environ['VW_PASSWORD'])
56
+ except TwoFactorRequiredError:
57
+ await vw.login_password('me@example.org', os.environ['VW_PASSWORD'], two_factor_token=input('code: '))
58
+ for item in await vw.search('github'):
59
+ print(item.name, item.login.username if item.login else '')
60
+ ```
61
+
62
+ Personal API key plus master password, for scripts and services (no two-step prompt):
63
+
64
+ ```python
65
+ import os
66
+ from vaultwarden2 import AsyncVaultwardenClient
67
+
68
+ async with AsyncVaultwardenClient('https://vault.example.org') as vw:
69
+ await vw.login_api_key(
70
+ os.environ['VW_CLIENT_ID'], # 'user.<uuid>'
71
+ os.environ['VW_CLIENT_SECRET'],
72
+ os.environ['VW_PASSWORD'], # still needed to decrypt the vault
73
+ )
74
+ item = await vw.create_login('Example', username='alice', password='s3cr3t', uris=['https://example.org'])
75
+ ```
76
+ """
77
+
78
+ async def dump_vault(
79
+ self,
80
+ path: str | os.PathLike[str],
81
+ passphrase: str,
82
+ *,
83
+ compressor: Compressor | None = None,
84
+ argon2: Argon2Params | None = None,
85
+ ) -> None:
86
+ """Write the personal vault to an encrypted, compressed backup file.
87
+
88
+ Contains the folders and personal items (archived ones included, items in the trash excluded) in the
89
+ JSON export format, compressed and encrypted with a key derived from ``passphrase``. Read it back with
90
+ [load_dump][vaultwarden2.dump.load_dump]. See [vaultwarden2.dump][] for the file format.
91
+
92
+ Args:
93
+ path: The file to create. It must not exist yet; it is created with mode ``0600``.
94
+ passphrase: The passphrase protecting the file. It is the only protection: use a long one.
95
+ compressor: Compression module or object with ``compress``/``decompress`` (``gzip``, ``bz2``, ``lzma``,
96
+ ...). Defaults to zstd when available, else lzma.
97
+ argon2: Cost of the passphrase key derivation. Defaults to RFC 9106 parameters (64 MiB, 3 passes).
98
+
99
+ Raises:
100
+ FileExistsError: ``path`` exists already.
101
+ CryptoError: The passphrase is empty.
102
+ """
103
+ vault = await self.sync(include_archived=True) # a backup must not drop archived items
104
+ await _offload.arun_cpu(functools.partial(dump.write_dump, compressor=compressor, argon2=argon2), vault, path, passphrase)
105
+
106
+ async def dump_organization(
107
+ self,
108
+ organization: OrganizationRef,
109
+ path: str | os.PathLike[str],
110
+ passphrase: str,
111
+ *,
112
+ compressor: Compressor | None = None,
113
+ argon2: Argon2Params | None = None,
114
+ ) -> None:
115
+ """Write an organization's collections and items to an encrypted, compressed backup file.
116
+
117
+ Args:
118
+ organization: The organization, as object, id or unique name.
119
+ path: The file to create. It must not exist yet; it is created with mode ``0600``.
120
+ passphrase: The passphrase protecting the file.
121
+ compressor: Compression module or object with ``compress``/``decompress``. Defaults to zstd when
122
+ available, else lzma.
123
+ argon2: Cost of the passphrase key derivation. Defaults to RFC 9106 parameters.
124
+
125
+ Raises:
126
+ FileExistsError: ``path`` exists already.
127
+ CryptoError: The passphrase is empty, or an item cannot be decrypted.
128
+ """
129
+ export = await self.export_organization(organization)
130
+ await _offload.arun_cpu(functools.partial(dump.write_dump, compressor=compressor, argon2=argon2), export, path, passphrase)