/
authentication.go
79 lines (70 loc) · 1.94 KB
/
authentication.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 "strings"
var validAuthTypes = map[string]string{
"local": "local",
}
type JWTAuthenticationConfig struct {
Key string `json:"key"`
FilePath string `json:"filePath"`
SignatureType string `json:"signatureType"`
}
func (jc *JWTAuthenticationConfig) Validate() error {
if validateParameters(jc.Key, jc.FilePath, jc.SignatureType) {
return NewConfigurationError("missing jwt configuration parameters")
}
if jc.SignatureType == "" {
return NewConfigurationError("bad jwt configuration: missing jwt signature type")
}
if jc.Key == "" && jc.FilePath == "" {
return NewConfigurationError("bad jwt configuration: no verification key provided")
}
if jc.Key == "" && jc.FilePath != "" {
if err := validateFileName(jc.FilePath); err != nil {
return NewConfigurationErrorf("bad jwt configuration: %s", err.Error())
}
}
return nil
}
type AuthenticationConfig struct {
Enable bool `json:"enable"`
JwtConfig *JWTAuthenticationConfig `json:"jwtConfig"`
Type string `json:"type"`
Config string `json:"config"`
}
func defaultAuthenticationConfig() *AuthenticationConfig {
bindViperEnv(
"Authentication.Enable",
"Authentication.JwtConfig.Key",
"Authentication.JwtConfig.FilePath",
"Authentication.JwtConfig.SignatureType",
"Authentication.Type",
"Authentication.Config",
)
return &AuthenticationConfig{
Enable: false,
JwtConfig: &JWTAuthenticationConfig{
Key: "",
FilePath: "",
SignatureType: "",
},
Type: "",
Config: "",
}
}
func (c *AuthenticationConfig) Validate() error {
if !c.Enable {
return nil
}
if c.JwtConfig != nil {
return c.JwtConfig.Validate()
} else {
_, ok := validAuthTypes[strings.ToLower(c.Type)]
if !ok {
return NewConfigurationError("invalid authentication type")
}
if c.Config == "" {
return NewConfigurationError("invalid authentication config")
}
return nil
}
}