auth fixes
middleware handles logout
This commit is contained in:
@@ -17,8 +17,10 @@ import (
|
||||
// RegisterRoutes registers all authentication-related routes to the Echo engine
|
||||
func RegisterRoutes(e *echo.Echo, config auth.ConfigProvider) {
|
||||
// check the auth feature flag
|
||||
fmt.Println("Registering routes for Auth.")
|
||||
disableAuth := os.Getenv("DISABLE_AUTH")
|
||||
if strings.ToLower(disableAuth) == "true" {
|
||||
fmt.Println("Auth is disabled. Skipping auth routes.")
|
||||
return // Do nothing if auth is not enabled
|
||||
}
|
||||
|
||||
@@ -45,15 +47,16 @@ func RegisterRoutes(e *echo.Echo, config auth.ConfigProvider) {
|
||||
})
|
||||
})
|
||||
|
||||
// Register logout route
|
||||
e.GET(config.GetAuthLogoutPath(), func(c echo.Context) error {
|
||||
return LogoutHandler(c, config)
|
||||
})
|
||||
//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)
|
||||
})
|
||||
//// 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,6 +68,8 @@ func RegisterRoutes(e *echo.Echo, config auth.ConfigProvider) {
|
||||
// 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
|
||||
|
||||
@@ -229,18 +229,3 @@ func handleOAuthCallback(c echo.Context, config auth.ConfigProvider) error {
|
||||
// Redirect to home page
|
||||
return c.Redirect(http.StatusFound, config.GetAuthHomePath())
|
||||
}
|
||||
|
||||
// LogoutHandler handles the logout process by clearing the auth cookie
|
||||
func LogoutHandler(c echo.Context, config auth.ConfigProvider) error {
|
||||
// Clear the token cookie
|
||||
cookie := new(http.Cookie)
|
||||
cookie.Name = "auth_token"
|
||||
cookie.Value = ""
|
||||
cookie.Path = "/"
|
||||
cookie.Expires = time.Now().Add(-1 * time.Hour) // Set expiry in the past
|
||||
cookie.HttpOnly = true
|
||||
c.SetCookie(cookie)
|
||||
|
||||
// Redirect to home page
|
||||
return c.Redirect(http.StatusFound, config.GetAuthHomePath())
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package cognitoauth
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"queryorchestration/internal/serviceconfig/auth"
|
||||
|
||||
@@ -117,6 +118,17 @@ func JWTAuthMiddleware(config auth.ConfigProvider) echo.MiddlewareFunc {
|
||||
return next(c)
|
||||
}
|
||||
|
||||
// if logout then remove the auth cookie
|
||||
if c.Path() == config.GetAuthLogoutPath() {
|
||||
cookie := new(http.Cookie)
|
||||
cookie.Name = "auth_token"
|
||||
cookie.Value = ""
|
||||
cookie.Path = "/"
|
||||
cookie.Expires = time.Now().Add(-1 * time.Hour) // Set expiry in the past
|
||||
cookie.HttpOnly = true
|
||||
c.SetCookie(cookie)
|
||||
}
|
||||
|
||||
// Skip for home and logout as they should be accessible without auth
|
||||
if c.Path() == config.GetAuthHomePath() || c.Path() == config.GetAuthLogoutPath() {
|
||||
return next(c)
|
||||
|
||||
Reference in New Issue
Block a user