forked from ravenac95/sudolikeaboss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
101 lines (79 loc) · 2.17 KB
/
client.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
package main
import (
"fmt"
"github.com/ravenac95/sudolikeaboss/onepass"
"os"
"strconv"
"time"
)
const DEFAULT_TIMEOUT_STRING_SECONDS = "30"
const DEFAULT_HOST = "sudolikeaboss://local"
const DEFAULT_WEBSOCKET_URI = "ws://127.0.0.1:6263/4"
const DEFAULT_WEBSOCKET_PROTOCOL = ""
const DEFAULT_WEBSOCKET_ORIGIN = "chrome-extension://aomjjhallfgjeglblehebfpbcfeobpgk"
func LoadConfiguration() *onepass.Configuration {
defaultHost := os.Getenv("SUDOLIKEABOSS_DEFAULT_HOST")
if defaultHost == "" {
defaultHost = DEFAULT_HOST
}
websocketUri := os.Getenv("SUDOLIKEABOSS_WEBSOCKET_URI")
if websocketUri == "" {
websocketUri = DEFAULT_WEBSOCKET_URI
}
websocketProtocol := os.Getenv("SUDOLIKEABOSS_WEBSOCKET_PROTOCOL")
if websocketProtocol == "" {
websocketProtocol = DEFAULT_WEBSOCKET_PROTOCOL
}
websocketOrigin := os.Getenv("SUDOLIKEABOSS_WEBSOCKET_ORIGIN")
if websocketOrigin == "" {
websocketOrigin = DEFAULT_WEBSOCKET_ORIGIN
}
return &onepass.Configuration{
WebsocketUri: websocketUri,
WebsocketProtocol: websocketProtocol,
WebsocketOrigin: websocketOrigin,
DefaultHost: defaultHost,
}
}
func retrievePasswordFromOnepassword(configuration *onepass.Configuration, done chan bool) {
// Load configuration from a file
client, err := onepass.NewClientWithConfig(configuration)
if err != nil {
os.Exit(1)
}
response, err := client.SendHelloCommand()
if err != nil {
os.Exit(1)
}
response, err = client.SendShowPopupCommand()
if err != nil {
os.Exit(1)
}
password, err := response.GetPassword()
fmt.Println(password)
done <- true
}
// Run the main sudolikeaboss entry point
func runSudolikeaboss() {
done := make(chan bool)
configuration := LoadConfiguration()
timeoutString := os.Getenv("SUDOLIKEABOSS_TIMEOUT_SECS")
if timeoutString == "" {
timeoutString = DEFAULT_TIMEOUT_STRING_SECONDS
}
timeout, err := strconv.ParseInt(timeoutString, 10, 16)
if err != nil {
os.Exit(1)
}
go retrievePasswordFromOnepassword(configuration, done)
// Timeout if necessary
select {
case <-done:
// Do nothing no need
case <-time.After(time.Duration(timeout) * time.Second):
close(done)
os.Exit(1)
}
// Close the app neatly
os.Exit(0)
}