diff --git a/README.md b/README.md index 2cdee984..405aca75 100644 --- a/README.md +++ b/README.md @@ -51,25 +51,26 @@ import asyncio import os from onepassword.client import Client, DesktopAuth + async def main(): # Connects to 1Password. Fill in your own integration name and version. client = await Client.authenticate( - auth=DesktopAuth( - # TODO: Set to your 1Password account name. - account_name="your-account-name" - ), - # TODO: Set to your own integration name and version. - integration_name="My 1Password Integration", - integration_version="v1.0.0", -) + auth=DesktopAuth( + # TODO: Set to your 1Password account name. + account_name="your-account-name" + ), + # TODO: Set to your own integration name and version. + integration_name="My 1Password Integration", + integration_version="v1.0.0", + ) # Retrieves a secret from 1Password. Takes a secret reference as input and returns the secret to which it points. value = await client.secrets.resolve("op://vault/item/field") # use value here -if __name__ == '__main__': - asyncio.run(main()) +if __name__ == "__main__": + asyncio.run(main()) ``` Make sure to use [secret reference URIs](https://developer.1password.com/docs/cli/secret-reference-syntax/) with the syntax `op://vault/item/field` to securely load secrets from 1Password into your code. @@ -106,20 +107,25 @@ import asyncio import os from onepassword.client import Client + async def main(): # Gets your service account token from the OP_SERVICE_ACCOUNT_TOKEN environment variable. token = os.getenv("OP_SERVICE_ACCOUNT_TOKEN") # Connects to 1Password. Fill in your own integration name and version. - client = await Client.authenticate(auth=token, integration_name="My 1Password Integration", integration_version="v1.0.0") + client = await Client.authenticate( + auth=token, + integration_name="My 1Password Integration", + integration_version="v1.0.0", + ) # Retrieves a secret from 1Password. Takes a secret reference as input and returns the secret to which it points. value = await client.secrets.resolve("op://vault/item/field") # use value here -if __name__ == '__main__': - asyncio.run(main()) +if __name__ == "__main__": + asyncio.run(main()) ``` Make sure to use [secret reference URIs](https://developer.1password.com/docs/cli/secret-reference-syntax/) with the syntax `op://vault/item/field` to securely load secrets from 1Password into your code. diff --git a/example/desktop_app.py b/example/desktop_app.py index 234ed7de..b72bbbcc 100644 --- a/example/desktop_app.py +++ b/example/desktop_app.py @@ -1,5 +1,6 @@ # [developer-docs.sdk.python.sdk-import]-start from onepassword import * + # [developer-docs.sdk.python.sdk-import]-end import asyncio import os @@ -107,7 +108,9 @@ async def showcase_vault_operations(client: Client): # [developer-docs.sdk.python.list-vault]-end -async def showcase_group_permission_operations(client: Client, vault_id: str, group_id: str): +async def showcase_group_permission_operations( + client: Client, vault_id: str, group_id: str +): # [developer-docs.sdk.python.grant-group-permissions]-start # Grant group permissions in a vault await client.vaults.grant_group_permissions( @@ -202,8 +205,7 @@ async def showcase_batch_item_operations(client: Client, vault_id: str): item_ids = [] for res in batchCreateResponse.individual_responses: if res.content is not None: - print('Created item "{}" ({})'.format( - res.content.title, res.content.id)) + print('Created item "{}" ({})'.format(res.content.title, res.content.id)) item_ids.append(res.content.id) elif res.error is not None: print("[Batch create] Something went wrong: {}".format(res.error)) @@ -214,8 +216,7 @@ async def showcase_batch_item_operations(client: Client, vault_id: str): batchGetReponse = await client.items.get_all(vault_id, item_ids) for res in batchGetReponse.individual_responses: if res.content is not None: - print('Obtained item "{}" ({})'.format( - res.content.title, res.content.id)) + print('Obtained item "{}" ({})'.format(res.content.title, res.content.id)) elif res.error is not None: print("[Batch get] Something went wrong: {}".format(res.error)) # [developer-docs.sdk.python.batch-get-items]-end @@ -230,5 +231,6 @@ async def showcase_batch_item_operations(client: Client, vault_id: str): print("Deleted item {}".format(id)) # [developer-docs.sdk.python.batch-delete-items]-end + if __name__ == "__main__": asyncio.run(main()) diff --git a/example/example.py b/example/example.py index 36c57985..ec73490d 100644 --- a/example/example.py +++ b/example/example.py @@ -197,7 +197,9 @@ async def main(): # [developer-docs.sdk.python.delete-item]-start # Delete an item await client.items.delete(created_item.vault_id, updated_item.id) - print(f"Item {updated_item.id} successfully deleted from vault {created_item.vault_id}.") + print( + f"Item {updated_item.id} successfully deleted from vault {created_item.vault_id}." + ) # [developer-docs.sdk.python.delete-item]-end await showcase_vault_operations(client) @@ -242,7 +244,7 @@ async def showcase_vault_operations(client: Client): updated_vault = await client.vaults.update(created_vault.id, update_params) print( f'Updated vault "{updated_vault.title}" ({updated_vault.id}): ' - f'{updated_vault.description!r}' + f"{updated_vault.description!r}" ) # [developer-docs.sdk.python.update-vault]-end @@ -321,8 +323,7 @@ async def showcase_batch_item_operations(client: Client, vault_id: str): item_ids = [] for res in batchCreateResponse.individual_responses: if res.content is not None: - print('Created item "{}" ({})'.format( - res.content.title, res.content.id)) + print('Created item "{}" ({})'.format(res.content.title, res.content.id)) item_ids.append(res.content.id) elif res.error is not None: print("[Batch create] Something went wrong: {}".format(res.error)) @@ -333,8 +334,7 @@ async def showcase_batch_item_operations(client: Client, vault_id: str): batchGetReponse = await client.items.get_all(vault_id, item_ids) for res in batchGetReponse.individual_responses: if res.content is not None: - print('Obtained item "{}" ({})'.format( - res.content.title, res.content.id)) + print('Obtained item "{}" ({})'.format(res.content.title, res.content.id)) elif res.error is not None: print("[Batch get] Something went wrong: {}".format(res.error)) # [developer-docs.sdk.python.batch-get-items]-end @@ -633,7 +633,10 @@ def generate_special_item_fields(): ], ) -async def showcase_group_permission_operations(client: Client, vault_id: str, group_id: str): + +async def showcase_group_permission_operations( + client: Client, vault_id: str, group_id: str +): # [developer-docs.sdk.python.grant-group-permissions]-start # Grant group permissions in a vault await client.vaults.grant_group_permissions( @@ -655,7 +658,7 @@ async def showcase_group_permission_operations(client: Client, vault_id: str, gr GroupVaultAccess( vault_id=vault_id, group_id=group_id, - permissions= READ_ITEMS | CREATE_ITEMS | UPDATE_ITEMS, + permissions=READ_ITEMS | CREATE_ITEMS | UPDATE_ITEMS, ) ], ) @@ -678,6 +681,5 @@ async def showcase_group_permission_operations(client: Client, vault_id: str, gr # [developer-docs.sdk.python.get-group]-end - if __name__ == "__main__": asyncio.run(main()) diff --git a/src/onepassword/core.py b/src/onepassword/core.py index 490a75ea..10e3ca23 100644 --- a/src/onepassword/core.py +++ b/src/onepassword/core.py @@ -23,7 +23,9 @@ class InnerClient: core: DesktopCore | UniffiCore config: dict[str, Any] - def __init__(self, client_id: int, core: "DesktopCore | UniffiCore", config: dict[str, any]): + def __init__( + self, client_id: int, core: "DesktopCore | UniffiCore", config: dict[str, any] + ): self.client_id = client_id self.core = core self.config = config diff --git a/src/onepassword/defaults.py b/src/onepassword/defaults.py index 1cd626ab..33e1a3ed 100644 --- a/src/onepassword/defaults.py +++ b/src/onepassword/defaults.py @@ -9,6 +9,7 @@ DEFAULT_REQUEST_LIBRARY_VERSION = "0.11.24" DEFAULT_OS_VERSION = "0.0.0" + class DesktopAuth: def __init__(self, account_name: str): """ @@ -19,6 +20,7 @@ def __init__(self, account_name: str): """ self.account_name = account_name + # Generates a configuration dictionary with the user's parameters def new_default_config(auth: DesktopAuth | str, integration_name, integration_version): client_config_dict = { diff --git a/src/onepassword/desktop_core.py b/src/onepassword/desktop_core.py index 9c58cfe8..e98bf4a3 100644 --- a/src/onepassword/desktop_core.py +++ b/src/onepassword/desktop_core.py @@ -16,19 +16,22 @@ def find_1password_lib_path(): if os_name == "Darwin": # macOS locations = [ "/Applications/1Password.app/Contents/Frameworks/libop_sdk_ipc_client.dylib", - str(Path.home() / "Applications/1Password.app/Contents/Frameworks/libop_sdk_ipc_client.dylib"), + str( + Path.home() + / "Applications/1Password.app/Contents/Frameworks/libop_sdk_ipc_client.dylib" + ), ] elif os_name == "Linux": locations = [ "/usr/bin/1password/libop_sdk_ipc_client.so", - "/opt/1Password/libop_sdk_ipc_client.so", - "/snap/bin/1password/libop_sdk_ipc_client.so", + "/opt/1Password/libop_sdk_ipc_client.so", + "/snap/bin/1password/libop_sdk_ipc_client.so", ] elif os_name == "Windows": locations = [ str(Path.home() / r"AppData\Local\1Password\op_sdk_ipc_client.dll"), r"C:\Program Files\1Password\app\8\op_sdk_ipc_client.dll", - r"C:\Program Files (x86)\1Password\app\8\op_sdk_ipc_client.dll", + r"C:\Program Files (x86)\1Password\app\8\op_sdk_ipc_client.dll", str(Path.home() / r"AppData\Local\1Password\app\8\op_sdk_ipc_client.dll"), ] else: @@ -40,6 +43,7 @@ def find_1password_lib_path(): raise FileNotFoundError("1Password desktop application not found") + class DesktopCore: def __init__(self, account_name: str): # Determine the path to the desktop app. @@ -51,11 +55,11 @@ def __init__(self, account_name: str): # Bind the Rust-exported functions self.send_message = self.lib.op_sdk_ipc_send_message self.send_message.argtypes = [ - POINTER(c_uint8), # msg_ptr - c_size_t, # msg_len - POINTER(POINTER(c_uint8)), # out_buf - POINTER(c_size_t), # out_len - POINTER(c_size_t), # out_cap + POINTER(c_uint8), # msg_ptr + c_size_t, # msg_len + POINTER(POINTER(c_uint8)), # out_buf + POINTER(c_size_t), # out_len + POINTER(c_size_t), # out_cap ] self.send_message.restype = c_int32 @@ -123,6 +127,7 @@ def release_client(self, client_id: int): except Exception as e: print(f"failed to release client: {e}") + ERR_CHANNEL_CLOSED = ( "desktop app connection channel is closed. " "Make sure Settings > Developer > Integrate with other apps is enabled, " diff --git a/src/onepassword/environments.py b/src/onepassword/environments.py index e54c7dfe..7b3ec982 100644 --- a/src/onepassword/environments.py +++ b/src/onepassword/environments.py @@ -7,7 +7,8 @@ class Environments: """ - The Environments API holds all the operations the SDK client can perform on 1Password Environments. + The Environments API holds all the operations the SDK client can perform + on 1Password Environments and the secrets inside them. """ def __init__(self, inner_client: InnerClient): diff --git a/src/onepassword/errors.py b/src/onepassword/errors.py index 811aebbe..16b76d28 100644 --- a/src/onepassword/errors.py +++ b/src/onepassword/errors.py @@ -15,6 +15,12 @@ def __init__(self, message): super().__init__(self.message) +class AuthExpiredException(Exception): + def __init__(self, message): + self.message = message + super().__init__(self.message) + + def raise_typed_exception(e: Exception): try: typed_error = json.loads(e.msg) @@ -28,6 +34,8 @@ def raise_typed_exception(e: Exception): raise DesktopSessionExpiredException(message) elif error_name == "RateLimitExceeded": raise RateLimitExceededException(message) + elif error_name == "AuthExpired": + raise AuthExpiredException(message) elif message is not None: raise Exception(message) else: diff --git a/src/onepassword/lib/aarch64/libop_uniffi_core.dylib b/src/onepassword/lib/aarch64/libop_uniffi_core.dylib old mode 100755 new mode 100644 index 35636374..16385395 Binary files a/src/onepassword/lib/aarch64/libop_uniffi_core.dylib and b/src/onepassword/lib/aarch64/libop_uniffi_core.dylib differ diff --git a/src/onepassword/lib/aarch64/libop_uniffi_core.so b/src/onepassword/lib/aarch64/libop_uniffi_core.so old mode 100755 new mode 100644 index 1d6aa0bc..df8d58cd Binary files a/src/onepassword/lib/aarch64/libop_uniffi_core.so and b/src/onepassword/lib/aarch64/libop_uniffi_core.so differ diff --git a/src/onepassword/lib/x86_64/libop_uniffi_core.dylib b/src/onepassword/lib/x86_64/libop_uniffi_core.dylib old mode 100755 new mode 100644 index d56feb01..95bbc442 Binary files a/src/onepassword/lib/x86_64/libop_uniffi_core.dylib and b/src/onepassword/lib/x86_64/libop_uniffi_core.dylib differ diff --git a/src/onepassword/lib/x86_64/libop_uniffi_core.so b/src/onepassword/lib/x86_64/libop_uniffi_core.so old mode 100755 new mode 100644 index acbf1e9c..13c96ed8 Binary files a/src/onepassword/lib/x86_64/libop_uniffi_core.so and b/src/onepassword/lib/x86_64/libop_uniffi_core.so differ diff --git a/src/onepassword/lib/x86_64/op_uniffi_core.dll b/src/onepassword/lib/x86_64/op_uniffi_core.dll index 8e7e7d61..d80d26cc 100644 Binary files a/src/onepassword/lib/x86_64/op_uniffi_core.dll and b/src/onepassword/lib/x86_64/op_uniffi_core.dll differ diff --git a/src/onepassword/types.py b/src/onepassword/types.py index d74f011e..976f4f51 100644 --- a/src/onepassword/types.py +++ b/src/onepassword/types.py @@ -90,7 +90,9 @@ class DocumentCreateParams(BaseModel): class EnvironmentVariable(BaseModel): """ - Represents an environment variable (name:value pair) and its masked state + One environment variable resolved by the credential broker. + + Used by the Environments API (`GetVariablesResponse`). """ name: str @@ -234,6 +236,7 @@ class GroupState(str, Enum): class VaultAccessorType(str, Enum): USER = "user" GROUP = "group" + BROKER = "broker" class VaultAccess(BaseModel): @@ -1329,7 +1332,7 @@ class Vault(BaseModel): """ attribute_version: int = Field(alias="attributeVersion") """ - The attribute version number of the vault. It gets incremented whenever vault presentation information changes, such as its title or icon. + The attribute version number of the vault. It gets incremented whenever vault presentation information changes, such as its name or icon. """ access: Optional[List[VaultAccess]] = Field(default=None) """ @@ -1395,7 +1398,7 @@ class VaultOverview(BaseModel): """ attribute_version: int = Field(alias="attributeVersion") """ - The attribute version number of the vault. It gets incremented whenever vault presentation information changes, such as its title or icon. + The attribute version number of the vault. It gets incremented whenever vault presentation information changes, such as its name or icon. """ created_at: Annotated[ datetime,