From 11e00596310da55833ee7bc7f9771ba79091b27e Mon Sep 17 00:00:00 2001 From: mstar Date: Fri, 4 Apr 2025 13:46:11 +0200 Subject: [PATCH] Add first helper method to user --- auth-new/helpers.go | 2 +- auth-new/passkey.go | 6 +++--- auth-new/password.go | 4 ++-- auth-new/totp.go | 2 +- cmd/model-gen/main.go | 1 + storage-new/models/User.go | 8 ++++++++ 6 files changed, 16 insertions(+), 7 deletions(-) diff --git a/auth-new/helpers.go b/auth-new/helpers.go index f3b1ea5..a1029ed 100644 --- a/auth-new/helpers.go +++ b/auth-new/helpers.go @@ -177,7 +177,7 @@ func oneStorageAuthToLoginState(in models.AuthenticationMethodType) LoginNextSta // // TODO: Decide whether to include the reason for disallowed login func (a *Authenticator) canUsernameLogin(username string) (bool, error) { - acc, err := dbgen.User.Where(dbgen.User.Username.Eq(username)).First() + acc, err := dbgen.User.GetByUsername(username) if err != nil { return false, err } diff --git a/auth-new/passkey.go b/auth-new/passkey.go index 397d76c..fe3e520 100644 --- a/auth-new/passkey.go +++ b/auth-new/passkey.go @@ -31,7 +31,7 @@ func (a *Authenticator) StartPasskeyLogin( if ok, err := a.canUsernameLogin(username); !ok { return nil, "", other.Error("auth", "user may not login", err) } - acc, err := dbgen.User.Where(dbgen.User.Username.Eq(username)).First() + acc, err := dbgen.User.GetByUsername(username) if err != nil { return nil, "", other.Error("auth", "failed to acquire user for login", err) } @@ -64,7 +64,7 @@ func (a *Authenticator) CompletePasskeyLogin( response *http.Request, ) (accessToken string, err error) { // Get user in question - acc, err := dbgen.User.Where(dbgen.User.Username.Eq(username)).First() + acc, err := dbgen.User.GetByUsername(username) if err != nil { return "", other.Error("auth", "failed to get user for passkey login completion", err) } @@ -140,7 +140,7 @@ func (a *Authenticator) StartPasskeyRegistration( if ok, err := a.canUsernameLogin(username); !ok { return nil, "", other.Error("auth", "user may not login", err) } - acc, err := dbgen.User.Where(dbgen.User.Username.Eq(username)).First() + acc, err := dbgen.User.GetByUsername(username) if err != nil { return nil, "", other.Error("auth", "failed to acquire user for login", err) } diff --git a/auth-new/password.go b/auth-new/password.go index 077eecb..09d7d18 100644 --- a/auth-new/password.go +++ b/auth-new/password.go @@ -24,7 +24,7 @@ func (a *Authenticator) PerformPasswordLogin( if ok, err := a.canUsernameLogin(username); !ok { return LoginNextFailure, "", other.Error("auth", "user may not login", err) } - acc, err := dbgen.User.Where(dbgen.User.Username.Eq(username)).First() + acc, err := dbgen.User.GetByUsername(username) switch err { case nil: break @@ -110,7 +110,7 @@ func (a *Authenticator) PerformPasswordLogin( // If there is no password set yet (i.e. during account registration or passkey only so far) // it creates the password link func (a *Authenticator) PerformPasswordRegister(username, password string) error { - acc, err := dbgen.User.Where(dbgen.User.Username.Eq(username)).First() + acc, err := dbgen.User.GetByUsername(username) if err != nil { return other.Error("auth", "failed to get user to add a password to", err) } diff --git a/auth-new/totp.go b/auth-new/totp.go index bcb5f58..cfee11b 100644 --- a/auth-new/totp.go +++ b/auth-new/totp.go @@ -129,7 +129,7 @@ func (a *Authenticator) StartTotpRegistration( if ok, err := a.canUsernameLogin(username); !ok { return nil, other.Error("auth", "user may not login", err) } - acc, err := dbgen.User.Where(dbgen.User.Username.Eq(username)).First() + acc, err := dbgen.User.GetByUsername(username) if err != nil { return nil, other.Error("auth", "failed to find account", err) } diff --git a/cmd/model-gen/main.go b/cmd/model-gen/main.go index daefa96..1c1cfa2 100644 --- a/cmd/model-gen/main.go +++ b/cmd/model-gen/main.go @@ -53,6 +53,7 @@ func main() { log.Info().Msg("Basic operations applied, applying extra features") g.ApplyInterface(func(models.INotification) {}, models.Notification{}) + g.ApplyInterface(func(models.IUser) {}, models.User{}) log.Info().Msg("Extra features applied, starting generation") g.Execute() diff --git a/storage-new/models/User.go b/storage-new/models/User.go index 9e8bf70..e1ea2bd 100644 --- a/storage-new/models/User.go +++ b/storage-new/models/User.go @@ -4,6 +4,7 @@ import ( "database/sql" "time" + "gorm.io/gen" "gorm.io/gorm" ) @@ -76,3 +77,10 @@ type User struct { RemoteInfo *UserRemoteLinks AuthMethods []UserAuthMethod } + +type IUser interface { + // Get a user by a username + // + // SELECT * FROM @@table WHERE username = @username LIMIT 1 + GetByUsername(username string) (*gen.T, error) +}