Files
compute/object/auth.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

99 lines
4.0 KiB
Go

// Copyright 2025 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 object
import (
"strings"
"github.com/hanzoai/iamsdk/v2/iamsdk"
"github.com/hanzoai/visor/conf"
)
// GetBearerUser validates an "Authorization: Bearer <IAM JWT>" header and
// returns the authenticated user, or nil. iamsdk.ParseJwtToken verifies the token
// SIGNATURE (jwt.ParseWithClaims + x509), so a forged/tampered token is rejected.
//
// Signature alone is NOT sufficient: Hanzo IAM publishes ONE shared JWKS holding
// every brand's cert (hanzo/lux/zoo/pars/...), so a validly-signed token from any
// brand — including public self-service signups — passes the signature check. We
// therefore bind the token to THIS deployment by BRAND (issuer), not by a single
// org:
// - owner (org) must be non-empty (an empty-org token can't be scoped), and
// - issuer must match the configured brand issuer(s) in iamIssuer, so a sibling
// brand's token (lux.id/zoo.id/pars.id) is rejected even though its signature
// verifies.
//
// EVERY org within this brand is accepted — the resell compute surface is
// multi-tenant (org "hanzo", "maxpower", and every self-service customer org).
// Each request is org-scoped downstream: resolveComputeOrg pins org = user.Owner
// (no ?owner override for real users) and Casbin enforces subOwner==objOwner, so
// one org can never reach another's machines. This is how API/console callers
// authenticate as a user (org = user.Owner) from a forwarded short-lived Bearer,
// without a browser cookie session.
func GetBearerUser(authHeader string) *iamsdk.User {
const prefix = "Bearer "
if len(authHeader) <= len(prefix) || !strings.EqualFold(authHeader[:len(prefix)], prefix) {
return nil
}
token := strings.TrimSpace(authHeader[len(prefix):])
if token == "" {
return nil
}
claims, err := iamsdk.ParseJwtToken(token)
if err != nil || claims == nil {
return nil
}
owner := strings.TrimSpace(claims.User.Owner)
if owner == "" {
return nil // empty-org token cannot be scoped — fail closed
}
if !issuerAllowed(claims.RegisteredClaims.Issuer) {
return nil // token from a different brand's IAM — reject on this surface
}
return &claims.User
}
// issuerAllowed reports whether a token's issuer matches the configured brand
// issuer(s) in `iamIssuer`. iamIssuer may be a single value or a comma-separated
// list, and a trailing "/" is ignored on BOTH sides so "https://hanzo.id" and
// "https://hanzo.id/" are equivalent (IAM stamps the slashed form; the KMS mint
// and others the bare form — normalising here avoids that footgun). Empty config
// fails CLOSED: with no configured brand issuer we cannot bind a brand, so every
// bearer is rejected rather than silently admitting all brands.
func issuerAllowed(tokenIssuer string) bool {
return matchIssuer(conf.GetConfigString("iamIssuer"), tokenIssuer)
}
// matchIssuer is the pure brand-issuer predicate (unit-tested). `configured` is
// the iamIssuer value (single or comma-list); `tokenIssuer` is the token's iss
// claim. Trailing slashes are ignored on both sides; empty `configured` or empty
// `tokenIssuer` fails closed.
func matchIssuer(configured, tokenIssuer string) bool {
got := strings.TrimRight(strings.TrimSpace(tokenIssuer), "/")
if got == "" {
return false
}
raw := strings.TrimSpace(configured)
if raw == "" {
return false
}
for _, e := range strings.Split(raw, ",") {
if want := strings.TrimRight(strings.TrimSpace(e), "/"); want != "" && strings.EqualFold(got, want) {
return true
}
}
return false
}