...

Source file src/github.com/jackc/pgx/v5/pgproto3/authentication_sasl_continue.go

Documentation: github.com/jackc/pgx/v5/pgproto3

     1  package pgproto3
     2  
     3  import (
     4  	"encoding/binary"
     5  	"encoding/json"
     6  	"errors"
     7  
     8  	"github.com/jackc/pgx/v5/internal/pgio"
     9  )
    10  
    11  // AuthenticationSASLContinue is a message sent from the backend containing a SASL challenge.
    12  type AuthenticationSASLContinue struct {
    13  	Data []byte
    14  }
    15  
    16  // Backend identifies this message as sendable by the PostgreSQL backend.
    17  func (*AuthenticationSASLContinue) Backend() {}
    18  
    19  // Backend identifies this message as an authentication response.
    20  func (*AuthenticationSASLContinue) AuthenticationResponse() {}
    21  
    22  // Decode decodes src into dst. src must contain the complete message with the exception of the initial 1 byte message
    23  // type identifier and 4 byte message length.
    24  func (dst *AuthenticationSASLContinue) Decode(src []byte) error {
    25  	if len(src) < 4 {
    26  		return errors.New("authentication message too short")
    27  	}
    28  
    29  	authType := binary.BigEndian.Uint32(src)
    30  
    31  	if authType != AuthTypeSASLContinue {
    32  		return errors.New("bad auth type")
    33  	}
    34  
    35  	dst.Data = src[4:]
    36  
    37  	return nil
    38  }
    39  
    40  // Encode encodes src into dst. dst will include the 1 byte message type identifier and the 4 byte message length.
    41  func (src *AuthenticationSASLContinue) Encode(dst []byte) ([]byte, error) {
    42  	dst, sp := beginMessage(dst, 'R')
    43  	dst = pgio.AppendUint32(dst, AuthTypeSASLContinue)
    44  	dst = append(dst, src.Data...)
    45  	return finishMessage(dst, sp)
    46  }
    47  
    48  // MarshalJSON implements encoding/json.Marshaler.
    49  func (src AuthenticationSASLContinue) MarshalJSON() ([]byte, error) {
    50  	return json.Marshal(struct {
    51  		Type string
    52  		Data string
    53  	}{
    54  		Type: "AuthenticationSASLContinue",
    55  		Data: string(src.Data),
    56  	})
    57  }
    58  
    59  // UnmarshalJSON implements encoding/json.Unmarshaler.
    60  func (dst *AuthenticationSASLContinue) UnmarshalJSON(data []byte) error {
    61  	// Ignore null, like in the main JSON package.
    62  	if string(data) == "null" {
    63  		return nil
    64  	}
    65  
    66  	var msg struct {
    67  		Data string
    68  	}
    69  	if err := json.Unmarshal(data, &msg); err != nil {
    70  		return err
    71  	}
    72  
    73  	dst.Data = []byte(msg.Data)
    74  	return nil
    75  }
    76  

View as plain text