🚩 Private development - version retention only ✅ Complete web dashboard (React + TypeScript + TailwindCSS) ✅ Production-ready server backend (Go + Gin + PostgreSQL) ✅ Linux agent with APT + Docker scanning + local CLI tools ✅ JWT authentication and REST API ✅ Update discovery and approval workflow 🚧 Status: Alpha software - active development 📦 Purpose: Version retention during development ⚠️ Not for public use or deployment
72 lines
1.8 KiB
Go
72 lines
1.8 KiB
Go
package middleware
|
|
|
|
import (
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/golang-jwt/jwt/v5"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
// AgentClaims represents JWT claims for agent authentication
|
|
type AgentClaims struct {
|
|
AgentID uuid.UUID `json:"agent_id"`
|
|
jwt.RegisteredClaims
|
|
}
|
|
|
|
// JWTSecret is set by the server at initialization
|
|
var JWTSecret string
|
|
|
|
// GenerateAgentToken creates a new JWT token for an agent
|
|
func GenerateAgentToken(agentID uuid.UUID) (string, error) {
|
|
claims := AgentClaims{
|
|
AgentID: agentID,
|
|
RegisteredClaims: jwt.RegisteredClaims{
|
|
ExpiresAt: jwt.NewNumericDate(time.Now().Add(24 * time.Hour)),
|
|
IssuedAt: jwt.NewNumericDate(time.Now()),
|
|
},
|
|
}
|
|
|
|
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
|
return token.SignedString([]byte(JWTSecret))
|
|
}
|
|
|
|
// AuthMiddleware validates JWT tokens from agents
|
|
func AuthMiddleware() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
authHeader := c.GetHeader("Authorization")
|
|
if authHeader == "" {
|
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "missing authorization header"})
|
|
c.Abort()
|
|
return
|
|
}
|
|
|
|
tokenString := strings.TrimPrefix(authHeader, "Bearer ")
|
|
if tokenString == authHeader {
|
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "invalid authorization format"})
|
|
c.Abort()
|
|
return
|
|
}
|
|
|
|
token, err := jwt.ParseWithClaims(tokenString, &AgentClaims{}, func(token *jwt.Token) (interface{}, error) {
|
|
return []byte(JWTSecret), nil
|
|
})
|
|
|
|
if err != nil || !token.Valid {
|
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "invalid token"})
|
|
c.Abort()
|
|
return
|
|
}
|
|
|
|
if claims, ok := token.Claims.(*AgentClaims); ok {
|
|
c.Set("agent_id", claims.AgentID)
|
|
c.Next()
|
|
} else {
|
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "invalid token claims"})
|
|
c.Abort()
|
|
}
|
|
}
|
|
}
|