sasl.go 2.0 KB

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