tfstated/pkg/model/account.go
Julien Dessaux 895615ad6e
All checks were successful
main / main (push) Successful in 7m12s
main / publish (push) Has been skipped
main / deploy (push) Has been skipped
chore(webui): rewrite the web session code again while preparing for csrf tokens
#60
2025-04-30 22:31:25 +02:00

34 lines
956 B
Go

package model
import (
"crypto/subtle"
"time"
"git.adyxax.org/adyxax/tfstated/pkg/helpers"
"go.n16f.net/uuid"
)
type AccountContextKey struct{}
type Account struct {
Id uuid.UUID `json:"id"`
Username string `json:"username"`
Salt []byte `json:"salt"`
PasswordHash []byte `json:"password_hash"`
IsAdmin bool `json:"is_admin"`
Created time.Time `json:"created"`
LastLogin time.Time `json:"last_login"`
Settings *Settings `json:"settings"`
PasswordReset *uuid.UUID `json:"password_reset"`
}
func (account *Account) CheckPassword(password string) bool {
hash := helpers.HashPassword(password, account.Salt)
return subtle.ConstantTimeCompare(hash, account.PasswordHash) == 1
}
func (account *Account) SetPassword(password string) {
account.Salt = helpers.GenerateSalt()
account.PasswordHash = helpers.HashPassword(password, account.Salt)
account.PasswordReset = nil
}