diff --git a/cmd/cognito_test/cognito.auth.harness/main.go b/cmd/cognito_test/cognito.auth.harness/main.go index babb3ed9..feb67d4c 100644 --- a/cmd/cognito_test/cognito.auth.harness/main.go +++ b/cmd/cognito_test/cognito.auth.harness/main.go @@ -1,19 +1,202 @@ package main import ( + "context" + "fmt" "log/slog" "net/http" "os" + "strings" "time" "queryorchestration/internal/serviceconfig/auth" "github.com/labstack/echo/v4" "github.com/labstack/echo/v4/middleware" + "github.com/lestrrat-go/jwx/v2/jwt" "queryorchestration/internal/cognitoauth" // Replace with your actual package import path ) +var endpointsList = []string{ + // Auth Service Endpoints + "/login", + "/login-callback", + "/logout", + "/home", + + // Query API Endpoints + "/query", + + // Document Service Endpoints + "/settings", + + // Export Service Endpoints + "/export", +} + +// HomeHandlerFunc returns the HomeHandler as an echo.HandlerFunc +func HomeHandlerFunc() echo.HandlerFunc { + return HomeHandler +} + +// homeHandler renders a simple HTML page with links to all endpoints +// and displays authentication status based on JWT token +func HomeHandler(c echo.Context) error { + var linksHTML string + baseURL := "http://localhost:8080" + + // Create list items for each endpoint + for _, endpoint := range endpointsList { + // Skip endpoints with path parameters for direct linking + if strings.Contains(endpoint, ":") { + displayPath := strings.Replace(endpoint, ":id", "{id}", -1) + linksHTML += fmt.Sprintf("
  • %s (requires parameter)
  • \n", displayPath) + } else { + linksHTML += fmt.Sprintf("
  • %s
  • \n", baseURL, endpoint, endpoint) + } + } + + // Check if user is authenticated by looking for token in cookie + tokenCookie, err := c.Cookie("auth_token") + isAuthenticated := (err == nil && tokenCookie.Value != "") + + // Variables for user info + username := "" + email := "" + var userGroups []string + + // If authenticated, try to decode the JWT token to get user info + if isAuthenticated { + // Parse the JWT token without verification (just to extract info for display) + // In a production application, you would want to verify the token here as well + token, _ := jwt.Parse([]byte(tokenCookie.Value), jwt.WithVerify(false)) + if token != nil { + claims, _ := token.AsMap(context.Background()) + username, _ = claims["cognito:username"].(string) + email, _ = claims["email"].(string) + + // Try to extract groups + if rawGroups, ok := claims["cognito:groups"]; ok { + if groups, ok := rawGroups.([]interface{}); ok { + userGroups = make([]string, len(groups)) + for i, g := range groups { + userGroups[i] = fmt.Sprint(g) + } + } + } + } + } + + // Create authentication status section + var authStatusHTML string + if isAuthenticated { + authStatusHTML = fmt.Sprintf(` +
    +

    Authentication Status: Authenticated

    +

    Username: %s

    +

    Email: %s

    +

    Groups: %s

    +

    Logout

    +
    + `, username, email, strings.Join(userGroups, ", ")) + } else { + authStatusHTML = ` +
    +

    Authentication Status: Not Authenticated

    +

    You are not currently logged in.

    +

    +
    + ` + } + + // Create the complete HTML page + html := fmt.Sprintf(` + + + + Doczy Home + + + +

    Doczy Home

    + + %s + +

    Available Endpoints

    + + +
    +

    Note: This is a debugging page. Some endpoints require authentication or specific permissions.

    +
    + + + `, authStatusHTML, linksHTML) + + return c.HTML(http.StatusOK, html) +} + func main() { // Create a new Echo instance e := echo.New() @@ -40,12 +223,10 @@ func main() { // Set route permissions routePermissions := map[string][]string{ - "/users": {"exporters", "uploaders", "querybuilders"}, - "/users/:id": {"exporters", "querybuilders"}, - "/orders": {"exporters"}, - "/reports/sales": {"exporters", "uploaders", "querybuilders"}, - "/settings": {"exporters"}, - "/api/inventory/update": {"exporters", "uploaders"}, + "/query": {"exporters", "uploaders", "querybuilders"}, + "/users/:id": {"exporters", "querybuilders"}, + "/export": {"exporters"}, + "/reports/sales": {"exporters", "uploaders", "querybuilders"}, } config.SetAuthRoutePermissions(routePermissions) @@ -63,8 +244,8 @@ func main() { return c.String(http.StatusOK, "User details endpoint for ID: "+id) }) - e.GET("/orders", func(c echo.Context) error { - return c.String(http.StatusOK, "Orders endpoint (OK)") + e.GET("/query", func(c echo.Context) error { + return c.String(http.StatusOK, "query endpoint (OK)") }) e.GET("/reports/sales", func(c echo.Context) error { @@ -72,9 +253,12 @@ func main() { }) e.GET("/settings", func(c echo.Context) error { - return c.String(http.StatusOK, "Settings endpoint (OK)") + return c.String(http.StatusOK, "document endpoint (OK)") }) + // add entry for /home to use the HomeHandlerFunc + e.GET("/home", HomeHandlerFunc()) + e.PUT("/api/inventory/update", func(c echo.Context) error { return c.String(http.StatusOK, "Inventory update endpoint (OK)") })