-
Notifications
You must be signed in to change notification settings - Fork 59
/
config.go
79 lines (72 loc) · 1.65 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
67
68
69
70
71
72
73
74
75
76
77
78
79
package config
import (
"github.com/LilyPad/GoLilyPad/server/connect"
yaml "gopkg.in/yaml.v2"
"io/ioutil"
"regexp"
)
type Config struct {
Bind string `yaml:"bind"`
Logins []ConfigLogin `yaml:"logins"`
}
func (this *Config) Authenticate(username string, password string, passwordSalt string) (ok bool, err error) {
for _, login := range this.Logins {
var realPassword string
if len(login.Username) != 0 && login.Username == username {
realPassword = login.Password
} else if len(login.Regexp) != 0 {
if login.regexp == nil {
login.regexp, err = regexp.Compile(login.Regexp)
if err != nil {
return
}
}
if login.regexp.MatchString(username) {
realPassword = login.Password
} else {
continue
}
} else {
continue
}
realPassword = connect.PasswordAndSaltHash(realPassword, passwordSalt)
if realPassword == password {
ok = true
return
}
}
ok = false
return
}
type ConfigLogin struct {
Username string `yaml:"username,omitempty"`
Regexp string `yaml:"regexp,omitempty"`
regexp *regexp.Regexp
Password string `yaml:"password"`
}
func DefaultConfig() (config *Config) {
config = new(Config)
config.Bind = ":5091"
config.Logins = []ConfigLogin{
{"example", "", nil, "example"},
{"", "^example-.*$", nil, "example"},
}
return
}
func LoadConfig(file string) (config *Config, err error) {
data, err := ioutil.ReadFile(file)
if err != nil {
return
}
config = new(Config)
err = yaml.Unmarshal(data, config)
return
}
func SaveConfig(file string, config *Config) (err error) {
data, err := yaml.Marshal(config)
if err != nil {
return
}
err = ioutil.WriteFile(file, data, 0644)
return
}