X-Git-Url: https://git.r.bdr.sh/rbdr/mobius/blobdiff_plain/c5d9af5aa4d9fb20316be45ab1b775bcf61bcad5..f6f1d88969e12eadb7013397cdad3c4c5625988c:/hotline/account.go?ds=inline diff --git a/hotline/account.go b/hotline/account.go index 7368592..3ad0687 100644 --- a/hotline/account.go +++ b/hotline/account.go @@ -1,24 +1,66 @@ package hotline import ( - "github.com/jhalter/mobius/concat" + "encoding/binary" + "fmt" + "golang.org/x/crypto/bcrypt" + "io" + "log" + "slices" ) const GuestAccount = "guest" // default account used when no login is provided for a connection type Account struct { - Login string `yaml:"Login"` - Name string `yaml:"Name"` - Password string `yaml:"Password"` - Access *[]byte `yaml:"Access"` // 8 byte bitmap + Login string `yaml:"Login"` + 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) (int, error) { + fields := []Field{ + NewField(FieldUserName, []byte(a.Name)), + NewField(FieldUserLogin, encodeString([]byte(a.Login))), + NewField(FieldUserAccess, a.Access[:]), + } + + if bcrypt.CompareHashAndPassword([]byte(a.Password), []byte("")) != nil { + fields = append(fields, NewField(FieldUserPassword, []byte("x"))) + } + + fieldCount := make([]byte, 2) + binary.BigEndian.PutUint16(fieldCount, uint16(len(fields))) + + var fieldBytes []byte + for _, field := range fields { + 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 + } + + n := copy(p, buf[a.readOffset:]) + a.readOffset += n + + return n, nil } -// MarshalBinary marshals an Account to byte slice -func (a *Account) MarshalBinary() (out []byte) { - return concat.Slices( - []byte{0x00, 0x3}, // param count -- always 3 - NewField(fieldUserName, []byte(a.Name)).Payload(), - NewField(fieldUserLogin, negateString([]byte(a.Login))).Payload(), - NewField(fieldUserAccess, *a.Access).Payload(), - ) +// 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) + } + + return string(hash) }