]> git.r.bdr.sh - rbdr/mobius/blame - hotline/account.go
Implement io.Writer interface for Transaction
[rbdr/mobius] / hotline / account.go
CommitLineData
6988a057
JH
1package hotline
2
3import (
d2810ae9 4 "encoding/binary"
d2810ae9 5 "golang.org/x/crypto/bcrypt"
6988a057
JH
6)
7
8const GuestAccount = "guest" // default account used when no login is provided for a connection
9
10type Account struct {
187d6dc5
JH
11 Login string `yaml:"Login"`
12 Name string `yaml:"Name"`
13 Password string `yaml:"Password"`
14 Access accessBitmap `yaml:"Access"`
6988a057
JH
15}
16
926c7f55
JH
17// Read implements io.Reader interface for Account
18func (a *Account) Read(p []byte) (n int, err error) {
d2810ae9
JH
19 fields := []Field{
20 NewField(fieldUserName, []byte(a.Name)),
21 NewField(fieldUserLogin, negateString([]byte(a.Login))),
187d6dc5 22 NewField(fieldUserAccess, a.Access[:]),
d2810ae9
JH
23 }
24
25 if bcrypt.CompareHashAndPassword([]byte(a.Password), []byte("")) != nil {
26 fields = append(fields, NewField(fieldUserPassword, []byte("x")))
27 }
28
29 fieldCount := make([]byte, 2)
30 binary.BigEndian.PutUint16(fieldCount, uint16(len(fields)))
31
926c7f55
JH
32 p = append(p, fieldCount...)
33
d2810ae9 34 for _, field := range fields {
926c7f55 35 p = append(p, field.Payload()...)
d2810ae9
JH
36 }
37
926c7f55 38 return len(p), nil
6988a057 39}