aboutsummaryrefslogtreecommitdiff
path: root/hotline/account.go
diff options
context:
space:
mode:
authorJeff Halter <868228+jhalter@users.noreply.github.com>2024-06-15 11:13:16 -0700
committerJeff Halter <868228+jhalter@users.noreply.github.com>2024-06-15 11:13:16 -0700
commit95159e5585762c06c654945070ba54262b7dcec9 (patch)
tree23609018c1460b056ce22067290ea12ee851d483 /hotline/account.go
parenta6216dd89252fa01dc176f98f1e4ecfd3f637566 (diff)
Refactoring and cleanup
* Split CLI client into separate project * Convert more functions to follow common Golang idioms e.g io.Reader, io.Writer * Use ldflags for versioning * Misc cleanup and simplification
Diffstat (limited to 'hotline/account.go')
-rw-r--r--hotline/account.go21
1 files changed, 18 insertions, 3 deletions
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