From c81aa00b8d89b7d68317f2a7418d435170245d90 Mon Sep 17 00:00:00 2001 From: Donovan Date: Sun, 20 Oct 2024 14:43:54 -0500 Subject: [PATCH] fix session leak --- core/email_queue.go | 8 +++--- core/nkode_api.go | 62 +++++++++++++++++++++++++++------------------ core/type.go | 6 +++++ 3 files changed, 48 insertions(+), 28 deletions(-) diff --git a/core/email_queue.go b/core/email_queue.go index 76a2765..429bf58 100644 --- a/core/email_queue.go +++ b/core/email_queue.go @@ -39,13 +39,13 @@ type SESClient struct { } const ( - defaultExpiration = 5 * time.Minute - cleanupInterval = 10 * time.Minute + emailRetryExpiration = 5 * time.Minute + sesCleanupInterval = 10 * time.Minute ) func NewSESClient() SESClient { return SESClient{ - ResetCache: cache.New(defaultExpiration, cleanupInterval), + ResetCache: cache.New(emailRetryExpiration, sesCleanupInterval), } } @@ -84,7 +84,7 @@ func (s *SESClient) SendEmail(email Email) error { Source: aws.String(email.Sender), } - if err = s.ResetCache.Add(email.Recipient, nil, defaultExpiration); err != nil { + if err = s.ResetCache.Add(email.Recipient, nil, emailRetryExpiration); err != nil { return err } diff --git a/core/nkode_api.go b/core/nkode_api.go index f43fcf8..faa1363 100644 --- a/core/nkode_api.go +++ b/core/nkode_api.go @@ -3,21 +3,28 @@ package core import ( "fmt" "github.com/google/uuid" + "github.com/patrickmn/go-cache" "log" "os" + "time" +) + +const ( + sessionExpiration = 5 * time.Minute + sessionCleanupInterval = 10 * time.Minute ) type NKodeAPI struct { - Db DbAccessor - SignupSessions map[SessionId]UserSignSession - EmailQueue *EmailQueue + Db DbAccessor + SignupSessionCache *cache.Cache + EmailQueue *EmailQueue } func NewNKodeAPI(db DbAccessor, queue *EmailQueue) NKodeAPI { return NKodeAPI{ - Db: db, - SignupSessions: make(map[SessionId]UserSignSession), - EmailQueue: queue, + Db: db, + EmailQueue: queue, + SignupSessionCache: cache.New(sessionExpiration, sessionCleanupInterval), } } @@ -54,8 +61,12 @@ func (n *NKodeAPI) GenerateSignupResetInterface(userEmail UserEmail, customerId if err != nil { return nil, err } - n.SignupSessions[signupSession.Id] = *signupSession + //n.SignupSessions[signupSession.Id] = *signupSession + if err := n.SignupSessionCache.Add(signupSession.Id.String(), *signupSession, sessionExpiration); err != nil { + return nil, err + } svgInterface, err := n.Db.GetSvgStringInterface(signupSession.LoginUserInterface.SvgId) + if err != nil { return nil, err } @@ -74,47 +85,57 @@ func (n *NKodeAPI) SetNKode(customerId CustomerId, sessionId SessionId, keySelec if err != nil { return nil, err } - session, exists := n.SignupSessions[sessionId] + session, exists := n.SignupSessionCache.Get(sessionId.String()) if !exists { log.Printf("session id does not exist %s", sessionId) return nil, ErrSignupSessionDNE } - confirmInterface, err := session.SetUserNKode(keySelection) + userSession, ok := session.(UserSignSession) + if !ok { + // handle the case where the type assertion fails + return nil, ErrSignupSessionDNE + } + confirmInterface, err := userSession.SetUserNKode(keySelection) if err != nil { return nil, err } - n.SignupSessions[sessionId] = session + n.SignupSessionCache.Set(sessionId.String(), userSession, sessionExpiration) return confirmInterface, nil } func (n *NKodeAPI) ConfirmNKode(customerId CustomerId, sessionId SessionId, keySelection KeySelection) error { - session, exists := n.SignupSessions[sessionId] + session, exists := n.SignupSessionCache.Get(sessionId.String()) if !exists { log.Printf("session id does not exist %s", sessionId) return ErrSignupSessionDNE } + userSession, ok := session.(UserSignSession) + if !ok { + // handle the case where the type assertion fails + return ErrSignupSessionDNE + } customer, err := n.Db.GetCustomer(customerId) if err != nil { return err } - passcode, err := session.DeducePasscode(keySelection) + passcode, err := userSession.DeducePasscode(keySelection) if err != nil { return err } - err = customer.IsValidNKode(session.Kp, passcode) + err = customer.IsValidNKode(userSession.Kp, passcode) if err != nil { return err } - user, err := NewUser(*customer, string(session.UserEmail), passcode, session.LoginUserInterface, session.Kp) + user, err := NewUser(*customer, string(userSession.UserEmail), passcode, userSession.LoginUserInterface, userSession.Kp) if err != nil { return err } - if session.Reset { + if userSession.Reset { err = n.Db.UpdateUserNKode(*user) } else { err = n.Db.WriteNewUser(*user) } - delete(n.SignupSessions, session.Id) + n.SignupSessionCache.Delete(userSession.Id.String()) return err } @@ -220,6 +241,7 @@ func (n *NKodeAPI) ResetNKode(userEmail UserEmail, customerId CustomerId) error if err != nil { return fmt.Errorf("error getting user in rest nkode %v", err) } + if user == nil { return nil } @@ -242,11 +264,3 @@ func (n *NKodeAPI) ResetNKode(userEmail UserEmail, customerId CustomerId) error n.EmailQueue.AddEmail(email) return nil } - -func getColors(colorIds []int) []RGBColor { - colors := make([]RGBColor, len(colorIds)) - for idx, colorIdx := range colorIds { - colors[idx] = SetColors[colorIdx] - } - return colors -} diff --git a/core/type.go b/core/type.go index f00d0ee..5f20d22 100644 --- a/core/type.go +++ b/core/type.go @@ -98,6 +98,12 @@ func CustomerIdToString(customerId CustomerId) string { type SessionId uuid.UUID type UserId uuid.UUID +func (s *SessionId) String() string { + id := uuid.UUID(*s) + return id.String() + +} + type UserEmail string func ParseEmail(email string) (UserEmail, error) {