Initialize AutoLibrary website with Gitea deployment

This commit is contained in:
KenanZhu committed 2026-11-04 09:59:06 +08:00
commit 18216953e7
101 files changed
+12581

No files matched your search

+6
View File
@@ -0,0 +1,6 @@
package middlewares
import (
_ "autolibrary.top/webserver/backend/middlewares/auth_middleware"
_ "autolibrary.top/webserver/backend/middlewares/casdoor_middleware"
)
@@ -0,0 +1,49 @@
package auth_middleware
import (
"log"
"net/http"
"autolibrary.top/webserver/config"
"github.com/gin-gonic/gin"
)
func APIKeyAuth() gin.HandlerFunc {
return func(c *gin.Context) {
cfg := config.GetConfig()
if !cfg.Backend.Auth.Required {
c.Next()
return
}
apiKey := c.GetHeader("X-API-Key")
if apiKey == "" {
apiKey = c.Query("api_key")
}
// Check if API key is configured
expectedKey := cfg.Backend.Auth.Key
if expectedKey == "" || expectedKey == "null" {
log.Printf("[E] AUTH > API key not configured")
c.JSON(http.StatusInternalServerError, gin.H{
"code": http.StatusInternalServerError,
"msg": "AUTH > API key has not been configured on this server",
})
c.Abort()
return
}
// Check if API key is valid
if apiKey == "" || apiKey != expectedKey {
log.Printf("[W] AUTH > invalid API key or key is missing")
c.JSON(http.StatusUnauthorized, gin.H{
"code": http.StatusUnauthorized,
"msg": "AUTH > invalid API key or key is missing",
})
c.Abort()
return
}
c.Next()
}
}
@@ -0,0 +1,92 @@
package casdoor_middleware
import (
"log"
"net/http"
"strings"
"autolibrary.top/webserver/config"
"github.com/casdoor/casdoor-go-sdk/casdoorsdk"
"github.com/gin-gonic/gin"
)
func CasdoorAuth() gin.HandlerFunc {
cfg := config.GetConfig()
return func(c *gin.Context) {
// If Casdoor is not configured, return 501 Not Implemented
if cfg.Backend.Casdoor.Endpoint == "" || cfg.Backend.Casdoor.ClientID == "" {
c.JSON(http.StatusNotImplemented, gin.H{
"code": http.StatusNotImplemented,
"msg": "AUTH > Casdoor authentication is not configured on this server",
"data": nil,
})
c.Abort()
return
}
authHeader := c.GetHeader("Authorization")
if authHeader == "" {
c.JSON(http.StatusUnauthorized, gin.H{
"code": http.StatusUnauthorized,
"msg": "AUTH > missing Authorization header",
"data": nil,
})
c.Abort()
return
}
token := strings.TrimPrefix(authHeader, "Bearer ")
if token == authHeader {
c.JSON(http.StatusUnauthorized, gin.H{
"code": http.StatusUnauthorized,
"msg": "AUTH > Authorization header must be Bearer <token>",
"data": nil,
})
c.Abort()
return
}
claims, err := casdoorsdk.ParseJwtToken(token)
if err != nil {
log.Printf("[W] AUTH > invalid JWT token: %v", err)
c.JSON(http.StatusUnauthorized, gin.H{
"code": http.StatusUnauthorized,
"msg": "AUTH > invalid or expired token",
"data": nil,
})
c.Abort()
return
}
c.Set("user_id", claims.User.Id)
c.Set("user_name", claims.User.Name)
c.Set("display_name", claims.User.DisplayName)
c.Set("is_admin", claims.User.IsAdmin)
c.Set("user_tag", claims.User.Tag)
c.Next()
}
}
// CasdoorAdminAuth requires both valid Casdoor JWT and admin privileges
func CasdoorAdminAuth() gin.HandlerFunc {
auth := CasdoorAuth()
return func(c *gin.Context) {
auth(c)
if c.IsAborted() {
return
}
isAdmin, _ := c.Get("is_admin")
if isAdmin == nil || !isAdmin.(bool) {
c.JSON(http.StatusForbidden, gin.H{
"code": http.StatusForbidden,
"msg": "AUTH > admin privileges required",
"data": nil,
})
c.Abort()
return
}
c.Next()
}
}