jim800121chen 22f0837ba8 feat(visionA-backend): Phase 0 → 0.7 雲端後端(雙 binary + OIDC BFF + stage 部署)
從 edge-ai-platform POC 轉為正式產品的雲端後端,含以下整合階段:

- Phase 0:雛形骨架 — `cmd/api-server` (REST :3721) + `cmd/remote-proxy`
  (tunnel :3800 / internal :3801) 雙 binary 共用 internal/,沿用 POC 的
  WebSocket+yamux tunnel 協定但解耦 relay 與 API
- Phase 0.6:OIDC BFF 接 Innovedus Member Center
  - internal/oidc package(coreos/go-oidc + PKCE S256 + state + nonce)
  - internal/usersession package(HMAC-SHA256 cookie + RotateSessionID
    防 session fixation, OWASP ASVS V3.2.1)
  - 4 個 OIDC handler(/api/auth/login|callback|me|logout)+ AuthMiddleware
  - 完全拔除 StaticAuthProvider,OIDC 是唯一認證路徑
  - 9 個 ADR(含 ADR-010 BFF / ADR-011 取代 static auth /
    ADR-012 pending session shared cookie / ADR-013 PKCE-only public client)
- Phase 0.7:A1 改造 + security audit 修復
  - OIDC ClientSecret 變選填,支援 stage MC 的 public PKCE-only client
    (AuthStyleInParams 強制 token endpoint 不送 client_secret)
  - 預留 ServiceClient* 欄位給未來 client_credentials grant
  - 移除 13+ 處 resolveUserID(uc, StaticUserID) fallback 改 strict mode
    (Audit C1:multi-tenant 隔離破口)
  - Pairing exchange MarkUsed 失敗 abort + revoke session token(Audit M3)
  - 新增 all_endpoints_require_auth_test 整合測試(51 endpoint × 401)

驗證:go test -race -count=3 ./... 17 packages 全綠 / go vet 0 warning

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-01 11:21:20 +08:00

70 lines
2.4 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package api
import (
"context"
"net/http"
"net/http/httptest"
"testing"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
"visiona-backend/internal/session"
)
// TestNewProxyHandler_NoForwarder 驗證沒注入 Forwarder 時回 501。
func TestNewProxyHandler_NoForwarder(t *testing.T) {
r := gin.New()
r.Use(RequestIDMiddleware())
g := r.Group("/api")
g.POST("/devices/scan", newProxyHandler(Deps{}, proxyOptions{}))
w := httptest.NewRecorder()
r.ServeHTTP(w, httptest.NewRequest(http.MethodPost, "/api/devices/scan", nil))
assert.Equal(t, http.StatusNotImplemented, w.Code)
}
// TestNewProxyHandler_TunnelDisconnected 驗證沒 session 時回 502 TUNNEL_DISCONNECTED。
//
// 這裡用 fakeSessionStoreList 回空)+ 非 nil forwarder 的「半個」 proxy handler
// 因為 nil forwarder 的 path 會先 return 501見上方 test。我們用真實 forwarder
// 但不 dial — 直接在 pickActiveSessionToken 回 ErrSessionNotFound 就攔掉了。
//
// Phase 0.7 security fix C1handler 強制要求 UserContext用 injectStaticUserContext
// 顯式注入避免回 500見 .autoflow/05-implementation/review/phase-0.7-security-audit.md
func TestNewProxyHandler_TunnelDisconnected(t *testing.T) {
r := gin.New()
r.Use(RequestIDMiddleware())
r.Use(injectStaticUserContext("demo-user", ""))
g := r.Group("/api")
g.POST("/devices/scan", newProxyHandler(Deps{
SessionStore: &fakeSessionStore{}, // List 回空
Forwarder: session.NewForwarder("http://localhost:0", nil),
}, proxyOptions{}))
w := httptest.NewRecorder()
r.ServeHTTP(w, httptest.NewRequest(http.MethodPost, "/api/devices/scan", nil))
assert.Equal(t, http.StatusBadGateway, w.Code)
assert.Contains(t, w.Body.String(), ErrCodeTunnelDisconnect)
}
// TestPickActiveSessionToken 驗證 helper 回第一筆 match 的 session。
func TestPickActiveSessionToken(t *testing.T) {
store := &fakeSessionStore{
sessions: []*session.Summary{
{Token: "t-other", UserID: "other"},
{Token: "t-me", UserID: "demo-user"},
},
}
tok, err := pickActiveSessionToken(context.Background(), store, "demo-user", nil)
assert.NoError(t, err)
assert.Equal(t, "t-me", tok)
}
// TestPickActiveSessionToken_Empty 驗證沒 session 時回 ErrSessionNotFound。
func TestPickActiveSessionToken_Empty(t *testing.T) {
store := &fakeSessionStore{}
_, err := pickActiveSessionToken(context.Background(), store, "demo-user", nil)
assert.ErrorIs(t, err, session.ErrSessionNotFound)
}