]>
Commit | Line | Data |
---|---|---|
1 | package hotline | |
2 | ||
3 | import ( | |
4 | "encoding/binary" | |
5 | "golang.org/x/crypto/bcrypt" | |
6 | "io" | |
7 | "log" | |
8 | "slices" | |
9 | ) | |
10 | ||
11 | const GuestAccount = "guest" // default account used when no login is provided for a connection | |
12 | ||
13 | type Account struct { | |
14 | Login string `yaml:"Login"` | |
15 | Name string `yaml:"Name"` | |
16 | Password string `yaml:"Password"` | |
17 | Access accessBitmap `yaml:"Access"` | |
18 | } | |
19 | ||
20 | // Read implements io.Reader interface for Account | |
21 | func (a *Account) Read(p []byte) (n int, err error) { | |
22 | fields := []Field{ | |
23 | NewField(FieldUserName, []byte(a.Name)), | |
24 | NewField(FieldUserLogin, encodeString([]byte(a.Login))), | |
25 | NewField(FieldUserAccess, a.Access[:]), | |
26 | } | |
27 | ||
28 | if bcrypt.CompareHashAndPassword([]byte(a.Password), []byte("")) != nil { | |
29 | fields = append(fields, NewField(FieldUserPassword, []byte("x"))) | |
30 | } | |
31 | ||
32 | fieldCount := make([]byte, 2) | |
33 | binary.BigEndian.PutUint16(fieldCount, uint16(len(fields))) | |
34 | ||
35 | var fieldBytes []byte | |
36 | for _, field := range fields { | |
37 | fieldBytes = append(fieldBytes, field.Payload()...) | |
38 | } | |
39 | ||
40 | return copy(p, slices.Concat(fieldCount, fieldBytes)), io.EOF | |
41 | } | |
42 | ||
43 | // hashAndSalt generates a password hash from a users obfuscated plaintext password | |
44 | func hashAndSalt(pwd []byte) string { | |
45 | hash, err := bcrypt.GenerateFromPassword(pwd, bcrypt.MinCost) | |
46 | if err != nil { | |
47 | log.Println(err) | |
48 | } | |
49 | ||
50 | return string(hash) | |
51 | } |