github.com/hoveychen/kafka-go@v0.4.42/sasl/plain/plain.go (about)

     1  package plain
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/hoveychen/kafka-go/sasl"
     8  )
     9  
    10  // Mechanism implements the PLAIN mechanism and passes the credentials in clear
    11  // text.
    12  type Mechanism struct {
    13  	Username string
    14  	Password string
    15  }
    16  
    17  func (Mechanism) Name() string {
    18  	return "PLAIN"
    19  }
    20  
    21  func (m Mechanism) Start(ctx context.Context) (sasl.StateMachine, []byte, error) {
    22  	// Mechanism is stateless, so it can also implement sasl.Session
    23  	return m, []byte(fmt.Sprintf("\x00%s\x00%s", m.Username, m.Password)), nil
    24  }
    25  
    26  func (m Mechanism) Next(ctx context.Context, challenge []byte) (bool, []byte, error) {
    27  	// kafka will return error if it rejected the credentials, so we'd only
    28  	// arrive here on success.
    29  	return true, nil, nil
    30  }