github.com/datastax/go-cassandra-native-protocol@v0.0.0-20220706104457-5e8aad05cf90/message/auth_response.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  // AuthResponse is a request message sent in reply to an Authenticate or AuthChallenge response, and contains the
    26  // authentication data requested by the server. The server will then reply with either an AuthSuccess response message,
    27  // or with an AuthChallenge response message, if it requires additional authentication data.
    28  // +k8s:deepcopy-gen=true
    29  // +k8s:deepcopy-gen:interfaces=github.com/datastax/go-cassandra-native-protocol/message.Message
    30  type AuthResponse struct {
    31  
    32  	// Token is a protocol [bytes]; the details of what this token contains (and when it can be null/empty, if ever)
    33  	// depends on the actual authenticator used.
    34  	Token []byte
    35  }
    36  
    37  func (m *AuthResponse) IsResponse() bool {
    38  	return false
    39  }
    40  
    41  func (m *AuthResponse) GetOpCode() primitive.OpCode {
    42  	return primitive.OpCodeAuthResponse
    43  }
    44  
    45  func (m *AuthResponse) String() string {
    46  	return "AUTH_RESPONSE"
    47  }
    48  
    49  type authResponseCodec struct{}
    50  
    51  func (c *authResponseCodec) Encode(msg Message, dest io.Writer, _ primitive.ProtocolVersion) error {
    52  	authResponse, ok := msg.(*AuthResponse)
    53  	if !ok {
    54  		return errors.New(fmt.Sprintf("expected *message.AuthResponse, got %T", msg))
    55  	}
    56  	return primitive.WriteBytes(authResponse.Token, dest)
    57  }
    58  
    59  func (c *authResponseCodec) EncodedLength(msg Message, _ primitive.ProtocolVersion) (int, error) {
    60  	authResponse, ok := msg.(*AuthResponse)
    61  	if !ok {
    62  		return -1, errors.New(fmt.Sprintf("expected *message.AuthResponse, got %T", msg))
    63  	}
    64  	return primitive.LengthOfBytes(authResponse.Token), nil
    65  }
    66  
    67  func (c *authResponseCodec) Decode(source io.Reader, _ primitive.ProtocolVersion) (Message, error) {
    68  	if token, err := primitive.ReadBytes(source); err != nil {
    69  		return nil, err
    70  	} else {
    71  		return &AuthResponse{Token: token}, nil
    72  	}
    73  }
    74  
    75  func (c *authResponseCodec) GetOpCode() primitive.OpCode {
    76  	return primitive.OpCodeAuthResponse
    77  }