gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

authentication_sasl_continue.go (2003B)


      1 package pgproto3
      2 
      3 import (
      4 	"encoding/binary"
      5 	"encoding/json"
      6 	"errors"
      7 
      8 	"github.com/jackc/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 {
     42 	dst = append(dst, 'R')
     43 	sp := len(dst)
     44 	dst = pgio.AppendInt32(dst, -1)
     45 	dst = pgio.AppendUint32(dst, AuthTypeSASLContinue)
     46 
     47 	dst = append(dst, src.Data...)
     48 
     49 	pgio.SetInt32(dst[sp:], int32(len(dst[sp:])))
     50 
     51 	return dst
     52 }
     53 
     54 // MarshalJSON implements encoding/json.Marshaler.
     55 func (src AuthenticationSASLContinue) MarshalJSON() ([]byte, error) {
     56 	return json.Marshal(struct {
     57 		Type string
     58 		Data string
     59 	}{
     60 		Type: "AuthenticationSASLContinue",
     61 		Data: string(src.Data),
     62 	})
     63 }
     64 
     65 // UnmarshalJSON implements encoding/json.Unmarshaler.
     66 func (dst *AuthenticationSASLContinue) UnmarshalJSON(data []byte) error {
     67 	// Ignore null, like in the main JSON package.
     68 	if string(data) == "null" {
     69 		return nil
     70 	}
     71 
     72 	var msg struct {
     73 		Data string
     74 	}
     75 	if err := json.Unmarshal(data, &msg); err != nil {
     76 		return err
     77 	}
     78 
     79 	dst.Data = []byte(msg.Data)
     80 	return nil
     81 }