]>
Commit | Line | Data |
---|---|---|
1 | package hotline | |
2 | ||
3 | import ( | |
4 | "encoding/binary" | |
5 | "golang.org/x/crypto/bcrypt" | |
6 | "log" | |
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 accessBitmap `yaml:"Access"` | |
16 | } | |
17 | ||
18 | // Read implements io.Reader interface for Account | |
19 | func (a *Account) Read(p []byte) (n int, err error) { | |
20 | fields := []Field{ | |
21 | NewField(FieldUserName, []byte(a.Name)), | |
22 | NewField(FieldUserLogin, encodeString([]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 | p = append(p, fieldCount...) | |
34 | ||
35 | for _, field := range fields { | |
36 | p = append(p, field.Payload()...) | |
37 | } | |
38 | ||
39 | return len(p), nil | |
40 | } | |
41 | ||
42 | // hashAndSalt generates a password hash from a users obfuscated plaintext password | |
43 | func hashAndSalt(pwd []byte) string { | |
44 | hash, err := bcrypt.GenerateFromPassword(pwd, bcrypt.MinCost) | |
45 | if err != nil { | |
46 | log.Println(err) | |
47 | } | |
48 | ||
49 | return string(hash) | |
50 | } |