]> git.r.bdr.sh - rbdr/mobius/blame - hotline/account.go
Update go.yml
[rbdr/mobius] / hotline / account.go
CommitLineData
6988a057
JH
1package hotline
2
3import (
d2810ae9 4 "encoding/binary"
d2810ae9 5 "golang.org/x/crypto/bcrypt"
b129b7cb 6 "io"
69af8ddb 7 "log"
b129b7cb 8 "slices"
6988a057
JH
9)
10
11const GuestAccount = "guest" // default account used when no login is provided for a connection
12
13type Account struct {
187d6dc5
JH
14 Login string `yaml:"Login"`
15 Name string `yaml:"Name"`
16 Password string `yaml:"Password"`
17 Access accessBitmap `yaml:"Access"`
6988a057
JH
18}
19
926c7f55
JH
20// Read implements io.Reader interface for Account
21func (a *Account) Read(p []byte) (n int, err error) {
d2810ae9 22 fields := []Field{
d005ef04 23 NewField(FieldUserName, []byte(a.Name)),
76d0c1f6 24 NewField(FieldUserLogin, encodeString([]byte(a.Login))),
d005ef04 25 NewField(FieldUserAccess, a.Access[:]),
d2810ae9
JH
26 }
27
28 if bcrypt.CompareHashAndPassword([]byte(a.Password), []byte("")) != nil {
d005ef04 29 fields = append(fields, NewField(FieldUserPassword, []byte("x")))
d2810ae9
JH
30 }
31
32 fieldCount := make([]byte, 2)
33 binary.BigEndian.PutUint16(fieldCount, uint16(len(fields)))
34
b129b7cb 35 var fieldBytes []byte
d2810ae9 36 for _, field := range fields {
b129b7cb 37 fieldBytes = append(fieldBytes, field.Payload()...)
d2810ae9
JH
38 }
39
b129b7cb 40 return copy(p, slices.Concat(fieldCount, fieldBytes)), io.EOF
6988a057 41}
69af8ddb
JH
42
43// hashAndSalt generates a password hash from a users obfuscated plaintext password
44func hashAndSalt(pwd []byte) string {
45 hash, err := bcrypt.GenerateFromPassword(pwd, bcrypt.MinCost)
46 if err != nil {
47 log.Println(err)
48 }
49
50 return string(hash)
51}