File size: 761 Bytes
6fefda3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
package middleware
import (
"github.com/labstack/echo"
"jetbrains-ai-proxy/internal/config"
"log"
"net/http"
"strings"
)
func BearerAuth() echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
// 获取Authorization header
auth := c.Request().Header.Get("Authorization")
if auth == "" || !strings.HasPrefix(auth, "Bearer ") {
return echo.NewHTTPError(http.StatusUnauthorized, "invalid authorization header")
}
token := strings.TrimPrefix(auth, "Bearer ")
if token != config.JetbrainsAiConfig.BearerToken || token == "" {
log.Printf("invalid token: %s", token)
return echo.NewHTTPError(http.StatusUnauthorized, "invalid token")
}
return next(c)
}
}
}
|