mirror of
https://github.com/hanzoai/ai.git
synced 2026-11-21 02:36:49 +00:00
Replaces beego's ControllerRegister, filter chain and context with the native web package end to end. The controllers, filters and routes are retyped from beego.Context/beego.Controller to their web equivalents (web.Context mirrors beego's API, so the swap is mechanical), and the 307 explicit routes register on a shared routers.App (web.Router). The dead NSInclude namespace is dropped: without a generated commentsRouter it registered nothing. - controllers/base.go embeds web.Controller; the 4 streaming/util controllers, the 17 filters, util/cookie.go and object/record.go take *web.Context. - routers.InstallFilters inserts the 14 BeforeRouter + 2 AfterExec filters on App in the same order the standalone used; bootstrap and the route tests share it. - The session provider binds through App.UseSessions (its Store matches web.Store); Bootstrap publishes App as the request handler instead of beego.BeeApp.Handlers. - web gains the Controller compatibility surface the handlers use: EnableRender, GetFile, Context.SetCookie/Output.Cookie; RunMode defaults to prod (compact ServeJSON), matching the runtime default. The /swagger static (beego SetStaticPath) is dropped — the edge serves statics. Verified on SQLite (UseMemoryDB): /v1/cloud/* rewrites and dispatches, the /v1/iam/* alias is selective (in-set dispatches, out-of-set 404s), an unauthenticated gated /v1/cloud/get-providers is denied 401/403 before dispatch, and the real filter chain short-circuits on auth-reject in order. go build ./... and the web/routers/controllers/route tests are green; the beego module stays until the session and lifecycle cuts.
51 lines
1.8 KiB
Go
51 lines
1.8 KiB
Go
// Copyright 2023-2025 Hanzo AI Inc. All Rights Reserved.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package routers
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"github.com/hanzoai/ai/conf"
|
|
"github.com/hanzoai/ai/util"
|
|
"github.com/hanzoai/ai/web"
|
|
)
|
|
|
|
// GetOrg resolves the organization for data-scoping in filters from the
|
|
// VERIFIED request principal — never a raw client header.
|
|
//
|
|
// On the direct (non-gateway) ingress the X-Org-Id header is fully
|
|
// client-controlled, so trusting it would let any caller act as any org
|
|
// (cross-tenant read/write + billing attribution). It is honored ONLY when it
|
|
// matches the authenticated principal's own org, or the principal is a global
|
|
// admin (cross-org platform access). A non-admin can never escape their own org
|
|
// via a header; an unauthenticated caller's header is ignored entirely.
|
|
//
|
|
// Behind the gateway the injected X-Org-Id equals the JWT owner, so this
|
|
// resolves identically — the gateway path is unaffected.
|
|
func GetOrg(ctx *web.Context) string {
|
|
requested := strings.TrimSpace(ctx.Input.Header("X-Org-Id"))
|
|
|
|
user := sessionOrBearerUser(ctx)
|
|
if user != nil && user.Owner != "" {
|
|
if requested != "" && (requested == user.Owner || util.IsSuperAdmin(user)) {
|
|
return requested
|
|
}
|
|
return user.Owner
|
|
}
|
|
|
|
// No verified principal: never trust a client-supplied org header.
|
|
return conf.GetConfigString("IAM_ORG")
|
|
}
|