-
Notifications
You must be signed in to change notification settings - Fork 0
/
authenticationGroup.go
73 lines (62 loc) · 1.68 KB
/
authenticationGroup.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
package https
import (
"fmt"
)
const SINGLE_AUTH = "single"
const AND_AUTH = "and"
const OR_AUTH = "or"
type AuthGroup struct {
validAuthInterfaces []AuthInterface
innerGroups []AuthGroup
authType string
singleAuthKey string
errMessage string
}
func NewAuth(key string) AuthGroup {
return AuthGroup{
singleAuthKey: key,
authType: SINGLE_AUTH,
}
}
func NewOrAuth(authGroup1, authGroup2 AuthGroup, moreAuthGroups ...AuthGroup) AuthGroup {
return AuthGroup{
innerGroups: append([]AuthGroup{authGroup1, authGroup2}, moreAuthGroups...),
authType: OR_AUTH,
}
}
func NewAndAuth(authGroup1, authGroup2 AuthGroup, moreAuthGroups ...AuthGroup) AuthGroup {
return AuthGroup{
innerGroups: append([]AuthGroup{authGroup1, authGroup2}, moreAuthGroups...),
authType: AND_AUTH,
}
}
func (ag *AuthGroup) appendIndentedError(errMsg string) {
if errMsg != "" {
ag.errMessage += "\n-> " + errMsg
}
}
func (ag *AuthGroup) validate(authInterfaces map[string]AuthInterface) {
switch ag.authType {
case SINGLE_AUTH:
val, ok := authInterfaces[ag.singleAuthKey]
if !ok {
ag.appendIndentedError(fmt.Sprintf("%s is undefined!", ag.singleAuthKey))
return
}
if err := val.Validate(); err != nil {
ag.appendIndentedError(err.Error())
return
}
ag.validAuthInterfaces = append(ag.validAuthInterfaces, val)
case AND_AUTH, OR_AUTH:
for _, innerAG := range ag.innerGroups {
innerAG.validate(authInterfaces)
ag.validAuthInterfaces = append(ag.validAuthInterfaces, innerAG.validAuthInterfaces...)
if ag.authType == OR_AUTH && innerAG.errMessage == "" {
ag.errMessage = ""
return
}
ag.errMessage += innerAG.errMessage
}
}
}