This commit is contained in:
jay brown
2025-04-16 12:04:47 -07:00
parent cb0207a74f
commit d0bcf7b0d2
5 changed files with 47 additions and 196 deletions
-188
View File
@@ -47,17 +47,6 @@ func RegisterRoutes(e *echo.Echo, config auth.ConfigProvider) {
})
})
//fmt.Printf("registering logoutpath: %s \v", config.GetAuthLogoutPath())
//// Register logout route
//e.GET(config.GetAuthLogoutPath(), func(c echo.Context) error {
// return LogoutHandler(c)
//})
//// Default home page handler if requested
//e.GET(config.GetAuthHomePath(), func(c echo.Context) error {
// return HomeHandler(c, config)
//})
// Apply the JWT Auth middleware first
e.Use(JWTAuthMiddleware(config))
@@ -65,183 +54,6 @@ func RegisterRoutes(e *echo.Echo, config auth.ConfigProvider) {
e.Use(TokenValidationMiddleware(config))
}
// HomeHandler implements a simple home page that shows auth status and available endpoints
func HomeHandler(c echo.Context, config auth.ConfigProvider) error {
// Create a list of all registered routes
println("HomeHandler called.")
var endpoints []string
// Add the auth routes
endpoints = append(endpoints, []string{
config.GetAuthLoginPath(),
config.GetAuthCallbackPath(),
config.GetAuthHomePath(),
config.GetAuthLogoutPath(),
}...)
// Add routes from the permission map
for route := range config.GetAuthRoutePermissions() {
// Skip routes that are already in the list
alreadyAdded := false
for _, endpoint := range endpoints {
if route == endpoint {
alreadyAdded = true
break
}
}
if !alreadyAdded {
endpoints = append(endpoints, route)
}
}
// 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)
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
userGroups, _ = GetUserGroups(claims)
}
}
// Create HTML for the endpoints list
var linksHTML string
baseURL := c.Scheme() + "://" + c.Request().Host
// Create list items for each endpoint
for _, endpoint := range endpoints {
// Skip endpoints with path parameters for direct linking
if strings.Contains(endpoint, ":") {
displayPath := strings.Replace(endpoint, ":id", "{id}", -1)
linksHTML += fmt.Sprintf("<li>%s (requires parameter)</li>\n", displayPath)
} else {
linksHTML += fmt.Sprintf("<li><a href=\"%s%s\">%s</a></li>\n", baseURL, endpoint, endpoint)
}
}
// Create authentication status section
var authStatusHTML string
if isAuthenticated {
authStatusHTML = fmt.Sprintf(`
<div class="auth-status authenticated">
<h2>Authentication Status: Authenticated</h2>
<p><strong>Username:</strong> %s</p>
<p><strong>Email:</strong> %s</p>
<p><strong>Groups:</strong> %s</p>
<p><a href="%s/logout" class="logout-btn">Logout</a></p>
</div>
`, username, email, strings.Join(userGroups, ", "), baseURL)
} else {
authStatusHTML = fmt.Sprintf(`
<div class="auth-status unauthenticated">
<h2>Authentication Status: Not Authenticated</h2>
<p>You are not currently logged in.</p>
<p><a href="%s/login" class="login-btn">Login</a></p>
</div>
`, baseURL)
}
// Create the complete HTML page
html := fmt.Sprintf(`
<!DOCTYPE html>
<html>
<head>
<title>Authentication Home</title>
<style>
body {
font-family: Arial, sans-serif;
line-height: 1.6;
margin: 20px;
max-width: 800px;
margin: 0 auto;
padding: 20px;
}
h1, h2 {
color: #333;
border-bottom: 1px solid #ddd;
padding-bottom: 10px;
}
ul {
margin-top: 20px;
}
li {
margin-bottom: 8px;
}
a {
color: #0066cc;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
.note {
background-color: #f8f9fa;
border-left: 4px solid #5bc0de;
padding: 10px 15px;
margin-top: 20px;
font-size: 0.9em;
}
.auth-status {
margin: 20px 0;
padding: 15px;
border-radius: 5px;
}
.authenticated {
background-color: #dff0d8;
border: 1px solid #d6e9c6;
}
.unauthenticated {
background-color: #f2dede;
border: 1px solid #ebccd1;
}
.login-btn, .logout-btn {
display: inline-block;
padding: 8px 16px;
background-color: #0066cc;
color: white;
border-radius: 4px;
text-decoration: none;
margin-top: 10px;
}
.logout-btn {
background-color: #d9534f;
}
</style>
</head>
<body>
<h1>Authentication Home</h1>
%s
<h2>Available Endpoints</h2>
<ul>
%s
</ul>
<div class="note">
<p><strong>Note:</strong> This is a debugging page. Some endpoints require authentication or specific permissions.</p>
</div>
</body>
</html>
`, authStatusHTML, linksHTML)
return c.HTML(http.StatusOK, html)
}
// GetTokenFromRequest extracts the token from the request
func GetTokenFromRequest(c echo.Context) string {
// First try from Authorization header