package middleware import ( "context" "net/http" "strings" "git.kundeng.us/phoenix/textsender-auth/internal/services" ) type contextKey string const ( UserContextKey contextKey = "user" ) func AuthMiddleware(authService *services.JWTService) func(http.Handler) http.Handler { return func(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { authHeader := r.Header.Get("Authorization") if authHeader == "" { http.Error(w, "Authorization header required", http.StatusUnauthorized) return } // Extract token from "Bearer " parts := strings.Split(authHeader, " ") if len(parts) != 2 || parts[0] != "Bearer" { http.Error(w, "Invalid authorization header format", http.StatusUnauthorized) return } token := parts[1] // Validate token with auth service user, err := authService.ValidateToken(token) if err != nil { http.Error(w, "Invalid token", http.StatusUnauthorized) return } // Add user to context ctx := context.WithValue(r.Context(), UserContextKey, user) next.ServeHTTP(w, r.WithContext(ctx)) }) } }