-
Notifications
You must be signed in to change notification settings - Fork 0
/
loadSecurity.go
42 lines (36 loc) · 961 Bytes
/
loadSecurity.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
package security
import (
"fmt"
"github.com/dkzhang/RmsGo/myUtils/genPasswd"
"gopkg.in/yaml.v2"
"io/ioutil"
"sync"
)
type LoginSecurity struct {
TheTokenSecurity TokenSecurity `yaml:"TokenSecurity"`
}
type TokenSecurity struct {
Key string `yaml:"Key"`
}
func LoadLoginSecurity(filepath string) (sec LoginSecurity, err error) {
loginSecFile, err := ioutil.ReadFile(filepath)
if err != nil {
return LoginSecurity{}, fmt.Errorf("LoadLoginSecurity ioutil.ReadFile error: %v", err)
}
err = yaml.Unmarshal(loginSecFile, &sec)
if err != nil {
return LoginSecurity{}, fmt.Errorf("LoadLoginSecurity yaml.Unmarshal error: %v", err)
}
return sec, nil
}
var seedOnce sync.Once
func GenLoginSecurity() (sec LoginSecurity, err error) {
pwLen := 12
pwType := genPasswd.FlagLowerChar
seedOnce.Do(func() {
genPasswd.RandomSeed()
})
return LoginSecurity{
TheTokenSecurity: TokenSecurity{
Key: genPasswd.GeneratePasswd(pwLen, pwType)}}, nil
}