X-Git-Url: https://git.r.bdr.sh/rbdr/mobius/blobdiff_plain/d005ef04cfaa26943e6dd33807d741577ffb232a..9f89cd9fbbcec9f7f42c87ee0adc21427aab9f1c:/hotline/account.go?ds=inline diff --git a/hotline/account.go b/hotline/account.go index c51162d..7f770b1 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) - } +// HashAndSalt generates a password hash from a users obfuscated plaintext password +func HashAndSalt(pwd []byte) string { + hash, _ := bcrypt.GenerateFromPassword(pwd, bcrypt.MinCost) return string(hash) }