sasl.go 2.6 KB

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