password_message.go (1294B)
1 package pgproto3 2 3 import ( 4 "bytes" 5 "encoding/json" 6 7 "github.com/jackc/pgio" 8 ) 9 10 type PasswordMessage struct { 11 Password string 12 } 13 14 // Frontend identifies this message as sendable by a PostgreSQL frontend. 15 func (*PasswordMessage) Frontend() {} 16 17 // Frontend identifies this message as an authentication response. 18 func (*PasswordMessage) InitialResponse() {} 19 20 // Decode decodes src into dst. src must contain the complete message with the exception of the initial 1 byte message 21 // type identifier and 4 byte message length. 22 func (dst *PasswordMessage) Decode(src []byte) error { 23 buf := bytes.NewBuffer(src) 24 25 b, err := buf.ReadBytes(0) 26 if err != nil { 27 return err 28 } 29 dst.Password = string(b[:len(b)-1]) 30 31 return nil 32 } 33 34 // Encode encodes src into dst. dst will include the 1 byte message type identifier and the 4 byte message length. 35 func (src *PasswordMessage) Encode(dst []byte) []byte { 36 dst = append(dst, 'p') 37 dst = pgio.AppendInt32(dst, int32(4+len(src.Password)+1)) 38 39 dst = append(dst, src.Password...) 40 dst = append(dst, 0) 41 42 return dst 43 } 44 45 // MarshalJSON implements encoding/json.Marshaler. 46 func (src PasswordMessage) MarshalJSON() ([]byte, error) { 47 return json.Marshal(struct { 48 Type string 49 Password string 50 }{ 51 Type: "PasswordMessage", 52 Password: src.Password, 53 }) 54 }