...
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
13 type AuthenticationSASL struct {
14 AuthMechanisms []string
15 }
16
17
18 func (*AuthenticationSASL) Backend() {}
19
20
21 func (*AuthenticationSASL) AuthenticationResponse() {}
22
23
24
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
49 func (src *AuthenticationSASL) Encode(dst []byte) ([]byte, error) {
50 dst, sp := beginMessage(dst, 'R')
51 dst = pgio.AppendUint32(dst, AuthTypeSASL)
52
53 for _, s := range src.AuthMechanisms {
54 dst = append(dst, []byte(s)...)
55 dst = append(dst, 0)
56 }
57 dst = append(dst, 0)
58
59 return finishMessage(dst, sp)
60 }
61
62
63 func (src AuthenticationSASL) MarshalJSON() ([]byte, error) {
64 return json.Marshal(struct {
65 Type string
66 AuthMechanisms []string
67 }{
68 Type: "AuthenticationSASL",
69 AuthMechanisms: src.AuthMechanisms,
70 })
71 }
72
View as plain text