gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

authentication_ok.go (1377B)


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