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