@@ -2,30 +2,43 @@ package sasl
2
2
3
3
import "context"
4
4
5
- // Mechanism implements the SASL state machine. It is initialized by calling
6
- // Start at which point the initial bytes should be sent to the server. The
7
- // caller then loops by passing the server's response into Next and then sending
8
- // Next's returned bytes to the server. Eventually either Next will indicate
9
- // that the authentication has been successfully completed or an error will
10
- // cause the state machine to exit prematurely.
5
+ // Mechanism implements the SASL state machine for a particular mode of
6
+ // authentication. It is used by the kafka.Dialer to perform the SASL
7
+ // handshake.
11
8
//
12
- // A Mechanism must be re-usable, but it does not need to be safe for concurrent
13
- // access by multiple go routines .
9
+ // A Mechanism must be re-usable and safe for concurrent access by multiple
10
+ // goroutines .
14
11
type Mechanism interface {
15
- // Start begins SASL authentication. It returns the authentication mechanism
16
- // name and "initial response" data (if required by the selected mechanism).
17
- // A non-nil error causes the client to abort the authentication attempt.
12
+ // Name returns the identifier for this SASL mechanism. This string will be
13
+ // passed to the SASL handshake request and much match one of the mechanisms
14
+ // supported by Kafka.
15
+ Name () string
16
+
17
+ // Start begins SASL authentication. It returns an authentication state
18
+ // machine and "initial response" data (if required by the selected
19
+ // mechanism). A non-nil error causes the client to abort the authentication
20
+ // attempt.
18
21
//
19
22
// A nil ir value is different from a zero-length value. The nil value
20
23
// indicates that the selected mechanism does not use an initial response,
21
24
// while a zero-length value indicates an empty initial response, which must
22
25
// be sent to the server.
23
- //
24
- // In order to ensure that the Mechanism is reusable, calling Start must
25
- // reset any internal state.
26
- Start (ctx context.Context ) (mech string , ir []byte , err error )
26
+ Start (ctx context.Context ) (sess StateMachine , ir []byte , err error )
27
+ }
27
28
28
- // Next continues challenge-response authentication. A non-nil error causes
29
- // the client to abort the authentication attempt.
29
+ // StateMachine implements the SASL challenge/response flow for a single SASL
30
+ // handshake. A StateMachine will be created by the Mechanism per connection,
31
+ // so it does not need to be safe for concurrent access by multiple goroutines.
32
+ //
33
+ // Once the StateMachine is created by the Mechanism, the caller loops by
34
+ // passing the server's response into Next and then sending Next's returned
35
+ // bytes to the server. Eventually either Next will indicate that the
36
+ // authentication has been successfully completed via the done return value, or
37
+ // it will indicate that the authentication failed by returning a non-nil error.
38
+ type StateMachine interface {
39
+ // Next continues challenge-response authentication. A non-nil error
40
+ // indicates that the client should abort the authentication attempt. If
41
+ // the client has been successfully authenticated, then the done return
42
+ // value will be true.
30
43
Next (ctx context.Context , challenge []byte ) (done bool , response []byte , err error )
31
44
}
0 commit comments