mirror of
https://github.com/hanzoai/base.git
synced 2026-10-21 03:41:50 +00:00
One mount-prefix knob for the data plane: BASE_API_PREFIX (default /v1). Multi-app deployments set /v1/<app> so a gateway can route by path. Hanzo IAM is always a fixed sibling at /v1/iam — apps do not mount their own IAM at /v1/<app>/iam. Ripped: - BASE_AUTH_MODE env knob — was a no-op after the IAM-native rip (the "builtin" branch had no routes left to bind). - apis/superuser_auth_oidc.go (~460 lines) — stale parallel OIDC superuser path at /_/auth/oidc/*. The canonical path is /v1/iam/oauth/* via the platform plugin (proxy or embedded). - apis/base.go EXTERNAL_AUTH_ONLY env-var fallback — the platform plugin is mandatory and sets the store keys directly. One way only. installer.go: skip the first-superuser installer when the platform plugin has flagged external-only mode (IAM owns first login). LLM.md updated to reflect the new contract.
105 lines
2.8 KiB
Go
105 lines
2.8 KiB
Go
package apis
|
|
|
|
import (
|
|
"database/sql"
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/fatih/color"
|
|
"github.com/hanzoai/dbx"
|
|
"github.com/hanzoai/base/core"
|
|
"github.com/hanzoai/base/tools/osutils"
|
|
)
|
|
|
|
// DefaultInstallerFunc is the default Base installer function.
|
|
//
|
|
// It will attempt to open a link in the browser (with a short-lived auth
|
|
// token for the systemSuperuser) to the installer UI so that users can
|
|
// create their own custom superuser record.
|
|
//
|
|
// See https://github.com/hanzoai/base/discussions/5814.
|
|
func DefaultInstallerFunc(app core.App, systemSuperuser *core.Record, baseURL string) error {
|
|
token, err := systemSuperuser.NewStaticAuthToken(30 * time.Minute)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// launch url (ignore errors and always print a help text as fallback)
|
|
url := fmt.Sprintf("%s/_/#/baseinstal/%s", strings.TrimRight(baseURL, "/"), token)
|
|
_ = osutils.LaunchURL(url)
|
|
color.Magenta("\n(!) Launch the URL below in the browser if it hasn't been open already to create your first superuser account:")
|
|
color.New(color.Bold).Add(color.FgCyan).Println(url)
|
|
color.New(color.FgHiBlack, color.Italic).Printf("(you can also create your first superuser by running: %s superuser upsert EMAIL PASS)\n\n", executablePath())
|
|
|
|
return nil
|
|
}
|
|
|
|
func loadInstaller(
|
|
app core.App,
|
|
baseURL string,
|
|
installerFunc func(app core.App, systemSuperuser *core.Record, baseURL string) error,
|
|
) error {
|
|
// IAM is the only auth source — once the platform plugin marks the
|
|
// store as external-only, the local "first superuser" installer is
|
|
// moot. IAM seeds the superuser via EMBEDDED_IAM_ROOT_EMAIL (embedded
|
|
// mode) or via the upstream IAM tenant (external mode).
|
|
if externalOnly, _ := app.Store().Get(StoreKeyExternalAuthOnly).(bool); externalOnly {
|
|
return nil
|
|
}
|
|
|
|
if installerFunc == nil || !needInstallerSuperuser(app) {
|
|
return nil
|
|
}
|
|
|
|
superuser, err := findOrCreateInstallerSuperuser(app)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return installerFunc(app, superuser, baseURL)
|
|
}
|
|
|
|
func needInstallerSuperuser(app core.App) bool {
|
|
total, err := app.CountRecords(core.CollectionNameSuperusers, dbx.Not(dbx.HashExp{
|
|
"email": core.DefaultInstallerEmail,
|
|
}))
|
|
|
|
return err == nil && total == 0
|
|
}
|
|
|
|
func findOrCreateInstallerSuperuser(app core.App) (*core.Record, error) {
|
|
col, err := app.FindCachedCollectionByNameOrId(core.CollectionNameSuperusers)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
record, err := app.FindAuthRecordByEmail(col, core.DefaultInstallerEmail)
|
|
if err != nil {
|
|
if !errors.Is(err, sql.ErrNoRows) {
|
|
return nil, err
|
|
}
|
|
|
|
record = core.NewRecord(col)
|
|
record.SetEmail(core.DefaultInstallerEmail)
|
|
record.SetVerified(true)
|
|
|
|
err = app.Save(record)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
return record, nil
|
|
}
|
|
|
|
func executablePath() string {
|
|
if osutils.IsProbablyGoRun() {
|
|
return "go run ."
|
|
}
|
|
|
|
return os.Args[0]
|
|
}
|