-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.go
104 lines (86 loc) · 2.59 KB
/
auth.go
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package mq
import (
"fmt"
"io"
)
func NewAuth() *Auth {
return &Auth{fixed: bits(AUTH)}
}
type Auth struct {
fixed bits
reasonCode wuint8
reasonString wstring
authMethod wstring
authData bindata
UserProperties
}
func (p *Auth) SetReasonCode(v ReasonCode) { p.reasonCode = wuint8(v) }
func (p *Auth) ReasonCode() ReasonCode { return ReasonCode(p.reasonCode) }
func (p *Auth) SetAuthMethod(v string) { p.authMethod = wstring(v) }
func (p *Auth) AuthMethod() string { return string(p.authMethod) }
func (p *Auth) SetAuthData(v []byte) { p.authData = bindata(v) }
func (p *Auth) AuthData() []byte { return []byte(p.authData) }
func (p *Auth) SetReasonString(v string) { p.reasonString = wstring(v) }
func (p *Auth) ReasonString() string { return string(p.reasonString) }
func (p *Auth) String() string {
return fmt.Sprintf("%s %v bytes",
firstByte(p.fixed).String(),
p.width(),
)
}
func (p *Auth) dump(w io.Writer) {
fmt.Fprintf(w, "AuthData: %q\n", string(p.AuthData()))
fmt.Fprintf(w, "AuthMethod: %q\n", p.AuthMethod())
fmt.Fprintf(w, "ReasonCode: %v\n", p.ReasonCode())
fmt.Fprintf(w, "ReasonString: %q\n", p.ReasonString())
p.UserProperties.dump(w)
}
func (p *Auth) WriteTo(w io.Writer) (int64, error) {
b := make([]byte, p.width())
p.fill(b, 0)
n, err := w.Write(b)
return int64(n), err
}
func (p *Auth) UnmarshalBinary(data []byte) error {
b := &buffer{data: data}
b.get(&p.reasonCode)
b.getAny(p.propertyMap(), p.appendUserProperty)
return b.err
}
func (p *Auth) width() int {
return p.fill(_LEN, 0)
}
func (p *Auth) fill(b []byte, i int) int {
remainingLen := vbint(p.variableHeader(_LEN, 0))
i += p.fixed.fill(b, i) // firstByte header
i += remainingLen.fill(b, i) // remaining length
i += p.variableHeader(b, i)
return i
}
func (p *Auth) variableHeader(b []byte, i int) int {
n := i
proplen := p.properties(_LEN, 0)
if p.reasonCode == 0 && proplen == 0 {
return 0
}
i += p.reasonCode.fill(b, i)
i += vbint(proplen).fill(b, i)
i += p.properties(b, i)
return i - n
}
func (p *Auth) properties(b []byte, i int) int {
n := i
// using c.propertyMap is slow compared to direct field access
i += p.authMethod.fillProp(b, i, AuthMethod)
i += p.authData.fillProp(b, i, AuthData)
i += p.reasonString.fillProp(b, i, ReasonString)
i += p.UserProperties.properties(b, i)
return i - n
}
func (p *Auth) propertyMap() map[Ident]func() wireType {
return map[Ident]func() wireType{
AuthMethod: func() wireType { return &p.authMethod },
AuthData: func() wireType { return &p.authData },
ReasonString: func() wireType { return &p.reasonString },
}
}