6 "golang.org/x/crypto/bcrypt"
11 const GuestAccount = "guest" // default account used when no login is provided for a connection
14 Login string `yaml:"Login"`
15 Name string `yaml:"Name"`
16 Password string `yaml:"Password"`
17 Access accessBitmap `yaml:"Access,flow"`
19 readOffset int // Internal offset to track read progress
22 func NewAccount(login, name, password string, access accessBitmap) *Account {
26 Password: hashAndSalt([]byte(password)),
31 // Read implements io.Reader interface for Account
32 func (a *Account) Read(p []byte) (int, error) {
34 NewField(FieldUserName, []byte(a.Name)),
35 NewField(FieldUserLogin, encodeString([]byte(a.Login))),
36 NewField(FieldUserAccess, a.Access[:]),
39 if bcrypt.CompareHashAndPassword([]byte(a.Password), []byte("")) != nil {
40 fields = append(fields, NewField(FieldUserPassword, []byte("x")))
43 fieldCount := make([]byte, 2)
44 binary.BigEndian.PutUint16(fieldCount, uint16(len(fields)))
47 for _, field := range fields {
48 b, err := io.ReadAll(&field)
50 return 0, fmt.Errorf("error reading field: %w", err)
52 fieldBytes = append(fieldBytes, b...)
55 buf := slices.Concat(fieldCount, fieldBytes)
56 if a.readOffset >= len(buf) {
57 return 0, io.EOF // All bytes have been read
60 n := copy(p, buf[a.readOffset:])
66 // hashAndSalt generates a password hash from a users obfuscated plaintext password
67 func hashAndSalt(pwd []byte) string {
68 hash, _ := bcrypt.GenerateFromPassword(pwd, bcrypt.MinCost)