Your IP : 216.73.216.224


Current Path : /home/hotlineuser/mobius/hotline/
Upload File :
Current File : //home/hotlineuser/mobius/hotline/account.go

package hotline

import (
	"encoding/binary"
	"fmt"
	"io"
	"slices"

	"golang.org/x/crypto/bcrypt"
)

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"`
	FileRoot string       `yaml:"FileRoot"`

	readOffset int // Internal offset to track read progress
}

func NewAccount(login, name, password string, access AccessBitmap) *Account {
	return &Account{
		Login:    login,
		Name:     name,
		Password: HashAndSalt([]byte(password)),
		Access:   access,
	}
}

// Read implements io.Reader interface for Account
func (a *Account) Read(p []byte) (int, 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 {
		b, err := io.ReadAll(&field)
		if err != nil {
			return 0, fmt.Errorf("error reading field: %w", err)
		}
		fieldBytes = append(fieldBytes, b...)
	}

	buf := slices.Concat(fieldCount, fieldBytes)
	return readFrom(p, &a.readOffset, buf)
}

// HashAndSalt generates a password hash from a users obfuscated plaintext password
func HashAndSalt(pwd []byte) string {
	hash, _ := bcrypt.GenerateFromPassword(pwd, bcrypt.MinCost)

	return string(hash)
}