From 00197eeacb639036999ea068a4101bf77d2f1fa5 Mon Sep 17 00:00:00 2001 From: Dylan Tinianov Date: Thu, 10 Aug 2023 12:24:06 -0400 Subject: [PATCH] update session --- auth/auth.go | 16 +++------------- controller/projects.go | 5 ----- middleware/sessions/sessions.go | 19 +------------------ 3 files changed, 4 insertions(+), 36 deletions(-) diff --git a/auth/auth.go b/auth/auth.go index 1d50960c..2c5845dd 100644 --- a/auth/auth.go +++ b/auth/auth.go @@ -25,6 +25,7 @@ import ( "github.com/dapperlabs/flow-playground-api/middleware/sessions" "github.com/dapperlabs/flow-playground-api/model" "github.com/dapperlabs/flow-playground-api/storage" + "github.com/getsentry/sentry-go" "github.com/google/uuid" "github.com/pkg/errors" ) @@ -68,7 +69,8 @@ func (a *Authenticator) GetOrCreateUser(ctx context.Context) (*model.User, error if session.Values[userIDKey] != nil { user, err = a.getCurrentUser(session.Values[userIDKey].(string)) if err != nil { - fmt.Printf("Failed to load user id %s from session\n", session.Values[userIDKey].(string)) + sentry.CaptureException(errors.New(fmt.Sprintf( + "Failed to load user id %s from session\n", session.Values[userIDKey].(string)))) } else { userLoaded = true } @@ -99,12 +101,9 @@ func (a *Authenticator) GetOrCreateUser(ctx context.Context) (*model.User, error // This function checks for access using both the new and legacy authentication schemes. If // a user has legacy access, their authentication is then migrated to use the new scheme. func (a *Authenticator) CheckProjectAccess(ctx context.Context, proj *model.Project) error { - fmt.Println("Check Project Access()") - session := sessions.Get(ctx, a.sessionName) if session.Values[userIDKey] == nil { - fmt.Println("No userIDKey in session") return errors.New("no userIdKey found in session") } @@ -113,19 +112,14 @@ func (a *Authenticator) CheckProjectAccess(ctx context.Context, proj *model.Proj return errors.New("access denied") } - fmt.Println("UserID:", user.ID) - if a.hasProjectAccess(user, proj) { err = sessions.Save(ctx, session) if err != nil { - fmt.Println("Failed to Save Session: ", err.Error()) return errors.New("access denied") } - fmt.Println("Check Project Access(): user has access") return nil } - fmt.Println("User does not have Project Access") if a.hasLegacyProjectAccess(ctx, proj) { user, err = a.migrateLegacyProjectAccess(user, proj) @@ -147,23 +141,19 @@ func (a *Authenticator) CheckProjectAccess(ctx context.Context, proj *model.Proj } func (a *Authenticator) getCurrentUser(userIDStr string) (*model.User, error) { - fmt.Println("getCurrentUser()") var user model.User var userID uuid.UUID err := userID.UnmarshalText([]byte(userIDStr)) if err != nil { - fmt.Println("failed to unmarshal userIDStr") return nil, errors.Wrap(err, "failed to unmarshal userIDStr") } err = a.store.GetUser(userID, &user) if err != nil { - fmt.Println("Failed to get user from db", err.Error()) return nil, errors.Wrap(err, "failed to get user from db") } - fmt.Println("Returning User") return &user, nil } diff --git a/controller/projects.go b/controller/projects.go index bf0368bb..6d0bee5c 100644 --- a/controller/projects.go +++ b/controller/projects.go @@ -138,22 +138,17 @@ func (p *Projects) Delete(id uuid.UUID) error { } func (p *Projects) Get(id uuid.UUID) (*model.Project, error) { - fmt.Println("Projects.Get()") err := p.store.ProjectAccessed(id) if err != nil { - fmt.Println("Projects.Get(): Failed to update accessed time:", err.Error()) return nil, errors.Wrap(err, "failed to update project accessed time") } - fmt.Println("Projects.Get(): Getting Project from Store") var proj model.Project err = p.store.GetProject(id, &proj) if err != nil { - fmt.Println("Failed to get project in projects.get():", err.Error()) return nil, errors.Wrap(err, "failed to get project") } - fmt.Println("Projects.Get(): Returning project") return &proj, nil } diff --git a/middleware/sessions/sessions.go b/middleware/sessions/sessions.go index 89d06745..03c81805 100644 --- a/middleware/sessions/sessions.go +++ b/middleware/sessions/sessions.go @@ -20,7 +20,6 @@ package sessions import ( "context" - "fmt" "net/http" "github.com/gorilla/sessions" @@ -40,17 +39,6 @@ func Middleware(store sessions.Store) func(http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { ctx := context.WithValue(r.Context(), sessionCtxKeySession, store) - fmt.Println("Cookies:", r.Cookies()) - if len(r.Cookies()) == 0 { - fmt.Println("COOKIES MISSING FROM REQUEST") - } - - session, err := store.Get(r, "flow-playground") - if err != nil { - fmt.Println(" error getting flow-playground session:", err.Error()) - } - - fmt.Println(" Session Values:", session.Values) r = r.WithContext(ctx) next.ServeHTTP(w, r) @@ -63,12 +51,7 @@ func Get(ctx context.Context, name string) *sessions.Session { store := ctx.Value(sessionCtxKeySession).(sessions.Store) // ignore error because a session is always returned even if one does not exist - session, err := store.Get(httpcontext.Request(ctx), name) - if err != nil { - fmt.Println("invalid cookie:", err.Error()) - } - - // TODO: No cached session is present causing no userIDKey to be there?? + session, _ := store.Get(httpcontext.Request(ctx), name) return session }