Skip to content

Commit

Permalink
update session
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanTinianov committed Aug 10, 2023
1 parent a232c15 commit 00197ee
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 36 deletions.
16 changes: 3 additions & 13 deletions auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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")
}

Expand All @@ -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)
Expand All @@ -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
}

Expand Down
5 changes: 0 additions & 5 deletions controller/projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
19 changes: 1 addition & 18 deletions middleware/sessions/sessions.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package sessions

import (
"context"
"fmt"
"net/http"

"github.com/gorilla/sessions"
Expand All @@ -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)
Expand All @@ -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
}
Expand Down

0 comments on commit 00197ee

Please sign in to comment.