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