chore(webui): rewrite the web session code again while preparing for csrf tokens
#60
This commit is contained in:
parent
3bb5e735c6
commit
895615ad6e
20 changed files with 162 additions and 149 deletions
|
@ -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 {
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
package model
|
||||
|
||||
type SettingsContextKey struct{}
|
||||
|
||||
type Settings struct {
|
||||
LightMode bool `json:"light_mode"`
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue