Move utils into shared folder

This commit is contained in:
Melody Becker 2025-04-05 12:21:44 +02:00
parent 500bf48295
commit c25f27e82e
Signed by: mstar
SSH key fingerprint: SHA256:vkXfS9FG2pVNVfvDrzd1VW9n8VJzqqdKQGljxxX8uK8
22 changed files with 111 additions and 137 deletions

View file

@ -8,12 +8,12 @@ import (
"strings"
"time"
"git.mstar.dev/mstar/linstrom/ap"
"git.mstar.dev/mstar/linstrom/shared"
"github.com/go-webauthn/webauthn/webauthn"
"github.com/google/uuid"
"github.com/mstarongithub/passkey"
"github.com/rs/zerolog/log"
"git.mstar.dev/mstar/linstrom/ap"
"git.mstar.dev/mstar/linstrom/util"
"gorm.io/gorm"
)
@ -102,7 +102,7 @@ type RemoteAccountLinks struct {
// Find an account in the db using a given full handle (@max@example.com)
// Returns an account and nil if an account is found, otherwise nil and the error
func (s *Storage) FindAccountByFullHandle(handle string) (*Account, error) {
defer util.Untrace(util.Trace(&log.Logger))
defer shared.Untrace(shared.Trace(&log.Logger))
log.Debug().Str("account-handle", handle).Msg("Looking for account by handle")
log.Debug().Str("account-handle", handle).Msg("Checking if there's a cache hit")
@ -155,7 +155,7 @@ func (s *Storage) FindAccountByFullHandle(handle string) (*Account, error) {
// Find an account given a specific ID
func (s *Storage) FindAccountById(id string) (*Account, error) {
defer util.Untrace(util.Trace(&log.Logger))
defer shared.Untrace(shared.Trace(&log.Logger))
log.Debug().Str("account-id", id).Msg("Looking for account by id")
log.Debug().Str("account-id", id).Msg("First trying to hit cache")
acc, err := s.cacheAccIdToData(id)
@ -187,7 +187,7 @@ func (s *Storage) FindAccountById(id string) (*Account, error) {
}
func (s *Storage) FindLocalAccountByUsername(username string) (*Account, error) {
defer util.Untrace(util.Trace(&log.Logger))
defer shared.Untrace(shared.Trace(&log.Logger))
log.Debug().Str("account-username", username).Msg("Looking for local account")
log.Debug().Str("account-username", username).Msg("Checking cache first")
@ -238,7 +238,7 @@ func (s *Storage) FindLocalAccountByUsername(username string) (*Account, error)
}
func (s *Storage) FindAccountByPasskeyId(pkeyId []byte) (*Account, error) {
defer util.Untrace(util.Trace(&log.Logger))
defer shared.Untrace(shared.Trace(&log.Logger))
log.Debug().Bytes("account-passkey-id", pkeyId).Msg("Looking for account")
log.Debug().Bytes("account-passkey-id", pkeyId).Msg("Checking cache first")
@ -290,7 +290,7 @@ func (s *Storage) FindAccountByPasskeyId(pkeyId []byte) (*Account, error) {
// Update a given account in storage and cache
func (s *Storage) UpdateAccount(acc *Account) error {
defer util.Untrace(util.Trace(&log.Logger))
defer shared.Untrace(shared.Trace(&log.Logger))
// If the account is nil or doesn't have an id, error out
if acc == nil || acc.ID == "" {
return ErrInvalidData
@ -307,7 +307,7 @@ func (s *Storage) UpdateAccount(acc *Account) error {
// Create a new empty account for future use
func (s *Storage) NewEmptyAccount() (*Account, error) {
defer util.Untrace(util.Trace(&log.Logger))
defer shared.Untrace(shared.Trace(&log.Logger))
log.Debug().Msg("Creating new empty account")
acc := Account{}
// Generate the 64 bit id for passkey and webauthn stuff
@ -353,7 +353,7 @@ func (s *Storage) NewEmptyAccount() (*Account, error) {
// The handle in this case is only the part before the domain (example: @bob@example.com would have a handle of bob)
// It also sets up a bunch of values that tend to be obvious for local accounts
func (s *Storage) NewLocalAccount(handle string) (*Account, error) {
defer util.Untrace(util.Trace(&log.Logger))
defer shared.Untrace(shared.Trace(&log.Logger))
log.Trace().Caller().Send()
log.Debug().Str("account-handle", handle).Msg("Creating new local account")
acc, err := s.NewEmptyAccount()
@ -391,7 +391,7 @@ func (s *Storage) NewLocalAccount(handle string) (*Account, error) {
}
func (s *Storage) DeleteAccount(accId string) error {
defer util.Untrace(util.Trace(&log.Logger))
defer shared.Untrace(shared.Trace(&log.Logger))
return s.db.Delete(&Account{ID: accId}).Error
}
@ -399,27 +399,27 @@ func (s *Storage) DeleteAccount(accId string) error {
// Implements the webauthn.User interface for interaction with passkeys
func (a *Account) WebAuthnID() []byte {
defer util.Untrace(util.Trace(&log.Logger))
defer shared.Untrace(shared.Trace(&log.Logger))
return a.WebAuthnId
}
func (u *Account) WebAuthnName() string {
defer util.Untrace(util.Trace(&log.Logger))
defer shared.Untrace(shared.Trace(&log.Logger))
return u.Username
}
func (u *Account) WebAuthnDisplayName() string {
defer util.Untrace(util.Trace(&log.Logger))
defer shared.Untrace(shared.Trace(&log.Logger))
return u.DisplayName
}
func (u *Account) WebAuthnCredentials() []webauthn.Credential {
defer util.Untrace(util.Trace(&log.Logger))
defer shared.Untrace(shared.Trace(&log.Logger))
return u.PasskeyCredentials
}
func (u *Account) WebAuthnIcon() string {
defer util.Untrace(util.Trace(&log.Logger))
defer shared.Untrace(shared.Trace(&log.Logger))
return ""
}
@ -427,7 +427,7 @@ func (u *Account) WebAuthnIcon() string {
// Implements the passkey.User interface
func (u *Account) PutCredential(new webauthn.Credential) {
defer util.Untrace(util.Trace(&log.Logger))
defer shared.Untrace(shared.Trace(&log.Logger))
u.PasskeyCredentials = append(u.PasskeyCredentials, new)
}
@ -435,7 +435,7 @@ func (u *Account) PutCredential(new webauthn.Credential) {
// Implements the passkey.UserStore interface
func (s *Storage) GetOrCreateUser(userID string) passkey.User {
defer util.Untrace(util.Trace(&log.Logger))
defer shared.Untrace(shared.Trace(&log.Logger))
log.Debug().
Str("account-handle", userID).
Msg("Looking for or creating account for passkey stuff")
@ -458,7 +458,7 @@ func (s *Storage) GetOrCreateUser(userID string) passkey.User {
}
func (s *Storage) GetUserByWebAuthnId(id []byte) passkey.User {
defer util.Untrace(util.Trace(&log.Logger))
defer shared.Untrace(shared.Trace(&log.Logger))
log.Debug().Bytes("webauthn-id", id).Msg("Looking for account with webauthn id")
acc := Account{}
res := s.db.Where(Account{WebAuthnId: id}).First(&acc)
@ -474,7 +474,7 @@ func (s *Storage) GetUserByWebAuthnId(id []byte) passkey.User {
}
func (s *Storage) SaveUser(rawUser passkey.User) {
defer util.Untrace(util.Trace(&log.Logger))
defer shared.Untrace(shared.Trace(&log.Logger))
user, ok := rawUser.(*Account)
if !ok {
log.Error().Any("raw-user", rawUser).Msg("Failed to cast raw user to db account")