aboutsummaryrefslogtreecommitdiff
path: root/vendor/github.com/jackc/pgx/pgproto3/authentication.go
blob: 77750b862ff87fa93f3db588efba2e16f08df67d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package pgproto3

import (
	"encoding/binary"

	"github.com/jackc/pgx/pgio"
	"github.com/pkg/errors"
)

const (
	AuthTypeOk                = 0
	AuthTypeCleartextPassword = 3
	AuthTypeMD5Password       = 5
)

type Authentication struct {
	Type uint32

	// MD5Password fields
	Salt [4]byte
}

func (*Authentication) Backend() {}

func (dst *Authentication) Decode(src []byte) error {
	*dst = Authentication{Type: binary.BigEndian.Uint32(src[:4])}

	switch dst.Type {
	case AuthTypeOk:
	case AuthTypeCleartextPassword:
	case AuthTypeMD5Password:
		copy(dst.Salt[:], src[4:8])
	default:
		return errors.Errorf("unknown authentication type: %d", dst.Type)
	}

	return nil
}

func (src *Authentication) Encode(dst []byte) []byte {
	dst = append(dst, 'R')
	sp := len(dst)
	dst = pgio.AppendInt32(dst, -1)
	dst = pgio.AppendUint32(dst, src.Type)

	switch src.Type {
	case AuthTypeMD5Password:
		dst = append(dst, src.Salt[:]...)
	}

	pgio.SetInt32(dst[sp:], int32(len(dst[sp:])))

	return dst
}