auth fixes

middleware handles logout
This commit is contained in:
jay brown
2025-04-15 12:04:54 -07:00
parent 1bbfa95a40
commit bf98a0971f
5 changed files with 59 additions and 29 deletions
+13 -8
View File
@@ -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
-15
View File
@@ -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())
}
+12
View File
@@ -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)
+23
View File
@@ -23,6 +23,8 @@ import (
echoSwagger "github.com/swaggo/echo-swagger"
)
const configContextKey = "config"
type Config interface {
server.Config
SetRouter(*echo.Echo)
@@ -37,6 +39,18 @@ type BaseConfig struct {
Router *echo.Echo
}
// GetConfigFromContext returns the config from the context.
// experimental since it is not used yet and may pose cylical dependency issues
// in handlers that are called by the generated
// swagger code. This needs to be verified.
func GetConfigFromContext(ctx echo.Context) (Config, error) {
cfg, ok := ctx.Get(configContextKey).(Config)
if !ok {
return nil, errors.New("config not found in context")
}
return cfg, nil
}
func (c *BaseConfig) SetRouter(e *echo.Echo) {
c.Router = e
}
@@ -188,6 +202,15 @@ func New(ctx context.Context, cfg Config) (*Server, error) {
cognitoauth.RegisterRoutes(cfg.GetRouter(), cfg)
// auth end
// Add config middleware before other middleware
// so that the config is available to all middleware and handlers.
e.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
c.Set(string(configContextKey), cfg)
return next(c)
}
})
e.Use(echoprometheus.NewMiddleware("echo"))
e.GET("/metrics", echoprometheus.NewHandler())