Files
compute/controllers/base.go
zeekayandHanzo Dev 70371d4941 rip(iam-v1): one IAM lineage in this binary, and it is not the archived one
visor required github.com/hanzoai/iam-v1 DIRECTLY (go.mod, 6 import sites)
while ALSO linking github.com/hanzoai/iam through cloud. One binary, both
implementations of the same idea.

iam-v1 is ARCHIVED. Its description says "no longer in any graph"; go list -deps
said otherwise. An archived repo cannot take a push, so it cannot take a
security fix — which disqualifies it as a dependency of a deployed service no
matter how good the code inside is.

The code inside is, in fact, the same code. iam-v1's ROOT package is a copy of
the Casdoor Go SDK that also lives, extracted and maintained, at
github.com/hanzoai/iamsdk/v2/iamsdk: 67 of 73 non-test files byte-identical
once the package clause is normalised. Same shape, same bounded context, same
semantics — a genuine duplicate, not a name collision, so it collapses.

Measured before landing this, so the swap drops nothing:
  - ParseJwtToken + publicKeyFromPEM: byte-identical. Signature verification,
    the JWKS-then-cert-PEM fallback, the algorithm allowlist — unchanged.
  - jwks.go: byte-identical. Key fetch unchanged.
  - iamsdk's User is a strict superset of iam-v1's.
  - Zero exported symbols exist there and not here.
  - What iam-v1 had and iamsdk lacked — Claims.Orgs and Claims.BillingAccount,
    both signed and both minted by live IAM — went into iamsdk v2.2.1 first,
    with tests. Nothing was dropped to make the merge fit.
  - iam-v1's root errors.go sentinels are NOT ported: nothing in that module
    ever returns them. (teamrole.ErrCrossOrg is a different package's live
    guard, and visor never imported that subpackage.) Dead names, not guards.

No shim, no alias: the import is plain `iamsdk`, spelled once. The old
`iam "…/iam-v1"` alias is what let two lineages wear one name at the call site.

visor's own guards are untouched — GetBearerUser still fails closed on an empty
owner and still refuses a sibling brand's issuer; authz.IsAllowed is unchanged.

deps_test.go stops the archived module coming back, reading go.mod rather than
imports so a TRANSITIVE reintroduction fails too, and TestRetiredInDetects
proves the detector fires on the exact require line this commit deletes.

Gate: go build ./... + go vet ./... + go test ./... green before and after.
go list -deps ./... | grep hanzoai/iam-v1 → 1 package before, 0 after; go.sum
references → 0.

Co-authored-by: Hanzo Dev <[email protected]>
2026-07-28 22:57:17 -07:00

105 lines
3.5 KiB
Go

// Copyright 2023 Hanzo Industries 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 controllers
import (
"strings"
"github.com/hanzoai/iamsdk/v2/iamsdk"
"github.com/zap-proto/zip"
"github.com/hanzoai/visor/object"
)
// ApiController is the ZAP-native controller base. It carries the request
// context (zip.Ctx) and the buffered JSON response body (Data["json"]) the
// record filter reads back. It replaces the Beego controller one-for-one:
// handlers keep their method receiver, but the framework underneath is zip over
// fiber/fasthttp — no cookie or server-side session. A caller authenticates statelessly
// with a forwarded IAM Bearer JWT (object.GetBearerUser); the resolved Principal
// is the ONE identity seam.
type ApiController struct {
Ctx *zip.Ctx
Data map[string]interface{}
}
// New builds a controller bound to a request context, with its response buffer
// ready. This is the ONE construction path the route wrappers use.
func New(c *zip.Ctx) *ApiController {
return &ApiController{Ctx: c, Data: map[string]interface{}{}}
}
func GetUserName(user *iamsdk.User) string {
if user == nil {
return ""
}
return user.Name
}
func wrapActionResponse(affected bool, e ...error) *Response {
if len(e) != 0 && e[0] != nil {
return &Response{Status: "error", Msg: e[0].Error()}
} else if affected {
return &Response{Status: "ok", Msg: "", Data: "Affected"}
} else {
return &Response{Status: "ok", Msg: "", Data: "Unaffected"}
}
}
// GetSessionClaims parses the forwarded IAM Bearer JWT into its claims — the
// stateless replacement for the Beego cookie session. Signature is verified by
// iamsdk.ParseJwtToken; brand/issuer binding is enforced by the ApiFilter (via
// object.GetBearerUser) before any handler runs, so this is a plain decode.
func (c *ApiController) GetSessionClaims() *iamsdk.Claims {
const prefix = "Bearer "
h := c.Ctx.Header("Authorization")
if len(h) <= len(prefix) || !strings.EqualFold(h[:len(prefix)], prefix) {
return nil
}
token := strings.TrimSpace(h[len(prefix):])
if token == "" {
return nil
}
claims, err := iamsdk.ParseJwtToken(token)
if err != nil || claims == nil {
return nil
}
return claims
}
// SetSessionClaims is a no-op in the stateless model: there is no server-side
// session to write. Signin returns the claims (and access token) to the client,
// which then carries the token as a Bearer on every subsequent request.
func (c *ApiController) SetSessionClaims(claims *iamsdk.Claims) {}
// GetSessionUser resolves the authenticated user from the forwarded IAM Bearer
// JWT (brand/issuer-bound in object.GetBearerUser), or nil.
func (c *ApiController) GetSessionUser() *iamsdk.User {
return object.GetBearerUser(c.Ctx.Header("Authorization"))
}
// SetSessionUser is a no-op in the stateless model (see SetSessionClaims).
func (c *ApiController) SetSessionUser(user *iamsdk.User) {}
func (c *ApiController) GetSessionUsername() string {
user := c.GetSessionUser()
if user == nil {
return ""
}
return GetUserName(user)
}