authentication_sasl_final.go (1991B)
1 package pgproto3 2 3 import ( 4 "encoding/binary" 5 "encoding/json" 6 "errors" 7 8 "github.com/jackc/pgio" 9 ) 10 11 // AuthenticationSASLFinal is a message sent from the backend indicating a SASL authentication has completed. 12 type AuthenticationSASLFinal struct { 13 Data []byte 14 } 15 16 // Backend identifies this message as sendable by the PostgreSQL backend. 17 func (*AuthenticationSASLFinal) Backend() {} 18 19 // Backend identifies this message as an authentication response. 20 func (*AuthenticationSASLFinal) 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 *AuthenticationSASLFinal) 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 != AuthTypeSASLFinal { 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 *AuthenticationSASLFinal) Encode(dst []byte) []byte { 42 dst = append(dst, 'R') 43 sp := len(dst) 44 dst = pgio.AppendInt32(dst, -1) 45 dst = pgio.AppendUint32(dst, AuthTypeSASLFinal) 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.Unmarshaler. 55 func (src AuthenticationSASLFinal) MarshalJSON() ([]byte, error) { 56 return json.Marshal(struct { 57 Type string 58 Data string 59 }{ 60 Type: "AuthenticationSASLFinal", 61 Data: string(src.Data), 62 }) 63 } 64 65 // UnmarshalJSON implements encoding/json.Unmarshaler. 66 func (dst *AuthenticationSASLFinal) 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 }