gtsocial-umbx

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

authentication_sasl.go (1955B)


      1 package pgproto3
      2 
      3 import (
      4 	"bytes"
      5 	"encoding/binary"
      6 	"encoding/json"
      7 	"errors"
      8 
      9 	"github.com/jackc/pgio"
     10 )
     11 
     12 // AuthenticationSASL is a message sent from the backend indicating that SASL authentication is required.
     13 type AuthenticationSASL struct {
     14 	AuthMechanisms []string
     15 }
     16 
     17 // Backend identifies this message as sendable by the PostgreSQL backend.
     18 func (*AuthenticationSASL) Backend() {}
     19 
     20 // Backend identifies this message as an authentication response.
     21 func (*AuthenticationSASL) AuthenticationResponse() {}
     22 
     23 // Decode decodes src into dst. src must contain the complete message with the exception of the initial 1 byte message
     24 // type identifier and 4 byte message length.
     25 func (dst *AuthenticationSASL) Decode(src []byte) error {
     26 	if len(src) < 4 {
     27 		return errors.New("authentication message too short")
     28 	}
     29 
     30 	authType := binary.BigEndian.Uint32(src)
     31 
     32 	if authType != AuthTypeSASL {
     33 		return errors.New("bad auth type")
     34 	}
     35 
     36 	authMechanisms := src[4:]
     37 	for len(authMechanisms) > 1 {
     38 		idx := bytes.IndexByte(authMechanisms, 0)
     39 		if idx > 0 {
     40 			dst.AuthMechanisms = append(dst.AuthMechanisms, string(authMechanisms[:idx]))
     41 			authMechanisms = authMechanisms[idx+1:]
     42 		}
     43 	}
     44 
     45 	return nil
     46 }
     47 
     48 // Encode encodes src into dst. dst will include the 1 byte message type identifier and the 4 byte message length.
     49 func (src *AuthenticationSASL) Encode(dst []byte) []byte {
     50 	dst = append(dst, 'R')
     51 	sp := len(dst)
     52 	dst = pgio.AppendInt32(dst, -1)
     53 	dst = pgio.AppendUint32(dst, AuthTypeSASL)
     54 
     55 	for _, s := range src.AuthMechanisms {
     56 		dst = append(dst, []byte(s)...)
     57 		dst = append(dst, 0)
     58 	}
     59 	dst = append(dst, 0)
     60 
     61 	pgio.SetInt32(dst[sp:], int32(len(dst[sp:])))
     62 
     63 	return dst
     64 }
     65 
     66 // MarshalJSON implements encoding/json.Marshaler.
     67 func (src AuthenticationSASL) MarshalJSON() ([]byte, error) {
     68 	return json.Marshal(struct {
     69 		Type           string
     70 		AuthMechanisms []string
     71 	}{
     72 		Type:           "AuthenticationSASL",
     73 		AuthMechanisms: src.AuthMechanisms,
     74 	})
     75 }