authentication_sasl_continue.go 1.9 KB

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