]>
Commit | Line | Data |
---|---|---|
1 | package hotline | |
2 | ||
3 | import ( | |
4 | "encoding/binary" | |
5 | "github.com/jhalter/mobius/concat" | |
6 | "golang.org/x/crypto/bcrypt" | |
7 | ) | |
8 | ||
9 | const GuestAccount = "guest" // default account used when no login is provided for a connection | |
10 | ||
11 | type Account struct { | |
12 | Login string `yaml:"Login"` | |
13 | Name string `yaml:"Name"` | |
14 | Password string `yaml:"Password"` | |
15 | Access *[]byte `yaml:"Access"` // 8 byte bitmap | |
16 | } | |
17 | ||
18 | // MarshalBinary marshals an Account to byte slice | |
19 | func (a *Account) MarshalBinary() (out []byte) { | |
20 | fields := []Field{ | |
21 | NewField(fieldUserName, []byte(a.Name)), | |
22 | NewField(fieldUserLogin, negateString([]byte(a.Login))), | |
23 | NewField(fieldUserAccess, *a.Access), | |
24 | } | |
25 | ||
26 | if bcrypt.CompareHashAndPassword([]byte(a.Password), []byte("")) != nil { | |
27 | fields = append(fields, NewField(fieldUserPassword, []byte("x"))) | |
28 | } | |
29 | ||
30 | fieldCount := make([]byte, 2) | |
31 | binary.BigEndian.PutUint16(fieldCount, uint16(len(fields))) | |
32 | ||
33 | var fieldPayload []byte | |
34 | for _, field := range fields { | |
35 | fieldPayload = append(fieldPayload, field.Payload()...) | |
36 | } | |
37 | ||
38 | return concat.Slices( | |
39 | fieldCount, | |
40 | fieldPayload, | |
41 | ) | |
42 | } |