mirror of
https://github.com/lordmathis/lemma.git
synced 2025-11-06 07:54:22 +00:00
Implement auth package tests
This commit is contained in:
62
server/internal/context/context.go
Normal file
62
server/internal/context/context.go
Normal file
@@ -0,0 +1,62 @@
|
||||
// Package context provides functions for managing request context
|
||||
package context
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"novamd/internal/models"
|
||||
)
|
||||
|
||||
type contextKey string
|
||||
|
||||
const (
|
||||
// HandlerContextKey is the key used to store handler context in the request context
|
||||
HandlerContextKey contextKey = "handlerContext"
|
||||
)
|
||||
|
||||
// UserClaims represents user information from authentication
|
||||
type UserClaims struct {
|
||||
UserID int
|
||||
Role string
|
||||
}
|
||||
|
||||
// HandlerContext holds the request-specific data available to all handlers
|
||||
type HandlerContext struct {
|
||||
UserID int
|
||||
UserRole string
|
||||
Workspace *models.Workspace // Optional, only set for workspace routes
|
||||
}
|
||||
|
||||
// GetRequestContext retrieves the handler context from the request
|
||||
func GetRequestContext(w http.ResponseWriter, r *http.Request) (*HandlerContext, bool) {
|
||||
ctx := r.Context().Value(HandlerContextKey)
|
||||
if ctx == nil {
|
||||
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||
return nil, false
|
||||
}
|
||||
return ctx.(*HandlerContext), true
|
||||
}
|
||||
|
||||
// WithHandlerContext adds handler context to the request
|
||||
func WithHandlerContext(r *http.Request, hctx *HandlerContext) *http.Request {
|
||||
return r.WithContext(context.WithValue(r.Context(), HandlerContextKey, hctx))
|
||||
}
|
||||
|
||||
// GetUserFromContext retrieves user claims from the context
|
||||
func GetUserFromContext(ctx context.Context) (*UserClaims, error) {
|
||||
val := ctx.Value(HandlerContextKey)
|
||||
if val == nil {
|
||||
return nil, fmt.Errorf("no user found in context")
|
||||
}
|
||||
|
||||
hctx, ok := val.(*HandlerContext)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("invalid context type")
|
||||
}
|
||||
|
||||
return &UserClaims{
|
||||
UserID: hctx.UserID,
|
||||
Role: hctx.UserRole,
|
||||
}, nil
|
||||
}
|
||||
52
server/internal/context/middleware.go
Normal file
52
server/internal/context/middleware.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package context
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"novamd/internal/db"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
|
||||
// WithUserContextMiddleware extracts user information from JWT claims
|
||||
// and adds it to the request context
|
||||
func WithUserContextMiddleware(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
claims, err := GetUserFromContext(r.Context())
|
||||
if err != nil {
|
||||
http.Error(w, "Unauthorized", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
hctx := &HandlerContext{
|
||||
UserID: claims.UserID,
|
||||
UserRole: claims.Role,
|
||||
}
|
||||
|
||||
r = WithHandlerContext(r, hctx)
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
// WithWorkspaceContextMiddleware adds workspace information to the request context
|
||||
func WithWorkspaceContextMiddleware(db db.Database) func(http.Handler) http.Handler {
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx, ok := GetRequestContext(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
workspaceName := chi.URLParam(r, "workspaceName")
|
||||
workspace, err := db.GetWorkspaceByName(ctx.UserID, workspaceName)
|
||||
if err != nil {
|
||||
http.Error(w, "Workspace not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
// Update existing context with workspace
|
||||
ctx.Workspace = workspace
|
||||
r = WithHandlerContext(r, ctx)
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user