X-Git-Url: https://git.r.bdr.sh/rbdr/mobius/blobdiff_plain/b129b7cbc9fd9a9c11a77e5922861ef08893efa1..95159e5585762c06c654945070ba54262b7dcec9:/hotline/account.go diff --git a/hotline/account.go b/hotline/account.go index 4c5a9b9..3ad0687 100644 --- a/hotline/account.go +++ b/hotline/account.go @@ -2,6 +2,7 @@ package hotline import ( "encoding/binary" + "fmt" "golang.org/x/crypto/bcrypt" "io" "log" @@ -15,10 +16,12 @@ type Account struct { Name string `yaml:"Name"` Password string `yaml:"Password"` Access accessBitmap `yaml:"Access"` + + readOffset int // Internal offset to track read progress } // 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, encodeString([]byte(a.Login))), @@ -34,10 +37,22 @@ func (a *Account) Read(p []byte) (n int, err error) { var fieldBytes []byte for _, field := range fields { - fieldBytes = append(fieldBytes, field.Payload()...) + b, err := io.ReadAll(&field) + if err != nil { + return 0, fmt.Errorf("error reading field: %w", err) + } + fieldBytes = append(fieldBytes, b...) + } + + buf := slices.Concat(fieldCount, fieldBytes) + if a.readOffset >= len(buf) { + return 0, io.EOF // All bytes have been read } - return copy(p, slices.Concat(fieldCount, fieldBytes)), io.EOF + n := copy(p, buf[a.readOffset:]) + a.readOffset += n + + return n, nil } // hashAndSalt generates a password hash from a users obfuscated plaintext password