summaryrefslogtreecommitdiff
path: root/pkg/model/account.go
blob: 4336dfa976ba3fe26438d936392d9ae0d31e1e5f (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
package model

import (
	"crypto/sha256"
	"crypto/subtle"
	"time"

	"git.adyxax.org/adyxax/tfstated/pkg/scrypto"
	"golang.org/x/crypto/pbkdf2"
)

const (
	PBKDF2Iterations = 600000
	SaltSize         = 32
)

type AccountContextKey struct{}

type Account struct {
	Id           int
	Username     string
	Salt         []byte
	PasswordHash []byte
	IsAdmin      bool
	Created      time.Time
	LastLogin    time.Time
	Settings     any
}

func (account *Account) CheckPassword(password string) bool {
	hash := HashPassword(password, account.Salt)
	return subtle.ConstantTimeCompare(hash, account.PasswordHash) == 1
}

func GenerateSalt() []byte {
	return scrypto.RandomBytes(SaltSize)
}

func HashPassword(password string, salt []byte) []byte {
	return pbkdf2.Key([]byte(password), salt, PBKDF2Iterations, 32, sha256.New)
}