X-Git-Url: https://git.r.bdr.sh/rbdr/mobius/blobdiff_plain/c5d9af5aa4d9fb20316be45ab1b775bcf61bcad5..69c2fb504953cf8de3730b2f64cfa8d7b6b13859:/hotline/account.go diff --git a/hotline/account.go b/hotline/account.go index 7368592..2c400ac 100644 --- a/hotline/account.go +++ b/hotline/account.go @@ -1,7 +1,8 @@ package hotline import ( - "github.com/jhalter/mobius/concat" + "encoding/binary" + "golang.org/x/crypto/bcrypt" ) const GuestAccount = "guest" // default account used when no login is provided for a connection @@ -13,12 +14,26 @@ type Account struct { Access *[]byte `yaml:"Access"` // 8 byte bitmap } -// 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(), - ) +// Read implements io.Reader interface for Account +func (a *Account) Read(p []byte) (n int, err error) { + fields := []Field{ + NewField(fieldUserName, []byte(a.Name)), + NewField(fieldUserLogin, negateString([]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))) + + p = append(p, fieldCount...) + + for _, field := range fields { + p = append(p, field.Payload()...) + } + + return len(p), nil }