middleware test
This commit is contained in:
+26
-26
@@ -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")
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
|
||||
Reference in New Issue
Block a user