X-Git-Url: https://git.r.bdr.sh/rbdr/mobius/blobdiff_plain/d2810ae9038b057e8f18e8b495a57d8f96ae5be6..e995052ef530b1eae2d0476ff519f7225bbb481d:/hotline/account.go?ds=sidebyside diff --git a/hotline/account.go b/hotline/account.go index 3da818f..4c5a9b9 100644 --- a/hotline/account.go +++ b/hotline/account.go @@ -2,41 +2,50 @@ package hotline import ( "encoding/binary" - "github.com/jhalter/mobius/concat" "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"` } -// MarshalBinary marshals an Account to byte slice -func (a *Account) MarshalBinary() (out []byte) { +// 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), + 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"))) + fields = append(fields, NewField(FieldUserPassword, []byte("x"))) } fieldCount := make([]byte, 2) binary.BigEndian.PutUint16(fieldCount, uint16(len(fields))) - var fieldPayload []byte + var fieldBytes []byte for _, field := range fields { - fieldPayload = append(fieldPayload, field.Payload()...) + fieldBytes = append(fieldBytes, field.Payload()...) } - return concat.Slices( - fieldCount, - fieldPayload, - ) + return copy(p, slices.Concat(fieldCount, fieldBytes)), io.EOF +} + +// 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) }