workos 1.5.1__py3-none-any.whl → 5.38.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.
- workos/__about__.py +1 -1
- workos/__init__.py +3 -7
- workos/_base_client.py +138 -0
- workos/_client_configuration.py +10 -0
- workos/api_keys.py +53 -0
- workos/async_client.py +144 -0
- workos/audit_logs.py +125 -0
- workos/client.py +110 -18
- workos/directory_sync.py +379 -99
- workos/events.py +111 -0
- workos/exceptions.py +53 -26
- workos/fga.py +649 -0
- workos/mfa.py +205 -0
- workos/organization_domains.py +179 -0
- workos/organizations.py +403 -73
- workos/passwordless.py +67 -43
- workos/pipes.py +93 -0
- workos/portal.py +51 -28
- workos/session.py +337 -0
- workos/sso.py +311 -101
- workos/types/__init__.py +4 -0
- workos/types/api_keys/__init__.py +1 -0
- workos/types/api_keys/api_keys.py +20 -0
- workos/types/audit_logs/__init__.py +6 -0
- workos/types/audit_logs/audit_log_event.py +16 -0
- workos/types/audit_logs/audit_log_event_actor.py +12 -0
- workos/types/audit_logs/audit_log_event_context.py +8 -0
- workos/types/audit_logs/audit_log_event_target.py +12 -0
- workos/types/audit_logs/audit_log_export.py +18 -0
- workos/types/audit_logs/audit_log_metadata.py +4 -0
- workos/types/directory_sync/__init__.py +5 -0
- workos/types/directory_sync/directory.py +31 -0
- workos/types/directory_sync/directory_group.py +16 -0
- workos/types/directory_sync/directory_state.py +28 -0
- workos/types/directory_sync/directory_type.py +24 -0
- workos/types/directory_sync/directory_user.py +50 -0
- workos/types/directory_sync/list_filters.py +21 -0
- workos/types/events/__init__.py +13 -0
- workos/types/events/authentication_payload.py +70 -0
- workos/types/events/connection_payload_with_legacy_fields.py +5 -0
- workos/types/events/directory_group_membership_payload.py +9 -0
- workos/types/events/directory_group_with_previous_attributes.py +6 -0
- workos/types/events/directory_payload.py +16 -0
- workos/types/events/directory_payload_with_legacy_fields.py +29 -0
- workos/types/events/directory_user_with_previous_attributes.py +6 -0
- workos/types/events/event.py +324 -0
- workos/types/events/event_model.py +103 -0
- workos/types/events/event_type.py +59 -0
- workos/types/events/list_filters.py +10 -0
- workos/types/events/organization_domain_verification_failed_payload.py +14 -0
- workos/types/events/previous_attributes.py +3 -0
- workos/types/events/session_payload.py +27 -0
- workos/types/feature_flags/__init__.py +3 -0
- workos/types/feature_flags/feature_flag.py +12 -0
- workos/types/feature_flags/list_filters.py +5 -0
- workos/types/fga/__init__.py +5 -0
- workos/types/fga/authorization_resource_types.py +9 -0
- workos/types/fga/authorization_resources.py +10 -0
- workos/types/fga/check.py +51 -0
- workos/types/fga/list_filters.py +24 -0
- workos/types/fga/warnings.py +33 -0
- workos/types/fga/warrant.py +49 -0
- workos/types/list_resource.py +198 -0
- workos/types/metadata.py +4 -0
- workos/types/mfa/__init__.py +5 -0
- workos/types/mfa/authentication_challenge.py +14 -0
- workos/types/mfa/authentication_challenge_verification_response.py +9 -0
- workos/types/mfa/authentication_factor.py +70 -0
- workos/types/mfa/authentication_factor_totp_and_challenge_response.py +10 -0
- workos/types/mfa/enroll_authentication_factor_type.py +8 -0
- workos/types/organization_domains/__init__.py +1 -0
- workos/types/organization_domains/organization_domain.py +18 -0
- workos/types/organizations/__init__.py +6 -0
- workos/types/organizations/domain_data_input.py +7 -0
- workos/types/organizations/list_filters.py +6 -0
- workos/types/organizations/organization.py +13 -0
- workos/types/organizations/organization_common.py +12 -0
- workos/types/passwordless/__init__.py +2 -0
- workos/types/passwordless/passwordless_session.py +12 -0
- workos/types/passwordless/passwordless_session_type.py +3 -0
- workos/types/pipes/__init__.py +6 -0
- workos/types/pipes/pipes.py +34 -0
- workos/types/portal/__init__.py +2 -0
- workos/types/portal/portal_link.py +7 -0
- workos/types/portal/portal_link_intent.py +11 -0
- workos/types/portal/portal_link_intent_options.py +9 -0
- workos/types/roles/__init__.py +0 -0
- workos/types/roles/role.py +27 -0
- workos/types/sso/__init__.py +4 -0
- workos/types/sso/connection.py +70 -0
- workos/types/sso/connection_domain.py +8 -0
- workos/types/sso/profile.py +35 -0
- workos/types/sso/sso_provider_type.py +10 -0
- workos/types/user_management/__init__.py +12 -0
- workos/types/user_management/authenticate_with_common.py +66 -0
- workos/types/user_management/authentication_response.py +53 -0
- workos/types/user_management/email_verification.py +18 -0
- workos/types/user_management/impersonator.py +8 -0
- workos/types/user_management/invitation.py +26 -0
- workos/types/user_management/list_filters.py +29 -0
- workos/types/user_management/magic_auth.py +18 -0
- workos/types/user_management/oauth_tokens.py +21 -0
- workos/types/user_management/organization_membership.py +25 -0
- workos/types/user_management/password_hash_type.py +4 -0
- workos/types/user_management/password_reset.py +18 -0
- workos/types/user_management/screen_hint.py +3 -0
- workos/types/user_management/session.py +79 -0
- workos/types/user_management/user.py +22 -0
- workos/types/user_management/user_management_provider_type.py +11 -0
- workos/types/vault/__init__.py +2 -0
- workos/types/vault/key.py +25 -0
- workos/types/vault/object.py +38 -0
- workos/types/webhooks/__init__.py +0 -0
- workos/types/webhooks/webhook.py +330 -0
- workos/types/webhooks/webhook_model.py +14 -0
- workos/types/webhooks/webhook_payload.py +4 -0
- workos/types/widgets/__init__.py +2 -0
- workos/types/widgets/widget_scope.py +4 -0
- workos/types/widgets/widget_token_response.py +7 -0
- workos/types/workos_model.py +26 -0
- workos/typing/__init__.py +1 -0
- workos/typing/literals.py +32 -0
- workos/typing/sync_or_async.py +5 -0
- workos/typing/untyped_literal.py +37 -0
- workos/typing/webhooks.py +18 -0
- workos/user_management.py +2400 -0
- workos/utils/_base_http_client.py +252 -0
- workos/utils/crypto_provider.py +39 -0
- workos/utils/http_client.py +214 -0
- workos/utils/pagination_order.py +4 -0
- workos/utils/request_helper.py +27 -0
- workos/vault.py +544 -0
- workos/webhooks.py +96 -39
- workos/widgets.py +55 -0
- {workos-1.5.1.dist-info → workos-5.38.0.dist-info}/LICENSE +1 -1
- workos-5.38.0.dist-info/METADATA +107 -0
- workos-5.38.0.dist-info/RECORD +141 -0
- {workos-1.5.1.dist-info → workos-5.38.0.dist-info}/WHEEL +1 -1
- workos/audit_trail.py +0 -172
- workos/resources/base.py +0 -36
- workos/resources/event.py +0 -42
- workos/resources/event_action.py +0 -11
- workos/resources/sso.py +0 -53
- workos/utils/connection_types.py +0 -17
- workos/utils/request.py +0 -95
- workos/utils/validation.py +0 -45
- workos-1.5.1.dist-info/METADATA +0 -77
- workos-1.5.1.dist-info/RECORD +0 -25
- /workos/{resources/__init__.py → py.typed} +0 -0
- {workos-1.5.1.dist-info → workos-5.38.0.dist-info}/top_level.txt +0 -0
workos/vault.py
ADDED
|
@@ -0,0 +1,544 @@
|
|
|
1
|
+
import base64
|
|
2
|
+
from typing import Optional, Protocol, Sequence, Tuple
|
|
3
|
+
from workos.types.vault import VaultObject, ObjectVersion, ObjectDigest, ObjectMetadata
|
|
4
|
+
from workos.types.vault.key import DataKey, DataKeyPair, KeyContext, DecodedKeys
|
|
5
|
+
from workos.types.list_resource import (
|
|
6
|
+
ListArgs,
|
|
7
|
+
ListMetadata,
|
|
8
|
+
ListPage,
|
|
9
|
+
WorkOSListResource,
|
|
10
|
+
)
|
|
11
|
+
from workos.utils.http_client import SyncHTTPClient
|
|
12
|
+
from workos.utils.pagination_order import PaginationOrder
|
|
13
|
+
from workos.utils.request_helper import (
|
|
14
|
+
DEFAULT_LIST_RESPONSE_LIMIT,
|
|
15
|
+
REQUEST_METHOD_DELETE,
|
|
16
|
+
REQUEST_METHOD_GET,
|
|
17
|
+
REQUEST_METHOD_POST,
|
|
18
|
+
REQUEST_METHOD_PUT,
|
|
19
|
+
RequestHelper,
|
|
20
|
+
)
|
|
21
|
+
from workos.utils.crypto_provider import CryptoProvider
|
|
22
|
+
|
|
23
|
+
DEFAULT_RESPONSE_LIMIT = DEFAULT_LIST_RESPONSE_LIMIT
|
|
24
|
+
|
|
25
|
+
VaultObjectList = WorkOSListResource[ObjectDigest, ListArgs, ListMetadata]
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class VaultModule(Protocol):
|
|
29
|
+
def read_object(self, *, object_id: str) -> VaultObject:
|
|
30
|
+
"""
|
|
31
|
+
Get a Vault object with the value decrypted.
|
|
32
|
+
|
|
33
|
+
Kwargs:
|
|
34
|
+
object_id (str): The unique identifier for the object.
|
|
35
|
+
Returns:
|
|
36
|
+
VaultObject: A vault object with metadata, name and decrypted value.
|
|
37
|
+
"""
|
|
38
|
+
...
|
|
39
|
+
|
|
40
|
+
def read_object_by_name(self, *, name: str) -> VaultObject:
|
|
41
|
+
"""
|
|
42
|
+
Get a Vault object by name with the value decrypted.
|
|
43
|
+
|
|
44
|
+
Kwargs:
|
|
45
|
+
name (str): The unique name of the object.
|
|
46
|
+
Returns:
|
|
47
|
+
VaultObject: A vault object with metadata, name and decrypted value.
|
|
48
|
+
"""
|
|
49
|
+
...
|
|
50
|
+
|
|
51
|
+
def list_objects(
|
|
52
|
+
self,
|
|
53
|
+
*,
|
|
54
|
+
limit: int = DEFAULT_RESPONSE_LIMIT,
|
|
55
|
+
before: Optional[str] = None,
|
|
56
|
+
after: Optional[str] = None,
|
|
57
|
+
) -> VaultObjectList:
|
|
58
|
+
"""
|
|
59
|
+
Gets a list of encrypted Vault objects.
|
|
60
|
+
|
|
61
|
+
Kwargs:
|
|
62
|
+
limit (int): The maximum number of objects to return. (Optional)
|
|
63
|
+
before (str): A cursor to return resources before. (Optional)
|
|
64
|
+
after (str): A cursor to return resources after. (Optional)
|
|
65
|
+
|
|
66
|
+
Returns:
|
|
67
|
+
VaultObjectList: A list of vault objects with built-in pagination iterator.
|
|
68
|
+
"""
|
|
69
|
+
...
|
|
70
|
+
|
|
71
|
+
def list_object_versions(
|
|
72
|
+
self,
|
|
73
|
+
*,
|
|
74
|
+
object_id: str,
|
|
75
|
+
) -> Sequence[ObjectVersion]:
|
|
76
|
+
"""
|
|
77
|
+
Gets a list of versions for a specific Vault object.
|
|
78
|
+
|
|
79
|
+
Kwargs:
|
|
80
|
+
object_id (str): The unique identifier for the object.
|
|
81
|
+
|
|
82
|
+
Returns:
|
|
83
|
+
Sequence[ObjectVersion]: A list of object versions.
|
|
84
|
+
"""
|
|
85
|
+
...
|
|
86
|
+
|
|
87
|
+
def create_object(
|
|
88
|
+
self,
|
|
89
|
+
*,
|
|
90
|
+
name: str,
|
|
91
|
+
value: str,
|
|
92
|
+
key_context: KeyContext,
|
|
93
|
+
) -> ObjectMetadata:
|
|
94
|
+
"""
|
|
95
|
+
Create a new Vault encrypted object.
|
|
96
|
+
|
|
97
|
+
Kwargs:
|
|
98
|
+
name (str): The name of the object.
|
|
99
|
+
value (str): The value to encrypt and store.
|
|
100
|
+
key_context (KeyContext): A set of key-value dictionary pairs that determines which root keys to use when encrypting data.
|
|
101
|
+
|
|
102
|
+
Returns:
|
|
103
|
+
VaultObject: The created vault object.
|
|
104
|
+
"""
|
|
105
|
+
...
|
|
106
|
+
|
|
107
|
+
def update_object(
|
|
108
|
+
self,
|
|
109
|
+
*,
|
|
110
|
+
object_id: str,
|
|
111
|
+
value: str,
|
|
112
|
+
version_check: Optional[str] = None,
|
|
113
|
+
) -> VaultObject:
|
|
114
|
+
"""
|
|
115
|
+
Update an existing Vault object.
|
|
116
|
+
|
|
117
|
+
Kwargs:
|
|
118
|
+
object_id (str): The unique identifier for the object.
|
|
119
|
+
value (str): The new value to encrypt and store.
|
|
120
|
+
version_check (str): A version of the object to prevent clobbering of data during concurrent updates. (Optional)
|
|
121
|
+
|
|
122
|
+
Returns:
|
|
123
|
+
VaultObject: The updated vault object.
|
|
124
|
+
"""
|
|
125
|
+
...
|
|
126
|
+
|
|
127
|
+
def delete_object(
|
|
128
|
+
self,
|
|
129
|
+
*,
|
|
130
|
+
object_id: str,
|
|
131
|
+
) -> None:
|
|
132
|
+
"""
|
|
133
|
+
Permanently delete a Vault encrypted object. Warning: this cannont be undone.
|
|
134
|
+
|
|
135
|
+
Kwargs:
|
|
136
|
+
object_id (str): The unique identifier for the object.
|
|
137
|
+
"""
|
|
138
|
+
...
|
|
139
|
+
|
|
140
|
+
def create_data_key(self, *, key_context: KeyContext) -> DataKeyPair:
|
|
141
|
+
"""
|
|
142
|
+
Generate a data key for local encryption based on the provided key context.
|
|
143
|
+
The encrypted data key MUST be stored by the application, as it cannot be retrieved after generation.
|
|
144
|
+
|
|
145
|
+
Kwargs:
|
|
146
|
+
key_context (KeyContext): A set of key-value dictionary pairs that determines which root keys to use when encrypting data.
|
|
147
|
+
"""
|
|
148
|
+
...
|
|
149
|
+
|
|
150
|
+
def decrypt_data_key(
|
|
151
|
+
self,
|
|
152
|
+
*,
|
|
153
|
+
keys: str,
|
|
154
|
+
) -> DataKey:
|
|
155
|
+
"""
|
|
156
|
+
Decrypt encrypted data keys that were previously generated by create_data_key.
|
|
157
|
+
|
|
158
|
+
This method takes the encrypted data key blob and uses the WorkOS Vault service
|
|
159
|
+
to decrypt it, returning the plaintext data key that can be used for local
|
|
160
|
+
encryption/decryption operations.
|
|
161
|
+
|
|
162
|
+
Kwargs:
|
|
163
|
+
keys (str): The base64-encoded encrypted data key blob returned by create_data_key.
|
|
164
|
+
|
|
165
|
+
Returns:
|
|
166
|
+
DataKey: The decrypted data key containing the key ID and the plaintext key material.
|
|
167
|
+
"""
|
|
168
|
+
...
|
|
169
|
+
|
|
170
|
+
def encrypt(
|
|
171
|
+
self,
|
|
172
|
+
*,
|
|
173
|
+
data: str,
|
|
174
|
+
key_context: KeyContext,
|
|
175
|
+
associated_data: Optional[str] = None,
|
|
176
|
+
) -> str:
|
|
177
|
+
"""
|
|
178
|
+
Encrypt data locally using AES-GCM with a data key derived from the provided context.
|
|
179
|
+
|
|
180
|
+
This method generates a new data key for each encryption operation, ensuring that
|
|
181
|
+
the same plaintext will produce different ciphertext each time it's encrypted.
|
|
182
|
+
The encrypted data key is embedded in the result so it can be decrypted later.
|
|
183
|
+
|
|
184
|
+
Kwargs:
|
|
185
|
+
data (str): The plaintext data to encrypt.
|
|
186
|
+
key_context (KeyContext): A set of key-value dictionary pairs that determines which root keys to use when encrypting data.
|
|
187
|
+
associated_data (str): Additional authenticated data (AAD) that will be authenticated but not encrypted. (Optional)
|
|
188
|
+
|
|
189
|
+
Returns:
|
|
190
|
+
str: Base64-encoded encrypted data containing the IV, authentication tag, encrypted data key, and ciphertext.
|
|
191
|
+
"""
|
|
192
|
+
...
|
|
193
|
+
|
|
194
|
+
def decrypt(
|
|
195
|
+
self, *, encrypted_data: str, associated_data: Optional[str] = None
|
|
196
|
+
) -> str:
|
|
197
|
+
"""
|
|
198
|
+
Decrypt data that was previously encrypted using the encrypt method.
|
|
199
|
+
|
|
200
|
+
This method extracts the encrypted data key from the encrypted payload,
|
|
201
|
+
decrypts it using the WorkOS Vault service, and then uses the resulting
|
|
202
|
+
data key to decrypt the actual data using AES-GCM.
|
|
203
|
+
|
|
204
|
+
Kwargs:
|
|
205
|
+
encrypted_data (str): The base64-encoded encrypted data returned by the encrypt method.
|
|
206
|
+
associated_data (str): The same additional authenticated data (AAD) that was used during encryption, if any. (Optional)
|
|
207
|
+
|
|
208
|
+
Returns:
|
|
209
|
+
str: The original plaintext data.
|
|
210
|
+
|
|
211
|
+
Raises:
|
|
212
|
+
ValueError: If the encrypted_data format is invalid or if associated_data doesn't match what was used during encryption.
|
|
213
|
+
cryptography.exceptions.InvalidTag: If the authentication tag verification fails (data has been tampered with).
|
|
214
|
+
"""
|
|
215
|
+
...
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
class Vault(VaultModule):
|
|
219
|
+
_http_client: SyncHTTPClient
|
|
220
|
+
_crypto_provider: CryptoProvider
|
|
221
|
+
|
|
222
|
+
def __init__(self, http_client: SyncHTTPClient):
|
|
223
|
+
self._http_client = http_client
|
|
224
|
+
self._crypto_provider = CryptoProvider()
|
|
225
|
+
|
|
226
|
+
def read_object(
|
|
227
|
+
self,
|
|
228
|
+
*,
|
|
229
|
+
object_id: str,
|
|
230
|
+
) -> VaultObject:
|
|
231
|
+
if not object_id:
|
|
232
|
+
raise ValueError("Incomplete arguments: 'object_id' is a required argument")
|
|
233
|
+
|
|
234
|
+
response = self._http_client.request(
|
|
235
|
+
RequestHelper.build_parameterized_url(
|
|
236
|
+
"vault/v1/kv/{object_id}",
|
|
237
|
+
object_id=object_id,
|
|
238
|
+
),
|
|
239
|
+
method=REQUEST_METHOD_GET,
|
|
240
|
+
)
|
|
241
|
+
|
|
242
|
+
return VaultObject.model_validate(response)
|
|
243
|
+
|
|
244
|
+
def read_object_by_name(
|
|
245
|
+
self,
|
|
246
|
+
*,
|
|
247
|
+
name: str,
|
|
248
|
+
) -> VaultObject:
|
|
249
|
+
if not name:
|
|
250
|
+
raise ValueError("Incomplete arguments: 'name' is a required argument")
|
|
251
|
+
|
|
252
|
+
response = self._http_client.request(
|
|
253
|
+
RequestHelper.build_parameterized_url(
|
|
254
|
+
"vault/v1/kv/name/{name}",
|
|
255
|
+
name=name,
|
|
256
|
+
),
|
|
257
|
+
method=REQUEST_METHOD_GET,
|
|
258
|
+
)
|
|
259
|
+
|
|
260
|
+
return VaultObject.model_validate(response)
|
|
261
|
+
|
|
262
|
+
def list_objects(
|
|
263
|
+
self,
|
|
264
|
+
*,
|
|
265
|
+
limit: int = DEFAULT_RESPONSE_LIMIT,
|
|
266
|
+
before: Optional[str] = None,
|
|
267
|
+
after: Optional[str] = None,
|
|
268
|
+
) -> VaultObjectList:
|
|
269
|
+
list_params: ListArgs = {
|
|
270
|
+
"limit": limit,
|
|
271
|
+
"before": before,
|
|
272
|
+
"after": after,
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
response = self._http_client.request(
|
|
276
|
+
"vault/v1/kv",
|
|
277
|
+
method=REQUEST_METHOD_GET,
|
|
278
|
+
params=list_params,
|
|
279
|
+
)
|
|
280
|
+
|
|
281
|
+
# Ensure object field is present
|
|
282
|
+
response_dict = dict(response)
|
|
283
|
+
if "object" not in response_dict:
|
|
284
|
+
response_dict["object"] = "list"
|
|
285
|
+
|
|
286
|
+
return VaultObjectList(
|
|
287
|
+
list_method=self.list_objects,
|
|
288
|
+
list_args=list_params,
|
|
289
|
+
**ListPage[ObjectDigest](**response_dict).model_dump(),
|
|
290
|
+
)
|
|
291
|
+
|
|
292
|
+
def list_object_versions(
|
|
293
|
+
self,
|
|
294
|
+
*,
|
|
295
|
+
object_id: str,
|
|
296
|
+
) -> Sequence[ObjectVersion]:
|
|
297
|
+
response = self._http_client.request(
|
|
298
|
+
RequestHelper.build_parameterized_url(
|
|
299
|
+
"vault/v1/kv/{object_id}/versions",
|
|
300
|
+
object_id=object_id,
|
|
301
|
+
),
|
|
302
|
+
method=REQUEST_METHOD_GET,
|
|
303
|
+
)
|
|
304
|
+
|
|
305
|
+
return [
|
|
306
|
+
ObjectVersion.model_validate(version)
|
|
307
|
+
for version in response.get("data", [])
|
|
308
|
+
]
|
|
309
|
+
|
|
310
|
+
def create_object(
|
|
311
|
+
self,
|
|
312
|
+
*,
|
|
313
|
+
name: str,
|
|
314
|
+
value: str,
|
|
315
|
+
key_context: KeyContext,
|
|
316
|
+
) -> ObjectMetadata:
|
|
317
|
+
if not name or not value:
|
|
318
|
+
raise ValueError(
|
|
319
|
+
"Incomplete arguments: 'name' and 'value' are required arguments"
|
|
320
|
+
)
|
|
321
|
+
|
|
322
|
+
request_data = {
|
|
323
|
+
"name": name,
|
|
324
|
+
"value": value,
|
|
325
|
+
"key_context": key_context,
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
response = self._http_client.request(
|
|
329
|
+
"vault/v1/kv",
|
|
330
|
+
method=REQUEST_METHOD_POST,
|
|
331
|
+
json=request_data,
|
|
332
|
+
)
|
|
333
|
+
|
|
334
|
+
return ObjectMetadata.model_validate(response)
|
|
335
|
+
|
|
336
|
+
def update_object(
|
|
337
|
+
self,
|
|
338
|
+
*,
|
|
339
|
+
object_id: str,
|
|
340
|
+
value: str,
|
|
341
|
+
version_check: Optional[str] = None,
|
|
342
|
+
) -> VaultObject:
|
|
343
|
+
if not object_id:
|
|
344
|
+
raise ValueError("Incomplete arguments: 'object_id' is a required argument")
|
|
345
|
+
|
|
346
|
+
request_data = {
|
|
347
|
+
"value": value,
|
|
348
|
+
}
|
|
349
|
+
if version_check is not None:
|
|
350
|
+
request_data["version_check"] = version_check
|
|
351
|
+
|
|
352
|
+
response = self._http_client.request(
|
|
353
|
+
RequestHelper.build_parameterized_url(
|
|
354
|
+
"vault/v1/kv/{object_id}",
|
|
355
|
+
object_id=object_id,
|
|
356
|
+
),
|
|
357
|
+
method=REQUEST_METHOD_PUT,
|
|
358
|
+
json=request_data,
|
|
359
|
+
)
|
|
360
|
+
|
|
361
|
+
return VaultObject.model_validate(response)
|
|
362
|
+
|
|
363
|
+
def delete_object(
|
|
364
|
+
self,
|
|
365
|
+
*,
|
|
366
|
+
object_id: str,
|
|
367
|
+
) -> None:
|
|
368
|
+
if not object_id:
|
|
369
|
+
raise ValueError("Incomplete arguments: 'object_id' is a required argument")
|
|
370
|
+
|
|
371
|
+
self._http_client.request(
|
|
372
|
+
RequestHelper.build_parameterized_url(
|
|
373
|
+
"vault/v1/kv/{object_id}",
|
|
374
|
+
object_id=object_id,
|
|
375
|
+
),
|
|
376
|
+
method=REQUEST_METHOD_DELETE,
|
|
377
|
+
)
|
|
378
|
+
|
|
379
|
+
def create_data_key(self, *, key_context: KeyContext) -> DataKeyPair:
|
|
380
|
+
request_data = {
|
|
381
|
+
"context": key_context,
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
response = self._http_client.request(
|
|
385
|
+
"vault/v1/keys/data-key",
|
|
386
|
+
method=REQUEST_METHOD_POST,
|
|
387
|
+
json=request_data,
|
|
388
|
+
)
|
|
389
|
+
|
|
390
|
+
return DataKeyPair.model_validate(
|
|
391
|
+
{
|
|
392
|
+
"context": response["context"],
|
|
393
|
+
"data_key": {"id": response["id"], "key": response["data_key"]},
|
|
394
|
+
"encrypted_keys": response["encrypted_keys"],
|
|
395
|
+
}
|
|
396
|
+
)
|
|
397
|
+
|
|
398
|
+
def decrypt_data_key(
|
|
399
|
+
self,
|
|
400
|
+
*,
|
|
401
|
+
keys: str,
|
|
402
|
+
) -> DataKey:
|
|
403
|
+
request_data = {
|
|
404
|
+
"keys": keys,
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
response = self._http_client.request(
|
|
408
|
+
"vault/v1/keys/decrypt",
|
|
409
|
+
method=REQUEST_METHOD_POST,
|
|
410
|
+
json=request_data,
|
|
411
|
+
)
|
|
412
|
+
|
|
413
|
+
return DataKey.model_validate(
|
|
414
|
+
{"id": response["id"], "key": response["data_key"]}
|
|
415
|
+
)
|
|
416
|
+
|
|
417
|
+
def encrypt(
|
|
418
|
+
self,
|
|
419
|
+
*,
|
|
420
|
+
data: str,
|
|
421
|
+
key_context: KeyContext,
|
|
422
|
+
associated_data: Optional[str] = None,
|
|
423
|
+
) -> str:
|
|
424
|
+
key_pair = self.create_data_key(key_context=key_context)
|
|
425
|
+
|
|
426
|
+
key = self._base64_to_bytes(key_pair.data_key.key)
|
|
427
|
+
key_blob = self._base64_to_bytes(key_pair.encrypted_keys)
|
|
428
|
+
prefix_len_buffer = self._encode_u32(len(key_blob))
|
|
429
|
+
aad_buffer = associated_data.encode("utf-8") if associated_data else None
|
|
430
|
+
iv = self._crypto_provider.random_bytes(12)
|
|
431
|
+
|
|
432
|
+
result = self._crypto_provider.encrypt(
|
|
433
|
+
data.encode("utf-8"), key, iv, aad_buffer
|
|
434
|
+
)
|
|
435
|
+
|
|
436
|
+
combined = (
|
|
437
|
+
result["iv"]
|
|
438
|
+
+ result["tag"]
|
|
439
|
+
+ prefix_len_buffer
|
|
440
|
+
+ key_blob
|
|
441
|
+
+ result["ciphertext"]
|
|
442
|
+
)
|
|
443
|
+
|
|
444
|
+
return self._bytes_to_base64(combined)
|
|
445
|
+
|
|
446
|
+
def decrypt(
|
|
447
|
+
self, *, encrypted_data: str, associated_data: Optional[str] = None
|
|
448
|
+
) -> str:
|
|
449
|
+
decoded = self._decode(encrypted_data)
|
|
450
|
+
data_key = self.decrypt_data_key(keys=decoded.keys)
|
|
451
|
+
|
|
452
|
+
key = self._base64_to_bytes(data_key.key)
|
|
453
|
+
aad_buffer = associated_data.encode("utf-8") if associated_data else None
|
|
454
|
+
|
|
455
|
+
decrypted_bytes = self._crypto_provider.decrypt(
|
|
456
|
+
ciphertext=decoded.ciphertext,
|
|
457
|
+
key=key,
|
|
458
|
+
iv=decoded.iv,
|
|
459
|
+
tag=decoded.tag,
|
|
460
|
+
aad=aad_buffer,
|
|
461
|
+
)
|
|
462
|
+
|
|
463
|
+
return decrypted_bytes.decode("utf-8")
|
|
464
|
+
|
|
465
|
+
def _base64_to_bytes(self, data: str) -> bytes:
|
|
466
|
+
return base64.b64decode(data)
|
|
467
|
+
|
|
468
|
+
def _bytes_to_base64(self, data: bytes) -> str:
|
|
469
|
+
return base64.b64encode(data).decode("utf-8")
|
|
470
|
+
|
|
471
|
+
def _encode_u32(self, value: int) -> bytes:
|
|
472
|
+
"""
|
|
473
|
+
Encode a 32-bit unsigned integer as LEB128.
|
|
474
|
+
|
|
475
|
+
Returns:
|
|
476
|
+
bytes: LEB128-encoded representation of the input value.
|
|
477
|
+
"""
|
|
478
|
+
if value < 0 or value > 0xFFFFFFFF:
|
|
479
|
+
raise ValueError("Value must be a 32-bit unsigned integer")
|
|
480
|
+
|
|
481
|
+
encoded = bytearray()
|
|
482
|
+
while True:
|
|
483
|
+
byte = value & 0x7F
|
|
484
|
+
value >>= 7
|
|
485
|
+
if value != 0:
|
|
486
|
+
byte |= 0x80 # Set continuation bit
|
|
487
|
+
encoded.append(byte)
|
|
488
|
+
if value == 0:
|
|
489
|
+
break
|
|
490
|
+
|
|
491
|
+
return bytes(encoded)
|
|
492
|
+
|
|
493
|
+
def _decode(self, encrypted_data_b64: str) -> DecodedKeys:
|
|
494
|
+
"""
|
|
495
|
+
This function extracts IV, tag, keyBlobLength, keyBlob, and ciphertext
|
|
496
|
+
from a base64-encoded payload.
|
|
497
|
+
Encoding format: [IV][TAG][4B Length][keyBlob][ciphertext]
|
|
498
|
+
"""
|
|
499
|
+
try:
|
|
500
|
+
payload = base64.b64decode(encrypted_data_b64)
|
|
501
|
+
except Exception as e:
|
|
502
|
+
raise ValueError("Base64 decoding failed") from e
|
|
503
|
+
|
|
504
|
+
iv = payload[0:12]
|
|
505
|
+
tag = payload[12:28]
|
|
506
|
+
|
|
507
|
+
try:
|
|
508
|
+
key_len, leb_len = self._decode_u32(payload[28:])
|
|
509
|
+
except Exception as e:
|
|
510
|
+
raise ValueError("Failed to decode key length") from e
|
|
511
|
+
|
|
512
|
+
keys_index = 28 + leb_len
|
|
513
|
+
keys_end = keys_index + key_len
|
|
514
|
+
keys_slice = payload[keys_index:keys_end]
|
|
515
|
+
keys = base64.b64encode(keys_slice).decode("utf-8")
|
|
516
|
+
ciphertext = payload[keys_end:]
|
|
517
|
+
|
|
518
|
+
return DecodedKeys(iv=iv, tag=tag, keys=keys, ciphertext=ciphertext)
|
|
519
|
+
|
|
520
|
+
def _decode_u32(self, buf: bytes) -> Tuple[int, int]:
|
|
521
|
+
"""
|
|
522
|
+
Decode an unsigned LEB128-encoded 32-bit integer from bytes.
|
|
523
|
+
|
|
524
|
+
Returns:
|
|
525
|
+
(value, length_consumed)
|
|
526
|
+
|
|
527
|
+
Raises:
|
|
528
|
+
ValueError if decoding fails or overflows.
|
|
529
|
+
"""
|
|
530
|
+
res = 0
|
|
531
|
+
bit = 0
|
|
532
|
+
|
|
533
|
+
for i, b in enumerate(buf):
|
|
534
|
+
if i > 4:
|
|
535
|
+
raise ValueError("LEB128 integer overflow (was more than 4 bytes)")
|
|
536
|
+
|
|
537
|
+
res |= (b & 0x7F) << (7 * bit)
|
|
538
|
+
|
|
539
|
+
if (b & 0x80) == 0:
|
|
540
|
+
return res, i + 1
|
|
541
|
+
|
|
542
|
+
bit += 1
|
|
543
|
+
|
|
544
|
+
raise ValueError("LEB128 integer not found")
|