This commit is contained in:
2023-08-09 19:16:56 -05:00
parent dc6a948405
commit e508310ece

36
main.go
View File

@@ -81,10 +81,20 @@ func main() {
func authMiddleware(allowedGroups []string) gin.HandlerFunc { func authMiddleware(allowedGroups []string) gin.HandlerFunc {
return func(c *gin.Context) { return func(c *gin.Context) {
var groups []string var groups []string
var JwksURL = os.Getenv("jwksurl") JwksURL := os.Getenv("jwksurl")
tokenString := c.GetHeader("Authorization") tokenString := c.GetHeader("Authorization")
if tokenString != "" { if tokenString != "" {
tokenString = strings.TrimPrefix(tokenString, "Bearer ") tokenString = strings.TrimPrefix(tokenString, "Bearer ")
} else {
tokenString = c.GetHeader("X-authentik-jwt")
}
if tokenString == "" {
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{
"message": "No Token in header",
})
return
}
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
@@ -152,29 +162,6 @@ func authMiddleware(allowedGroups []string) gin.HandlerFunc {
groups = append(groups, groupName) groups = append(groups, groupName)
} }
} }
} else {
if groupsenv != "" {
groups = strings.Split(groupsenv, ",")
} else {
groupsHeader := c.GetHeader("X-Authentik-Groups")
requestHeaders := c.Request.Header
for key, values := range requestHeaders {
for _, value := range values {
println(key + ": " + value)
}
}
// Dump response headers
responseHeaders := c.Writer.Header()
for key, values := range responseHeaders {
for _, value := range values {
println(key + ": " + value)
}
}
groups = strings.Split(groupsHeader, "|")
}
}
isAllowed := false isAllowed := false
for _, allowedGroup := range allowedGroups { for _, allowedGroup := range allowedGroups {
@@ -197,7 +184,6 @@ func authMiddleware(allowedGroups []string) gin.HandlerFunc {
return return
} }
// Call the next handler
c.Next() c.Next()
} }
} }