password_message.go 1.2 KB

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