authentication_ok.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package pgproto3
  2. import (
  3. "encoding/binary"
  4. "encoding/json"
  5. "errors"
  6. "github.com/jackc/pgx/v5/internal/pgio"
  7. )
  8. // AuthenticationOk is a message sent from the backend indicating that authentication was successful.
  9. type AuthenticationOk struct{}
  10. // Backend identifies this message as sendable by the PostgreSQL backend.
  11. func (*AuthenticationOk) Backend() {}
  12. // Backend identifies this message as an authentication response.
  13. func (*AuthenticationOk) AuthenticationResponse() {}
  14. // Decode decodes src into dst. src must contain the complete message with the exception of the initial 1 byte message
  15. // type identifier and 4 byte message length.
  16. func (dst *AuthenticationOk) Decode(src []byte) error {
  17. if len(src) != 4 {
  18. return errors.New("bad authentication message size")
  19. }
  20. authType := binary.BigEndian.Uint32(src)
  21. if authType != AuthTypeOk {
  22. return errors.New("bad auth type")
  23. }
  24. return nil
  25. }
  26. // Encode encodes src into dst. dst will include the 1 byte message type identifier and the 4 byte message length.
  27. func (src *AuthenticationOk) Encode(dst []byte) ([]byte, error) {
  28. dst, sp := beginMessage(dst, 'R')
  29. dst = pgio.AppendUint32(dst, AuthTypeOk)
  30. return finishMessage(dst, sp)
  31. }
  32. // MarshalJSON implements encoding/json.Marshaler.
  33. func (src AuthenticationOk) MarshalJSON() ([]byte, error) {
  34. return json.Marshal(struct {
  35. Type string
  36. }{
  37. Type: "AuthenticationOK",
  38. })
  39. }