chore(webui): rewrite the web session code again while preparing for csrf tokens
All checks were successful
main / main (push) Successful in 7m12s
main / publish (push) Has been skipped
main / deploy (push) Has been skipped

#60
This commit is contained in:
Julien Dessaux 2025-04-30 22:31:25 +02:00
parent 3bb5e735c6
commit 895615ad6e
Signed by: adyxax
GPG key ID: F92E51B86E07177E
20 changed files with 162 additions and 149 deletions

View file

@ -2,7 +2,6 @@ package model
import (
"crypto/subtle"
"encoding/json"
"time"
"git.adyxax.org/adyxax/tfstated/pkg/helpers"
@ -12,15 +11,15 @@ import (
type AccountContextKey struct{}
type Account struct {
Id uuid.UUID
Username string
Salt []byte
PasswordHash []byte
IsAdmin bool
Created time.Time
LastLogin time.Time
Settings json.RawMessage
PasswordReset *uuid.UUID
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 {

View file

@ -1,20 +1,40 @@
package model
import (
"encoding/json"
"fmt"
"time"
"go.n16f.net/uuid"
)
type SessionData struct {
Account *Account `json:"account"`
CsrfToken uuid.UUID `json:"csrf_token"`
Settings *Settings `json:"settings"`
}
func NewSessionData(account *Account, previousSessionSettings *Settings) (*SessionData, error) {
data := SessionData{Account: account}
if err := data.CsrfToken.Generate(uuid.V4); err != nil {
return nil, fmt.Errorf("failed to generate csrf token uuid: %w", err)
}
if account != nil {
data.Settings = account.Settings
} else if previousSessionSettings != nil {
data.Settings = previousSessionSettings
} else {
data.Settings = &Settings{}
}
return &data, nil
}
type SessionContextKey struct{}
type Session struct {
Id []byte
AccountId *uuid.UUID
Created time.Time
Updated time.Time
Settings json.RawMessage
Id []byte
Created time.Time
Updated time.Time
Data *SessionData
}
func (session *Session) IsExpired() bool {

View file

@ -1,7 +1,5 @@
package model
type SettingsContextKey struct{}
type Settings struct {
LightMode bool `json:"light_mode"`
}