aboutsummaryrefslogtreecommitdiff
path: root/hotline/account.go
blob: 9a0fb7dea55f56dcf0aace97223e0e32fac69650 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package hotline

import (
	"encoding/binary"
	"golang.org/x/crypto/bcrypt"
	"log"
)

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   accessBitmap `yaml:"Access"`
}

// 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[:]),
	}

	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)))

	p = append(p, fieldCount...)

	for _, field := range fields {
		p = append(p, field.Payload()...)
	}

	return len(p), nil
}

// 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)
}