Files
vm/routers/base.go
zeekayandGitHub f7321c9463 build(visor): GOEXPERIMENT=jsonv2 per SCALE_STANDARD (#8)
* refactor: import github.com/hanzoai/iam (drop iamsdk/v2)

Switch from github.com/hanzoai/iamsdk/v2/iamsdk to canonical
github.com/hanzoai/iam root package. One canonical path, no drift.
Pinned to iam@86e34e93 SHA pseudo-version until v1.16.2 ships.

* build(visor): GOEXPERIMENT=jsonv2 in Dockerfile per SCALE_STANDARD

Per SCALE_STANDARD.md §2 — every Go production Dockerfile that emits
JSON to a client builds with GOEXPERIMENT=jsonv2. Verified on
hanzoai/zip's json_bench_test.go:
- Edge POST roundtrip: -12% time, -23% allocs
- Marshal-only: -22% time
- Unmarshal-only: -19% time, -25% allocs

build.sh inherits the ENV from the parent ARG so the existing go-build
invocation picks up jsonv2 unchanged. The conn-memory regression test
is deferred to a follow-up that migrates visor's beego v1.12 listener
to zip per §9 step-1.
2026-05-29 21:05:29 -07:00

86 lines
2.1 KiB
Go

// Copyright 2024 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 routers
import (
"fmt"
"net/http"
"github.com/beego/beego/context"
"github.com/hanzoai/visor/conf"
"github.com/hanzoai/visor/util"
iam "github.com/hanzoai/iam"
)
type Response struct {
Status string `json:"status"`
Msg string `json:"msg"`
Data interface{} `json:"data"`
Data2 interface{} `json:"data2"`
}
func GetSessionUser(ctx *context.Context) *iam.User {
s := ctx.Input.Session("user")
if s == nil {
return nil
}
claims := s.(iam.Claims)
return &claims.User
}
func getUsername(ctx *context.Context) (username string) {
user := GetSessionUser(ctx)
if user != nil {
username = util.GetIdFromOwnerAndName(user.Owner, user.Name)
} else {
username, _ = getUsernameByClientIdSecret(ctx)
}
return
}
func requestDeny(ctx *context.Context) {
ctx.ResponseWriter.WriteHeader(http.StatusForbidden)
response := &Response{
Status: "error",
Msg: "Unauthorized operation",
}
err := ctx.Output.JSON(response, false, false)
if err != nil {
return
}
}
func getUsernameByClientIdSecret(ctx *context.Context) (string, error) {
clientId, clientSecret, ok := ctx.Request.BasicAuth()
if !ok {
clientId = ctx.Input.Query("clientId")
clientSecret = ctx.Input.Query("clientSecret")
}
if clientId == "" || clientSecret == "" {
return "", nil
}
applicationName := conf.GetConfigString("iamApplication")
if clientSecret != conf.GetConfigString("clientSecret") {
return "", fmt.Errorf("Incorrect client secret for application: %s", applicationName)
}
return fmt.Sprintf("app/%s", applicationName), nil
}