middleware test

This commit is contained in:
jay brown
2025-03-14 12:36:52 -07:00
parent d809796293
commit 67fe6a6055
3 changed files with 243 additions and 27 deletions
+26 -26
View File
@@ -184,29 +184,29 @@ func tokenFromCookie(name string) func(echo.Context) (string, error) {
}
}
// GroupGuard middleware ensures a user has at least one of the required groups
func GroupGuard(requiredGroups ...string) echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
userClaims, ok := c.Get("user").(jwt.MapClaims)
if !ok {
return echo.NewHTTPError(http.StatusUnauthorized, "user not authenticated")
}
groups, ok := userClaims["cognito:groups"].([]interface{})
if !ok {
return echo.NewHTTPError(http.StatusForbidden, "no groups found for user")
}
for _, requiredGroup := range requiredGroups {
for _, group := range groups {
if groupStr, ok := group.(string); ok && strings.EqualFold(groupStr, requiredGroup) {
return next(c)
}
}
}
return echo.NewHTTPError(http.StatusForbidden, "insufficient permissions")
}
}
}
//// GroupGuard middleware ensures a user has at least one of the required groups
//func GroupGuard(requiredGroups ...string) echo.MiddlewareFunc {
// return func(next echo.HandlerFunc) echo.HandlerFunc {
// return func(c echo.Context) error {
// userClaims, ok := c.Get("user").(jwt.MapClaims)
// if !ok {
// return echo.NewHTTPError(http.StatusUnauthorized, "user not authenticated")
// }
//
// groups, ok := userClaims["cognito:groups"].([]interface{})
// if !ok {
// return echo.NewHTTPError(http.StatusForbidden, "no groups found for user")
// }
//
// for _, requiredGroup := range requiredGroups {
// for _, group := range groups {
// if groupStr, ok := group.(string); ok && strings.EqualFold(groupStr, requiredGroup) {
// return next(c)
// }
// }
// }
//
// return echo.NewHTTPError(http.StatusForbidden, "insufficient permissions")
// }
// }
//}