This repository has been archived by the owner on Dec 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configuration.go
202 lines (157 loc) · 4.04 KB
/
configuration.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/user"
"github.com/davecgh/go-spew/spew"
"github.com/fatih/color"
)
type tConfig struct {
Debug bool `json:"-"`
NewSetup bool `json:"-"`
HomeDir string `json:"-"`
PGPKeyID string `json:"key"`
Tokens []*tOTP `json:"tokens,omitempty"`
}
func checkConfiguration(debug bool) {
var (
TmpConfig tConfig
)
usr, err := user.Current()
if err != nil {
fmt.Printf("Failed to read user dir. Error: %s", err.Error())
}
//setting HomeDir to pass along to ReadGlobalConfiguration.
Config = &TmpConfig
Config.Debug = debug
Config.HomeDir = usr.HomeDir
err = Config.ReadGlobalConfiguration()
if err != nil && !Config.NewSetup {
os.Exit(1)
}
if Config.NewSetup {
err = Config.CreateNewSetup()
if err != nil {
os.Exit(2)
}
}
if Config.Debug {
fmt.Printf("Using configuration: %s", spew.Sdump(Config))
}
}
// ReadGlobalConfiguration reads the general config file into a tConfig struct.
func (c *tConfig) ReadGlobalConfiguration() error {
var (
err error
tmpByte []byte
TmpConfig tConfig
)
_, err = ioutil.ReadFile(c.HomeDir + "/.gotp/config")
if err != nil {
// Check if file is missing or permissions don't allow read
if err.Error() == "open "+c.HomeDir+"/.gotp/config: no such file or directory" {
c.NewSetup = true
} else {
color.Red("::Error opening config file: %s\n", err.Error())
return err
}
} else {
// Reading config file into tConfig.
tmpByte, err = ioutil.ReadFile(c.HomeDir + "/.gotp/config")
if err != nil {
color.Red("::Failed to open config file: %s\n", err.Error())
return err
}
err = json.Unmarshal(tmpByte, &TmpConfig)
if err != nil {
color.Red("::Failed to read config file: %s\n", err.Error())
return err
}
// Setting HomeDir and re-assigning the global tConfig to TmpConfig
TmpConfig.HomeDir = c.HomeDir
TmpConfig.Debug = c.Debug
Config = &TmpConfig
// Load keys from seperate file
err = Config.ReadTokens()
if err != nil {
color.Red("::Failed to read OTP config file: %s\n", err.Error())
return err
}
}
return nil
}
// CreateNewSetup leads the user through the process of creating
// a new general configuration file and setting up gOTP for
// proper usage.
func (c *tConfig) CreateNewSetup() error {
var (
err error
tmpByte []byte
)
fmt.Printf("::Starting Initial Configuration\n")
// Creating config dir if possible.
err = os.Mkdir(c.HomeDir+"/.gotp/", 0700)
if err != nil {
// We only want to fail when the dir couldn't be created.
if err.Error() != "mkdir "+c.HomeDir+"/.gotp/: file exists" {
color.Red("::Failed to create config dir: %s\n", err.Error())
return err
}
} else {
fmt.Printf("::Created config dir\n")
}
tmpByte, err = json.Marshal(c)
if err != nil {
color.Red("::Failed to encode config struct: %s\n", err.Error())
return err
}
// Now we can write the config file to disk.
err = ioutil.WriteFile(c.HomeDir+"/.gotp/config", tmpByte, 0600)
return nil
}
func (c *tConfig) ReadTokens() error {
var (
err error
data []byte
)
data, err = ioutil.ReadFile(c.HomeDir + "/.gotp/token")
if err != nil {
// It's okay to have no token config (yet)
if err.Error() == "open "+c.HomeDir+"/.gotp/token: no such file or directory" {
return nil
}
color.Red("::Failed to open OTP config file: %s\n", err.Error())
return err
}
err = json.Unmarshal(data, &c.Tokens)
if err != nil {
if err.Error() == "open "+c.HomeDir+"/.gotp/token: no such file or directory" {
return nil
}
color.Red("::Failed to read OTP config file: %s\n", err.Error())
return err
}
if Config.Debug {
spew.Dump(c.Tokens)
}
return nil
}
func (c *tConfig) SaveTokens() error {
var (
err error
data []byte
)
data, err = json.Marshal(c.Tokens)
if err != nil {
color.Red("::Failed to encode OTP config struct: %s\n", err.Error())
return err
}
err = ioutil.WriteFile(c.HomeDir+"/.gotp/token", data, 0600)
if err != nil {
color.Red("::Failed to write OTP config file: %s\n", err.Error())
return err
}
return nil
}