github.com/datastax/go-cassandra-native-protocol@v0.0.0-20220706104457-5e8aad05cf90/message/auth_success.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  // AuthSuccess is a response message sent in reply to an AuthResponse request, to indicate that the authentication was
    26  // successful.
    27  // +k8s:deepcopy-gen=true
    28  // +k8s:deepcopy-gen:interfaces=github.com/datastax/go-cassandra-native-protocol/message.Message
    29  type AuthSuccess struct {
    30  	Token []byte
    31  }
    32  
    33  func (m *AuthSuccess) IsResponse() bool {
    34  	return true
    35  }
    36  
    37  func (m *AuthSuccess) GetOpCode() primitive.OpCode {
    38  	return primitive.OpCodeAuthSuccess
    39  }
    40  
    41  func (m *AuthSuccess) String() string {
    42  	return "AUTH_SUCCESS"
    43  }
    44  
    45  type authSuccessCodec struct{}
    46  
    47  func (c *authSuccessCodec) Encode(msg Message, dest io.Writer, _ primitive.ProtocolVersion) error {
    48  	authSuccess, ok := msg.(*AuthSuccess)
    49  	if !ok {
    50  		return errors.New(fmt.Sprintf("expected *message.AuthSuccess, got %T", msg))
    51  	}
    52  	// protocol specs allow the token to be null on AUTH SUCCESS
    53  	return primitive.WriteBytes(authSuccess.Token, dest)
    54  }
    55  
    56  func (c *authSuccessCodec) EncodedLength(msg Message, _ primitive.ProtocolVersion) (int, error) {
    57  	authSuccess, ok := msg.(*AuthSuccess)
    58  	if !ok {
    59  		return -1, errors.New(fmt.Sprintf("expected *message.AuthSuccess, got %T", msg))
    60  	}
    61  	return primitive.LengthOfBytes(authSuccess.Token), nil
    62  }
    63  
    64  func (c *authSuccessCodec) Decode(source io.Reader, _ primitive.ProtocolVersion) (Message, error) {
    65  	if token, err := primitive.ReadBytes(source); err != nil {
    66  		return nil, err
    67  	} else {
    68  		return &AuthSuccess{Token: token}, nil
    69  	}
    70  }
    71  
    72  func (c *authSuccessCodec) GetOpCode() primitive.OpCode {
    73  	return primitive.OpCodeAuthSuccess
    74  }