forked from keybase/client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
installer.go
113 lines (100 loc) · 2.84 KB
/
installer.go
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
package installer
import (
"os"
"path/filepath"
"strings"
"github.com/keybase/client/go/kbnm/hostmanifest"
)
const kbnmAppName = "io.keybase.kbnm"
// CurrentUser returns a hostmanifest.User while allowing for overrides using
// environment variables:
// * KBNM_INSTALL_ROOT != "" will force the user paths to be root
// * KBNM_INSTALL_OVERLAY will prefix the paths
func CurrentUser() (hostmanifest.User, error) {
u, err := hostmanifest.CurrentUser()
if err != nil {
return nil, err
}
if os.Getenv("KBNM_INSTALL_ROOT") != "" {
u.Admin = true
u.Path = ""
}
overlay := os.Getenv("KBNM_INSTALL_OVERLAY")
if overlay != "" {
u.Path = filepath.Join(overlay, u.Path)
}
return u, err
}
// UninstallKBNM removes NativeMessaging whitelisting for KBNM.
func UninstallKBNM() error {
u, err := CurrentUser()
if err != nil {
return err
}
app := hostmanifest.App{
Name: kbnmAppName,
}
for _, whitelist := range hostmanifest.KnownInstallers() {
if err := whitelist.Uninstall(u, app); err != nil {
return err
}
}
return nil
}
// InstallKBNM writes NativeMessaging whitelisting for KBNM.
func InstallKBNM(path string) error {
u, err := CurrentUser()
if err != nil {
return err
}
// If we're installing in an overlay, we need to strip it as a prefix from
// the path we detect.
overlay := os.Getenv("KBNM_INSTALL_OVERLAY")
if overlay != "" {
// This is a bit arcane because filepath.HasPrefix deprecated due to
// being broken, but what it does is it attempts to map path as
// relative to overlay. If it succeeds and they're both absolute paths
// (which means the relative path won't start with "../"), then make
// path an absolute path with the overlay removed
// filepath.Rel takes care of normalizing paths which is an advantage
// over direct string comparisons which would be simpler.
rel, err := filepath.Rel(overlay, path)
if err == nil && !strings.HasPrefix(rel, ".") {
path = "/" + rel
}
}
app := hostmanifest.App{
Name: kbnmAppName,
Description: "Keybase Native Messaging API",
Path: path,
Type: "stdio",
}
var manifest hostmanifest.AppManifest
for browser, whitelist := range hostmanifest.KnownInstallers() {
switch browser {
case "chrome", "chromium":
manifest = hostmanifest.ChromeApp{
App: app,
AllowedOrigins: []string{
// Production public version in the store
"chrome-extension://ognfafcpbkogffpmmdglhbjboeojlefj/",
// Hard-coded key from the repo version
"chrome-extension://kockbbfoibcdfibclaojljblnhpnjndg/",
// Keybase-internal version
"chrome-extension://gnjkbjlgkpiaehpibpdefaieklbfljjm/",
},
}
case "firefox":
manifest = hostmanifest.FirefoxApp{
App: app,
AllowedExtensions: []string{
"keybase@keybase.io",
},
}
}
if err := whitelist.Install(u, manifest); err != nil {
return err
}
}
return nil
}