-
Notifications
You must be signed in to change notification settings - Fork 149
/
auth_method_type.go
85 lines (69 loc) · 1.59 KB
/
auth_method_type.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
80
81
82
83
84
85
// This is mostly cribbed from https://github.com/uber-go/zap/blob/master/zapcore/level.go
// as that works nicely with cobra
package auth
import (
"bytes"
"fmt"
)
type AuthMethod uint8
const (
// User & password read from a secret
UserAccount AuthMethod = iota
// OIDC authentication (recommended)
OIDC
// EE CLI tokens
TokenPassthrough
)
// This is a function to mimic a const slice
func DefaultAuthMethods() []AuthMethod {
return []AuthMethod{UserAccount, OIDC}
}
func DefaultAuthMethodStrings() []string {
res := []string{}
for _, method := range DefaultAuthMethods() {
res = append(res, method.String())
}
return res
}
func ParseAuthMethodArray(authStrings []string) (map[AuthMethod]bool, error) {
res := map[AuthMethod]bool{}
for _, methodString := range authStrings {
method, err := ParseAuthMethod(methodString)
if err != nil {
return nil, err
}
res[method] = true
}
return res, nil
}
func (am *AuthMethod) String() string {
switch *am {
case UserAccount:
return "user-account"
case OIDC:
return "oidc"
case TokenPassthrough:
return "token-passthrough"
default:
return fmt.Sprintf("AuthMethod(%d)", am)
}
}
func (am *AuthMethod) UnmarshalText(text []byte) error {
text = bytes.ToLower(text)
switch string(text) {
case "user-account":
*am = UserAccount
case "oidc":
*am = OIDC
case "token-passthrough":
*am = TokenPassthrough
default:
return fmt.Errorf("unknown auth method '%q'", text)
}
return nil
}
func ParseAuthMethod(text string) (AuthMethod, error) {
var method AuthMethod
err := method.UnmarshalText([]byte(text))
return method, err
}