X-Git-Url: https://git.r.bdr.sh/rbdr/mobius/blobdiff_plain/d005ef04cfaa26943e6dd33807d741577ffb232a..71f9ce10c4d0ebc0d5aec5a2bf46b1c508409338:/hotline/account.go diff --git a/hotline/account.go b/hotline/account.go index c51162d..7b2aafe 100644 --- a/hotline/account.go +++ b/hotline/account.go @@ -2,8 +2,10 @@ package hotline import ( "encoding/binary" + "fmt" "golang.org/x/crypto/bcrypt" - "log" + "io" + "slices" ) const GuestAccount = "guest" // default account used when no login is provided for a connection @@ -12,14 +14,25 @@ type Account struct { Login string `yaml:"Login"` Name string `yaml:"Name"` Password string `yaml:"Password"` - Access accessBitmap `yaml:"Access"` + Access accessBitmap `yaml:"Access,flow"` + + readOffset int // Internal offset to track read progress +} + +func NewAccount(login, name, password string, access accessBitmap) *Account { + return &Account{ + Login: login, + Name: name, + Password: hashAndSalt([]byte(password)), + Access: access, + } } // Read implements io.Reader interface for Account -func (a *Account) Read(p []byte) (n int, err error) { +func (a *Account) Read(p []byte) (int, error) { fields := []Field{ NewField(FieldUserName, []byte(a.Name)), - NewField(FieldUserLogin, negateString([]byte(a.Login))), + NewField(FieldUserLogin, encodeString([]byte(a.Login))), NewField(FieldUserAccess, a.Access[:]), } @@ -30,21 +43,29 @@ func (a *Account) Read(p []byte) (n int, err error) { fieldCount := make([]byte, 2) binary.BigEndian.PutUint16(fieldCount, uint16(len(fields))) - p = append(p, fieldCount...) - + var fieldBytes []byte for _, field := range fields { - p = append(p, field.Payload()...) + b, err := io.ReadAll(&field) + if err != nil { + return 0, fmt.Errorf("error reading field: %w", err) + } + fieldBytes = append(fieldBytes, b...) } - return len(p), nil + buf := slices.Concat(fieldCount, fieldBytes) + if a.readOffset >= len(buf) { + return 0, io.EOF // All bytes have been read + } + + n := copy(p, buf[a.readOffset:]) + a.readOffset += n + + return n, nil } // hashAndSalt generates a password hash from a users obfuscated plaintext password func hashAndSalt(pwd []byte) string { - hash, err := bcrypt.GenerateFromPassword(pwd, bcrypt.MinCost) - if err != nil { - log.Println(err) - } + hash, _ := bcrypt.GenerateFromPassword(pwd, bcrypt.MinCost) return string(hash) }