-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathauth.go
64 lines (51 loc) · 1.25 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
package nsq
import (
"bufio"
"encoding/binary"
"io"
"github.com/pkg/errors"
)
// Auth represents the AUTH command.
type Auth struct {
// Secret set for authentication.
Secret string
}
// Name returns the name of the command in order to satisfy the Command
// interface.
func (c Auth) Name() string {
return "AUTH"
}
// Write serializes the command to the given buffered output, satisfies the
// Command interface.
func (c Auth) Write(w *bufio.Writer) (err error) {
if _, err = w.WriteString("AUTH\n"); err != nil {
err = errors.Wrap(err, "writing AUTH command")
return
}
if err = binary.Write(w, binary.BigEndian, uint32(len(c.Secret))); err != nil {
err = errors.Wrap(err, "writing AUTH secret size")
return
}
if _, err = w.WriteString(c.Secret); err != nil {
err = errors.Wrap(err, "writing AUTH secret data")
return
}
return
}
func readAuth(r *bufio.Reader) (cmd Auth, err error) {
var size uint32
var data []byte
if err = binary.Read(r, binary.BigEndian, &size); err != nil {
err = errors.Wrap(err, "reading AUTH secret size")
return
}
data = make([]byte, int(size))
if _, err = io.ReadFull(r, data); err != nil {
err = errors.Wrap(err, "reading AUTH secret data")
return
}
cmd = Auth{
Secret: string(data),
}
return
}