...
1 package device
2
3 import (
4 "encoding/gob"
5 "encoding/json"
6 "os"
7 )
8
9 func init() {
10 gob.Register(LoginPolicy{})
11 gob.Register(FieldPolicy{})
12 }
13
14 type LoginPolicy struct {
15 Username FieldPolicy `json:"username"`
16 Password FieldPolicy `json:"password"`
17 }
18
19 type FieldPolicy struct {
20 MinLength int `json:"min_len"`
21 MaxLength int `json:"max_len"`
22 NumericOnly bool `json:"num_only"`
23 }
24
25 func GetLoginPolicy() (LoginPolicy, error) {
26 policyJSON := getLoginPolicyJSON()
27
28 var devicePolicy LoginPolicy
29 err := json.Unmarshal([]byte(policyJSON), &devicePolicy)
30 if err != nil {
31 return LoginPolicy{}, err
32 }
33
34 return devicePolicy, nil
35 }
36
37 func getLoginPolicyJSON() string {
38 defaultPolicy := `{"username":{"min_len":1,"max_len":12,"num_only":false}, "password":{"min_len":1,"max_len":12,"num_only":false}}`
39
40 fromEnv, exist := os.LookupEnv("IAM_DEVICE_POLICY")
41 if !exist {
42 return defaultPolicy
43 }
44
45 return fromEnv
46 }
47
View as plain text