fix session leak
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user