]> git.r.bdr.sh - rbdr/mobius/blobdiff - hotline/account.go
Allow for personal ~ folder
[rbdr/mobius] / hotline / account.go
index 4c5a9b98a608810047a8830b01495973be172bda..39ea9740275c1771fa3892b1d4429661f4de0b8e 100644 (file)
@@ -2,9 +2,9 @@ package hotline
 
 import (
        "encoding/binary"
+       "fmt"
        "golang.org/x/crypto/bcrypt"
        "io"
-       "log"
        "slices"
 )
 
@@ -14,14 +14,26 @@ type Account struct {
        Login    string       `yaml:"Login"`
        Name     string       `yaml:"Name"`
        Password string       `yaml:"Password"`
-       Access   accessBitmap `yaml:"Access"`
+       Access   AccessBitmap `yaml:"Access"`
+       FileRoot string       `yaml:"FileRoot"`
+
+       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, encodeString([]byte(a.Login))),
+               NewField(FieldUserLogin, EncodeString([]byte(a.Login))),
                NewField(FieldUserAccess, a.Access[:]),
        }
 
@@ -34,18 +46,27 @@ 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...)
        }
 
-       return copy(p, slices.Concat(fieldCount, fieldBytes)), io.EOF
+       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)
 }