github.com/segmentio/kafka-go@v0.4.48-0.20240318174348-3f6244eb34fd/protocol/saslauthenticate/saslauthenticate.go (about) 1 package saslauthenticate 2 3 import ( 4 "encoding/binary" 5 "io" 6 7 "github.com/segmentio/kafka-go/protocol" 8 ) 9 10 func init() { 11 protocol.Register(&Request{}, &Response{}) 12 } 13 14 type Request struct { 15 AuthBytes []byte `kafka:"min=v0,max=v1"` 16 } 17 18 func (r *Request) RawExchange(rw io.ReadWriter) (protocol.Message, error) { 19 if err := r.writeTo(rw); err != nil { 20 return nil, err 21 } 22 return r.readResp(rw) 23 } 24 25 func (*Request) Required(versions map[protocol.ApiKey]int16) bool { 26 const v0 = 0 27 return versions[protocol.SaslHandshake] == v0 28 } 29 30 func (r *Request) writeTo(w io.Writer) error { 31 size := len(r.AuthBytes) + 4 32 buf := make([]byte, size) 33 binary.BigEndian.PutUint32(buf[:4], uint32(len(r.AuthBytes))) 34 copy(buf[4:], r.AuthBytes) 35 _, err := w.Write(buf) 36 return err 37 } 38 39 func (r *Request) readResp(read io.Reader) (protocol.Message, error) { 40 var lenBuf [4]byte 41 if _, err := io.ReadFull(read, lenBuf[:]); err != nil { 42 return nil, err 43 } 44 respLen := int32(binary.BigEndian.Uint32(lenBuf[:])) 45 data := make([]byte, respLen) 46 47 if _, err := io.ReadFull(read, data[:]); err != nil { 48 return nil, err 49 } 50 return &Response{ 51 AuthBytes: data, 52 }, nil 53 } 54 55 func (r *Request) ApiKey() protocol.ApiKey { return protocol.SaslAuthenticate } 56 57 type Response struct { 58 ErrorCode int16 `kafka:"min=v0,max=v1"` 59 ErrorMessage string `kafka:"min=v0,max=v1,nullable"` 60 AuthBytes []byte `kafka:"min=v0,max=v1"` 61 SessionLifetimeMs int64 `kafka:"min=v1,max=v1"` 62 } 63 64 func (r *Response) ApiKey() protocol.ApiKey { return protocol.SaslAuthenticate } 65 66 var _ protocol.RawExchanger = (*Request)(nil)