diff --git a/controllers/adminusers.go b/controllers/adminusers.go index 9ffb762..5870512 100644 --- a/controllers/adminusers.go +++ b/controllers/adminusers.go @@ -21,7 +21,7 @@ func AdminUsersList(c *core.Context) *core.Response { // initiate authority auth := new(utils.Authority) - var session = new(utils.SessionUser) + session := c.GetSession() // true if session is active hassession := session.Init(c) @@ -135,7 +135,7 @@ func AdminUsersAdd(c *core.Context) *core.Response { // initiate authority auth := new(utils.Authority) - var session = new(utils.SessionUser) + session := c.GetSession() // true if session is active hassession := session.Init(c) diff --git a/controllers/authentication.go b/controllers/authentication.go index 9e3eff9..500533d 100644 --- a/controllers/authentication.go +++ b/controllers/authentication.go @@ -19,7 +19,6 @@ import ( "git.smarteching.com/goffee/core" "git.smarteching.com/goffee/cup/events" "git.smarteching.com/goffee/cup/models" - "git.smarteching.com/goffee/cup/utils" "github.com/google/uuid" "gorm.io/gorm" ) @@ -101,7 +100,7 @@ func Signup(c *core.Context) *core.Response { // cache the token userAgent := c.GetUserAgent() - hashedCacheKey := utils.CreateAuthTokenHashedCacheKey(user.ID, userAgent) + hashedCacheKey := core.CreateAuthTokenHashedCacheKey(user.ID, userAgent) err = c.GetCache().Set(hashedCacheKey, token) if err != nil { c.GetLogger().Error(err.Error()) @@ -223,12 +222,12 @@ func Signin(c *core.Context) *core.Response { } // cache the token userAgent := c.GetUserAgent() - hashedCacheKey := utils.CreateAuthTokenHashedCacheKey(user.ID, userAgent) + hashedCacheKey := core.CreateAuthTokenHashedCacheKey(user.ID, userAgent) err = c.GetCache().Set(hashedCacheKey, token) // delete data from old sessions sessionKey := fmt.Sprintf("sess_%v", userAgent) - hashedSessionKey := utils.CreateAuthTokenHashedCacheKey(user.ID, sessionKey) + hashedSessionKey := core.CreateAuthTokenHashedCacheKey(user.ID, sessionKey) _ = c.GetCache().Delete(hashedSessionKey) if err != nil { @@ -463,7 +462,7 @@ func Signout(c *core.Context) *core.Response { })) } userAgent := c.GetUserAgent() - hashedCacheKey := utils.CreateAuthTokenHashedCacheKey(uint(c.CastToInt(payload["userID"])), userAgent) + hashedCacheKey := core.CreateAuthTokenHashedCacheKey(uint(c.CastToInt(payload["userID"])), userAgent) err = c.GetCache().Delete(hashedCacheKey) if err != nil { diff --git a/hooks/auth-check.go b/hooks/auth-check.go index 779cbda..1b3e1e7 100644 --- a/hooks/auth-check.go +++ b/hooks/auth-check.go @@ -9,7 +9,6 @@ import ( "git.smarteching.com/goffee/core" "git.smarteching.com/goffee/cup/models" - "git.smarteching.com/goffee/cup/utils" "gorm.io/gorm" ) @@ -31,7 +30,7 @@ var CheckSessionCookie core.Hook = func(c *core.Context) { } else { userAgent := c.GetUserAgent() - hashedCacheKey := utils.CreateAuthTokenHashedCacheKey(uint(c.CastToInt(payload["userID"])), userAgent) + hashedCacheKey := core.CreateAuthTokenHashedCacheKey(uint(c.CastToInt(payload["userID"])), userAgent) cachedToken, err := c.GetCache().Get(hashedCacheKey) if err != nil { @@ -102,7 +101,7 @@ var AuthCheck core.Hook = func(c *core.Context) { return } userAgent := c.GetUserAgent() - hashedCacheKey := utils.CreateAuthTokenHashedCacheKey(uint(c.CastToInt(payload["userID"])), userAgent) + hashedCacheKey := core.CreateAuthTokenHashedCacheKey(uint(c.CastToInt(payload["userID"])), userAgent) cachedToken, err := c.GetCache().Get(hashedCacheKey) if err != nil { diff --git a/utils/helpers.go b/utils/helpers.go index 8ca7dd8..7a2e922 100644 --- a/utils/helpers.go +++ b/utils/helpers.go @@ -6,8 +6,6 @@ package utils import ( - "crypto/md5" - "fmt" "log" "time" @@ -71,13 +69,7 @@ func CreateSeedData() { } } -// generate a hashed string to be used as key for caching auth jwt token -func CreateAuthTokenHashedCacheKey(userID uint, userAgent string) string { - cacheKey := fmt.Sprintf("userid:_%v_useragent:_%v_jwt_token", userID, userAgent) - hashedCacheKey := fmt.Sprintf("%v", fmt.Sprintf("%x", md5.Sum([]byte(cacheKey)))) - return hashedCacheKey -} func FormatUnix(value int64) string { return time.Unix(value, 0).Format("2006-01-02 15:04:05") diff --git a/utils/session.go b/utils/session.go deleted file mode 100644 index 27e202d..0000000 --- a/utils/session.go +++ /dev/null @@ -1,170 +0,0 @@ -// Copyright (c) 2024 Zeni Kim -// Use of this source code is governed by MIT-style -// license that can be found in the LICENSE file. - -package utils - -import ( - "errors" - "fmt" - "sync" - "time" - - "encoding/json" - - "git.smarteching.com/goffee/core" - "git.smarteching.com/goffee/cup/models" - "gorm.io/gorm" -) - -type SessionUser struct { - mu sync.RWMutex - context *core.Context - userID uint - hashedSessionKey string - authenticated bool - sessionStart time.Time - values map[string]interface{} -} - -// start the struct -func (s *SessionUser) Init(c *core.Context) bool { - - // check session cookie - pass := true - token := "" - s.context = c - - payload := make(map[string]interface{}) - // get cookie - usercookie, err := c.GetCookie() - if err != nil { - - } - - token = usercookie.Token - - if token == "" { - - pass = false - - } else { - - payload, err = c.GetJWT().DecodeToken(token) - - if err != nil { - - pass = false - - } else { - - userID := uint(c.CastToInt(payload["userID"])) - userAgent := c.GetUserAgent() - - // get data from redis - hashedCacheKey := CreateAuthTokenHashedCacheKey(userID, userAgent) - cachedToken, err := c.GetCache().Get(hashedCacheKey) - - if err != nil { - pass = false - } else if cachedToken != token { - pass = false - } else { - var user models.User - res := c.GetGorm().Where("id = ?", userID).First(&user) - if res.Error != nil && !errors.Is(res.Error, gorm.ErrRecordNotFound) { - pass = false - } - // if have session start the struct - if pass { - userAgent := c.GetUserAgent() - sessionKey := fmt.Sprintf("sess_%v", userAgent) - s.hashedSessionKey = CreateAuthTokenHashedCacheKey(userID, sessionKey) - - s.values = make(map[string]interface{}) - s.authenticated = true - s.userID = userID - value, _ := c.GetCache().Get(s.hashedSessionKey) - - if len(value) > 0 { - _ = json.Unmarshal([]byte(value), &s.values) - } - - return true - - } else { - - s.hashedSessionKey = "" - s.authenticated = false - s.userID = 0 - return false - } - } - } - } - - return false -} - -func (s *SessionUser) Set(key string, value interface{}) error { - s.mu.Lock() - s.values[key] = value - s.mu.Unlock() - return s.Save() -} - -func (s *SessionUser) Get(key string) (interface{}, bool) { - s.mu.RLock() - defer s.mu.RUnlock() - val, ok := s.values[key] - return val, ok -} - -func (s *SessionUser) Delete(key string) interface{} { - s.mu.RLock() - v, ok := s.values[key] - s.mu.RUnlock() - if ok { - s.mu.Lock() - delete(s.values, key) - s.mu.Unlock() - } - s.Save() - return v -} - -func (s *SessionUser) Flush() error { - s.mu.Lock() - s.context.GetCache().Delete(s.hashedSessionKey) - s.mu.Unlock() - return nil -} - -func (s *SessionUser) Save() error { - - var value string - - s.mu.RLock() - - if len(s.values) > 0 { - buf, err := json.Marshal(&s.values) - if err != nil { - s.mu.RUnlock() - return err - } - value = string(buf) - } - - if len(value) > 0 { - s.context.GetCache().Set(s.hashedSessionKey, value) - } else { - s.context.GetCache().Delete(s.hashedSessionKey) - } - s.mu.RUnlock() - return nil -} - -func (s *SessionUser) GetUserID() uint { - - return s.userID -}