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,914 @@
|
|
|
1
|
+
# Do not edit this file directly. It has been autogenerated from
|
|
2
|
+
# src/vaultwarden2/_async/organizations.py
|
|
3
|
+
"""Organizations, collections, members, groups and policies."""
|
|
4
|
+
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
from dataclasses import replace
|
|
8
|
+
from typing import TYPE_CHECKING, Any
|
|
9
|
+
|
|
10
|
+
from vaultwarden2 import _core, _encode, _merge, _offload, crypto
|
|
11
|
+
from vaultwarden2._json import JsonObject
|
|
12
|
+
from vaultwarden2.errors import ApiError, BulkOperationError, NotUnlockedError, UnsafeWriteError, VaultwardenError
|
|
13
|
+
from vaultwarden2.models import (
|
|
14
|
+
Cipher,
|
|
15
|
+
Collection,
|
|
16
|
+
CollectionAccess,
|
|
17
|
+
Group,
|
|
18
|
+
Member,
|
|
19
|
+
Organization,
|
|
20
|
+
OrganizationExport,
|
|
21
|
+
OrganizationUserType,
|
|
22
|
+
Policy,
|
|
23
|
+
PolicyType,
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
if TYPE_CHECKING:
|
|
27
|
+
from collections.abc import Sequence
|
|
28
|
+
|
|
29
|
+
from vaultwarden2.models import CollectionRef, GroupRef, ItemRef, MemberRef, OrganizationRef
|
|
30
|
+
|
|
31
|
+
from . import vault as _vault_mod
|
|
32
|
+
from .base import _collection_access, _collection_id, _item_id
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class OrganizationsMixin(_vault_mod.VaultMixin):
|
|
36
|
+
"""Organizations and everything inside them.
|
|
37
|
+
|
|
38
|
+
Organization items are encrypted with an item key wrapped with the organization key (older items directly with
|
|
39
|
+
the organization key). Every confirmed member receives the organization key encrypted to their own public key. Admin rights in the organization are needed for most operations here.
|
|
40
|
+
"""
|
|
41
|
+
|
|
42
|
+
def create_organization(self, name: str, *, billing_email: str | None = None, collection_name: str = 'Default collection') -> Organization:
|
|
43
|
+
"""Create an organization owned by the logged-in user, with one initial collection.
|
|
44
|
+
|
|
45
|
+
The organization key and the organization's RSA key pair are generated on this machine.
|
|
46
|
+
|
|
47
|
+
Args:
|
|
48
|
+
name: The organization name (plain text on the server).
|
|
49
|
+
billing_email: The billing contact. Defaults to the account's email.
|
|
50
|
+
collection_name: Name of the first collection (encrypted).
|
|
51
|
+
|
|
52
|
+
Returns:
|
|
53
|
+
The new organization, with the user as owner.
|
|
54
|
+
|
|
55
|
+
Raises:
|
|
56
|
+
NotUnlockedError: The account has no key pair.
|
|
57
|
+
ApiError: The server refused (for example organization creation is restricted).
|
|
58
|
+
"""
|
|
59
|
+
keys = self._require_keys()
|
|
60
|
+
if keys.private_key is None:
|
|
61
|
+
raise NotUnlockedError('account has no key pair')
|
|
62
|
+
profile = self._profile or self._load_profile()
|
|
63
|
+
payload, org_key = _offload.run_cpu(
|
|
64
|
+
_core.organization_payload, name, billing_email or profile.get_str('email') or '', collection_name, keys.private_key.public_key()
|
|
65
|
+
)
|
|
66
|
+
body = JsonObject(self.request('POST', '/api/organizations', json=payload))
|
|
67
|
+
org_id = body.get_str('id') or ''
|
|
68
|
+
keys.org_keys[org_id] = org_key
|
|
69
|
+
for org in self.organizations():
|
|
70
|
+
if org.id == org_id:
|
|
71
|
+
return org
|
|
72
|
+
raise ApiError(500, 'created organization not found in profile')
|
|
73
|
+
|
|
74
|
+
def rename_organization(self, organization: OrganizationRef, name: str, *, billing_email: str | None = None) -> None:
|
|
75
|
+
"""Rename an organization (owner only).
|
|
76
|
+
|
|
77
|
+
Args:
|
|
78
|
+
organization: The organization, as object, id or unique name.
|
|
79
|
+
name: The new name.
|
|
80
|
+
billing_email: A new billing email. Defaults to keeping the current one.
|
|
81
|
+
|
|
82
|
+
Raises:
|
|
83
|
+
UnsafeWriteError: The server did not return the current billing email and none was given; nothing was sent.
|
|
84
|
+
VaultwardenError: The organization or a referenced member, group or collection was not found.
|
|
85
|
+
ApiError: Not the owner.
|
|
86
|
+
"""
|
|
87
|
+
org_id = self._resolve_organization(organization)
|
|
88
|
+
if billing_email is None:
|
|
89
|
+
billing_email = JsonObject(self.request('GET', f'/api/organizations/{org_id}')).get_str('billingEmail')
|
|
90
|
+
if not billing_email:
|
|
91
|
+
raise UnsafeWriteError('the server did not return the billing email; pass billing_email= to avoid clearing it')
|
|
92
|
+
self.request('PUT', f'/api/organizations/{org_id}', json={'name': name, 'billingEmail': billing_email})
|
|
93
|
+
|
|
94
|
+
def delete_organization(self, organization: OrganizationRef, password: str) -> None:
|
|
95
|
+
"""Delete an organization with all its items and collections. This cannot be undone.
|
|
96
|
+
|
|
97
|
+
Args:
|
|
98
|
+
organization: The organization, as object, id or unique name.
|
|
99
|
+
password: The master password of the owner (the server re-checks it).
|
|
100
|
+
|
|
101
|
+
Raises:
|
|
102
|
+
VaultwardenError: The organization or a referenced member, group or collection was not found.
|
|
103
|
+
ApiError: Wrong password or not the owner.
|
|
104
|
+
"""
|
|
105
|
+
org_id = self._resolve_organization(organization)
|
|
106
|
+
self.request('DELETE', f'/api/organizations/{org_id}', json={'masterPasswordHash': self._password_hash(password)})
|
|
107
|
+
self._require_keys().org_keys.pop(org_id, None)
|
|
108
|
+
|
|
109
|
+
def leave_organization(self, organization: OrganizationRef) -> None:
|
|
110
|
+
"""Leave an organization. Its items are no longer accessible afterwards.
|
|
111
|
+
|
|
112
|
+
Args:
|
|
113
|
+
organization: The organization, as object, id or unique name.
|
|
114
|
+
|
|
115
|
+
Raises:
|
|
116
|
+
VaultwardenError: The organization or a referenced member, group or collection was not found.
|
|
117
|
+
ApiError: The server refused (for example the last owner cannot leave).
|
|
118
|
+
"""
|
|
119
|
+
org_id = self._resolve_organization(organization)
|
|
120
|
+
self.request('POST', f'/api/organizations/{org_id}/leave')
|
|
121
|
+
self._require_keys().org_keys.pop(org_id, None)
|
|
122
|
+
|
|
123
|
+
def organization_collections(self, organization: OrganizationRef) -> list[Collection]:
|
|
124
|
+
"""All collections of an organization (admin view), decrypted.
|
|
125
|
+
|
|
126
|
+
Args:
|
|
127
|
+
organization: The organization, as object, id or unique name.
|
|
128
|
+
|
|
129
|
+
Returns:
|
|
130
|
+
The collections, including ones the user is not assigned to.
|
|
131
|
+
|
|
132
|
+
Raises:
|
|
133
|
+
VaultwardenError: The organization or a referenced member, group or collection was not found.
|
|
134
|
+
"""
|
|
135
|
+
org_id = self._resolve_organization(organization)
|
|
136
|
+
body = JsonObject(self.request('GET', f'/api/organizations/{org_id}/collections'))
|
|
137
|
+
keys = self._require_keys()
|
|
138
|
+
return [_core.decrypt_collection({**c.raw, 'organizationId': c.get_str('organizationId') or org_id}, keys) for c in body.objs('data')]
|
|
139
|
+
|
|
140
|
+
def create_collection(
|
|
141
|
+
self,
|
|
142
|
+
organization: OrganizationRef,
|
|
143
|
+
name: str,
|
|
144
|
+
*,
|
|
145
|
+
members: Sequence[tuple[MemberRef, CollectionAccess | None]] = (),
|
|
146
|
+
external_id: str | None = None,
|
|
147
|
+
) -> Collection:
|
|
148
|
+
"""Create a collection.
|
|
149
|
+
|
|
150
|
+
Collections are stored flat. The web vault and the apps nest them by name: `Ops/Servers` is shown inside `Ops`.
|
|
151
|
+
|
|
152
|
+
Args:
|
|
153
|
+
organization: The organization, as object, id or unique name.
|
|
154
|
+
name: The collection name (encrypted with the organization key). Use `/` to nest it under another collection.
|
|
155
|
+
members: Members to give access, as ``(member, access)`` pairs. ``access`` is a
|
|
156
|
+
[CollectionAccess][vaultwarden2.models.CollectionAccess] for read-only / hide-passwords / manage rights,
|
|
157
|
+
or None for normal access. ``member`` is an object, membership id or email.
|
|
158
|
+
external_id: An identifier for directory synchronisation.
|
|
159
|
+
|
|
160
|
+
Returns:
|
|
161
|
+
The new collection.
|
|
162
|
+
|
|
163
|
+
Raises:
|
|
164
|
+
VaultwardenError: The organization or a referenced member, group or collection was not found.
|
|
165
|
+
"""
|
|
166
|
+
org_id = self._resolve_organization(organization)
|
|
167
|
+
org_key = self._org_key(org_id)
|
|
168
|
+
users = [self._member_access(org_id, member, access) for member, access in members]
|
|
169
|
+
payload = {'name': _merge.verified_encrypt(name, org_key), 'externalId': external_id, 'groups': [], 'users': users}
|
|
170
|
+
body = self.request('POST', f'/api/organizations/{org_id}/collections', json=payload)
|
|
171
|
+
return _core.decrypt_collection({**body, 'organizationId': org_id}, self._require_keys())
|
|
172
|
+
|
|
173
|
+
def rename_collection(self, collection: Collection, name: str) -> Collection:
|
|
174
|
+
"""Rename a collection; member and group access stay as they are.
|
|
175
|
+
|
|
176
|
+
Args:
|
|
177
|
+
collection: The collection (from ``organization_collections`` or ``collections``).
|
|
178
|
+
name: The new name.
|
|
179
|
+
|
|
180
|
+
Returns:
|
|
181
|
+
The renamed collection.
|
|
182
|
+
|
|
183
|
+
Raises:
|
|
184
|
+
UnsafeWriteError: The server did not return the current access lists, so they could not be kept;
|
|
185
|
+
nothing was sent.
|
|
186
|
+
"""
|
|
187
|
+
org_id = collection.organization_id
|
|
188
|
+
details = JsonObject(self.request('GET', f'/api/organizations/{org_id}/collections/{collection.id}/details'))
|
|
189
|
+
users = self.request('GET', f'/api/organizations/{org_id}/collections/{collection.id}/users')
|
|
190
|
+
payload = {
|
|
191
|
+
'name': _merge.verified_encrypt(name, self._org_key(org_id)),
|
|
192
|
+
'externalId': details.get_str('externalId'),
|
|
193
|
+
'groups': _existing_list(details.get('groups'), "the collection's group access"),
|
|
194
|
+
'users': _existing_list(users, "the collection's member access"),
|
|
195
|
+
}
|
|
196
|
+
body = self.request('PUT', f'/api/organizations/{org_id}/collections/{collection.id}', json=payload)
|
|
197
|
+
return _core.decrypt_collection({**body, 'organizationId': org_id}, self._require_keys())
|
|
198
|
+
|
|
199
|
+
def delete_collection(self, collection: Collection) -> None:
|
|
200
|
+
"""Delete a collection. Its items stay in the organization.
|
|
201
|
+
|
|
202
|
+
Args:
|
|
203
|
+
collection: The collection.
|
|
204
|
+
"""
|
|
205
|
+
self.request('DELETE', f'/api/organizations/{collection.organization_id}/collections/{collection.id}')
|
|
206
|
+
|
|
207
|
+
def organization_ciphers(self, organization: OrganizationRef) -> list[Cipher]:
|
|
208
|
+
"""All items of an organization (admin view), decrypted.
|
|
209
|
+
|
|
210
|
+
Args:
|
|
211
|
+
organization: The organization, as object, id or unique name.
|
|
212
|
+
|
|
213
|
+
Returns:
|
|
214
|
+
The organization's items, including ones in collections the user is not assigned to.
|
|
215
|
+
|
|
216
|
+
Raises:
|
|
217
|
+
CryptoError: An item cannot be decrypted.
|
|
218
|
+
"""
|
|
219
|
+
org_id = self._resolve_organization(organization)
|
|
220
|
+
body = JsonObject(self.request('GET', '/api/ciphers/organization-details', params={'organizationId': org_id}))
|
|
221
|
+
return _offload.run_cpu(_core.decrypt_ciphers, [c.raw for c in body.objs('data')], self._require_keys())
|
|
222
|
+
|
|
223
|
+
def move_to_organization(self, item: ItemRef, organization: OrganizationRef, collections: Sequence[CollectionRef]) -> Cipher:
|
|
224
|
+
"""Move a personal item, with its attachments, into an organization.
|
|
225
|
+
|
|
226
|
+
Everything is re-encrypted with the organization key: fields, attachment keys, and any other ciphertext in the
|
|
227
|
+
item (such as passkeys). The payload is verified before it is sent.
|
|
228
|
+
|
|
229
|
+
Args:
|
|
230
|
+
item: The personal item, as object or id.
|
|
231
|
+
organization: The organization, as object, id or unique name.
|
|
232
|
+
collections: The collections to put the item in (objects or ids); at least one.
|
|
233
|
+
|
|
234
|
+
Returns:
|
|
235
|
+
The item, now owned by the organization.
|
|
236
|
+
|
|
237
|
+
Raises:
|
|
238
|
+
VaultwardenError: No collections given, or the item already belongs to an organization.
|
|
239
|
+
UnsafeWriteError: The item holds data that cannot be re-encrypted (unknown key or format), an old-style
|
|
240
|
+
attachment without its own key, or verification failed. Nothing was sent.
|
|
241
|
+
"""
|
|
242
|
+
if not collections:
|
|
243
|
+
raise VaultwardenError('organization items need at least one collection')
|
|
244
|
+
org_id = self._resolve_organization(organization)
|
|
245
|
+
keys = self._require_keys()
|
|
246
|
+
cipher = self._resolve_cipher(item)
|
|
247
|
+
if cipher.organization_id:
|
|
248
|
+
raise VaultwardenError('item already belongs to an organization')
|
|
249
|
+
body = _offload.run_cpu(_encode.encode_share, cipher, keys, self._user_id(), org_id)
|
|
250
|
+
_offload.run_cpu(_merge.verify_new, body, keys, replace(cipher, organization_id=org_id))
|
|
251
|
+
payload = {'cipher': body, 'collectionIds': [_collection_id(c) for c in collections]}
|
|
252
|
+
return _core.decrypt_cipher(self.request('PUT', f'/api/ciphers/{cipher.id}/share', json=payload), keys)
|
|
253
|
+
|
|
254
|
+
def set_collections(self, item: ItemRef, collections: Sequence[CollectionRef]) -> Cipher:
|
|
255
|
+
"""Replace the collections an organization item belongs to.
|
|
256
|
+
|
|
257
|
+
Args:
|
|
258
|
+
item: The organization item, as object or id.
|
|
259
|
+
collections: The new collections (objects or ids); at least one, so the item stays reachable.
|
|
260
|
+
|
|
261
|
+
Returns:
|
|
262
|
+
The updated item.
|
|
263
|
+
|
|
264
|
+
Raises:
|
|
265
|
+
UnsafeWriteError: The list is empty; nothing was sent.
|
|
266
|
+
"""
|
|
267
|
+
if not collections:
|
|
268
|
+
raise UnsafeWriteError('an organization item needs at least one collection')
|
|
269
|
+
item_id = _item_id(item)
|
|
270
|
+
self.request('PUT', f'/api/ciphers/{item_id}/collections_v2', json={'collectionIds': [_collection_id(c) for c in collections]})
|
|
271
|
+
return self.get_cipher(item_id)
|
|
272
|
+
|
|
273
|
+
def members(self, organization: OrganizationRef) -> list[Member]:
|
|
274
|
+
"""The members of an organization, with role, status and collection access.
|
|
275
|
+
|
|
276
|
+
Args:
|
|
277
|
+
organization: The organization, as object, id or unique name.
|
|
278
|
+
|
|
279
|
+
Returns:
|
|
280
|
+
One entry per membership (including invited and revoked ones).
|
|
281
|
+
"""
|
|
282
|
+
org_id = self._resolve_organization(organization)
|
|
283
|
+
body = JsonObject(self.request('GET', f'/api/organizations/{org_id}/users', params={'includeCollections': 'true'}))
|
|
284
|
+
return [_core.parse_member(m.raw, org_id) for m in body.objs('data')]
|
|
285
|
+
|
|
286
|
+
def _resolve_member(self, org_id: str, member: MemberRef) -> Member:
|
|
287
|
+
"""Find a membership by object, id or email.
|
|
288
|
+
|
|
289
|
+
Args:
|
|
290
|
+
org_id: The organization id.
|
|
291
|
+
member: The member reference.
|
|
292
|
+
|
|
293
|
+
Returns:
|
|
294
|
+
The membership.
|
|
295
|
+
|
|
296
|
+
Raises:
|
|
297
|
+
VaultwardenError: No such member.
|
|
298
|
+
"""
|
|
299
|
+
for m in self.members(org_id):
|
|
300
|
+
if m.id == (member.id if isinstance(member, Member) else member) or (isinstance(member, str) and m.email and m.email.lower() == member.lower()):
|
|
301
|
+
return m
|
|
302
|
+
raise VaultwardenError(f'member {member if isinstance(member, str) else member.id!r} not found')
|
|
303
|
+
|
|
304
|
+
def _member_access(self, org_id: str, member: MemberRef, access: CollectionAccess | None) -> dict[str, Any]:
|
|
305
|
+
"""Collection access payload for one member.
|
|
306
|
+
|
|
307
|
+
Args:
|
|
308
|
+
org_id: The organization id.
|
|
309
|
+
member: The member reference.
|
|
310
|
+
access: The access rights, or None for normal access.
|
|
311
|
+
|
|
312
|
+
Returns:
|
|
313
|
+
The access entry as the server expects it.
|
|
314
|
+
"""
|
|
315
|
+
resolved = self._resolve_member(org_id, member)
|
|
316
|
+
return _core.collection_access_payload(replace(access or CollectionAccess(id=''), id=resolved.id))
|
|
317
|
+
|
|
318
|
+
def invite(
|
|
319
|
+
self,
|
|
320
|
+
organization: OrganizationRef,
|
|
321
|
+
emails: str | Sequence[str],
|
|
322
|
+
*,
|
|
323
|
+
role: OrganizationUserType = OrganizationUserType.USER,
|
|
324
|
+
collections: Sequence[CollectionRef] = (),
|
|
325
|
+
groups: Sequence[GroupRef] = (),
|
|
326
|
+
) -> None:
|
|
327
|
+
"""Invite users to an organization.
|
|
328
|
+
|
|
329
|
+
On servers that send no emails (neither ``SMTP_HOST`` nor ``USE_SENDMAIL`` configured), existing accounts are accepted immediately and only need
|
|
330
|
+
[confirm_member][vaultwarden2.VaultwardenClient.confirm_member]; otherwise the user accepts the mailed
|
|
331
|
+
invitation first.
|
|
332
|
+
|
|
333
|
+
Args:
|
|
334
|
+
organization: The organization, as object, id or unique name.
|
|
335
|
+
emails: One email address or several.
|
|
336
|
+
role: The role of the new members.
|
|
337
|
+
collections: Collections, as objects or ids, or [CollectionAccess][vaultwarden2.models.CollectionAccess] entries
|
|
338
|
+
to set read-only, hide-passwords or manage rights.
|
|
339
|
+
groups: Groups to add the new members to, as objects, ids or unique names.
|
|
340
|
+
|
|
341
|
+
Raises:
|
|
342
|
+
VaultwardenError: The organization or a referenced member, group or collection was not found.
|
|
343
|
+
ApiError: The server refused (for example already a member, or invitations disabled).
|
|
344
|
+
"""
|
|
345
|
+
org_id = self._resolve_organization(organization)
|
|
346
|
+
payload = {
|
|
347
|
+
'emails': [emails] if isinstance(emails, str) else list(emails),
|
|
348
|
+
'type': int(role),
|
|
349
|
+
'groups': [self._resolve_group(org_id, g) for g in groups],
|
|
350
|
+
'collections': [_core.collection_access_payload(_collection_access(c)) for c in collections],
|
|
351
|
+
'permissions': {},
|
|
352
|
+
}
|
|
353
|
+
self.request('POST', f'/api/organizations/{org_id}/users/invite', json=payload)
|
|
354
|
+
|
|
355
|
+
def confirm_member(self, organization: OrganizationRef, member: MemberRef, *, expected_public_key: str | None = None) -> Member:
|
|
356
|
+
"""Confirm an accepted member: share the organization key with them.
|
|
357
|
+
|
|
358
|
+
The key is encrypted to the member's public key as provided by the server. A malicious server could substitute
|
|
359
|
+
that key; compare it out of band and pass it as ``expected_public_key`` to rule that out.
|
|
360
|
+
|
|
361
|
+
Args:
|
|
362
|
+
organization: The organization, as object, id or unique name.
|
|
363
|
+
member: The member, as [Member][vaultwarden2.models.Member], membership id or email address.
|
|
364
|
+
expected_public_key: The member's public key (base64, as returned by
|
|
365
|
+
[member_public_key][vaultwarden2.VaultwardenClient.member_public_key]) verified out of band.
|
|
366
|
+
|
|
367
|
+
Returns:
|
|
368
|
+
The confirmed membership.
|
|
369
|
+
|
|
370
|
+
Raises:
|
|
371
|
+
VaultwardenError: The member has no account yet, or the key does not match ``expected_public_key``
|
|
372
|
+
(nothing was sent).
|
|
373
|
+
"""
|
|
374
|
+
org_id = self._resolve_organization(organization)
|
|
375
|
+
resolved = self._resolve_member(org_id, member)
|
|
376
|
+
if not resolved.user_id:
|
|
377
|
+
raise VaultwardenError('member has no account yet (invitation not accepted)')
|
|
378
|
+
public_b64 = self.member_public_key(resolved.user_id)
|
|
379
|
+
if expected_public_key is not None and expected_public_key != public_b64:
|
|
380
|
+
raise VaultwardenError('member public key does not match the expected key; not confirming')
|
|
381
|
+
encrypted = crypto.rsa_encrypt(self._org_key(org_id).to_bytes(), crypto.load_public_key(public_b64))
|
|
382
|
+
self.request('POST', f'/api/organizations/{org_id}/users/{resolved.id}/confirm', json={'key': str(encrypted)})
|
|
383
|
+
return self._resolve_member(org_id, resolved.id)
|
|
384
|
+
|
|
385
|
+
def member_public_key(self, user_id: str) -> str:
|
|
386
|
+
"""The public key of a user, as stored on the server.
|
|
387
|
+
|
|
388
|
+
Args:
|
|
389
|
+
user_id: The user id (``Member.user_id``, not the membership id).
|
|
390
|
+
|
|
391
|
+
Returns:
|
|
392
|
+
The base64 public key.
|
|
393
|
+
|
|
394
|
+
Raises:
|
|
395
|
+
ApiError: No such user, or the server returned no key.
|
|
396
|
+
"""
|
|
397
|
+
key = JsonObject(self.request('GET', f'/api/users/{user_id}/public-key')).get_str('publicKey')
|
|
398
|
+
if not key:
|
|
399
|
+
raise ApiError(500, 'server returned no public key')
|
|
400
|
+
return key
|
|
401
|
+
|
|
402
|
+
def update_member(
|
|
403
|
+
self,
|
|
404
|
+
organization: OrganizationRef,
|
|
405
|
+
member: MemberRef,
|
|
406
|
+
*,
|
|
407
|
+
role: OrganizationUserType | None = None,
|
|
408
|
+
collections: Sequence[CollectionRef] | None = None,
|
|
409
|
+
groups: Sequence[GroupRef] | None = None,
|
|
410
|
+
) -> Member:
|
|
411
|
+
"""Change a member's role, collection access and/or groups.
|
|
412
|
+
|
|
413
|
+
Admins and owners always have access to all collections; the server drops per-collection assignments for them.
|
|
414
|
+
|
|
415
|
+
Args:
|
|
416
|
+
organization: The organization, as object, id or unique name.
|
|
417
|
+
member: The member, as [Member][vaultwarden2.models.Member], membership id or email address.
|
|
418
|
+
role: The new role. None keeps the current one.
|
|
419
|
+
collections: Collections, as objects or ids, or [CollectionAccess][vaultwarden2.models.CollectionAccess] entries
|
|
420
|
+
to set read-only, hide-passwords or manage rights. None keeps the current access; an empty list removes all.
|
|
421
|
+
groups: The groups (objects, ids or unique names). None keeps the current groups.
|
|
422
|
+
|
|
423
|
+
Returns:
|
|
424
|
+
The updated membership.
|
|
425
|
+
|
|
426
|
+
Raises:
|
|
427
|
+
UnsafeWriteError: The server did not return data the update must send back (current access, groups or
|
|
428
|
+
permissions); nothing was sent.
|
|
429
|
+
VaultwardenError: The organization or a referenced member, group or collection was not found.
|
|
430
|
+
"""
|
|
431
|
+
org_id = self._resolve_organization(organization)
|
|
432
|
+
current = self._resolve_member(org_id, member)
|
|
433
|
+
if collections is None:
|
|
434
|
+
current_access = _existing_list(JsonObject(current.raw).get('collections'), "the member's collection access")
|
|
435
|
+
else:
|
|
436
|
+
current_access = [_core.collection_access_payload(_collection_access(c)) for c in collections]
|
|
437
|
+
# vaultwarden reports every manager as "custom" (type 4); `permissions` is null when the member has no extra
|
|
438
|
+
# permissions (only "manage all collections" is supported, as access_all). Null is sent back as {}, which
|
|
439
|
+
# the server reads the same way. A missing key means the answer is incomplete: refuse.
|
|
440
|
+
if 'permissions' not in JsonObject(current.raw):
|
|
441
|
+
raise UnsafeWriteError("the server did not return the member's permissions; not updating to avoid losing them")
|
|
442
|
+
permissions = JsonObject(current.raw).get('permissions')
|
|
443
|
+
payload = {
|
|
444
|
+
'type': int(role if role is not None else (current.type if current.type is not None else OrganizationUserType.USER)),
|
|
445
|
+
'collections': current_access,
|
|
446
|
+
'groups': [self._resolve_group(org_id, g) for g in groups] if groups is not None else self._member_group_ids(org_id, current.id),
|
|
447
|
+
'permissions': permissions if isinstance(permissions, dict) else {},
|
|
448
|
+
}
|
|
449
|
+
self.request('PUT', f'/api/organizations/{org_id}/users/{current.id}', json=payload)
|
|
450
|
+
return self._resolve_member(org_id, current.id)
|
|
451
|
+
|
|
452
|
+
def revoke_member(self, organization: OrganizationRef, member: MemberRef) -> None:
|
|
453
|
+
"""Revoke a member's access. The membership is kept and can be restored.
|
|
454
|
+
|
|
455
|
+
Args:
|
|
456
|
+
organization: The organization, as object, id or unique name.
|
|
457
|
+
member: The member, as [Member][vaultwarden2.models.Member], membership id or email address.
|
|
458
|
+
"""
|
|
459
|
+
org_id = self._resolve_organization(organization)
|
|
460
|
+
self.request('PUT', f'/api/organizations/{org_id}/users/{(self._resolve_member(org_id, member)).id}/revoke')
|
|
461
|
+
|
|
462
|
+
def restore_member(self, organization: OrganizationRef, member: MemberRef) -> None:
|
|
463
|
+
"""Restore a revoked member.
|
|
464
|
+
|
|
465
|
+
Args:
|
|
466
|
+
organization: The organization, as object, id or unique name.
|
|
467
|
+
member: The member, as [Member][vaultwarden2.models.Member], membership id or email address.
|
|
468
|
+
"""
|
|
469
|
+
org_id = self._resolve_organization(organization)
|
|
470
|
+
self.request('PUT', f'/api/organizations/{org_id}/users/{(self._resolve_member(org_id, member)).id}/restore')
|
|
471
|
+
|
|
472
|
+
def remove_member(self, organization: OrganizationRef, member: MemberRef) -> None:
|
|
473
|
+
"""Remove a member from the organization.
|
|
474
|
+
|
|
475
|
+
Args:
|
|
476
|
+
organization: The organization, as object, id or unique name.
|
|
477
|
+
member: The member, as [Member][vaultwarden2.models.Member], membership id or email address.
|
|
478
|
+
"""
|
|
479
|
+
org_id = self._resolve_organization(organization)
|
|
480
|
+
self.request('DELETE', f'/api/organizations/{org_id}/users/{(self._resolve_member(org_id, member)).id}')
|
|
481
|
+
|
|
482
|
+
def _member_ids(self, org_id: str, members: Sequence[MemberRef] | None, *, status: int | None = None) -> list[str]:
|
|
483
|
+
"""Membership ids for member references, or all members with a given status.
|
|
484
|
+
|
|
485
|
+
Args:
|
|
486
|
+
org_id: The organization id.
|
|
487
|
+
members: The member references, or None for all members.
|
|
488
|
+
status: With ``members`` None, only members in this status.
|
|
489
|
+
|
|
490
|
+
Returns:
|
|
491
|
+
The membership ids.
|
|
492
|
+
|
|
493
|
+
Raises:
|
|
494
|
+
VaultwardenError: A referenced member was not found.
|
|
495
|
+
"""
|
|
496
|
+
all_members = self.members(org_id)
|
|
497
|
+
if members is None:
|
|
498
|
+
return [m.id for m in all_members if status is None or m.status == status]
|
|
499
|
+
ids = []
|
|
500
|
+
for ref in members:
|
|
501
|
+
key = ref.id if isinstance(ref, Member) else ref
|
|
502
|
+
match = next((m for m in all_members if m.id == key or (m.email and m.email.lower() == key.lower())), None)
|
|
503
|
+
if match is None:
|
|
504
|
+
raise VaultwardenError(f'member {key!r} not found')
|
|
505
|
+
ids.append(match.id)
|
|
506
|
+
return ids
|
|
507
|
+
|
|
508
|
+
@staticmethod
|
|
509
|
+
def _bulk_result(body: Any) -> None:
|
|
510
|
+
"""Raise if a bulk member response reports failures.
|
|
511
|
+
|
|
512
|
+
Args:
|
|
513
|
+
body: The bulk response (``{data: [{id, error}]}``).
|
|
514
|
+
|
|
515
|
+
Raises:
|
|
516
|
+
BulkOperationError: At least one entry failed.
|
|
517
|
+
"""
|
|
518
|
+
failures = {}
|
|
519
|
+
for entry in JsonObject(body).objs('data'):
|
|
520
|
+
error = entry.get_str('error')
|
|
521
|
+
if error:
|
|
522
|
+
failures[entry.get_str('id') or '?'] = error
|
|
523
|
+
if failures:
|
|
524
|
+
raise BulkOperationError(failures)
|
|
525
|
+
|
|
526
|
+
def confirm_members(self, organization: OrganizationRef, members: Sequence[MemberRef] | None = None) -> list[str]:
|
|
527
|
+
"""Confirm several accepted members at once.
|
|
528
|
+
|
|
529
|
+
Public keys come from the server unverified; use ``confirm_member`` with ``expected_public_key`` where that
|
|
530
|
+
matters.
|
|
531
|
+
|
|
532
|
+
Args:
|
|
533
|
+
organization: The organization, as object, id or unique name.
|
|
534
|
+
members: The members, each as object, membership id or email address. None confirms every accepted member.
|
|
535
|
+
|
|
536
|
+
Returns:
|
|
537
|
+
The confirmed membership ids.
|
|
538
|
+
|
|
539
|
+
Raises:
|
|
540
|
+
BulkOperationError: Some confirmations failed; the others were applied.
|
|
541
|
+
"""
|
|
542
|
+
org_id = self._resolve_organization(organization)
|
|
543
|
+
ids = self._member_ids(org_id, members, status=1 if members is None else None)
|
|
544
|
+
if not ids:
|
|
545
|
+
return []
|
|
546
|
+
org_key = self._org_key(org_id)
|
|
547
|
+
body = JsonObject(self.request('POST', f'/api/organizations/{org_id}/users/public-keys', json={'ids': ids}))
|
|
548
|
+
keys = [
|
|
549
|
+
{'id': entry.get_str('id'), 'key': str(crypto.rsa_encrypt(org_key.to_bytes(), crypto.load_public_key(entry.get_str('key') or '')))}
|
|
550
|
+
for entry in body.objs('data')
|
|
551
|
+
]
|
|
552
|
+
self._bulk_result(self.request('POST', f'/api/organizations/{org_id}/users/confirm', json={'keys': keys}))
|
|
553
|
+
return [k['id'] or '' for k in keys]
|
|
554
|
+
|
|
555
|
+
def remove_members(self, organization: OrganizationRef, members: Sequence[MemberRef]) -> None:
|
|
556
|
+
"""Remove several members.
|
|
557
|
+
|
|
558
|
+
Args:
|
|
559
|
+
organization: The organization, as object, id or unique name.
|
|
560
|
+
members: The members, each as object, membership id or email address.
|
|
561
|
+
|
|
562
|
+
Raises:
|
|
563
|
+
BulkOperationError: Some removals failed; the others were applied.
|
|
564
|
+
"""
|
|
565
|
+
org_id = self._resolve_organization(organization)
|
|
566
|
+
ids = self._member_ids(org_id, members)
|
|
567
|
+
self._bulk_result(self.request('DELETE', f'/api/organizations/{org_id}/users', json={'ids': ids}))
|
|
568
|
+
|
|
569
|
+
def revoke_members(self, organization: OrganizationRef, members: Sequence[MemberRef]) -> None:
|
|
570
|
+
"""Revoke several members.
|
|
571
|
+
|
|
572
|
+
Args:
|
|
573
|
+
organization: The organization, as object, id or unique name.
|
|
574
|
+
members: The members, each as object, membership id or email address.
|
|
575
|
+
|
|
576
|
+
Raises:
|
|
577
|
+
BulkOperationError: Some failed; the others were applied.
|
|
578
|
+
"""
|
|
579
|
+
org_id = self._resolve_organization(organization)
|
|
580
|
+
ids = self._member_ids(org_id, members)
|
|
581
|
+
self._bulk_result(self.request('PUT', f'/api/organizations/{org_id}/users/revoke', json={'ids': ids}))
|
|
582
|
+
|
|
583
|
+
def restore_members(self, organization: OrganizationRef, members: Sequence[MemberRef]) -> None:
|
|
584
|
+
"""Restore several revoked members.
|
|
585
|
+
|
|
586
|
+
Args:
|
|
587
|
+
organization: The organization, as object, id or unique name.
|
|
588
|
+
members: The members, each as object, membership id or email address.
|
|
589
|
+
|
|
590
|
+
Raises:
|
|
591
|
+
BulkOperationError: Some failed; the others were applied.
|
|
592
|
+
"""
|
|
593
|
+
org_id = self._resolve_organization(organization)
|
|
594
|
+
ids = self._member_ids(org_id, members)
|
|
595
|
+
self._bulk_result(self.request('PUT', f'/api/organizations/{org_id}/users/restore', json={'ids': ids}))
|
|
596
|
+
|
|
597
|
+
def groups(self, organization: OrganizationRef) -> list[Group]:
|
|
598
|
+
"""The groups of an organization with their collection access.
|
|
599
|
+
|
|
600
|
+
Needs ``ORG_GROUPS_ENABLED=true`` on the server.
|
|
601
|
+
|
|
602
|
+
Args:
|
|
603
|
+
organization: The organization, as object, id or unique name.
|
|
604
|
+
|
|
605
|
+
Returns:
|
|
606
|
+
The groups.
|
|
607
|
+
"""
|
|
608
|
+
org_id = self._resolve_organization(organization)
|
|
609
|
+
body = JsonObject(self.request('GET', f'/api/organizations/{org_id}/groups/details'))
|
|
610
|
+
return [_core.parse_group(g.raw, org_id) for g in body.objs('data')]
|
|
611
|
+
|
|
612
|
+
def _resolve_group(self, org_id: str, group: GroupRef) -> str:
|
|
613
|
+
"""Turn a group reference into an id.
|
|
614
|
+
|
|
615
|
+
Args:
|
|
616
|
+
org_id: The organization id.
|
|
617
|
+
group: A group, its id or its unique name.
|
|
618
|
+
|
|
619
|
+
Returns:
|
|
620
|
+
The group id.
|
|
621
|
+
|
|
622
|
+
Raises:
|
|
623
|
+
VaultwardenError: Not found, or the name is ambiguous.
|
|
624
|
+
"""
|
|
625
|
+
if isinstance(group, Group):
|
|
626
|
+
return group.id
|
|
627
|
+
groups = self.groups(org_id)
|
|
628
|
+
if any(g.id == group for g in groups):
|
|
629
|
+
return group
|
|
630
|
+
matches = [g.id for g in groups if g.name == group]
|
|
631
|
+
if len(matches) != 1:
|
|
632
|
+
raise VaultwardenError(f'group {group!r} not found' if not matches else f'group name {group!r} is ambiguous; pass the id')
|
|
633
|
+
return matches[0]
|
|
634
|
+
|
|
635
|
+
def _member_group_ids(self, org_id: str, member_id: str) -> list[str]:
|
|
636
|
+
"""The groups a member belongs to.
|
|
637
|
+
|
|
638
|
+
Args:
|
|
639
|
+
org_id: The organization id.
|
|
640
|
+
member_id: The membership id.
|
|
641
|
+
|
|
642
|
+
Returns:
|
|
643
|
+
The group ids.
|
|
644
|
+
|
|
645
|
+
Raises:
|
|
646
|
+
UnsafeWriteError: The server did not return the member's groups.
|
|
647
|
+
"""
|
|
648
|
+
body = self.request('GET', f'/api/organizations/{org_id}/users/{member_id}', params={'includeGroups': 'true'})
|
|
649
|
+
return [g for g in _existing_list(JsonObject(body).get('groups'), "the member's groups") if isinstance(g, str)]
|
|
650
|
+
|
|
651
|
+
def group_members(self, organization: OrganizationRef, group: GroupRef) -> list[Member]:
|
|
652
|
+
"""The members of a group.
|
|
653
|
+
|
|
654
|
+
Args:
|
|
655
|
+
organization: The organization, as object, id or unique name.
|
|
656
|
+
group: The group, as object, id or unique name.
|
|
657
|
+
|
|
658
|
+
Returns:
|
|
659
|
+
The members.
|
|
660
|
+
"""
|
|
661
|
+
org_id = self._resolve_organization(organization)
|
|
662
|
+
group_id = self._resolve_group(org_id, group)
|
|
663
|
+
ids = set(self._group_member_ids(org_id, group_id))
|
|
664
|
+
return [m for m in self.members(org_id) if m.id in ids]
|
|
665
|
+
|
|
666
|
+
def _group_member_ids(self, org_id: str, group_id: str) -> list[str]:
|
|
667
|
+
"""Membership ids of a group.
|
|
668
|
+
|
|
669
|
+
Args:
|
|
670
|
+
org_id: The organization id.
|
|
671
|
+
group_id: The group id.
|
|
672
|
+
|
|
673
|
+
Returns:
|
|
674
|
+
The membership ids.
|
|
675
|
+
|
|
676
|
+
Raises:
|
|
677
|
+
UnsafeWriteError: The server did not return the group's members.
|
|
678
|
+
"""
|
|
679
|
+
body = self.request('GET', f'/api/organizations/{org_id}/groups/{group_id}/users')
|
|
680
|
+
return [i for i in _existing_list(body, "the group's members") if isinstance(i, str)]
|
|
681
|
+
|
|
682
|
+
def _group_payload(
|
|
683
|
+
self,
|
|
684
|
+
org_id: str,
|
|
685
|
+
name: str,
|
|
686
|
+
collections: Sequence[CollectionRef],
|
|
687
|
+
members: Sequence[MemberRef],
|
|
688
|
+
external_id: str | None,
|
|
689
|
+
) -> dict[str, Any]:
|
|
690
|
+
"""Request body for creating or updating a group.
|
|
691
|
+
|
|
692
|
+
Args:
|
|
693
|
+
org_id: The organization id.
|
|
694
|
+
name: The group name.
|
|
695
|
+
collections: Collection references with access rights.
|
|
696
|
+
members: Member references.
|
|
697
|
+
external_id: The directory-sync identifier.
|
|
698
|
+
|
|
699
|
+
Returns:
|
|
700
|
+
The request body.
|
|
701
|
+
"""
|
|
702
|
+
return {
|
|
703
|
+
'name': name,
|
|
704
|
+
'accessAll': False,
|
|
705
|
+
'externalId': external_id,
|
|
706
|
+
'collections': [_core.collection_access_payload(_collection_access(c)) for c in collections],
|
|
707
|
+
'users': self._member_ids(org_id, members) if members else [],
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
def create_group(
|
|
711
|
+
self,
|
|
712
|
+
organization: OrganizationRef,
|
|
713
|
+
name: str,
|
|
714
|
+
*,
|
|
715
|
+
collections: Sequence[CollectionRef] = (),
|
|
716
|
+
members: Sequence[MemberRef] = (),
|
|
717
|
+
external_id: str | None = None,
|
|
718
|
+
) -> Group:
|
|
719
|
+
"""Create a group.
|
|
720
|
+
|
|
721
|
+
Args:
|
|
722
|
+
organization: The organization, as object, id or unique name.
|
|
723
|
+
name: The group name (plain text on the server).
|
|
724
|
+
collections: Collections, as objects or ids, or [CollectionAccess][vaultwarden2.models.CollectionAccess] entries
|
|
725
|
+
to set read-only, hide-passwords or manage rights.
|
|
726
|
+
members: The members, each as object, membership id or email address.
|
|
727
|
+
external_id: An identifier for directory synchronisation.
|
|
728
|
+
|
|
729
|
+
Returns:
|
|
730
|
+
The new group.
|
|
731
|
+
|
|
732
|
+
Raises:
|
|
733
|
+
VaultwardenError: The organization or a referenced member, group or collection was not found.
|
|
734
|
+
"""
|
|
735
|
+
org_id = self._resolve_organization(organization)
|
|
736
|
+
payload = self._group_payload(org_id, name, collections, members, external_id)
|
|
737
|
+
body = JsonObject(self.request('POST', f'/api/organizations/{org_id}/groups', json=payload))
|
|
738
|
+
return self._get_group(org_id, body.get_str('id') or '')
|
|
739
|
+
|
|
740
|
+
def _get_group(self, org_id: str, group_id: str) -> Group:
|
|
741
|
+
"""One group with its collection access.
|
|
742
|
+
|
|
743
|
+
Args:
|
|
744
|
+
org_id: The organization id.
|
|
745
|
+
group_id: The group id.
|
|
746
|
+
|
|
747
|
+
Returns:
|
|
748
|
+
The group.
|
|
749
|
+
"""
|
|
750
|
+
return _core.parse_group(self.request('GET', f'/api/organizations/{org_id}/groups/{group_id}/details'), org_id)
|
|
751
|
+
|
|
752
|
+
def update_group(
|
|
753
|
+
self,
|
|
754
|
+
organization: OrganizationRef,
|
|
755
|
+
group: GroupRef,
|
|
756
|
+
*,
|
|
757
|
+
name: str | None = None,
|
|
758
|
+
collections: Sequence[CollectionRef] | None = None,
|
|
759
|
+
members: Sequence[MemberRef] | None = None,
|
|
760
|
+
) -> Group:
|
|
761
|
+
"""Change a group's name, collection access and/or members.
|
|
762
|
+
|
|
763
|
+
Args:
|
|
764
|
+
organization: The organization, as object, id or unique name.
|
|
765
|
+
group: The group, as object, id or unique name.
|
|
766
|
+
name: The new name. None keeps it.
|
|
767
|
+
collections: Collections, as objects or ids, or [CollectionAccess][vaultwarden2.models.CollectionAccess] entries
|
|
768
|
+
to set read-only, hide-passwords or manage rights. None keeps the current access.
|
|
769
|
+
members: The members, each as object, membership id or email address. None keeps the current members.
|
|
770
|
+
|
|
771
|
+
Returns:
|
|
772
|
+
The updated group.
|
|
773
|
+
|
|
774
|
+
Raises:
|
|
775
|
+
UnsafeWriteError: The server did not return the current access or members; nothing was sent.
|
|
776
|
+
VaultwardenError: The organization or a referenced member, group or collection was not found.
|
|
777
|
+
"""
|
|
778
|
+
org_id = self._resolve_organization(organization)
|
|
779
|
+
current = self._get_group(org_id, self._resolve_group(org_id, group))
|
|
780
|
+
payload = self._group_payload(
|
|
781
|
+
org_id,
|
|
782
|
+
name if name is not None else current.name,
|
|
783
|
+
collections if collections is not None else _group_access(current),
|
|
784
|
+
members if members is not None else self._group_member_ids(org_id, current.id),
|
|
785
|
+
current.external_id,
|
|
786
|
+
)
|
|
787
|
+
self.request('PUT', f'/api/organizations/{org_id}/groups/{current.id}', json=payload)
|
|
788
|
+
return self._get_group(org_id, current.id)
|
|
789
|
+
|
|
790
|
+
def delete_group(self, organization: OrganizationRef, group: GroupRef) -> None:
|
|
791
|
+
"""Delete a group. Its members stay in the organization.
|
|
792
|
+
|
|
793
|
+
Args:
|
|
794
|
+
organization: The organization, as object, id or unique name.
|
|
795
|
+
group: The group, as object, id or unique name.
|
|
796
|
+
"""
|
|
797
|
+
org_id = self._resolve_organization(organization)
|
|
798
|
+
self.request('DELETE', f'/api/organizations/{org_id}/groups/{self._resolve_group(org_id, group)}')
|
|
799
|
+
|
|
800
|
+
def policies(self, organization: OrganizationRef) -> list[Policy]:
|
|
801
|
+
"""The policies configured for an organization.
|
|
802
|
+
|
|
803
|
+
Args:
|
|
804
|
+
organization: The organization, as object, id or unique name.
|
|
805
|
+
|
|
806
|
+
Returns:
|
|
807
|
+
The configured policies; types that were never configured are not listed.
|
|
808
|
+
"""
|
|
809
|
+
org_id = self._resolve_organization(organization)
|
|
810
|
+
body = JsonObject(self.request('GET', f'/api/organizations/{org_id}/policies'))
|
|
811
|
+
return [_core.parse_policy(p.raw) for p in body.objs('data')]
|
|
812
|
+
|
|
813
|
+
def set_policy(self, organization: OrganizationRef, policy_type: PolicyType | int, *, enabled: bool = True, data: dict[str, Any] | None = None) -> Policy:
|
|
814
|
+
"""Enable or disable a policy and set its options.
|
|
815
|
+
|
|
816
|
+
Careful: policies take effect on existing members immediately. For example, enabling the two-step login policy
|
|
817
|
+
revokes every member without two-step login. Check a policy's effect before enabling it on a live organization.
|
|
818
|
+
|
|
819
|
+
Args:
|
|
820
|
+
organization: The organization, as object, id or unique name.
|
|
821
|
+
policy_type: The policy, see [PolicyType][vaultwarden2.models.PolicyType].
|
|
822
|
+
enabled: Enable (True) or disable (False) the policy.
|
|
823
|
+
data: Policy-specific options with camelCase keys as the server uses them, for example
|
|
824
|
+
``{'minLength': 20}`` for the password generator policy.
|
|
825
|
+
|
|
826
|
+
Returns:
|
|
827
|
+
The policy as stored.
|
|
828
|
+
|
|
829
|
+
Raises:
|
|
830
|
+
ApiError: The server refused (for example a policy that requires another one first).
|
|
831
|
+
"""
|
|
832
|
+
org_id = self._resolve_organization(organization)
|
|
833
|
+
body = self.request('PUT', f'/api/organizations/{org_id}/policies/{int(policy_type)}', json={'policy': {'enabled': enabled, 'data': data}})
|
|
834
|
+
return _core.parse_policy(body)
|
|
835
|
+
|
|
836
|
+
def organization_api_key(self, organization: OrganizationRef, password: str, *, rotate: bool = False) -> tuple[str, str]:
|
|
837
|
+
"""The organization API key (owner only).
|
|
838
|
+
|
|
839
|
+
Args:
|
|
840
|
+
organization: The organization, as object, id or unique name.
|
|
841
|
+
password: The owner's master password (the server re-checks it).
|
|
842
|
+
rotate: Issue a new secret and invalidate the old one.
|
|
843
|
+
|
|
844
|
+
Returns:
|
|
845
|
+
``(client_id, client_secret)``; the client id is ``organization.<id>``.
|
|
846
|
+
|
|
847
|
+
Raises:
|
|
848
|
+
ApiError: Wrong password or not the owner.
|
|
849
|
+
"""
|
|
850
|
+
org_id = self._resolve_organization(organization)
|
|
851
|
+
endpoint = 'rotate-api-key' if rotate else 'api-key'
|
|
852
|
+
body = JsonObject(self.request('POST', f'/api/organizations/{org_id}/{endpoint}', json={'masterPasswordHash': self._password_hash(password)}))
|
|
853
|
+
secret = body.get_str('apiKey')
|
|
854
|
+
if not secret:
|
|
855
|
+
raise ApiError(500, 'server returned no apiKey')
|
|
856
|
+
return f'organization.{org_id}', secret
|
|
857
|
+
|
|
858
|
+
def export_organization(self, organization: OrganizationRef) -> OrganizationExport:
|
|
859
|
+
"""All collections and items of an organization, decrypted.
|
|
860
|
+
|
|
861
|
+
Write it out with [to_export_json][vaultwarden2.export.to_export_json] or
|
|
862
|
+
[dump_organization][vaultwarden2.VaultwardenClient.dump_organization].
|
|
863
|
+
|
|
864
|
+
Args:
|
|
865
|
+
organization: The organization, as object, id or unique name.
|
|
866
|
+
|
|
867
|
+
Returns:
|
|
868
|
+
The collections and items.
|
|
869
|
+
|
|
870
|
+
Raises:
|
|
871
|
+
CryptoError: An item cannot be decrypted.
|
|
872
|
+
"""
|
|
873
|
+
org_id = self._resolve_organization(organization)
|
|
874
|
+
body = JsonObject(self.request('GET', f'/api/organizations/{org_id}/export'))
|
|
875
|
+
keys = self._require_keys()
|
|
876
|
+
collections = tuple(_core.decrypt_collection({**c.raw, 'organizationId': c.get_str('organizationId') or org_id}, keys) for c in body.objs('collections'))
|
|
877
|
+
ciphers = _offload.run_cpu(_core.decrypt_ciphers, [c.raw for c in body.objs('ciphers')], keys)
|
|
878
|
+
return OrganizationExport(organization_id=org_id, collections=collections, ciphers=tuple(ciphers))
|
|
879
|
+
|
|
880
|
+
|
|
881
|
+
def _existing_list(value: Any, what: str) -> list[Any]:
|
|
882
|
+
"""A list returned by the server that an update will send back.
|
|
883
|
+
|
|
884
|
+
Refuses instead of sending an empty list in its place, which would clear the data on the server.
|
|
885
|
+
|
|
886
|
+
Args:
|
|
887
|
+
value: The value from the server response.
|
|
888
|
+
what: Description for the error message.
|
|
889
|
+
|
|
890
|
+
Returns:
|
|
891
|
+
The list.
|
|
892
|
+
|
|
893
|
+
Raises:
|
|
894
|
+
UnsafeWriteError: The server did not return a list.
|
|
895
|
+
"""
|
|
896
|
+
if not isinstance(value, list):
|
|
897
|
+
raise UnsafeWriteError(f'the server did not return {what}; not updating to avoid clearing it')
|
|
898
|
+
return value
|
|
899
|
+
|
|
900
|
+
|
|
901
|
+
def _group_access(group: Group) -> list[CollectionAccess]:
|
|
902
|
+
"""The current collection access of a group, for sending it back unchanged.
|
|
903
|
+
|
|
904
|
+
Args:
|
|
905
|
+
group: The group.
|
|
906
|
+
|
|
907
|
+
Returns:
|
|
908
|
+
The access entries.
|
|
909
|
+
|
|
910
|
+
Raises:
|
|
911
|
+
UnsafeWriteError: The server did not return the group's collection access.
|
|
912
|
+
"""
|
|
913
|
+
_existing_list(JsonObject(group.raw).get('collections'), "the group's collection access")
|
|
914
|
+
return list(group.collections)
|