revert auth func

This commit is contained in:
2023-06-24 16:27:36 -04:00
parent 3db9bb2914
commit e62a33fd06
5 changed files with 52 additions and 54 deletions

View File

@@ -3,11 +3,8 @@ package controller
import (
"fmt"
"log"
"net/http"
"strings"
"github.com/caarlos0/env/v6"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
"github.com/sashabaranov/go-openai"
@@ -51,48 +48,3 @@ func NewController() *Controller {
return controller
}
func (c *Controller) AuthMiddleware(
allowedGroups []string,
currentGroups string,
) gin.HandlerFunc {
return func(c *gin.Context) {
var groups []string
if currentGroups != "" {
groups = strings.Split(currentGroups, ",")
} else {
// Get the user groups from the request headers
groupsHeader := c.GetHeader("X-authentik-groups")
// Split the groups header value into individual groups
groups = strings.Split(groupsHeader, "|")
}
// Check if the user belongs to any of the allowed groups
isAllowed := false
for _, allowedGroup := range allowedGroups {
for _, group := range groups {
if group == allowedGroup {
isAllowed = true
break
}
}
if isAllowed {
break
}
}
// If the user is not in any of the allowed groups, respond with unauthorized access
if !isAllowed {
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{
"message": "Unauthorized access",
"groups": groups,
})
return
}
// Call the next handler
c.Next()
}
}