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:
@@ -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))
|
||||
|
||||
Reference in New Issue
Block a user