This repository has been archived by the owner on Jun 6, 2022. It is now read-only.
forked from ravenac95/sudolikeaboss
-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
132 lines (109 loc) · 2.75 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
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
package main
import (
"fmt"
"os"
"os/user"
"path"
"time"
"github.com/brycekahle/sudolikeaboss/onepass"
"github.com/kelseyhightower/envconfig"
log "github.com/sirupsen/logrus"
)
type Configuration struct {
TimeoutSecs int `split_words:"true" default:"30"`
DefaultHost string `split_words:"true" default:"sudolikeaboss://local"`
StateDirectory string `split_words:"true"`
Websocket struct {
URI string `default:"ws://127.0.0.1:6263/4"`
Protocol string
Origin string `default:"resource://onepassword-at-agilebits-dot-com"`
}
}
func LoadConfiguration() *Configuration {
conf := Configuration{}
err := envconfig.Process("SUDOLIKEABOSS", &conf)
if err != nil {
log.Fatal(err)
}
if conf.StateDirectory == "" {
usr, err := user.Current()
if err != nil {
log.Fatal(err)
}
conf.StateDirectory = path.Join(usr.HomeDir, ".sudolikeaboss")
}
return &conf
}
func retrievePasswordFromOnepassword(configuration *onepass.Configuration, done chan bool) {
// Load configuration from a file
client, err := onepass.NewClientWithConfig(configuration)
if err != nil {
os.Exit(1)
}
_, err = client.Authenticate(false)
if err != nil {
os.Exit(1)
}
response, err := client.SendShowPopupCommand()
if err != nil {
os.Exit(1)
}
password, err := response.GetPassword()
if err != nil {
os.Exit(1)
}
fmt.Println(password)
done <- true
}
func registerWithOnepassword(configuration *onepass.Configuration, done chan bool) {
// Load configuration from a file
client, err := onepass.NewClientWithConfig(configuration)
if err != nil {
os.Exit(1)
}
_, err = client.Authenticate(true)
if err != nil {
os.Exit(1)
}
fmt.Println("")
fmt.Println("Congrats sudolikeaboss is registered!")
done <- true
}
// Run the main sudolikeaboss entry point
func runSudolikeaboss() {
done := make(chan bool)
conf := LoadConfiguration()
oc := onepass.Configuration{
WebsocketURI: conf.Websocket.URI,
WebsocketOrigin: conf.Websocket.Origin,
WebsocketProtocol: conf.Websocket.Protocol,
StateDirectory: conf.StateDirectory,
DefaultHost: conf.DefaultHost,
}
go retrievePasswordFromOnepassword(&oc, done)
// Timeout if necessary
select {
case <-done:
// Do nothing no need
case <-time.After(time.Duration(conf.TimeoutSecs) * time.Second):
close(done)
os.Exit(1)
}
// Close the app neatly
os.Exit(0)
}
func runSudolikeabossRegistration() {
done := make(chan bool)
conf := LoadConfiguration()
oc := onepass.Configuration{
WebsocketURI: conf.Websocket.URI,
WebsocketOrigin: conf.Websocket.Origin,
WebsocketProtocol: conf.Websocket.Protocol,
StateDirectory: conf.StateDirectory,
DefaultHost: conf.DefaultHost,
}
go registerWithOnepassword(&oc, done)
// Close the app neatly
<-done
os.Exit(0)
}