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"`
18 FileRoot string `yaml:"FileRoot"`
20 readOffset int // Internal offset to track read progress
23 func NewAccount(login, name, password string, access AccessBitmap) *Account {
27 Password: HashAndSalt([]byte(password)),
32 // Read implements io.Reader interface for Account
33 func (a *Account) Read(p []byte) (int, error) {
35 NewField(FieldUserName, []byte(a.Name)),
36 NewField(FieldUserLogin, EncodeString([]byte(a.Login))),
37 NewField(FieldUserAccess, a.Access[:]),
40 if bcrypt.CompareHashAndPassword([]byte(a.Password), []byte("")) != nil {
41 fields = append(fields, NewField(FieldUserPassword, []byte("x")))
44 fieldCount := make([]byte, 2)
45 binary.BigEndian.PutUint16(fieldCount, uint16(len(fields)))
48 for _, field := range fields {
49 b, err := io.ReadAll(&field)
51 return 0, fmt.Errorf("error reading field: %w", err)
53 fieldBytes = append(fieldBytes, b...)
56 buf := slices.Concat(fieldCount, fieldBytes)
57 if a.readOffset >= len(buf) {
58 return 0, io.EOF // All bytes have been read
61 n := copy(p, buf[a.readOffset:])
67 // HashAndSalt generates a password hash from a users obfuscated plaintext password
68 func HashAndSalt(pwd []byte) string {
69 hash, _ := bcrypt.GenerateFromPassword(pwd, bcrypt.MinCost)