-
Notifications
You must be signed in to change notification settings - Fork 14
/
mechanism.go
48 lines (38 loc) · 872 Bytes
/
mechanism.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
package kafka
import (
"fmt"
"github.com/segmentio/kafka-go/sasl"
"github.com/segmentio/kafka-go/sasl/plain"
"github.com/segmentio/kafka-go/sasl/scram"
)
type Mechanism string
const (
MechanismScram = "scram"
MechanismPlain = "plain"
)
type SASLConfig struct {
Type Mechanism
Username string
Password string
}
func (s *SASLConfig) Mechanism() (sasl.Mechanism, error) {
if s.Type == MechanismScram {
return scram.Mechanism(scram.SHA512, s.Username, s.Password)
}
return s.plain(), nil
}
func (s *SASLConfig) plain() sasl.Mechanism {
return &plain.Mechanism{
Username: s.Username,
Password: s.Password,
}
}
func (s *SASLConfig) IsEmpty() bool {
return s == nil
}
func (s *SASLConfig) JSON() string {
if s == nil {
return "{}"
}
return fmt.Sprintf(`{"Mechanism": %q, "Username": %q, "Password": %q}`, s.Type, s.Username, s.Password)
}