release gate / check (push) Failing after 7m28s
Product tiers (decided 2026-10-26): TIER=core|multi is the deployment knob. core (federated install, paying customers) derives AUTH_MODE=oidc with signup/bootstrap off - Authelia is the only way in and accounts are provisioned upstream (Superpanel -> LDAP -> Authelia). multi (hosted self-service) derives AUTH_MODE=password: signup, password login and SMTP-backed self-reset, with the OIDC bridge refused even when variables are set. Explicit AUTH_MODE/SIGNUP_ENABLED/ALLOW_BOOTSTRAP_SIGNUP always override the tier; invalid values fail startup instead of falling back. Go enforces every gate at the API; the web UI mirrors it (conditional credentials provider, SSO-only login page, signup/reset redirects). Authelia-only bootstrap: ProvisionExternal grants ADMIN to ADMIN_EMAILS addresses at first SSO login, and --seed accepts SEED_SUBJECT to link a pre-existing password account offline - the only path when 409 'account link required' meets a deployment where nobody can password-login to call oidc-link. Self-service password reset (multi tier): POST /api/password/forgot and /api/password/reset with hashed single-use 30-minute tokens (PasswordResetToken, migration 011 + schema.sql), enumeration-safe identical 200s, per-IP rate limits, tokenVersion bump on success (all prior sessions revoked), OIDC-only accounts excluded by design. New internal/mailer sends via netguard+outbound budget; NETGUARD_TRUSTED_HOSTS pins operator-chosen relays that resolve privately (split-horizon DNS) without the blanket INTEGRATIONS_ALLOW_PRIVATE switch. Web gains /forgot-password and /reset-password pages and a login-page link. Federated install fixes: the Authelia client now registers a claims_policy putting email in the ID token (production AccessDenied - Auth.js builds OIDC profiles exclusively from ID-token claims), config is validated before restarting the shared Authelia, uninstall removes the claims block marker-safely, and generated .env switches to TIER=core with a password-less admin seed. Tests: tier derivation/override unit tests, mode and tier integration tests (403 gates, bridge refusal, ADMIN_EMAILS promotion, offline link), reset-flow integration tests against a loopback SMTP fake, netguard trusted-host tests. Docs: RUNNING.md, web/docs/AUTHELIA.md section 7, .env.example, STRATEGY.md.
116 lines
3.7 KiB
Go
116 lines
3.7 KiB
Go
package scripts
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"github.com/andrew/superbrain/internal/db"
|
|
"golang.org/x/crypto/bcrypt"
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
// Seed creates or links the requested operator account; it never clears
|
|
// existing records.
|
|
//
|
|
// SEED_EMAIL required
|
|
// SEED_PASSWORD password for a local login account
|
|
// SEED_SUBJECT optional Authelia subject to link (provider "authelia")
|
|
// SEED_NAME optional display name
|
|
//
|
|
// SEED_SUBJECT is the offline escape hatch for AUTH_MODE=oidc deployments
|
|
// (core/federated shape): password login is disabled there, so an operator
|
|
// cannot sign in to call POST /api/admin/oidc-link — and a pre-existing
|
|
// password account (e.g. one created by an earlier seed) makes the SSO
|
|
// bridge answer 409 "Account link required" instead of auto-linking by
|
|
// email. Linking from the CLI is deliberate operator action, which is the
|
|
// proof-of-ownership the email match alone can never provide.
|
|
//
|
|
// Combinations:
|
|
// - password only: classic local account (existing email: no-op).
|
|
// - subject only, or neither: password-less SSO account. In AUTH_MODE=oidc
|
|
// this is the correct operator seed — a password would be unusable
|
|
// (login is disabled) AND would block the SSO bridge's verified-email
|
|
// auto-link with a 409. With SEED_SUBJECT the link is explicit; without
|
|
// it, the first SSO login links by verified email match.
|
|
// - password + subject: local account that also accepts SSO.
|
|
//
|
|
// Role: ADMIN when SEED_EMAIL is listed in ADMIN_EMAILS, else USER. Idempotent
|
|
// for both the user row and the account link; a subject already linked to a
|
|
// DIFFERENT user is refused.
|
|
func Seed(ctx context.Context, d *db.DB) error {
|
|
email, password := strings.ToLower(strings.TrimSpace(os.Getenv("SEED_EMAIL"))), os.Getenv("SEED_PASSWORD")
|
|
subject := strings.TrimSpace(os.Getenv("SEED_SUBJECT"))
|
|
if email == "" {
|
|
return fmt.Errorf("SEED_EMAIL is required")
|
|
}
|
|
if len(subject) > 255 {
|
|
return fmt.Errorf("SEED_SUBJECT is too long")
|
|
}
|
|
repo := db.NewUserRepo(d)
|
|
// Pre-flight: a subject already linked to a DIFFERENT account is refused
|
|
// before anything is created, so a bad SEED_SUBJECT never leaves an
|
|
// orphan user behind.
|
|
if subject != "" {
|
|
if u, e := repo.FindByAccount(ctx, "authelia", subject); e == nil {
|
|
if u.Email == nil || *u.Email != email {
|
|
return fmt.Errorf("subject is already linked to a different user")
|
|
}
|
|
fmt.Printf("subject already linked to %s; nothing to do\n", email)
|
|
return nil
|
|
} else if !errors.Is(e, db.ErrNotFound) {
|
|
return e
|
|
}
|
|
}
|
|
role := "USER"
|
|
if db.IsAdminEmail(email) {
|
|
role = "ADMIN"
|
|
}
|
|
|
|
u, e := repo.FindByEmail(ctx, email)
|
|
if e != nil && !errors.Is(e, db.ErrNotFound) {
|
|
return e
|
|
}
|
|
if e == nil {
|
|
// Existing account: link the subject when asked, otherwise no-op.
|
|
if subject == "" {
|
|
return nil
|
|
}
|
|
if e := repo.LinkExternal(ctx, u.ID, "authelia", subject); e != nil {
|
|
if errors.Is(e, db.ErrAccountLinked) {
|
|
return fmt.Errorf("subject is already linked to a different user")
|
|
}
|
|
return e
|
|
}
|
|
fmt.Printf("linked authelia subject to existing account %s\n", email)
|
|
return nil
|
|
}
|
|
|
|
name := os.Getenv("SEED_NAME")
|
|
if name == "" {
|
|
name = "Admin"
|
|
}
|
|
in := &db.NewUserInput{ID: db.NewID(), Name: name, Email: email, Role: role}
|
|
if password == "" {
|
|
in.External = true // password-less: SSO only
|
|
} else {
|
|
hash, e := bcrypt.GenerateFromPassword([]byte(password), 12)
|
|
if e != nil {
|
|
return e
|
|
}
|
|
in.Password = string(hash)
|
|
}
|
|
if _, e := repo.Create(ctx, in); e != nil {
|
|
return e
|
|
}
|
|
if subject != "" {
|
|
if e := repo.LinkExternal(ctx, in.ID, "authelia", subject); e != nil {
|
|
if errors.Is(e, db.ErrAccountLinked) {
|
|
return fmt.Errorf("subject is already linked to a different user")
|
|
}
|
|
return e
|
|
}
|
|
}
|
|
return nil
|
|
}
|