]> git.r.bdr.sh - rbdr/mobius/blobdiff - hotline/account.go
Convert bespoke methods to io.Reader/io.Writer interfaces
[rbdr/mobius] / hotline / account.go
index 736859210980c2c13851d0e1834567ec38ceeb31..4c5a9b98a608810047a8830b01495973be172bda 100644 (file)
@@ -1,24 +1,51 @@
 package hotline
 
 import (
 package hotline
 
 import (
-       "github.com/jhalter/mobius/concat"
+       "encoding/binary"
+       "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 {
 )
 
 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) {
-       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, 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 {
+               fieldBytes = append(fieldBytes, field.Payload()...)
+       }
+
+       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)
 }
 }