-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.go
66 lines (55 loc) · 1.39 KB
/
config.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
package main
import (
"encoding/json"
"errors"
"io"
)
var InvalidCredentials = errors.New("Invalid credentials provided. Must have a username/ password or none at all.")
type Credential struct {
Username string
Password string
}
type Config struct {
Credentials []Credential
StripProxyHeaders bool `json:"strip_proxy_headers"`
Port int
UseIncomingLocalAddr bool `json:"use_incoming_local_addr"`
DialTimeout int `json:"dial_timeout"`
}
func (config *Config) AuthenticationRequired() bool {
return len(config.Credentials) > 0
}
func validCredentials(username, password string) bool {
if username == "" && password == "" {
return true
}
if username != "" && password != "" {
return true
}
return false
}
func (config *Config) Validate() error {
for _, credential := range config.Credentials {
if !validCredentials(credential.Username, credential.Password) {
return InvalidCredentials
}
}
return nil
}
func (config *Config) IsAuthenticated(username, password string) bool {
for _, credential := range config.Credentials {
if credential.Username == username && credential.Password == password {
return true
}
}
return false
}
func NewConfigFromReader(reader io.Reader) (*Config, error) {
config := new(Config)
decoder := json.NewDecoder(reader)
err := decoder.Decode(&config)
if err != nil {
return nil, err
}
return config, nil
}