-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathshared_lib_core.go
More file actions
154 lines (130 loc) · 3.64 KB
/
shared_lib_core.go
File metadata and controls
154 lines (130 loc) · 3.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package internal
import (
"context"
"encoding/json"
"fmt"
"log"
"os"
"path"
"runtime"
)
// Request/Response mirror your Unix file (kept identical)
type Request struct {
Kind string `json:"kind"`
AccountName string `json:"account_name"`
Payload []byte `json:"payload"`
}
type Response struct {
Success bool `json:"success"`
Payload []byte `json:"payload"`
}
func (r Response) Error() string { return string(r.Payload) }
// find1PasswordLibPath returns the path to the 1Password shared library
// (libop_sdk_ipc_client.dylib/.so/.dll) depending on OS.
func find1PasswordLibPath(customLocation string) (string, error) {
var locations []string
home, err := os.UserHomeDir()
if err != nil {
return "", err
}
switch runtime.GOOS {
case "darwin":
locations = []string{
"/Applications/1Password.app/Contents/Frameworks/libop_sdk_ipc_client.dylib",
path.Join(home, "Applications/1Password.app/Contents/Frameworks/libop_sdk_ipc_client.dylib"),
}
case "linux":
locations = []string{
"/usr/bin/1password/libop_sdk_ipc_client.so",
"/opt/1Password/libop_sdk_ipc_client.so",
"/snap/bin/1password/libop_sdk_ipc_client.so",
}
case "windows":
locations = []string{
path.Join(home, `AppData\Local\1Password\op_sdk_ipc_client.dll`),
`C:\Program Files\1Password\app\8\op_sdk_ipc_client.dll`,
`C:\Program Files (x86)\1Password\app\8\op_sdk_ipc_client.dll`,
path.Join(home, `AppData\Local\1Password\app\8\op_sdk_ipc_client.dll`),
}
default:
return "", fmt.Errorf("unsupported OS: %s", runtime.GOOS)
}
if customLocation != "" {
locations = append([]string{customLocation}, locations...)
}
for _, libPath := range locations {
if _, err := os.Stat(libPath); err == nil {
return libPath, nil
}
}
return "", fmt.Errorf("1Password desktop application not found")
}
func GetSharedLibCore(accountName string, libraryPath string) (*CoreWrapper, error) {
if coreLib == nil {
libPath, err := find1PasswordLibPath(libraryPath)
if err != nil {
return nil, err
}
coreLib, err = loadCore(libPath)
if err != nil {
return nil, err
}
coreLib.accountName = accountName
}
coreWrapper := CoreWrapper{InnerCore: coreLib}
return &coreWrapper, nil
}
// InitClient creates a client instance in the current core module and returns its unique ID.
func (slc *SharedLibCore) InitClient(ctx context.Context, config []byte) ([]byte, error) {
const kind = "init_client"
request := Request{
Kind: kind,
AccountName: slc.accountName,
Payload: config,
}
requestMarshaled, err := json.Marshal(request)
if err != nil {
return nil, err
}
res, err := slc.callSharedLibrary(requestMarshaled)
if err != nil {
return nil, err
}
return res, nil
}
// Invoke performs an SDK operation.
func (slc *SharedLibCore) Invoke(ctx context.Context, invokeConfig []byte) ([]byte, error) {
const kind = "invoke"
request := Request{
Kind: kind,
AccountName: slc.accountName,
Payload: invokeConfig,
}
requestMarshaled, err := json.Marshal(request)
if err != nil {
return nil, err
}
res, err := slc.callSharedLibrary(requestMarshaled)
if err != nil {
return nil, err
}
return res, nil
}
// ReleaseClient releases memory in the core associated with the given client ID.
func (slc *SharedLibCore) ReleaseClient(clientID []byte) {
const kind = "release_client"
request := Request{
Kind: kind,
AccountName: slc.accountName,
Payload: clientID,
}
requestMarshaled, err := json.Marshal(request)
if err != nil {
log.Println("failed to marshal release_client request")
return
}
_, err = slc.callSharedLibrary(requestMarshaled)
if err != nil {
log.Println("failed to release client")
}
}