-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.go
175 lines (154 loc) · 3.96 KB
/
init.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
* Copyright 2022 Martin Proffitt <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package main
import (
"context"
"log"
"os"
"sync"
"time"
"github.com/notapipeline/bwv/pkg/bitw"
)
const DURATION = 15
var syncComplete chan bool = make(chan bool)
func getSecret(what string) string {
var (
value string
err error
)
if value, err = getSecretFromKWallet(what); err == nil {
return value
}
if value, err = getSecretFromSecretsService(what); err == nil {
return value
}
return ""
}
func getSecretsFromEnvOrStore() map[string]string {
secrets := map[string]string{
"BW_CLIENTID": "",
"BW_CLIENTSECRET": "",
"BW_PASSWORD": "",
"BW_EMAIL": "",
}
for k := range secrets {
var value string = os.Getenv(k)
if value == "" {
value = getSecret(k)
}
secrets[k] = value
}
return secrets
}
func getFromUser() map[string]string {
secrets := make(map[string]string)
secrets["BW_EMAIL"], _ = bitw.ReadLine("Email: ")
secrets["BW_PASSWORD"], _ = bitw.ReadPassword("Password: ")
return secrets
}
func contains(what string, where []string) bool {
for _, item := range where {
if what == item {
return true
}
}
return false
}
func init() {
initCommands := []string{"serve", "genkey", "revoke"}
if !contains(os.Args[1], initCommands) {
return
}
// Disable log datestamps if we're running as a systemd service
if os.Getenv("NO_DATELOG") == "true" {
log.SetFlags(log.Flags() &^ (log.Ldate | log.Ltime))
}
secrets := getSecretsFromEnvOrStore()
var (
loginResponse *bitw.LoginResponse
useApiKeys bool = secrets["BW_CLIENTID"] != "" && secrets["BW_CLIENTSECRET"] != ""
)
for {
if secrets["BW_PASSWORD"] == "" || secrets["BW_EMAIL"] == "" {
s := getFromUser()
for k, v := range s {
secrets[k] = v
}
}
var (
wg sync.WaitGroup
success bool = false
pwchan chan string = make(chan string, 1)
)
wg.Add(1)
go func() {
defer wg.Done()
p, err := bitw.Prelogin(secrets["BW_EMAIL"])
if err != nil {
log.Fatal(err)
}
hashed := bitw.SetMasterPassword(secrets["BW_PASSWORD"], secrets["BW_EMAIL"], p)
// for user logins we require the hashed password. For api logins, we don't.
// this means for user logins we're forced to wait for the pre-login phase
// to complete and the master password to be configured.
if !useApiKeys {
pwchan <- hashed
}
log.Println("Master password configured")
}()
wg.Add(1)
go func() {
var err error
defer wg.Done()
if useApiKeys {
if loginResponse, err = bitw.ApiLogin(secrets["BW_CLIENTID"], secrets["BW_CLIENTSECRET"]); err != nil {
log.Fatalf("Error : Failed to retrieve auth token : %s\n", err)
return
}
} else {
hashedpw := <-pwchan
if loginResponse, err = bitw.UserLogin(hashedpw, secrets["BW_EMAIL"]); err != nil {
log.Fatalf("Error : Failed to retrieve auth token : %s\n", err)
return
}
}
success = true
log.Println("Login complete")
}()
wg.Wait()
if success {
break
}
}
// force sync every DURATION minutes
go func() {
tick := time.Tick(DURATION * time.Second)
for {
log.Println("Syncing...")
syncStore(loginResponse)
<-tick
}
}()
}
func syncStore(loginResponse *bitw.LoginResponse) {
ctx := context.Background()
ctx = context.WithValue(ctx, bitw.AuthToken{}, loginResponse.AccessToken)
if err := bitw.Sync(ctx); err != nil {
log.Fatal(err)
}
syncComplete <- true
log.Println("Sync complete")
}