11import ctypes
22import json
33import os
4- import platform
5- import sys
4+ import base64
65from ctypes import c_uint8 , c_size_t , c_int32 , POINTER , byref , c_void_p
76from .core import UniffiCore
7+ from onepassword .errors import raise_typed_exception
88
99
1010def find_1password_lib_path ():
11- host_os = platform .system ().lower () # "darwin", "linux", "windows"
12-
13- core = UniffiCore ()
14- if core is None :
15- raise RuntimeError ("failed to get ExtismCore" )
16-
17- locations_raw = core .invoke ({
18- "invocation" : {
19- "parameters" : {
20- "methodName" : "GetDesktopAppIPCClientLocations" ,
21- "serializedParams" : {"host_os" : host_os },
22- }
23- }
24- })
25-
26- try :
27- locations = json .loads (locations_raw )
28- except Exception as e :
29- raise RuntimeError (f"failed to parse core response: { e } " )
11+ locations = [
12+ "/Users/andititu/core/target/debug/libop_sdk_ipc_client.dylib"
13+ ]
3014
3115 for lib_path in locations :
3216 if os .path .exists (lib_path ):
@@ -35,11 +19,12 @@ def find_1password_lib_path():
3519 raise FileNotFoundError ("1Password desktop application not found" )
3620
3721class DesktopCore :
38- def __init__ (self ):
22+ def __init__ (self , account_name : str ):
3923 # Determine the path to the desktop app.
4024 path = find_1password_lib_path ()
4125
4226 self .lib = ctypes .CDLL (path )
27+ self .account_name = account_name
4328
4429 # Bind the Rust-exported functions
4530 self .send_message = self .lib .op_sdk_ipc_send_message
@@ -56,43 +41,59 @@ def __init__(self):
5641 self .free_message .argtypes = [POINTER (c_uint8 ), c_size_t , c_size_t ]
5742 self .free_message .restype = None
5843
59- def call_shared_library (self , payload : bytes ) -> bytes :
44+ def call_shared_library (self , payload : str , operation_kind : str ) -> bytes :
45+ # Prepare the input
46+ encoded_payload = base64 .b64encode (payload .encode ("utf-8" )).decode ("utf-8" )
47+ data = {
48+ "kind" : operation_kind ,
49+ "account_name" : self .account_name ,
50+ "payload" : encoded_payload ,
51+ }
52+ message = json .dumps (data ).encode ("utf-8" )
53+
54+ # Prepare output parameters
6055 out_buf = POINTER (c_uint8 )()
6156 out_len = c_size_t ()
6257 out_cap = c_size_t ()
6358
6459 ret = self .send_message (
65- (ctypes .cast (payload , POINTER (c_uint8 ))),
66- len (payload ),
60+ (ctypes .cast (message , POINTER (c_uint8 ))),
61+ len (message ),
6762 byref (out_buf ),
6863 byref (out_len ),
6964 byref (out_cap ),
7065 )
7166
7267 if ret != 0 :
73- raise RuntimeError (f"send_message failed with code { ret } " )
68+ raise RuntimeError (f"send_message failed with code { ret } . Please make sure the Desktop app intehration setting is enabled, or contact 1Password support. " )
7469
7570 # Copy bytes into Python
7671 data = ctypes .string_at (out_buf , out_len .value )
7772
7873 # Free memory via Rust's exported function
7974 self .free_message (out_buf , out_len , out_cap )
8075
81- return data
76+ parsed = json .loads (data )
77+ payload = bytes (parsed .get ("payload" , [])).decode ("utf-8" )
78+
79+ success = parsed .get ("success" , False )
80+ if not success :
81+ raise_typed_exception (Exception (str (payload )))
82+
83+ return payload
8284
83- def init_client (self , config : dict ) -> int :
84- payload = json .dumps (config ). encode ( "utf-8" )
85- resp = self .call_shared_library (payload )
85+ async def init_client (self , config : dict ) -> int :
86+ payload = json .dumps (config )
87+ resp = self .call_shared_library (payload , "init_client" )
8688 return json .loads (resp )
8789
88- def invoke (self , invoke_config : dict ) -> str :
89- payload = json .dumps (invoke_config ).encode ("utf-8" )
90- resp = self .call_shared_library (payload )
91- return resp .decode ("utf-8" )
90+ async def invoke (self , invoke_config : dict ) -> str :
91+ payload = json .dumps (invoke_config )
92+ return self .call_shared_library (payload , "invoke" )
9293
9394 def release_client (self , client_id : int ):
94- payload = json .dumps (client_id ). encode ( "utf-8" )
95+ payload = json .dumps (client_id )
9596 try :
96- self .call_shared_library (payload )
97+ self .call_shared_library (payload , "release_client" )
9798 except Exception as e :
9899 print (f"failed to release client: { e } " )
0 commit comments