github.com/datastax/go-cassandra-native-protocol@v0.0.0-20220706104457-5e8aad05cf90/message/authenticate.go (about)

     1  // Copyright 2020 DataStax
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package message
    16  
    17  import (
    18  	"errors"
    19  	"fmt"
    20  	"io"
    21  
    22  	"github.com/datastax/go-cassandra-native-protocol/primitive"
    23  )
    24  
    25  // Authenticate is a response sent in reply to a Startup request when the server requires authentication. It must be
    26  // followed by an AuthResponse request message.
    27  // +k8s:deepcopy-gen=true
    28  // +k8s:deepcopy-gen:interfaces=github.com/datastax/go-cassandra-native-protocol/message.Message
    29  type Authenticate struct {
    30  	Authenticator string
    31  }
    32  
    33  func (m *Authenticate) IsResponse() bool {
    34  	return true
    35  }
    36  
    37  func (m *Authenticate) GetOpCode() primitive.OpCode {
    38  	return primitive.OpCodeAuthenticate
    39  }
    40  
    41  func (m *Authenticate) String() string {
    42  	return "AUTHENTICATE " + m.Authenticator
    43  }
    44  
    45  type authenticateCodec struct{}
    46  
    47  func (c *authenticateCodec) Encode(msg Message, dest io.Writer, _ primitive.ProtocolVersion) error {
    48  	authenticate, ok := msg.(*Authenticate)
    49  	if !ok {
    50  		return errors.New(fmt.Sprintf("expected *message.Authenticate, got %T", msg))
    51  	}
    52  	if authenticate.Authenticator == "" {
    53  		return errors.New("AUTHENTICATE authenticator cannot be empty")
    54  	}
    55  	return primitive.WriteString(authenticate.Authenticator, dest)
    56  }
    57  
    58  func (c *authenticateCodec) EncodedLength(msg Message, _ primitive.ProtocolVersion) (int, error) {
    59  	authenticate, ok := msg.(*Authenticate)
    60  	if !ok {
    61  		return -1, errors.New(fmt.Sprintf("expected *message.Authenticate, got %T", msg))
    62  	}
    63  	return primitive.LengthOfString(authenticate.Authenticator), nil
    64  }
    65  
    66  func (c *authenticateCodec) Decode(source io.Reader, _ primitive.ProtocolVersion) (Message, error) {
    67  	if authenticator, err := primitive.ReadString(source); err != nil {
    68  		return nil, err
    69  	} else {
    70  		return &Authenticate{Authenticator: authenticator}, nil
    71  	}
    72  }
    73  
    74  func (c *authenticateCodec) GetOpCode() primitive.OpCode {
    75  	return primitive.OpCodeAuthenticate
    76  }