Refactor session loading management
Signed-off-by: gearnode <bryan@frimin.fr>
This commit is contained in:
@@ -1,21 +0,0 @@
|
|||||||
package console_v1
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/getprobo/probo/pkg/usrmgr"
|
|
||||||
"github.com/go-chi/chi/v5"
|
|
||||||
)
|
|
||||||
|
|
||||||
type (
|
|
||||||
RegisterRequest struct {
|
|
||||||
Email string `json:"email"`
|
|
||||||
Password string `json:"password"`
|
|
||||||
FullName string `json:"fullName"`
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
// RegisterAuthRoutes registers the authentication routes
|
|
||||||
func RegisterAuthRoutes(r chi.Router, usrmgrSvc *usrmgr.Service, authCfg AuthConfig) {
|
|
||||||
r.Post("/auth/register", SignUpHandler(usrmgrSvc, authCfg))
|
|
||||||
r.Post("/auth/login", SignInHandler(usrmgrSvc, authCfg))
|
|
||||||
r.Post("/auth/logout", SignOutHandler(usrmgrSvc, authCfg))
|
|
||||||
}
|
|
||||||
@@ -51,28 +51,18 @@ type (
|
|||||||
usrmgrSvc *usrmgr.Service
|
usrmgrSvc *usrmgr.Service
|
||||||
authCfg AuthConfig
|
authCfg AuthConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
contextKey string
|
|
||||||
|
|
||||||
httpContext struct {
|
|
||||||
ResponseWriter http.ResponseWriter
|
|
||||||
Request *http.Request
|
|
||||||
}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
var (
|
||||||
sessionContextKey contextKey = "session"
|
sessionContextKey = struct{}{}
|
||||||
userContextKey contextKey = "user"
|
userContextKey = struct{}{}
|
||||||
httpContextKey contextKey = "http"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// SessionFromContext retrieves the session from the context
|
|
||||||
func SessionFromContext(ctx context.Context) *coredata.Session {
|
func SessionFromContext(ctx context.Context) *coredata.Session {
|
||||||
session, _ := ctx.Value(sessionContextKey).(*coredata.Session)
|
session, _ := ctx.Value(sessionContextKey).(*coredata.Session)
|
||||||
return session
|
return session
|
||||||
}
|
}
|
||||||
|
|
||||||
// UserFromContext retrieves the user from the context
|
|
||||||
func UserFromContext(ctx context.Context) *coredata.User {
|
func UserFromContext(ctx context.Context) *coredata.User {
|
||||||
user, _ := ctx.Value(userContextKey).(*coredata.User)
|
user, _ := ctx.Value(userContextKey).(*coredata.User)
|
||||||
return user
|
return user
|
||||||
@@ -81,10 +71,10 @@ func UserFromContext(ctx context.Context) *coredata.User {
|
|||||||
func NewMux(proboSvc *probo.Service, usrmgrSvc *usrmgr.Service, authCfg AuthConfig) *chi.Mux {
|
func NewMux(proboSvc *probo.Service, usrmgrSvc *usrmgr.Service, authCfg AuthConfig) *chi.Mux {
|
||||||
r := chi.NewMux()
|
r := chi.NewMux()
|
||||||
|
|
||||||
// Register authentication routes
|
r.Post("/auth/register", SignUpHandler(usrmgrSvc, authCfg))
|
||||||
RegisterAuthRoutes(r, usrmgrSvc, authCfg)
|
r.Post("/auth/login", SignInHandler(usrmgrSvc, authCfg))
|
||||||
|
r.Delete("/auth/logout", SignOutHandler(usrmgrSvc, authCfg))
|
||||||
|
|
||||||
// GraphQL playground and query endpoint
|
|
||||||
r.Get("/", playground.Handler("GraphQL", "/console/v1/query"))
|
r.Get("/", playground.Handler("GraphQL", "/console/v1/query"))
|
||||||
r.Post("/query", graphqlHandler(proboSvc, usrmgrSvc, authCfg))
|
r.Post("/query", graphqlHandler(proboSvc, usrmgrSvc, authCfg))
|
||||||
|
|
||||||
@@ -111,14 +101,11 @@ func graphqlHandler(proboSvc *probo.Service, usrmgrSvc *usrmgr.Service, authCfg
|
|||||||
})
|
})
|
||||||
srv.Use(extension.Introspection{})
|
srv.Use(extension.Introspection{})
|
||||||
|
|
||||||
// Add operation middleware for authentication
|
|
||||||
srv.AroundOperations(func(ctx context.Context, next graphql.OperationHandler) graphql.ResponseHandler {
|
srv.AroundOperations(func(ctx context.Context, next graphql.OperationHandler) graphql.ResponseHandler {
|
||||||
// Skip authentication for introspection queries
|
|
||||||
if op := graphql.GetOperationContext(ctx); op.OperationName == "IntrospectionQuery" {
|
if op := graphql.GetOperationContext(ctx); op.OperationName == "IntrospectionQuery" {
|
||||||
return next(ctx)
|
return next(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the user from context
|
|
||||||
user := UserFromContext(ctx)
|
user := UserFromContext(ctx)
|
||||||
if user == nil {
|
if user == nil {
|
||||||
return func(ctx context.Context) *graphql.Response {
|
return func(ctx context.Context) *graphql.Response {
|
||||||
@@ -128,17 +115,11 @@ func graphqlHandler(proboSvc *probo.Service, usrmgrSvc *usrmgr.Service, authCfg
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Continue with the operation
|
|
||||||
return next(ctx)
|
return next(ctx)
|
||||||
})
|
})
|
||||||
|
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
// Create HTTP context
|
ctx := r.Context()
|
||||||
httpCtx := &httpContext{
|
|
||||||
ResponseWriter: w,
|
|
||||||
Request: r,
|
|
||||||
}
|
|
||||||
ctx := context.WithValue(r.Context(), httpContextKey, httpCtx)
|
|
||||||
|
|
||||||
cookieValue, err := securecookie.Get(r, securecookie.DefaultConfig(
|
cookieValue, err := securecookie.Get(r, securecookie.DefaultConfig(
|
||||||
authCfg.CookieName,
|
authCfg.CookieName,
|
||||||
@@ -148,31 +129,52 @@ func graphqlHandler(proboSvc *probo.Service, usrmgrSvc *usrmgr.Service, authCfg
|
|||||||
if !errors.Is(err, securecookie.ErrCookieNotFound) {
|
if !errors.Is(err, securecookie.ErrCookieNotFound) {
|
||||||
panic(fmt.Errorf("failed to get session: %w", err))
|
panic(fmt.Errorf("failed to get session: %w", err))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
srv.ServeHTTP(w, r)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
sessionID, err := gid.ParseGID(cookieValue)
|
sessionID, err := gid.ParseGID(cookieValue)
|
||||||
if err == nil {
|
if err != nil {
|
||||||
// Get the session
|
securecookie.Clear(w, securecookie.DefaultConfig(
|
||||||
session, err := usrmgrSvc.GetSession(r.Context(), sessionID)
|
authCfg.CookieName,
|
||||||
if err == nil {
|
authCfg.CookieSecret,
|
||||||
// Add session to context
|
))
|
||||||
ctx = context.WithValue(ctx, sessionContextKey, session)
|
|
||||||
|
|
||||||
// Get the user
|
srv.ServeHTTP(w, r)
|
||||||
user, err := usrmgrSvc.GetUserBySession(r.Context(), sessionID)
|
return
|
||||||
if err == nil {
|
|
||||||
// Add user to context
|
|
||||||
ctx = context.WithValue(ctx, userContextKey, user)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
session, err := usrmgrSvc.GetSession(ctx, sessionID)
|
||||||
|
if err != nil {
|
||||||
|
securecookie.Clear(w, securecookie.DefaultConfig(
|
||||||
|
authCfg.CookieName,
|
||||||
|
authCfg.CookieSecret,
|
||||||
|
))
|
||||||
|
|
||||||
|
srv.ServeHTTP(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
user, err := usrmgrSvc.GetUserBySession(ctx, sessionID)
|
||||||
|
if err != nil {
|
||||||
|
securecookie.Clear(w, securecookie.DefaultConfig(
|
||||||
|
authCfg.CookieName,
|
||||||
|
authCfg.CookieSecret,
|
||||||
|
))
|
||||||
|
|
||||||
|
srv.ServeHTTP(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx = context.WithValue(ctx, sessionContextKey, session)
|
||||||
|
ctx = context.WithValue(ctx, userContextKey, user)
|
||||||
|
|
||||||
srv.ServeHTTP(w, r.WithContext(ctx))
|
srv.ServeHTTP(w, r.WithContext(ctx))
|
||||||
|
|
||||||
if session := SessionFromContext(r.Context()); session != nil {
|
if err := usrmgrSvc.UpdateSession(r.Context(), session); err != nil {
|
||||||
if err := usrmgrSvc.UpdateSession(r.Context(), session); err != nil {
|
panic(fmt.Errorf("failed to update session: %w", err))
|
||||||
panic(fmt.Errorf("failed to update session: %w", err))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user