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