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.
- python_vaultwarden2-0.1.0.dist-info/METADATA +69 -0
- python_vaultwarden2-0.1.0.dist-info/RECORD +34 -0
- python_vaultwarden2-0.1.0.dist-info/WHEEL +4 -0
- python_vaultwarden2-0.1.0.dist-info/licenses/LICENSE +202 -0
- vaultwarden2/__init__.py +47 -0
- vaultwarden2/_async/__init__.py +1 -0
- vaultwarden2/_async/account.py +379 -0
- vaultwarden2/_async/admin.py +485 -0
- vaultwarden2/_async/auth.py +218 -0
- vaultwarden2/_async/base.py +462 -0
- vaultwarden2/_async/client.py +130 -0
- vaultwarden2/_async/organizations.py +914 -0
- vaultwarden2/_async/vault.py +970 -0
- vaultwarden2/_core.py +1103 -0
- vaultwarden2/_encode.py +422 -0
- vaultwarden2/_json.py +215 -0
- vaultwarden2/_merge.py +529 -0
- vaultwarden2/_offload.py +65 -0
- vaultwarden2/_sync/__init__.py +1 -0
- vaultwarden2/_sync/account.py +381 -0
- vaultwarden2/_sync/admin.py +487 -0
- vaultwarden2/_sync/auth.py +220 -0
- vaultwarden2/_sync/base.py +464 -0
- vaultwarden2/_sync/client.py +132 -0
- vaultwarden2/_sync/organizations.py +914 -0
- vaultwarden2/_sync/vault.py +968 -0
- vaultwarden2/crypto.py +634 -0
- vaultwarden2/dump.py +362 -0
- vaultwarden2/errors.py +123 -0
- vaultwarden2/export.py +128 -0
- vaultwarden2/models.py +842 -0
- vaultwarden2/py.typed +0 -0
- vaultwarden2/search.py +206 -0
- vaultwarden2/totp.py +214 -0
|
@@ -0,0 +1,968 @@
|
|
|
1
|
+
# Do not edit this file directly. It has been autogenerated from
|
|
2
|
+
# src/vaultwarden2/_async/vault.py
|
|
3
|
+
"""Personal vault: items, folders, attachments."""
|
|
4
|
+
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
import functools
|
|
8
|
+
from dataclasses import replace
|
|
9
|
+
from typing import TYPE_CHECKING
|
|
10
|
+
|
|
11
|
+
from vaultwarden2 import _core, _encode, _merge, _offload, crypto
|
|
12
|
+
from vaultwarden2 import search as _search
|
|
13
|
+
from vaultwarden2._json import JsonObject
|
|
14
|
+
from vaultwarden2.crypto import SymmetricKey
|
|
15
|
+
from vaultwarden2.errors import ApiError, UnsafeWriteError, VaultwardenError
|
|
16
|
+
from vaultwarden2.models import (
|
|
17
|
+
Attachment,
|
|
18
|
+
Card,
|
|
19
|
+
Cipher,
|
|
20
|
+
CipherType,
|
|
21
|
+
Collection,
|
|
22
|
+
CustomField,
|
|
23
|
+
FieldType,
|
|
24
|
+
Folder,
|
|
25
|
+
Identity,
|
|
26
|
+
Login,
|
|
27
|
+
LoginUri,
|
|
28
|
+
Organization,
|
|
29
|
+
RepromptType,
|
|
30
|
+
SshKey,
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
if TYPE_CHECKING:
|
|
34
|
+
from collections.abc import Callable, Mapping, Sequence
|
|
35
|
+
|
|
36
|
+
from vaultwarden2.models import CollectionRef, FolderRef, ItemRef, OrganizationRef, Profile, Vault
|
|
37
|
+
|
|
38
|
+
from . import account as _account_mod
|
|
39
|
+
from .base import _collection_id, _item_id
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
class VaultMixin(_account_mod.AccountMixin):
|
|
43
|
+
"""Reading, searching and changing vault items, folders and attachments."""
|
|
44
|
+
|
|
45
|
+
def profile(self) -> Profile:
|
|
46
|
+
"""The logged-in account.
|
|
47
|
+
|
|
48
|
+
Returns:
|
|
49
|
+
The account profile (id, email, name, verification and premium state).
|
|
50
|
+
"""
|
|
51
|
+
return _core.parse_profile(self._load_profile())
|
|
52
|
+
|
|
53
|
+
def sync(self, *, include_archived: bool = False) -> Vault:
|
|
54
|
+
"""Fetch and decrypt the whole vault in one request.
|
|
55
|
+
|
|
56
|
+
Items that cannot be decrypted are skipped and listed in ``vault.undecryptable``; parts of items that cannot
|
|
57
|
+
be interpreted are listed in ``item.issues``. Both are reported on the ``vaultwarden2`` logger and as one
|
|
58
|
+
[VaultwardenDataWarning][vaultwarden2.VaultwardenDataWarning].
|
|
59
|
+
|
|
60
|
+
Args:
|
|
61
|
+
include_archived: Also return archived items. Items in the trash are always included
|
|
62
|
+
(see ``Cipher.is_deleted``).
|
|
63
|
+
|
|
64
|
+
Returns:
|
|
65
|
+
Profile, folders, items (personal and organization), collections and organizations.
|
|
66
|
+
|
|
67
|
+
Example:
|
|
68
|
+
```python
|
|
69
|
+
vault = vw.sync()
|
|
70
|
+
for item in vault.ciphers:
|
|
71
|
+
if item.login and not item.is_deleted:
|
|
72
|
+
print(item.name, item.login.username)
|
|
73
|
+
```
|
|
74
|
+
"""
|
|
75
|
+
keys = self._require_keys()
|
|
76
|
+
body = self.request('GET', '/api/sync', params={'excludeDomains': 'true'})
|
|
77
|
+
vault = _offload.run_cpu(_core.decrypt_vault, body, keys)
|
|
78
|
+
self._profile = JsonObject(vault.profile.raw)
|
|
79
|
+
if include_archived:
|
|
80
|
+
return vault
|
|
81
|
+
return replace(vault, ciphers=tuple(c for c in vault.ciphers if not c.is_archived))
|
|
82
|
+
|
|
83
|
+
def folders(self) -> list[Folder]:
|
|
84
|
+
"""All folders, decrypted.
|
|
85
|
+
|
|
86
|
+
Returns:
|
|
87
|
+
The folders of the logged-in account.
|
|
88
|
+
"""
|
|
89
|
+
keys = self._require_keys()
|
|
90
|
+
body = JsonObject(self.request('GET', '/api/folders'))
|
|
91
|
+
return [_core.decrypt_folder(f.raw, keys) for f in body.objs('data')]
|
|
92
|
+
|
|
93
|
+
def get_folder(self, folder_id: str) -> Folder:
|
|
94
|
+
"""One folder, decrypted.
|
|
95
|
+
|
|
96
|
+
Args:
|
|
97
|
+
folder_id: The folder id.
|
|
98
|
+
|
|
99
|
+
Returns:
|
|
100
|
+
The folder.
|
|
101
|
+
|
|
102
|
+
Raises:
|
|
103
|
+
ApiError: No such folder.
|
|
104
|
+
"""
|
|
105
|
+
return _core.decrypt_folder(self.request('GET', f'/api/folders/{folder_id}'), self._require_keys())
|
|
106
|
+
|
|
107
|
+
def ciphers(self, *, include_archived: bool = False) -> list[Cipher]:
|
|
108
|
+
"""All items (personal and organization), decrypted.
|
|
109
|
+
|
|
110
|
+
Unlike [sync][vaultwarden2.VaultwardenClient.sync], an item that cannot be decrypted is an error here.
|
|
111
|
+
|
|
112
|
+
Args:
|
|
113
|
+
include_archived: Also return archived items.
|
|
114
|
+
|
|
115
|
+
Returns:
|
|
116
|
+
The items.
|
|
117
|
+
|
|
118
|
+
Raises:
|
|
119
|
+
CryptoError: An item cannot be decrypted.
|
|
120
|
+
"""
|
|
121
|
+
keys = self._require_keys()
|
|
122
|
+
body = JsonObject(self.request('GET', '/api/ciphers'))
|
|
123
|
+
items = _offload.run_cpu(_core.decrypt_ciphers, [c.raw for c in body.objs('data')], keys)
|
|
124
|
+
return items if include_archived else [c for c in items if not c.is_archived]
|
|
125
|
+
|
|
126
|
+
def search(self, query: str, *, include_archived: bool = False, include_deleted: bool = False) -> list[Cipher]:
|
|
127
|
+
"""Find items by text, locally (the server cannot search encrypted data).
|
|
128
|
+
|
|
129
|
+
Searches names, usernames, URIs, notes and custom field names, case-insensitively. Passwords and hidden
|
|
130
|
+
values are never searched. See [vaultwarden2.search.search][].
|
|
131
|
+
|
|
132
|
+
Args:
|
|
133
|
+
query: The text to look for.
|
|
134
|
+
include_archived: Also search archived items.
|
|
135
|
+
include_deleted: Also search items in the trash.
|
|
136
|
+
|
|
137
|
+
Returns:
|
|
138
|
+
The matching items, in vault order.
|
|
139
|
+
"""
|
|
140
|
+
vault = self.sync(include_archived=True)
|
|
141
|
+
return _search.search(vault.ciphers, query, include_archived=include_archived, include_deleted=include_deleted)
|
|
142
|
+
|
|
143
|
+
def find_by_uri(self, url: str, *, mode: _search.SearchMode = 'host', include_archived: bool = False, include_deleted: bool = False) -> list[Cipher]:
|
|
144
|
+
"""Find login items for a URL, locally. See [vaultwarden2.search.find_by_uri][].
|
|
145
|
+
|
|
146
|
+
Args:
|
|
147
|
+
url: The address to look up; the scheme is optional (``github.com`` works).
|
|
148
|
+
mode: ``'host'`` finds items with a URI on exactly the same host (and port). ``'autofill'`` applies each
|
|
149
|
+
stored URI's own match rule like browser autofill (base domain by default; needs the ``psl`` extra for
|
|
150
|
+
exact base-domain matching).
|
|
151
|
+
include_archived: Also return archived items.
|
|
152
|
+
include_deleted: Also return items in the trash.
|
|
153
|
+
|
|
154
|
+
Returns:
|
|
155
|
+
The matching login items.
|
|
156
|
+
"""
|
|
157
|
+
vault = self.sync(include_archived=True)
|
|
158
|
+
return _search.find_by_uri(vault.ciphers, url, mode=mode, include_archived=include_archived, include_deleted=include_deleted)
|
|
159
|
+
|
|
160
|
+
def get_cipher(self, cipher_id: str) -> Cipher:
|
|
161
|
+
"""One item, fresh from the server and decrypted.
|
|
162
|
+
|
|
163
|
+
Args:
|
|
164
|
+
cipher_id: The item id.
|
|
165
|
+
|
|
166
|
+
Returns:
|
|
167
|
+
The item.
|
|
168
|
+
|
|
169
|
+
Raises:
|
|
170
|
+
ApiError: No such item, or no access to it.
|
|
171
|
+
CryptoError: The item cannot be decrypted.
|
|
172
|
+
"""
|
|
173
|
+
return _core.decrypt_cipher(self.request('GET', f'/api/ciphers/{cipher_id}'), self._require_keys())
|
|
174
|
+
|
|
175
|
+
def organizations(self) -> list[Organization]:
|
|
176
|
+
"""The organizations the user is a member of.
|
|
177
|
+
|
|
178
|
+
Also refreshes the organization keys, so items of newly confirmed memberships can be decrypted.
|
|
179
|
+
|
|
180
|
+
Returns:
|
|
181
|
+
The memberships, with role and status.
|
|
182
|
+
"""
|
|
183
|
+
profile = self._load_profile()
|
|
184
|
+
_core.load_org_keys(profile, self._require_keys())
|
|
185
|
+
return [_core.parse_organization(o) for o in profile.objs('organizations')]
|
|
186
|
+
|
|
187
|
+
def collections(self) -> list[Collection]:
|
|
188
|
+
"""The collections the user can access, decrypted.
|
|
189
|
+
|
|
190
|
+
Returns:
|
|
191
|
+
The collections, across all organizations.
|
|
192
|
+
"""
|
|
193
|
+
keys = self._require_keys()
|
|
194
|
+
body = JsonObject(self.request('GET', '/api/collections'))
|
|
195
|
+
return [_core.decrypt_collection(c.raw, keys) for c in body.objs('data')]
|
|
196
|
+
|
|
197
|
+
def _resolve_cipher(self, item: ItemRef) -> Cipher:
|
|
198
|
+
"""The current server version of an item.
|
|
199
|
+
|
|
200
|
+
Args:
|
|
201
|
+
item: An item or its id.
|
|
202
|
+
|
|
203
|
+
Returns:
|
|
204
|
+
The item, fetched again from the server.
|
|
205
|
+
"""
|
|
206
|
+
return self.get_cipher(item.id if isinstance(item, Cipher) else item)
|
|
207
|
+
|
|
208
|
+
def _resolve_folder(self, folder: FolderRef | None) -> str | None:
|
|
209
|
+
"""Turn a folder reference into an id.
|
|
210
|
+
|
|
211
|
+
Args:
|
|
212
|
+
folder: A folder, its id, its unique name, or None.
|
|
213
|
+
|
|
214
|
+
Returns:
|
|
215
|
+
The folder id, or None for "no folder".
|
|
216
|
+
|
|
217
|
+
Raises:
|
|
218
|
+
VaultwardenError: No folder with that id or name, or the name is used by several folders.
|
|
219
|
+
"""
|
|
220
|
+
if folder is None or isinstance(folder, Folder):
|
|
221
|
+
return folder.id if folder else None
|
|
222
|
+
folders = self.folders()
|
|
223
|
+
if any(f.id == folder for f in folders):
|
|
224
|
+
return folder
|
|
225
|
+
matches = [f.id for f in folders if f.name == folder]
|
|
226
|
+
if len(matches) != 1:
|
|
227
|
+
raise VaultwardenError(f'folder {folder!r} not found' if not matches else f'folder name {folder!r} is ambiguous; pass the id')
|
|
228
|
+
return matches[0]
|
|
229
|
+
|
|
230
|
+
def _resolve_organization(self, organization: OrganizationRef) -> str:
|
|
231
|
+
"""Turn an organization reference into an id.
|
|
232
|
+
|
|
233
|
+
Args:
|
|
234
|
+
organization: An organization, its id or its unique name.
|
|
235
|
+
|
|
236
|
+
Returns:
|
|
237
|
+
The organization id.
|
|
238
|
+
|
|
239
|
+
Raises:
|
|
240
|
+
VaultwardenError: Not found, or the name is ambiguous.
|
|
241
|
+
"""
|
|
242
|
+
if isinstance(organization, Organization):
|
|
243
|
+
return organization.id
|
|
244
|
+
orgs = self.organizations()
|
|
245
|
+
if any(o.id == organization for o in orgs):
|
|
246
|
+
return organization
|
|
247
|
+
matches = [o.id for o in orgs if o.name == organization]
|
|
248
|
+
if len(matches) != 1:
|
|
249
|
+
raise VaultwardenError(f'organization {organization!r} not found' if not matches else f'organization name {organization!r} is ambiguous; pass the id')
|
|
250
|
+
return matches[0]
|
|
251
|
+
|
|
252
|
+
def create_cipher(self, cipher: Cipher) -> Cipher:
|
|
253
|
+
"""Create an item from a model.
|
|
254
|
+
|
|
255
|
+
The item gets its own random item key (as in the official clients). The encrypted payload is decrypted again
|
|
256
|
+
and checked before it is sent. For everyday cases the helpers such as
|
|
257
|
+
[create_login][vaultwarden2.VaultwardenClient.create_login] are simpler.
|
|
258
|
+
|
|
259
|
+
Args:
|
|
260
|
+
cipher: The item to create. Its ``id`` is ignored. For an organization item set ``organization_id`` and
|
|
261
|
+
``collection_ids`` (at least one collection).
|
|
262
|
+
|
|
263
|
+
Returns:
|
|
264
|
+
The created item, decrypted, as stored on the server (with its id).
|
|
265
|
+
|
|
266
|
+
Raises:
|
|
267
|
+
UnsafeWriteError: The item carries data this library could not interpret (``cipher.issues``), or the
|
|
268
|
+
payload failed verification; nothing was sent.
|
|
269
|
+
ApiError: The server rejected the item.
|
|
270
|
+
|
|
271
|
+
Example:
|
|
272
|
+
```python
|
|
273
|
+
from vaultwarden2.models import Cipher, CipherType, Login
|
|
274
|
+
item = vw.create_cipher(Cipher(type=CipherType.LOGIN, name='GitHub', login=Login(username='alice')))
|
|
275
|
+
```
|
|
276
|
+
"""
|
|
277
|
+
keys = self._require_keys()
|
|
278
|
+
if cipher.issues:
|
|
279
|
+
raise UnsafeWriteError('the item contains data this library cannot interpret; refusing to copy it')
|
|
280
|
+
cipher = _merge.with_dated_history(cipher)
|
|
281
|
+
if not JsonObject(cipher.raw).get_str('key'): # own item key, like the official clients
|
|
282
|
+
cipher = replace(cipher, raw={**cipher.raw, 'key': _encode.new_item_key(cipher, keys)})
|
|
283
|
+
body = _encode.encode_cipher(cipher, keys, self._user_id())
|
|
284
|
+
for server_owned in ('id', 'lastKnownRevisionDate', 'revisionDate', 'creationDate', 'deletedDate'):
|
|
285
|
+
body.pop(server_owned, None)
|
|
286
|
+
_offload.run_cpu(_merge.verify_new, body, keys, cipher)
|
|
287
|
+
if cipher.organization_id:
|
|
288
|
+
result = self.request('POST', '/api/ciphers/create', json={'cipher': body, 'collectionIds': list(cipher.collection_ids)})
|
|
289
|
+
else:
|
|
290
|
+
result = self.request('POST', '/api/ciphers', json=body)
|
|
291
|
+
return _core.decrypt_cipher(result, keys)
|
|
292
|
+
|
|
293
|
+
def update_cipher(self, cipher: Cipher, *, strict: bool = False) -> Cipher:
|
|
294
|
+
"""Save changes to an item.
|
|
295
|
+
|
|
296
|
+
Start from an item read from the server (``get_cipher``, ``sync``, a create result) and change it with
|
|
297
|
+
``dataclasses.replace``. The upload is built from the item's *current* server version: only the fields you
|
|
298
|
+
changed are re-encrypted, every other field keeps its exact ciphertext, and the payload is decrypted and
|
|
299
|
+
checked before it is sent. If the login password changed, the previous one goes to the password history.
|
|
300
|
+
|
|
301
|
+
Args:
|
|
302
|
+
cipher: The edited item.
|
|
303
|
+
strict: Refuse if the item changed on the server in any way since it was read. By default only a change
|
|
304
|
+
to one of the same fields counts as a conflict.
|
|
305
|
+
|
|
306
|
+
Returns:
|
|
307
|
+
The saved item, decrypted. If nothing changed, nothing is sent and the current server version is returned.
|
|
308
|
+
|
|
309
|
+
Raises:
|
|
310
|
+
ConflictError: The item changed on the server in a conflicting way (``strict``: in any way); re-read and
|
|
311
|
+
retry. Nothing was sent.
|
|
312
|
+
UnsafeWriteError: The change would overwrite data this library cannot interpret, would change ``id``,
|
|
313
|
+
``type`` or ``organization_id``, the item was not read from the server, or verification failed.
|
|
314
|
+
Nothing was sent.
|
|
315
|
+
ApiError: The server rejected the update.
|
|
316
|
+
|
|
317
|
+
Example:
|
|
318
|
+
```python
|
|
319
|
+
from dataclasses import replace
|
|
320
|
+
item = vw.get_cipher(item_id)
|
|
321
|
+
item = vw.update_cipher(replace(item, notes='rotated on 2026-11-28'))
|
|
322
|
+
```
|
|
323
|
+
"""
|
|
324
|
+
if not cipher.id:
|
|
325
|
+
raise VaultwardenError('cipher has no id; use create_cipher() for new items')
|
|
326
|
+
keys = self._require_keys()
|
|
327
|
+
current = self.request('GET', f'/api/ciphers/{cipher.id}')
|
|
328
|
+
body = _offload.run_cpu(functools.partial(_merge.plan_update, strict=strict), cipher, current, keys, self._user_id())
|
|
329
|
+
if body is None:
|
|
330
|
+
return _core.decrypt_cipher(current, keys)
|
|
331
|
+
return _core.decrypt_cipher(self.request('PUT', f'/api/ciphers/{cipher.id}', json=body), keys)
|
|
332
|
+
|
|
333
|
+
def _partial_update(self, item: ItemRef, *, folder_id: str | None, favorite: bool | None) -> Cipher:
|
|
334
|
+
"""Change folder and favorite without re-encrypting anything (works for read-only organization items too).
|
|
335
|
+
|
|
336
|
+
Args:
|
|
337
|
+
item: An item or its id.
|
|
338
|
+
folder_id: The new folder id, or None to keep the current folder.
|
|
339
|
+
favorite: The new favorite flag, or None to keep it.
|
|
340
|
+
|
|
341
|
+
Returns:
|
|
342
|
+
The updated item.
|
|
343
|
+
"""
|
|
344
|
+
current = self._resolve_cipher(item)
|
|
345
|
+
body = {
|
|
346
|
+
'folderId': folder_id if folder_id is not None else current.folder_id,
|
|
347
|
+
'favorite': favorite if favorite is not None else current.favorite,
|
|
348
|
+
}
|
|
349
|
+
return _core.decrypt_cipher(self.request('PUT', f'/api/ciphers/{current.id}/partial', json=body), self._require_keys())
|
|
350
|
+
|
|
351
|
+
def trash(self, item: ItemRef) -> None:
|
|
352
|
+
"""Move an item to the trash. It can be restored with [restore][vaultwarden2.VaultwardenClient.restore].
|
|
353
|
+
|
|
354
|
+
Args:
|
|
355
|
+
item: The item, as object or id.
|
|
356
|
+
|
|
357
|
+
Raises:
|
|
358
|
+
ApiError: No such item, or no permission to delete it.
|
|
359
|
+
"""
|
|
360
|
+
self.request('PUT', f'/api/ciphers/{_item_id(item)}/delete')
|
|
361
|
+
|
|
362
|
+
def restore(self, item: ItemRef) -> Cipher:
|
|
363
|
+
"""Restore an item from the trash.
|
|
364
|
+
|
|
365
|
+
Args:
|
|
366
|
+
item: The item, as object or id.
|
|
367
|
+
|
|
368
|
+
Returns:
|
|
369
|
+
The restored item.
|
|
370
|
+
|
|
371
|
+
Raises:
|
|
372
|
+
ApiError: No such item in the trash.
|
|
373
|
+
"""
|
|
374
|
+
return _core.decrypt_cipher(self.request('PUT', f'/api/ciphers/{_item_id(item)}/restore'), self._require_keys())
|
|
375
|
+
|
|
376
|
+
def delete(self, item: ItemRef) -> None:
|
|
377
|
+
"""Delete an item permanently, without the trash. This cannot be undone.
|
|
378
|
+
|
|
379
|
+
Args:
|
|
380
|
+
item: The item, as object or id.
|
|
381
|
+
|
|
382
|
+
Raises:
|
|
383
|
+
ApiError: No such item, or no permission to delete it.
|
|
384
|
+
"""
|
|
385
|
+
self.request('DELETE', f'/api/ciphers/{_item_id(item)}')
|
|
386
|
+
|
|
387
|
+
def archive(self, item: ItemRef) -> Cipher:
|
|
388
|
+
"""Archive an item: it is kept, but hidden from normal listings and searches.
|
|
389
|
+
|
|
390
|
+
Args:
|
|
391
|
+
item: The item, as object or id.
|
|
392
|
+
|
|
393
|
+
Returns:
|
|
394
|
+
The archived item.
|
|
395
|
+
|
|
396
|
+
Raises:
|
|
397
|
+
ApiError: No such item.
|
|
398
|
+
"""
|
|
399
|
+
return _core.decrypt_cipher(self.request('PUT', f'/api/ciphers/{_item_id(item)}/archive'), self._require_keys())
|
|
400
|
+
|
|
401
|
+
def unarchive(self, item: ItemRef) -> Cipher:
|
|
402
|
+
"""Bring an archived item back to normal listings.
|
|
403
|
+
|
|
404
|
+
Args:
|
|
405
|
+
item: The item, as object or id.
|
|
406
|
+
|
|
407
|
+
Returns:
|
|
408
|
+
The item.
|
|
409
|
+
|
|
410
|
+
Raises:
|
|
411
|
+
ApiError: No such item.
|
|
412
|
+
"""
|
|
413
|
+
return _core.decrypt_cipher(self.request('PUT', f'/api/ciphers/{_item_id(item)}/unarchive'), self._require_keys())
|
|
414
|
+
|
|
415
|
+
# ------------------------------------------------------------ bulk operations (reversible only)
|
|
416
|
+
|
|
417
|
+
def _validated_ids(self, items: Sequence[ItemRef], action: str, check: Callable[[Cipher], str | None]) -> list[str]:
|
|
418
|
+
"""Check every item before a bulk request; any problem means nothing is sent.
|
|
419
|
+
|
|
420
|
+
The server applies bulk changes one by one and stops at the first error, leaving earlier items changed.
|
|
421
|
+
|
|
422
|
+
Args:
|
|
423
|
+
items: The items to act on.
|
|
424
|
+
action: Name of the action, for error messages.
|
|
425
|
+
check: Returns a problem description for an item that must not be included, or None.
|
|
426
|
+
|
|
427
|
+
Returns:
|
|
428
|
+
The item ids, in the given order.
|
|
429
|
+
|
|
430
|
+
Raises:
|
|
431
|
+
UnsafeWriteError: An item is listed twice, does not exist, is not accessible or fails ``check``.
|
|
432
|
+
"""
|
|
433
|
+
if not items:
|
|
434
|
+
return []
|
|
435
|
+
ids = [_item_id(i) for i in items]
|
|
436
|
+
if len(set(ids)) != len(ids):
|
|
437
|
+
raise UnsafeWriteError(f'{action}: the same item is listed twice')
|
|
438
|
+
current = {c.id: c for c in (self.sync(include_archived=True)).ciphers}
|
|
439
|
+
problems = []
|
|
440
|
+
for item_id in ids:
|
|
441
|
+
cipher = current.get(item_id)
|
|
442
|
+
problem = 'not found or not accessible' if cipher is None else check(cipher)
|
|
443
|
+
if problem:
|
|
444
|
+
problems.append(f'{item_id}: {problem}')
|
|
445
|
+
if problems:
|
|
446
|
+
raise UnsafeWriteError(f'{action} refused, nothing was changed: ' + '; '.join(problems))
|
|
447
|
+
return ids
|
|
448
|
+
|
|
449
|
+
def move_many(self, items: Sequence[ItemRef], folder: FolderRef | None) -> None:
|
|
450
|
+
"""Move several items to a folder, all or nothing.
|
|
451
|
+
|
|
452
|
+
Every item is checked first; if any is missing or inaccessible, nothing is sent.
|
|
453
|
+
|
|
454
|
+
Args:
|
|
455
|
+
items: The items, as objects or ids.
|
|
456
|
+
folder: The target folder (object, id or unique name), or None for no folder.
|
|
457
|
+
|
|
458
|
+
Raises:
|
|
459
|
+
UnsafeWriteError: An item failed the check; nothing was changed.
|
|
460
|
+
VaultwardenError: The folder was not found.
|
|
461
|
+
"""
|
|
462
|
+
folder_id = self._resolve_folder(folder)
|
|
463
|
+
ids = self._validated_ids(items, 'move', lambda c: None)
|
|
464
|
+
if ids:
|
|
465
|
+
self.request('POST', '/api/ciphers/move', json={'ids': ids, 'folderId': folder_id})
|
|
466
|
+
|
|
467
|
+
def trash_many(self, items: Sequence[ItemRef]) -> None:
|
|
468
|
+
"""Move several items to the trash, all or nothing.
|
|
469
|
+
|
|
470
|
+
Args:
|
|
471
|
+
items: The items, as objects or ids. None of them may already be in the trash or be read-only.
|
|
472
|
+
|
|
473
|
+
Raises:
|
|
474
|
+
UnsafeWriteError: An item failed the check; nothing was changed.
|
|
475
|
+
"""
|
|
476
|
+
ids = self._validated_ids(items, 'trash', lambda c: 'already in the trash' if c.is_deleted else None if c.edit else 'read-only')
|
|
477
|
+
if ids:
|
|
478
|
+
self.request('PUT', '/api/ciphers/delete', json={'ids': ids})
|
|
479
|
+
|
|
480
|
+
def restore_many(self, items: Sequence[ItemRef]) -> None:
|
|
481
|
+
"""Restore several items from the trash, all or nothing.
|
|
482
|
+
|
|
483
|
+
Args:
|
|
484
|
+
items: The items, as objects or ids. All of them must be in the trash.
|
|
485
|
+
|
|
486
|
+
Raises:
|
|
487
|
+
UnsafeWriteError: An item failed the check; nothing was changed.
|
|
488
|
+
"""
|
|
489
|
+
ids = self._validated_ids(items, 'restore', lambda c: None if c.is_deleted else 'not in the trash')
|
|
490
|
+
if ids:
|
|
491
|
+
self.request('PUT', '/api/ciphers/restore', json={'ids': ids})
|
|
492
|
+
|
|
493
|
+
def archive_many(self, items: Sequence[ItemRef]) -> None:
|
|
494
|
+
"""Archive several items, all or nothing.
|
|
495
|
+
|
|
496
|
+
Args:
|
|
497
|
+
items: The items, as objects or ids. None of them may be archived already or be in the trash.
|
|
498
|
+
|
|
499
|
+
Raises:
|
|
500
|
+
UnsafeWriteError: An item failed the check; nothing was changed.
|
|
501
|
+
"""
|
|
502
|
+
ids = self._validated_ids(items, 'archive', lambda c: 'already archived' if c.is_archived else 'in the trash' if c.is_deleted else None)
|
|
503
|
+
if ids:
|
|
504
|
+
self.request('PUT', '/api/ciphers/archive', json={'ids': ids})
|
|
505
|
+
|
|
506
|
+
def unarchive_many(self, items: Sequence[ItemRef]) -> None:
|
|
507
|
+
"""Bring several archived items back, all or nothing.
|
|
508
|
+
|
|
509
|
+
Args:
|
|
510
|
+
items: The items, as objects or ids. All of them must be archived.
|
|
511
|
+
|
|
512
|
+
Raises:
|
|
513
|
+
UnsafeWriteError: An item failed the check; nothing was changed.
|
|
514
|
+
"""
|
|
515
|
+
ids = self._validated_ids(items, 'unarchive', lambda c: None if c.is_archived else 'not archived')
|
|
516
|
+
if ids:
|
|
517
|
+
self.request('PUT', '/api/ciphers/unarchive', json={'ids': ids})
|
|
518
|
+
|
|
519
|
+
def create_folder(self, name: str) -> Folder:
|
|
520
|
+
"""Create a folder.
|
|
521
|
+
|
|
522
|
+
Folders are stored flat. The web vault and the apps nest them by name: `Work/Clients` is shown inside `Work`.
|
|
523
|
+
|
|
524
|
+
Args:
|
|
525
|
+
name: The folder name (encrypted before upload). Use `/` to nest it under another folder.
|
|
526
|
+
|
|
527
|
+
Returns:
|
|
528
|
+
The created folder.
|
|
529
|
+
"""
|
|
530
|
+
keys = self._require_keys()
|
|
531
|
+
body = self.request('POST', '/api/folders', json={'name': _merge.verified_encrypt(name, keys.user_key)})
|
|
532
|
+
return _core.decrypt_folder(body, keys)
|
|
533
|
+
|
|
534
|
+
def rename_folder(self, folder: FolderRef, name: str) -> Folder:
|
|
535
|
+
"""Rename a folder.
|
|
536
|
+
|
|
537
|
+
Only this folder changes. Sub-folders are separate folders named `<old name>/...`; they keep their names.
|
|
538
|
+
|
|
539
|
+
Args:
|
|
540
|
+
folder: The folder, as object, id or unique name.
|
|
541
|
+
name: The new name.
|
|
542
|
+
|
|
543
|
+
Returns:
|
|
544
|
+
The renamed folder.
|
|
545
|
+
|
|
546
|
+
Raises:
|
|
547
|
+
VaultwardenError: The folder was not found or the name is ambiguous.
|
|
548
|
+
"""
|
|
549
|
+
keys = self._require_keys()
|
|
550
|
+
folder_id = self._resolve_folder(folder)
|
|
551
|
+
body = self.request('PUT', f'/api/folders/{folder_id}', json={'name': _merge.verified_encrypt(name, keys.user_key)})
|
|
552
|
+
return _core.decrypt_folder(body, keys)
|
|
553
|
+
|
|
554
|
+
def delete_folder(self, folder: FolderRef) -> None:
|
|
555
|
+
"""Delete a folder. Its items are kept and moved to "no folder".
|
|
556
|
+
|
|
557
|
+
Sub-folders (`<name>/...`) are separate folders and are not deleted.
|
|
558
|
+
|
|
559
|
+
Args:
|
|
560
|
+
folder: The folder, as object, id or unique name.
|
|
561
|
+
|
|
562
|
+
Raises:
|
|
563
|
+
VaultwardenError: The folder was not found or the name is ambiguous.
|
|
564
|
+
"""
|
|
565
|
+
folder_id = self._resolve_folder(folder)
|
|
566
|
+
self.request('DELETE', f'/api/folders/{folder_id}')
|
|
567
|
+
|
|
568
|
+
def add_attachment(self, item: ItemRef, file_name: str, data: bytes) -> Cipher:
|
|
569
|
+
"""Encrypt a file and attach it to an item.
|
|
570
|
+
|
|
571
|
+
The file gets its own key; key, name and content are checked before upload. If the upload fails, the
|
|
572
|
+
half-created attachment record is removed again.
|
|
573
|
+
|
|
574
|
+
Args:
|
|
575
|
+
item: The item, as object or id.
|
|
576
|
+
file_name: The file name shown in the vault (encrypted).
|
|
577
|
+
data: The file content.
|
|
578
|
+
|
|
579
|
+
Returns:
|
|
580
|
+
The item with the new attachment in ``attachments``.
|
|
581
|
+
|
|
582
|
+
Raises:
|
|
583
|
+
UnsafeWriteError: The encryption self-check failed; nothing was sent.
|
|
584
|
+
ApiError: The server refused the attachment (for example storage limits or files over 525 MB).
|
|
585
|
+
"""
|
|
586
|
+
keys = self._require_keys()
|
|
587
|
+
cipher = self._resolve_cipher(item)
|
|
588
|
+
item_key = _core.cipher_key(JsonObject(cipher.raw), keys)
|
|
589
|
+
attachment_key = SymmetricKey.generate()
|
|
590
|
+
encrypted = crypto.encrypt_buffer(data, attachment_key)
|
|
591
|
+
request = _encode.encode_attachment_request(file_name, len(encrypted), attachment_key, item_key)
|
|
592
|
+
_merge.verify_attachment(request, encrypted, data, file_name, item_key)
|
|
593
|
+
meta = JsonObject(self.request('POST', f'/api/ciphers/{cipher.id}/attachment/v2', json=request))
|
|
594
|
+
attachment_id = meta.get_str('attachmentId')
|
|
595
|
+
if not attachment_id:
|
|
596
|
+
raise ApiError(500, 'server returned no attachmentId')
|
|
597
|
+
try:
|
|
598
|
+
self.request('POST', f'/api/ciphers/{cipher.id}/attachment/{attachment_id}', files={'data': (request['fileName'], encrypted, 'application/octet-stream')})
|
|
599
|
+
except VaultwardenError:
|
|
600
|
+
self.request('DELETE', f'/api/ciphers/{cipher.id}/attachment/{attachment_id}')
|
|
601
|
+
raise
|
|
602
|
+
return self.get_cipher(cipher.id)
|
|
603
|
+
|
|
604
|
+
def download_attachment(self, item: ItemRef, attachment: Attachment | str) -> bytes:
|
|
605
|
+
"""Download and decrypt an attachment.
|
|
606
|
+
|
|
607
|
+
Args:
|
|
608
|
+
item: The item, as object or id.
|
|
609
|
+
attachment: The attachment, as object (from ``item.attachments``) or id.
|
|
610
|
+
|
|
611
|
+
Returns:
|
|
612
|
+
The decrypted file content.
|
|
613
|
+
|
|
614
|
+
Raises:
|
|
615
|
+
ApiError: No such attachment.
|
|
616
|
+
CryptoError: The file cannot be decrypted.
|
|
617
|
+
"""
|
|
618
|
+
keys = self._require_keys()
|
|
619
|
+
cipher = self._resolve_cipher(item)
|
|
620
|
+
attachment_id = attachment.id if isinstance(attachment, Attachment) else attachment
|
|
621
|
+
meta = JsonObject(self.request('GET', f'/api/ciphers/{cipher.id}/attachment/{attachment_id}'))
|
|
622
|
+
url = meta.get_str('url')
|
|
623
|
+
if not url:
|
|
624
|
+
raise ApiError(500, 'server returned no attachment url')
|
|
625
|
+
response = self._http.get(url)
|
|
626
|
+
_core.raise_for_status(response.status_code, None)
|
|
627
|
+
item_key = _core.cipher_key(JsonObject(cipher.raw), keys)
|
|
628
|
+
enc_key = meta.get_str('key')
|
|
629
|
+
attachment_key = SymmetricKey.from_bytes(crypto.decrypt(enc_key, item_key)) if enc_key else item_key
|
|
630
|
+
return crypto.decrypt_buffer(response.content, attachment_key)
|
|
631
|
+
|
|
632
|
+
def delete_attachment(self, item: ItemRef, attachment: Attachment | str) -> None:
|
|
633
|
+
"""Delete an attachment permanently.
|
|
634
|
+
|
|
635
|
+
Args:
|
|
636
|
+
item: The item, as object or id.
|
|
637
|
+
attachment: The attachment, as object or id.
|
|
638
|
+
|
|
639
|
+
Raises:
|
|
640
|
+
ApiError: No such attachment.
|
|
641
|
+
"""
|
|
642
|
+
attachment_id = attachment.id if isinstance(attachment, Attachment) else attachment
|
|
643
|
+
self.request('DELETE', f'/api/ciphers/{_item_id(item)}/attachment/{attachment_id}')
|
|
644
|
+
|
|
645
|
+
def _create(
|
|
646
|
+
self,
|
|
647
|
+
cipher: Cipher,
|
|
648
|
+
folder: FolderRef | None,
|
|
649
|
+
fields: Mapping[str, str] | None,
|
|
650
|
+
organization: OrganizationRef | None = None,
|
|
651
|
+
collections: Sequence[CollectionRef] = (),
|
|
652
|
+
) -> Cipher:
|
|
653
|
+
"""Shared part of the ``create_*`` helpers: resolve references and create the item.
|
|
654
|
+
|
|
655
|
+
Args:
|
|
656
|
+
cipher: The prepared item.
|
|
657
|
+
folder: Folder reference, or None.
|
|
658
|
+
fields: Hidden custom fields to add, or None.
|
|
659
|
+
organization: Organization reference, or None for a personal item.
|
|
660
|
+
collections: Collection references for an organization item.
|
|
661
|
+
|
|
662
|
+
Returns:
|
|
663
|
+
The created item.
|
|
664
|
+
|
|
665
|
+
Raises:
|
|
666
|
+
VaultwardenError: An organization item without collections, or a reference was not found.
|
|
667
|
+
"""
|
|
668
|
+
hidden = tuple(CustomField(name=k, value=v, type=FieldType.HIDDEN) for k, v in (fields or {}).items())
|
|
669
|
+
org_id = self._resolve_organization(organization) if organization is not None else None
|
|
670
|
+
if org_id and not collections:
|
|
671
|
+
raise VaultwardenError('organization items need at least one collection')
|
|
672
|
+
return self.create_cipher(
|
|
673
|
+
replace(
|
|
674
|
+
cipher,
|
|
675
|
+
folder_id=self._resolve_folder(folder),
|
|
676
|
+
fields=cipher.fields + hidden,
|
|
677
|
+
organization_id=org_id,
|
|
678
|
+
collection_ids=tuple(_collection_id(c) for c in collections),
|
|
679
|
+
)
|
|
680
|
+
)
|
|
681
|
+
|
|
682
|
+
def create_login(
|
|
683
|
+
self,
|
|
684
|
+
name: str,
|
|
685
|
+
*,
|
|
686
|
+
username: str | None = None,
|
|
687
|
+
password: str | None = None,
|
|
688
|
+
uris: Sequence[str | LoginUri] = (),
|
|
689
|
+
totp: str | None = None,
|
|
690
|
+
notes: str | None = None,
|
|
691
|
+
folder: FolderRef | None = None,
|
|
692
|
+
favorite: bool = False,
|
|
693
|
+
organization: OrganizationRef | None = None,
|
|
694
|
+
collections: Sequence[CollectionRef] = (),
|
|
695
|
+
fields: Mapping[str, str] | None = None,
|
|
696
|
+
reprompt: bool = False,
|
|
697
|
+
) -> Cipher:
|
|
698
|
+
"""Create a login item.
|
|
699
|
+
|
|
700
|
+
Args:
|
|
701
|
+
name: The item name.
|
|
702
|
+
username: The username.
|
|
703
|
+
password: The password.
|
|
704
|
+
uris: Websites or apps the login belongs to: plain strings, or
|
|
705
|
+
[LoginUri][vaultwarden2.models.LoginUri] to set a match rule.
|
|
706
|
+
totp: A TOTP seed (base32 secret, ``otpauth://totp/...`` or ``steam://...``), see
|
|
707
|
+
[Login.totp_code][vaultwarden2.models.Login.totp_code].
|
|
708
|
+
notes: Free-text notes (encrypted).
|
|
709
|
+
folder: The folder to put the item in, as [Folder][vaultwarden2.models.Folder], id or unique name.
|
|
710
|
+
None (the default) means no folder.
|
|
711
|
+
favorite: Mark the item as favorite.
|
|
712
|
+
organization: Create the item in this organization (object, id or unique name) instead of the personal vault.
|
|
713
|
+
Requires ``collections``.
|
|
714
|
+
collections: Collections of ``organization`` to put the item in (objects or ids); at least one if ``organization`` is set.
|
|
715
|
+
fields: Extra values as ``{name: value}``, stored as hidden custom fields.
|
|
716
|
+
reprompt: Require the master password again before the password is shown in the official apps.
|
|
717
|
+
|
|
718
|
+
Returns:
|
|
719
|
+
The created item, decrypted, as stored on the server (with its id).
|
|
720
|
+
|
|
721
|
+
Raises:
|
|
722
|
+
VaultwardenError: The folder or organization was not found, or an organization item has no collection.
|
|
723
|
+
UnsafeWriteError: The payload failed verification before upload; nothing was sent.
|
|
724
|
+
ApiError: The server rejected the item.
|
|
725
|
+
|
|
726
|
+
Example:
|
|
727
|
+
```python
|
|
728
|
+
vw.create_login('GitHub', username='alice', password=secret, uris=['https://github.com'], folder='Work')
|
|
729
|
+
```
|
|
730
|
+
"""
|
|
731
|
+
login = Login(username=username, password=password, totp=totp, uris=tuple(u if isinstance(u, LoginUri) else LoginUri(uri=u) for u in uris))
|
|
732
|
+
cipher = Cipher(
|
|
733
|
+
type=CipherType.LOGIN,
|
|
734
|
+
name=name,
|
|
735
|
+
notes=notes,
|
|
736
|
+
favorite=favorite,
|
|
737
|
+
login=login,
|
|
738
|
+
reprompt=RepromptType.PASSWORD if reprompt else RepromptType.NONE,
|
|
739
|
+
)
|
|
740
|
+
return self._create(cipher, folder, fields, organization, collections)
|
|
741
|
+
|
|
742
|
+
def create_note(
|
|
743
|
+
self,
|
|
744
|
+
name: str,
|
|
745
|
+
notes: str,
|
|
746
|
+
*,
|
|
747
|
+
folder: FolderRef | None = None,
|
|
748
|
+
favorite: bool = False,
|
|
749
|
+
organization: OrganizationRef | None = None,
|
|
750
|
+
collections: Sequence[CollectionRef] = (),
|
|
751
|
+
fields: Mapping[str, str] | None = None,
|
|
752
|
+
) -> Cipher:
|
|
753
|
+
"""Create a secure note.
|
|
754
|
+
|
|
755
|
+
Args:
|
|
756
|
+
name: The item name.
|
|
757
|
+
notes: The note text (encrypted).
|
|
758
|
+
folder: The folder to put the item in, as [Folder][vaultwarden2.models.Folder], id or unique name.
|
|
759
|
+
None (the default) means no folder.
|
|
760
|
+
favorite: Mark the item as favorite.
|
|
761
|
+
organization: Create the item in this organization (object, id or unique name) instead of the personal vault.
|
|
762
|
+
Requires ``collections``.
|
|
763
|
+
collections: Collections of ``organization`` to put the item in (objects or ids); at least one if ``organization`` is set.
|
|
764
|
+
fields: Extra values as ``{name: value}``, stored as hidden custom fields.
|
|
765
|
+
|
|
766
|
+
Returns:
|
|
767
|
+
The created item, decrypted, as stored on the server (with its id).
|
|
768
|
+
|
|
769
|
+
Raises:
|
|
770
|
+
VaultwardenError: The folder or organization was not found, or an organization item has no collection.
|
|
771
|
+
UnsafeWriteError: The payload failed verification before upload; nothing was sent.
|
|
772
|
+
ApiError: The server rejected the item.
|
|
773
|
+
"""
|
|
774
|
+
cipher = Cipher(type=CipherType.SECURE_NOTE, name=name, notes=notes, favorite=favorite, secure_note_type=0)
|
|
775
|
+
return self._create(cipher, folder, fields, organization, collections)
|
|
776
|
+
|
|
777
|
+
def create_card(
|
|
778
|
+
self,
|
|
779
|
+
name: str,
|
|
780
|
+
*,
|
|
781
|
+
cardholder_name: str | None = None,
|
|
782
|
+
brand: str | None = None,
|
|
783
|
+
number: str | None = None,
|
|
784
|
+
exp_month: str | None = None,
|
|
785
|
+
exp_year: str | None = None,
|
|
786
|
+
code: str | None = None,
|
|
787
|
+
notes: str | None = None,
|
|
788
|
+
folder: FolderRef | None = None,
|
|
789
|
+
favorite: bool = False,
|
|
790
|
+
organization: OrganizationRef | None = None,
|
|
791
|
+
collections: Sequence[CollectionRef] = (),
|
|
792
|
+
) -> Cipher:
|
|
793
|
+
"""Create a payment card item.
|
|
794
|
+
|
|
795
|
+
Args:
|
|
796
|
+
name: The item name.
|
|
797
|
+
cardholder_name: The name on the card.
|
|
798
|
+
brand: The card brand, for example ``'Visa'``.
|
|
799
|
+
number: The card number.
|
|
800
|
+
exp_month: The expiry month, for example ``'12'``.
|
|
801
|
+
exp_year: The expiry year, for example ``'2030'``.
|
|
802
|
+
code: The security code (CVV).
|
|
803
|
+
notes: Free-text notes (encrypted).
|
|
804
|
+
folder: The folder to put the item in, as [Folder][vaultwarden2.models.Folder], id or unique name.
|
|
805
|
+
None (the default) means no folder.
|
|
806
|
+
favorite: Mark the item as favorite.
|
|
807
|
+
organization: Create the item in this organization (object, id or unique name) instead of the personal vault.
|
|
808
|
+
Requires ``collections``.
|
|
809
|
+
collections: Collections of ``organization`` to put the item in (objects or ids); at least one if ``organization`` is set.
|
|
810
|
+
|
|
811
|
+
Returns:
|
|
812
|
+
The created item, decrypted, as stored on the server (with its id).
|
|
813
|
+
|
|
814
|
+
Raises:
|
|
815
|
+
VaultwardenError: The folder or organization was not found, or an organization item has no collection.
|
|
816
|
+
UnsafeWriteError: The payload failed verification before upload; nothing was sent.
|
|
817
|
+
ApiError: The server rejected the item.
|
|
818
|
+
"""
|
|
819
|
+
card = Card(cardholder_name=cardholder_name, brand=brand, number=number, exp_month=exp_month, exp_year=exp_year, code=code)
|
|
820
|
+
return self._create(Cipher(type=CipherType.CARD, name=name, notes=notes, favorite=favorite, card=card), folder, None, organization, collections)
|
|
821
|
+
|
|
822
|
+
def create_identity(
|
|
823
|
+
self,
|
|
824
|
+
name: str,
|
|
825
|
+
*,
|
|
826
|
+
notes: str | None = None,
|
|
827
|
+
folder: FolderRef | None = None,
|
|
828
|
+
favorite: bool = False,
|
|
829
|
+
organization: OrganizationRef | None = None,
|
|
830
|
+
collections: Sequence[CollectionRef] = (),
|
|
831
|
+
**identity: str | None,
|
|
832
|
+
) -> Cipher:
|
|
833
|
+
"""Create an identity item.
|
|
834
|
+
|
|
835
|
+
Args:
|
|
836
|
+
name: The item name.
|
|
837
|
+
notes: Free-text notes (encrypted).
|
|
838
|
+
folder: The folder to put the item in, as [Folder][vaultwarden2.models.Folder], id or unique name.
|
|
839
|
+
None (the default) means no folder.
|
|
840
|
+
favorite: Mark the item as favorite.
|
|
841
|
+
organization: Create the item in this organization (object, id or unique name) instead of the personal vault.
|
|
842
|
+
Requires ``collections``.
|
|
843
|
+
collections: Collections of ``organization`` to put the item in (objects or ids); at least one if ``organization`` is set.
|
|
844
|
+
**identity: Identity fields by name, see [Identity][vaultwarden2.models.Identity]
|
|
845
|
+
(``first_name``, ``last_name``, ``email``, ``phone``, ``address1``, ...).
|
|
846
|
+
|
|
847
|
+
Returns:
|
|
848
|
+
The created item, decrypted, as stored on the server (with its id).
|
|
849
|
+
|
|
850
|
+
Raises:
|
|
851
|
+
VaultwardenError: The folder or organization was not found, or an organization item has no collection.
|
|
852
|
+
UnsafeWriteError: The payload failed verification before upload; nothing was sent.
|
|
853
|
+
ApiError: The server rejected the item.
|
|
854
|
+
TypeError: An unknown identity field was passed.
|
|
855
|
+
"""
|
|
856
|
+
cipher = Cipher(type=CipherType.IDENTITY, name=name, notes=notes, favorite=favorite, identity=Identity(**identity))
|
|
857
|
+
return self._create(cipher, folder, None, organization, collections)
|
|
858
|
+
|
|
859
|
+
def create_ssh_key(
|
|
860
|
+
self,
|
|
861
|
+
name: str,
|
|
862
|
+
*,
|
|
863
|
+
private_key: str,
|
|
864
|
+
public_key: str | None = None,
|
|
865
|
+
key_fingerprint: str | None = None,
|
|
866
|
+
notes: str | None = None,
|
|
867
|
+
folder: FolderRef | None = None,
|
|
868
|
+
favorite: bool = False,
|
|
869
|
+
organization: OrganizationRef | None = None,
|
|
870
|
+
collections: Sequence[CollectionRef] = (),
|
|
871
|
+
) -> Cipher:
|
|
872
|
+
"""Create an SSH key item.
|
|
873
|
+
|
|
874
|
+
The server and the official clients need all three of private key, public key and fingerprint; the latter
|
|
875
|
+
two are derived from the private key when not given.
|
|
876
|
+
|
|
877
|
+
Args:
|
|
878
|
+
name: The item name.
|
|
879
|
+
private_key: The private key (OpenSSH or PEM format, unencrypted).
|
|
880
|
+
public_key: The public key in OpenSSH format. Derived from ``private_key`` if omitted.
|
|
881
|
+
key_fingerprint: The ``SHA256:...`` fingerprint. Derived from ``private_key`` if omitted.
|
|
882
|
+
notes: Free-text notes (encrypted).
|
|
883
|
+
folder: The folder to put the item in, as [Folder][vaultwarden2.models.Folder], id or unique name.
|
|
884
|
+
None (the default) means no folder.
|
|
885
|
+
favorite: Mark the item as favorite.
|
|
886
|
+
organization: Create the item in this organization (object, id or unique name) instead of the personal vault.
|
|
887
|
+
Requires ``collections``.
|
|
888
|
+
collections: Collections of ``organization`` to put the item in (objects or ids); at least one if ``organization`` is set.
|
|
889
|
+
|
|
890
|
+
Returns:
|
|
891
|
+
The created item, decrypted, as stored on the server (with its id).
|
|
892
|
+
|
|
893
|
+
Raises:
|
|
894
|
+
VaultwardenError: The folder or organization was not found, or an organization item has no collection.
|
|
895
|
+
UnsafeWriteError: The payload failed verification before upload; nothing was sent.
|
|
896
|
+
ApiError: The server rejected the item.
|
|
897
|
+
CryptoError: The private key cannot be parsed and ``public_key``/``key_fingerprint`` were not given.
|
|
898
|
+
"""
|
|
899
|
+
if public_key is None or key_fingerprint is None:
|
|
900
|
+
derived_public, derived_fingerprint = crypto.ssh_public_key_and_fingerprint(private_key)
|
|
901
|
+
public_key = public_key or derived_public
|
|
902
|
+
key_fingerprint = key_fingerprint or derived_fingerprint
|
|
903
|
+
ssh_key = SshKey(private_key=private_key, public_key=public_key, key_fingerprint=key_fingerprint)
|
|
904
|
+
return self._create(Cipher(type=CipherType.SSH_KEY, name=name, notes=notes, favorite=favorite, ssh_key=ssh_key), folder, None, organization, collections)
|
|
905
|
+
|
|
906
|
+
def set_password(self, item: ItemRef, password: str) -> Cipher:
|
|
907
|
+
"""Change the password of a login item; the old password goes to the password history.
|
|
908
|
+
|
|
909
|
+
Args:
|
|
910
|
+
item: The item, as object or id.
|
|
911
|
+
password: The new password.
|
|
912
|
+
|
|
913
|
+
Returns:
|
|
914
|
+
The updated item.
|
|
915
|
+
|
|
916
|
+
Raises:
|
|
917
|
+
VaultwardenError: The item is not a login.
|
|
918
|
+
ConflictError: The item changed on the server in the meantime; nothing was sent.
|
|
919
|
+
"""
|
|
920
|
+
cipher = self._resolve_cipher(item)
|
|
921
|
+
if cipher.login is None:
|
|
922
|
+
raise VaultwardenError('item is not a login')
|
|
923
|
+
return self.update_cipher(replace(cipher, login=replace(cipher.login, password=password)))
|
|
924
|
+
|
|
925
|
+
def rename(self, item: ItemRef, name: str) -> Cipher:
|
|
926
|
+
"""Rename an item.
|
|
927
|
+
|
|
928
|
+
Args:
|
|
929
|
+
item: The item, as object or id.
|
|
930
|
+
name: The new name.
|
|
931
|
+
|
|
932
|
+
Returns:
|
|
933
|
+
The updated item.
|
|
934
|
+
|
|
935
|
+
Raises:
|
|
936
|
+
ConflictError: The name changed on the server in the meantime; nothing was sent.
|
|
937
|
+
"""
|
|
938
|
+
return self.update_cipher(replace(self._resolve_cipher(item), name=name))
|
|
939
|
+
|
|
940
|
+
def move(self, item: ItemRef, folder: FolderRef | None) -> Cipher:
|
|
941
|
+
"""Move an item to a folder. Nothing is re-encrypted.
|
|
942
|
+
|
|
943
|
+
Args:
|
|
944
|
+
item: The item, as object or id.
|
|
945
|
+
folder: The target folder (object, id or unique name), or None for no folder.
|
|
946
|
+
|
|
947
|
+
Returns:
|
|
948
|
+
The updated item.
|
|
949
|
+
|
|
950
|
+
Raises:
|
|
951
|
+
VaultwardenError: The folder was not found or the name is ambiguous.
|
|
952
|
+
"""
|
|
953
|
+
folder_id = self._resolve_folder(folder)
|
|
954
|
+
current = self._resolve_cipher(item)
|
|
955
|
+
body = {'folderId': folder_id, 'favorite': current.favorite}
|
|
956
|
+
return _core.decrypt_cipher(self.request('PUT', f'/api/ciphers/{current.id}/partial', json=body), self._require_keys())
|
|
957
|
+
|
|
958
|
+
def set_favorite(self, item: ItemRef, favorite: bool = True) -> Cipher:
|
|
959
|
+
"""Mark or unmark an item as favorite. Nothing is re-encrypted.
|
|
960
|
+
|
|
961
|
+
Args:
|
|
962
|
+
item: The item, as object or id.
|
|
963
|
+
favorite: True to mark, False to unmark.
|
|
964
|
+
|
|
965
|
+
Returns:
|
|
966
|
+
The updated item.
|
|
967
|
+
"""
|
|
968
|
+
return self._partial_update(item, folder_id=None, favorite=favorite)
|