-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
140 lines (117 loc) · 3.41 KB
/
main.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
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
package main
import (
"bufio"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/quexten/bw-bio-handler/logging"
"github.com/quexten/bw-bio-handler/pkg/bitw"
"github.com/quexten/bw-bio-handler/secret"
)
const appID = "com.quexten.bw-bio-handler"
var transportKey []byte
var secretStore secret.SecretStore
func main() {
if os.Args[1] == "install" {
install()
return
}
s, err := secret.GetStore()
if err != nil {
logging.Panicf(err.Error())
}
secretStore = s
transportKey = generateTransportKey()
setupCommunication()
readLoop()
}
func install() {
fmt.Println("Installing...")
fmt.Println("Copying polkit policy...")
workdir := os.Getenv("PWD")
cmd := exec.Command("pkexec", "cp", workdir+"/biometrics/policies/com.quexten.bw-bio-handler.policy", "/usr/share/polkit-1/actions/")
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
_ = cmd.Run()
// check file exists
_, err := os.Stat("/usr/share/polkit-1/actions/com.quexten.bw-bio-handler.policy")
if err != nil {
logging.Panicf("Failed to copy polkit policy: %s", err.Error())
}
fmt.Println("Detecting browsers...")
err = detectAndInstallBrowsers(".config")
if err != nil {
panic("Failed to detect browsers: " + err.Error())
}
err = detectAndInstallBrowsers(".mozilla")
if err != nil {
panic("Failed to detect browsers: " + err.Error())
}
fmt.Println("Getting secret...")
// read email from command line
scanner := bufio.NewScanner(os.Stdin)
fmt.Print("Enter email: ")
scanner.Scan()
email := scanner.Text()
fmt.Println("Enter password: ")
scanner.Scan()
password := scanner.Text()
fmt.Println("Enter api url (leave empty for default): ")
scanner.Scan()
apiURL := scanner.Text()
if apiURL == "" {
apiURL = "https://api.bitwarden.com"
}
fmt.Println("Enter identity url (leave empty for default): ")
scanner.Scan()
idtURL := scanner.Text()
if idtURL == "" {
idtURL = "https://identity.bitwarden.com"
}
err = bitw.DoLogin(email, password, apiURL, idtURL)
if err != nil {
panic("Failed to login: " + err.Error())
}
encKey := bitw.GetEncKeyB64()
userID := bitw.GetUserID()
fmt.Println("Got secret!")
fmt.Println("Storing in libsecret...")
store, err := secret.GetStore()
if err != nil {
panic("Failed to get secret store: " + err.Error())
}
store.SetSecret(userID, encKey)
fmt.Println("Done!")
fmt.Println("You can now activate the biometrics support in your browser. Enjoy!")
}
func detectAndInstallBrowsers(startPath string) error {
home := os.Getenv("HOME")
err := filepath.Walk(home+"/"+startPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return nil
}
var tempPath string
if !strings.HasPrefix(path, home) {
return nil
} else {
tempPath = strings.TrimPrefix(path, home)
}
if strings.Count(tempPath, "/") > 3 {
return nil
}
if info.IsDir() && info.Name() == "native-messaging-hosts" {
fmt.Printf("Found mozilla-like browser: %s\n", path)
manifest := strings.Replace(templateMozilla, "PATH", os.Getenv("PWD")+"/bw-bio-handler", 1)
err = os.WriteFile(path+"/com.8bit.bitwarden.json", []byte(manifest), 0644)
} else if info.IsDir() && info.Name() == "NativeMessagingHosts" {
fmt.Printf("Found chrome-like browser: %s\n", path)
manifest := strings.Replace(templateChrome, "PATH", os.Getenv("PWD")+"/bw-bio-handler", 1)
err = os.WriteFile(path+"/com.8bit.bitwarden.json", []byte(manifest), 0644)
}
return err
})
return err
}