From 56f53a2a6f85876485e2ae67d48b78b448caed6e Mon Sep 17 00:00:00 2001 From: tobi <31960611+tsmethurst@users.noreply.github.com> Date: Mon, 3 Oct 2022 10:46:11 +0200 Subject: [performance] add user cache and database (#879) * go fmt * add + use user cache and database * fix import * update tests * remove unused relation --- internal/api/security/tokencheck.go | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) (limited to 'internal/api/security/tokencheck.go') diff --git a/internal/api/security/tokencheck.go b/internal/api/security/tokencheck.go index 3df7ee943..9f2b7f36e 100644 --- a/internal/api/security/tokencheck.go +++ b/internal/api/security/tokencheck.go @@ -52,8 +52,8 @@ func (m *Module) TokenCheck(c *gin.Context) { log.Tracef("authenticated user %s with bearer token, scope is %s", userID, ti.GetScope()) // fetch user for this token - user := >smodel.User{} - if err := m.db.GetByID(ctx, userID, user); err != nil { + user, err := m.db.GetUserByID(ctx, userID) + if err != nil { if err != db.ErrNoEntries { log.Errorf("database error looking for user with id %s: %s", userID, err) return @@ -80,22 +80,25 @@ func (m *Module) TokenCheck(c *gin.Context) { c.Set(oauth.SessionAuthorizedUser, user) // fetch account for this token - acct, err := m.db.GetAccountByID(ctx, user.AccountID) - if err != nil { - if err != db.ErrNoEntries { - log.Errorf("database error looking for account with id %s: %s", user.AccountID, err) + if user.Account == nil { + acct, err := m.db.GetAccountByID(ctx, user.AccountID) + if err != nil { + if err != db.ErrNoEntries { + log.Errorf("database error looking for account with id %s: %s", user.AccountID, err) + return + } + log.Warnf("no account found for userID %s", userID) return } - log.Warnf("no account found for userID %s", userID) - return + user.Account = acct } - if !acct.SuspendedAt.IsZero() { + if !user.Account.SuspendedAt.IsZero() { log.Warnf("authenticated user %s's account (accountId=%s) has been suspended", userID, user.AccountID) return } - c.Set(oauth.SessionAuthorizedAccount, acct) + c.Set(oauth.SessionAuthorizedAccount, user.Account) } // check for application token -- cgit v1.2.3