...
1
2 package plain
3
4 import (
5 "context"
6 "errors"
7
8 "github.com/twmb/franz-go/pkg/sasl"
9 )
10
11
12 type Auth struct {
13
14 Zid string
15
16
17 User string
18
19
20 Pass string
21
22 _ struct{}
23 }
24
25
26
27
28
29
30 func (a Auth) AsMechanism() sasl.Mechanism {
31 return Plain(func(context.Context) (Auth, error) {
32 return a, nil
33 })
34 }
35
36
37
38 func Plain(authFn func(context.Context) (Auth, error)) sasl.Mechanism {
39 return plain(authFn)
40 }
41
42 type plain func(context.Context) (Auth, error)
43
44 func (plain) Name() string { return "PLAIN" }
45 func (fn plain) Authenticate(ctx context.Context, _ string) (sasl.Session, []byte, error) {
46 auth, err := fn(ctx)
47 if err != nil {
48 return nil, nil, err
49 }
50 if auth.User == "" || auth.Pass == "" {
51 return nil, nil, errors.New("PLAIN user and pass must be non-empty")
52 }
53 return session{}, []byte(auth.Zid + "\x00" + auth.User + "\x00" + auth.Pass), nil
54 }
55
56 type session struct{}
57
58 func (session) Challenge([]byte) (bool, []byte, error) {
59 return true, nil, nil
60 }
61
View as plain text