-
Notifications
You must be signed in to change notification settings - Fork 20
Add support for authenticating via desktop app #179
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
AndyTitu
merged 6 commits into
sdks-for-desktop-integrations
from
andi/sdk-desktop-integrations
Oct 13, 2025
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
155545b
Refactor code to add support for desktop integration
AndyTitu c8589e0
Inject core in all places needed
AndyTitu 099f7d2
Refactor item shares as well
AndyTitu fd67b9c
Refactor finlizer
AndyTitu 381023a
Remove py version 3.9 from tests
AndyTitu dd4ea87
Update code to latestcore data passing model
AndyTitu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| # [developer-docs.sdk.python.sdk-import]-start | ||
| from onepassword import * | ||
| import asyncio | ||
|
|
||
|
|
||
| async def main(): | ||
| # [developer-docs.sdk.python.client-initialization]-start | ||
| # Connects to the 1Password desktop app. | ||
| client = await Client.authenticate( | ||
| auth=DesktopAuth( | ||
| account_name="AndiTituTest" # Set to your 1Password account name. | ||
| ), | ||
| # Set the following to your own integration name and version. | ||
| integration_name="My 1Password Integration", | ||
| integration_version="v1.0.0", | ||
| ) | ||
|
|
||
| # [developer-docs.sdk.python.list-vaults]-start | ||
| vaults = await client.vaults.list() | ||
| for vault in vaults: | ||
| print(vault) | ||
| # [developer-docs.sdk.python.list-vaults]-end | ||
|
|
||
| # [developer-docs.sdk.python.list-items]-start | ||
| overviews = await client.items.list("xw33qlvug6moegr3wkk5zkenoa") | ||
| for overview in overviews: | ||
| print(overview.title) | ||
| # [developer-docs.sdk.python.list-items]-end | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| asyncio.run(main()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,59 +1,72 @@ | ||
| import json | ||
| import platform | ||
|
|
||
| from typing import Protocol | ||
| from onepassword.errors import raise_typed_exception | ||
|
|
||
| # In empirical tests, we determined that maximum message size that can cross the FFI boundary | ||
| # is ~128MB. Past this limit, FFI will throw an error and the program will crash. | ||
| # We set the limit to 50MB to be safe and consistent with the other SDKs (where this limit is 64MB), to be reconsidered upon further testing | ||
| MESSAGE_LIMIT = 50 * 1024 * 1024 | ||
|
|
||
| machine_arch = platform.machine().lower() | ||
|
|
||
| if machine_arch in ["x86_64", "amd64"]: | ||
| import onepassword.lib.x86_64.op_uniffi_core as core | ||
| elif machine_arch in ["aarch64", "arm64"]: | ||
| import onepassword.lib.aarch64.op_uniffi_core as core | ||
| else: | ||
| raise ImportError( | ||
| f"Your machine's architecture is not currently supported: {machine_arch}" | ||
| ) | ||
|
|
||
|
|
||
| # InitClient creates a client instance in the current core module and returns its unique ID. | ||
| async def _init_client(client_config): | ||
| try: | ||
| return await core.init_client(json.dumps(client_config)) | ||
| except Exception as e: | ||
| raise_typed_exception(e) | ||
|
|
||
|
|
||
| # Invoke calls specified business logic from the SDK core. | ||
| async def _invoke(invoke_config): | ||
| serialized_config = json.dumps(invoke_config) | ||
| if len(serialized_config.encode()) > MESSAGE_LIMIT: | ||
| raise ValueError( | ||
| f"message size exceeds the limit of {MESSAGE_LIMIT} bytes, please contact 1Password at [email protected] or https://developer.1password.com/joinslack if you need help." | ||
| ) | ||
| try: | ||
| return await core.invoke(serialized_config) | ||
| except Exception as e: | ||
| raise_typed_exception(e) | ||
|
|
||
|
|
||
| # Invoke calls specified business logic from the SDK core. | ||
| def _invoke_sync(invoke_config): | ||
| serialized_config = json.dumps(invoke_config) | ||
| if len(serialized_config.encode()) > MESSAGE_LIMIT: | ||
| raise ValueError( | ||
| f"message size exceeds the limit of {MESSAGE_LIMIT} bytes, please contact 1Password at [email protected] or https://developer.1password.com/joinslack if you need help." | ||
| ) | ||
| try: | ||
| return core.invoke_sync(serialized_config) | ||
| except Exception as e: | ||
| raise_typed_exception(e) | ||
|
|
||
|
|
||
| # ReleaseClient releases memory in the SDK core associated with the given client ID. | ||
| def _release_client(client_id): | ||
| return core.release_client(json.dumps(client_id)) | ||
| class Core(Protocol): | ||
| async def init_client(self, client_config: dict) -> str: ... | ||
| async def invoke(self, invoke_config: dict) -> str: ... | ||
| def invoke_sync(self, invoke_config: dict) -> str: ... | ||
| def release_client(self, client_id: int) -> None: ... | ||
|
|
||
| class UniffiCore: | ||
| def __init__(self): | ||
| machine_arch = platform.machine().lower() | ||
|
|
||
| if machine_arch in ["x86_64", "amd64"]: | ||
| import onepassword.lib.x86_64.op_uniffi_core as core | ||
| elif machine_arch in ["aarch64", "arm64"]: | ||
| import onepassword.lib.aarch64.op_uniffi_core as core | ||
| else: | ||
| raise ImportError( | ||
| f"Your machine's architecture is not currently supported: {machine_arch}" | ||
| ) | ||
|
|
||
| self.core = core | ||
|
|
||
| async def init_client(self, client_config: dict): | ||
| """Creates a client instance in the current core module and returns its unique ID.""" | ||
| try: | ||
| return await self.core.init_client(json.dumps(client_config)) | ||
| except Exception as e: | ||
| raise_typed_exception(e) | ||
|
|
||
| async def invoke(self, invoke_config: dict): | ||
| """Invoke business logic asynchronously.""" | ||
| serialized_config = json.dumps(invoke_config) | ||
| if len(serialized_config.encode()) > MESSAGE_LIMIT: | ||
| raise ValueError( | ||
| f"message size exceeds the limit of {MESSAGE_LIMIT} bytes, " | ||
| "please contact 1Password at [email protected] or " | ||
| "https://developer.1password.com/joinslack if you need help." | ||
| ) | ||
| try: | ||
| return await self.core.invoke(serialized_config) | ||
| except Exception as e: | ||
| raise_typed_exception(e) | ||
|
|
||
| def invoke_sync(self, invoke_config: dict): | ||
| """Invoke business logic synchronously.""" | ||
| serialized_config = json.dumps(invoke_config) | ||
| if len(serialized_config.encode()) > MESSAGE_LIMIT: | ||
| raise ValueError( | ||
| f"message size exceeds the limit of {MESSAGE_LIMIT} bytes, " | ||
| "please contact 1Password at [email protected] or " | ||
| "https://developer.1password.com/joinslack if you need help." | ||
| ) | ||
| try: | ||
| return self.core.invoke_sync(serialized_config) | ||
| except Exception as e: | ||
| raise_typed_exception(e) | ||
|
|
||
| def release_client(self, client_id: int): | ||
| """Releases memory in the SDK core associated with the given client ID.""" | ||
| try: | ||
| return self.core.release_client(json.dumps(client_id)) | ||
| except Exception as e: | ||
| raise_typed_exception(e) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| import ctypes | ||
| import json | ||
| import os | ||
| import base64 | ||
| from ctypes import c_uint8, c_size_t, c_int32, POINTER, byref, c_void_p | ||
|
Check failure on line 5 in src/onepassword/desktop_core.py
|
||
| from .core import UniffiCore | ||
|
Check failure on line 6 in src/onepassword/desktop_core.py
|
||
| from onepassword.errors import raise_typed_exception | ||
|
|
||
|
|
||
| def find_1password_lib_path(): | ||
| locations = [ | ||
| "/Users/andititu/core/target/debug/libop_sdk_ipc_client.dylib" | ||
| ] | ||
|
|
||
| for lib_path in locations: | ||
| if os.path.exists(lib_path): | ||
| return lib_path | ||
|
|
||
| raise FileNotFoundError("1Password desktop application not found") | ||
|
|
||
| class DesktopCore: | ||
| def __init__(self, account_name: str): | ||
| # Determine the path to the desktop app. | ||
| path = find_1password_lib_path() | ||
|
|
||
| self.lib = ctypes.CDLL(path) | ||
| self.account_name = account_name | ||
|
|
||
| # 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 | ||
| ] | ||
| self.send_message.restype = c_int32 | ||
|
|
||
| self.free_message = self.lib.op_sdk_ipc_free_response | ||
| self.free_message.argtypes = [POINTER(c_uint8), c_size_t, c_size_t] | ||
| self.free_message.restype = None | ||
|
|
||
| def call_shared_library(self, payload: str, operation_kind: str) -> bytes: | ||
| # Prepare the input | ||
| encoded_payload = base64.b64encode(payload.encode("utf-8")).decode("utf-8") | ||
| data = { | ||
| "kind": operation_kind, | ||
| "account_name": self.account_name, | ||
| "payload": encoded_payload, | ||
| } | ||
| message = json.dumps(data).encode("utf-8") | ||
|
|
||
| # Prepare output parameters | ||
| out_buf = POINTER(c_uint8)() | ||
| out_len = c_size_t() | ||
| out_cap = c_size_t() | ||
|
|
||
| ret = self.send_message( | ||
| (ctypes.cast(message, POINTER(c_uint8))), | ||
| len(message), | ||
| byref(out_buf), | ||
| byref(out_len), | ||
| byref(out_cap), | ||
| ) | ||
|
|
||
| if ret != 0: | ||
| raise RuntimeError(f"send_message failed with code {ret}. Please make sure the Desktop app intehration setting is enabled, or contact 1Password support.") | ||
|
|
||
| # Copy bytes into Python | ||
| data = ctypes.string_at(out_buf, out_len.value) | ||
|
|
||
| # Free memory via Rust's exported function | ||
| self.free_message(out_buf, out_len, out_cap) | ||
|
|
||
| parsed = json.loads(data) | ||
| payload = bytes(parsed.get("payload", [])).decode("utf-8") | ||
|
|
||
| success = parsed.get("success", False) | ||
| if not success: | ||
| raise_typed_exception(Exception(str(payload))) | ||
|
|
||
| return payload | ||
|
|
||
| async def init_client(self, config: dict) -> int: | ||
| payload = json.dumps(config) | ||
| resp = self.call_shared_library(payload, "init_client") | ||
| return json.loads(resp) | ||
|
|
||
| async def invoke(self, invoke_config: dict) -> str: | ||
| payload = json.dumps(invoke_config) | ||
| return self.call_shared_library(payload, "invoke") | ||
|
|
||
| def release_client(self, client_id: int): | ||
| payload = json.dumps(client_id) | ||
| try: | ||
| self.call_shared_library(payload, "release_client") | ||
| except Exception as e: | ||
| print(f"failed to release client: {e}") | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Python 3.9 will reach EOL 30th of October anyway: https://devguide.python.org/versions/