The Gitea repository was renamed brain -> superbrain (server-side redirect already in place; local origin updated at 0.3.5). Carry the rename through for accurate identity, the 0.3.2 precedent: go.mod + every internal import, the agent's outbound User-Agent contact URL, README/RUNNING clone URLs and local notes. CHANGELOG history entries stay as written. Pure identifier rename, no runtime behavior change; verified with build + vet + unit tests and the full database integration suite under -race.
116 lines
3.7 KiB
Go
116 lines
3.7 KiB
Go
package scripts
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"gitea.federated.computer/Federated_Computer/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
|
|
}
|