]> git.r.bdr.sh - rbdr/mobius/blame_incremental - hotline/account.go
Refactoring and cleanup
[rbdr/mobius] / hotline / account.go
... / ...
CommitLineData
1package hotline
2
3import (
4 "encoding/binary"
5 "fmt"
6 "golang.org/x/crypto/bcrypt"
7 "io"
8 "log"
9 "slices"
10)
11
12const GuestAccount = "guest" // default account used when no login is provided for a connection
13
14type Account struct {
15 Login string `yaml:"Login"`
16 Name string `yaml:"Name"`
17 Password string `yaml:"Password"`
18 Access accessBitmap `yaml:"Access"`
19
20 readOffset int // Internal offset to track read progress
21}
22
23// Read implements io.Reader interface for Account
24func (a *Account) Read(p []byte) (int, error) {
25 fields := []Field{
26 NewField(FieldUserName, []byte(a.Name)),
27 NewField(FieldUserLogin, encodeString([]byte(a.Login))),
28 NewField(FieldUserAccess, a.Access[:]),
29 }
30
31 if bcrypt.CompareHashAndPassword([]byte(a.Password), []byte("")) != nil {
32 fields = append(fields, NewField(FieldUserPassword, []byte("x")))
33 }
34
35 fieldCount := make([]byte, 2)
36 binary.BigEndian.PutUint16(fieldCount, uint16(len(fields)))
37
38 var fieldBytes []byte
39 for _, field := range fields {
40 b, err := io.ReadAll(&field)
41 if err != nil {
42 return 0, fmt.Errorf("error reading field: %w", err)
43 }
44 fieldBytes = append(fieldBytes, b...)
45 }
46
47 buf := slices.Concat(fieldCount, fieldBytes)
48 if a.readOffset >= len(buf) {
49 return 0, io.EOF // All bytes have been read
50 }
51
52 n := copy(p, buf[a.readOffset:])
53 a.readOffset += n
54
55 return n, nil
56}
57
58// hashAndSalt generates a password hash from a users obfuscated plaintext password
59func hashAndSalt(pwd []byte) string {
60 hash, err := bcrypt.GenerateFromPassword(pwd, bcrypt.MinCost)
61 if err != nil {
62 log.Println(err)
63 }
64
65 return string(hash)
66}