|
3 | 3 | package internal |
4 | 4 |
|
5 | 5 | import ( |
6 | | - "context" |
7 | 6 | "encoding/json" |
8 | 7 | "errors" |
9 | 8 | "fmt" |
10 | | - "log" |
11 | | - "os" |
12 | | - "path" |
13 | | - "runtime" |
14 | 9 | "unsafe" |
15 | 10 | ) |
16 | 11 |
|
@@ -85,60 +80,6 @@ type SharedLibCore struct { |
85 | 80 |
|
86 | 81 | var coreLib *SharedLibCore |
87 | 82 |
|
88 | | -// find1PasswordLibPath returns the path to the 1Password shared library |
89 | | -// (libop_sdk_ipc_client.dylib/.so/.dll) depending on OS. |
90 | | -func find1PasswordLibPath() (string, error) { |
91 | | - var locations []string |
92 | | - |
93 | | - home, err := os.UserHomeDir() |
94 | | - if err != nil { |
95 | | - return "", err |
96 | | - } |
97 | | - |
98 | | - switch runtime.GOOS { |
99 | | - case "darwin": |
100 | | - locations = []string{ |
101 | | - "/Applications/1Password.app/Contents/Frameworks/libop_sdk_ipc_client.dylib", |
102 | | - path.Join(home, "Applications/1Password.app/Contents/Frameworks/libop_sdk_ipc_client.dylib"), |
103 | | - } |
104 | | - |
105 | | - case "linux": |
106 | | - locations = []string{ |
107 | | - "/usr/bin/1password/libop_sdk_ipc_client.so", |
108 | | - "/opt/1Password/libop_sdk_ipc_client.so", |
109 | | - "/snap/bin/1password/libop_sdk_ipc_client.so", |
110 | | - } |
111 | | - |
112 | | - default: |
113 | | - return "", fmt.Errorf("unsupported OS: %s", runtime.GOOS) |
114 | | - } |
115 | | - for _, libPath := range locations { |
116 | | - if _, err := os.Stat(libPath); err == nil { |
117 | | - return libPath, nil |
118 | | - } |
119 | | - } |
120 | | - |
121 | | - return "", fmt.Errorf("1Password desktop application not found") |
122 | | -} |
123 | | - |
124 | | -func GetSharedLibCore(accountName string) (*CoreWrapper, error) { |
125 | | - if coreLib == nil { |
126 | | - path, err := find1PasswordLibPath() |
127 | | - if err != nil { |
128 | | - return nil, err |
129 | | - } |
130 | | - coreLib, err = loadCore(path) |
131 | | - if err != nil { |
132 | | - return nil, err |
133 | | - } |
134 | | - coreLib.accountName = accountName |
135 | | - } |
136 | | - |
137 | | - coreWrapper := CoreWrapper{InnerCore: coreLib} |
138 | | - |
139 | | - return &coreWrapper, nil |
140 | | -} |
141 | | - |
142 | 83 | func loadCore(path string) (*SharedLibCore, error) { |
143 | 84 | cPath := C.CString(path) |
144 | 85 | defer C.free(unsafe.Pointer(cPath)) |
@@ -173,56 +114,6 @@ func loadCore(path string) (*SharedLibCore, error) { |
173 | 114 | }, nil |
174 | 115 | } |
175 | 116 |
|
176 | | -// InitClient creates a client instance in the current core module and returns its unique ID. |
177 | | -func (slc *SharedLibCore) InitClient(ctx context.Context, config []byte) ([]byte, error) { |
178 | | - const kind = "init_client" |
179 | | - request := Request{ |
180 | | - Kind: kind, |
181 | | - AccountName: slc.accountName, |
182 | | - Payload: config, |
183 | | - } |
184 | | - |
185 | | - requestMarshaled, err := json.Marshal(request) |
186 | | - if err != nil { |
187 | | - return nil, err |
188 | | - } |
189 | | - res, err := slc.callSharedLibrary(requestMarshaled) |
190 | | - if err != nil { |
191 | | - return nil, err |
192 | | - } |
193 | | - |
194 | | - return res, nil |
195 | | -} |
196 | | - |
197 | | -func (slc *SharedLibCore) Invoke(ctx context.Context, invokeConfig []byte) ([]byte, error) { |
198 | | - const kind = "invoke" |
199 | | - request := Request{ |
200 | | - Kind: kind, |
201 | | - AccountName: slc.accountName, |
202 | | - Payload: invokeConfig, |
203 | | - } |
204 | | - |
205 | | - requestMarshaled, err := json.Marshal(request) |
206 | | - if err != nil { |
207 | | - return nil, err |
208 | | - } |
209 | | - |
210 | | - res, err := slc.callSharedLibrary(requestMarshaled) |
211 | | - if err != nil { |
212 | | - return nil, err |
213 | | - } |
214 | | - |
215 | | - return res, nil |
216 | | -} |
217 | | - |
218 | | -// ReleaseClient releases memory in the core associated with the given client ID. |
219 | | -func (slc *SharedLibCore) ReleaseClient(clientID []byte) { |
220 | | - _, err := slc.callSharedLibrary(clientID) |
221 | | - if err != nil { |
222 | | - log.Println("failed to release client") |
223 | | - } |
224 | | -} |
225 | | - |
226 | 117 | func (slc *SharedLibCore) callSharedLibrary(input []byte) ([]byte, error) { |
227 | 118 | if len(input) == 0 { |
228 | 119 | return nil, errors.New("internal: empty input") |
@@ -261,18 +152,3 @@ func (slc *SharedLibCore) callSharedLibrary(input []byte) ([]byte, error) { |
261 | 152 | return nil, response |
262 | 153 | } |
263 | 154 | } |
264 | | - |
265 | | -type Request struct { |
266 | | - Kind string `json:"kind"` |
267 | | - AccountName string `json:"account_name"` |
268 | | - Payload []byte `json:"payload"` |
269 | | -} |
270 | | - |
271 | | -type Response struct { |
272 | | - Success bool `json:"success"` |
273 | | - Payload []byte `json:"payload"` |
274 | | -} |
275 | | - |
276 | | -func (r Response) Error() string { |
277 | | - return string(r.Payload) |
278 | | -} |
0 commit comments