network/pkg/gateway/middleware_test.go
anonpenguin 917b0e5acd Enforce API key/JWT authentication and namespace gating in client
- Require API key or JWT by default for client connections - Auto-derive
namespace from JWT claim or API key format `ak_<rand>:<namespace>` -
Deny calls if per-call namespace override mismatches resolved namespace
- Guard Storage, PubSub, Database, and NetworkInfo operations with
access checks - Add context helpers for consistent namespace override
handling - Update docs and add end-to-end and unit tests for
authentication logic
2025-08-20 23:01:51 +03:00

38 lines
947 B
Go

package gateway
import (
"net/http"
"net/http/httptest"
"testing"
)
func TestExtractAPIKey(t *testing.T) {
r := httptest.NewRequest(http.MethodGet, "/", nil)
r.Header.Set("Authorization", "Bearer ak_foo:ns")
if got := extractAPIKey(r); got != "ak_foo:ns" {
t.Fatalf("got %q", got)
}
r.Header.Set("Authorization", "ApiKey ak2")
if got := extractAPIKey(r); got != "ak2" {
t.Fatalf("got %q", got)
}
r.Header.Set("Authorization", "ak3raw")
if got := extractAPIKey(r); got != "ak3raw" {
t.Fatalf("got %q", got)
}
r.Header = http.Header{}
r.Header.Set("X-API-Key", "xkey")
if got := extractAPIKey(r); got != "xkey" {
t.Fatalf("got %q", got)
}
}
func TestValidateNamespaceParam(t *testing.T) {
g := &Gateway{}
r := httptest.NewRequest(http.MethodGet, "/v1/storage/get?namespace=ns1&key=k", nil)
// no context namespace: should be false
if g.validateNamespaceParam(r) {
t.Fatalf("expected false without context ns")
}
}