fix: Terminal Auth per JWT-Token statt leerem API-Key

- TerminalModal nutzt jetzt JWT aus localStorage (api.getToken()) fuer WS-Auth
- Backend Middleware: ?token= Query-Parameter fuer WebSocket JWT-Auth
- Behebt: api_key= leer -> 401 -> Terminal nicht erreichbar
This commit is contained in:
cynfo3000
2026-03-09 23:39:38 +01:00
parent 358682e5f3
commit e9a47bee26
2 changed files with 19 additions and 5 deletions
+9 -4
View File
@@ -73,7 +73,7 @@ func (c *APIKeyCache) IsValid(key string) bool {
// CombinedAuthWithCache - API-Key (aus DB-Cache) ODER JWT Token
func CombinedAuthWithCache(cache *APIKeyCache, auth *AuthHandler, next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Try API key first
// Try API key first (Header oder Query-Parameter)
key := r.Header.Get("X-API-Key")
if key == "" {
key = r.URL.Query().Get("api_key")
@@ -83,11 +83,16 @@ func CombinedAuthWithCache(cache *APIKeyCache, auth *AuthHandler, next http.Hand
return
}
// Try JWT
// Try JWT — Authorization Header oder ?token= Query-Parameter (fuer WebSocket)
var jwtToken string
authHeader := r.Header.Get("Authorization")
if strings.HasPrefix(authHeader, "Bearer ") {
token := strings.TrimPrefix(authHeader, "Bearer ")
claims, err := auth.ValidateToken(token)
jwtToken = strings.TrimPrefix(authHeader, "Bearer ")
} else if t := r.URL.Query().Get("token"); t != "" {
jwtToken = t
}
if jwtToken != "" {
claims, err := auth.ValidateToken(jwtToken)
if err == nil {
ctx := context.WithValue(r.Context(), UserContextKey, claims)
next.ServeHTTP(w, r.WithContext(ctx))