fix(governance): ask for the public URL during owner setup (#2076)

This commit is contained in:
sam committed 2026-11-27 02:33:15 +11:00
1 parent 24a3efcf60
commit 6b4ff8101a
11 files changed
+147 -147

No files matched your search

+13 -10
View File
@@ -193,26 +193,29 @@ func (m *Module) authStatus(ctx context.Context, in *sessionInput) (*authStatusR
}
type registration struct {
PublicURL string `json:"public_url,omitempty"`
WorkspaceName string `json:"workspace_name,omitempty"`
OrganisationName string `json:"organisation_name,omitempty"`
Email string `json:"email"`
DisplayName string `json:"display_name"`
Password string `json:"password"`
Role string `json:"role,omitempty"`
Email string `json:"email"`
DisplayName string `json:"display_name"`
Password string `json:"password"`
Role string `json:"role,omitempty"`
}
func (r registration) account() AccountRegistration {
return AccountRegistration(r)
return AccountRegistration{Email: r.Email, DisplayName: r.DisplayName, Password: r.Password, Role: r.Role}
}
type setUpInput struct {
Client client
Body registration
Body struct {
PublicURL string `json:"public_url" doc:"The public origin of this Archie instance"`
Email string `json:"email"`
DisplayName string `json:"display_name"`
Password string `json:"password"`
}
}
func (m *Module) setUpAccount(ctx context.Context, in *setUpInput) (*userReply, error) {
if _, err := m.RegisterAccount(ctx, in.Body.account(), true); err != nil {
r := in.Body
if _, err := m.RegisterAccount(ctx, AccountRegistration{PublicURL: r.PublicURL, Email: r.Email, DisplayName: r.DisplayName, Password: r.Password}, true); err != nil {
return nil, m.accountError(ctx, err)
}
return m.openSession(ctx, &in.Client, in.Body.Email, in.Body.Password)
+27
View File
@@ -0,0 +1,27 @@
package governance
import (
"errors"
"testing"
)
// TestBootstrapRequiresThePublicOrigin guards the WebAuthn origin without requiring names for built-in rows.
func TestBootstrapRequiresThePublicOrigin(t *testing.T) {
for _, tc := range []struct {
url string
wantErr error
}{
{"https://archie.example.test/", nil},
{"", ErrInvalidIdentity},
{"https://archie.example.test/some/path", ErrInvalidIdentity},
} {
r := AccountRegistration{PublicURL: tc.url, Email: "owner@example.test", DisplayName: "Owner"}
role, org, err := registrationScope(t.Context(), &r, true)
if !errors.Is(err, tc.wantErr) {
t.Errorf("register with origin %q: %v, want %v", tc.url, err, tc.wantErr)
}
if err == nil && (r.PublicURL != "https://archie.example.test" || role != RoleOwner || org != DefaultOrgID) {
t.Errorf("bootstrap scope = %s/%s, origin = %q", org, role, r.PublicURL)
}
}
}
+5 -7
View File
@@ -387,13 +387,11 @@ const AccountSessionLifetime = 7 * 24 * time.Hour
// AccountRegistration carries the fields needed to register an account or
// bootstrap the instance owner.
type AccountRegistration struct {
PublicURL string `json:"public_url"`
WorkspaceName string `json:"workspace_name"`
OrganisationName string `json:"organisation_name"`
Email string `json:"email"`
DisplayName string `json:"display_name"`
Password string `json:"password"`
Role string `json:"role"`
PublicURL string `json:"public_url"`
Email string `json:"email"`
DisplayName string `json:"display_name"`
Password string `json:"password"`
Role string `json:"role"`
}
// Validate normalises email and display name and checks their bounds.
+14 -29
View File
@@ -35,20 +35,17 @@ type AuthSettingsRow struct {
// RegisterAccountParams carries the validated inputs to create a user account.
type RegisterAccountParams struct {
IdentityID string
Email string
DisplayName string
PasswordHash string
OrgID string
OrgName string
WorkspaceID string
WorkspaceName string
PublicURL string
Role string
ActorID string
RequestID string
Bootstrap bool
Now time.Time
IdentityID string
Email string
DisplayName string
PasswordHash string
OrgID string
PublicURL string
Role string
ActorID string
RequestID string
Bootstrap bool
Now time.Time
}
// AccountSetupRequired reports whether no local accounts exist yet.
@@ -76,7 +73,7 @@ func (s *Store) RegisterAccount(ctx context.Context, p RegisterAccountParams) (I
return ErrCredentialRejected
}
if p.Bootstrap {
if err := bootstrapOrgAndSettings(ctx, tx, p); err != nil {
if err := saveBootstrapOrigin(ctx, tx, p.PublicURL); err != nil {
return err
}
}
@@ -85,22 +82,10 @@ func (s *Store) RegisterAccount(ctx context.Context, p RegisterAccountParams) (I
return out, err
}
func bootstrapOrgAndSettings(ctx context.Context, tx *sqlx.Tx, p RegisterAccountParams) error {
if _, err := tx.ExecContext(ctx, `
INSERT INTO governance.orgs (id, name, created_at, updated_at) VALUES ($1, $2, $3, $3)
ON CONFLICT (id) DO UPDATE SET name = EXCLUDED.name, updated_at = EXCLUDED.updated_at`,
p.OrgID, p.OrgName, p.Now); err != nil {
return err
}
if _, err := tx.ExecContext(ctx, `
INSERT INTO governance.workspaces (id, org_id, name, created_at, updated_at) VALUES ($1, $2, $3, $4, $4)
ON CONFLICT (org_id, id) DO UPDATE SET name = EXCLUDED.name, updated_at = EXCLUDED.updated_at`,
p.WorkspaceID, p.OrgID, p.WorkspaceName, p.Now); err != nil {
return err
}
func saveBootstrapOrigin(ctx context.Context, tx *sqlx.Tx, origin string) error {
_, err := tx.ExecContext(ctx, `
INSERT INTO governance.authentication_settings (singleton, public_url) VALUES (true, $1)
ON CONFLICT (singleton) DO UPDATE SET public_url = EXCLUDED.public_url`, p.PublicURL)
ON CONFLICT (singleton) DO UPDATE SET public_url = EXCLUDED.public_url`, origin)
return err
}
+2 -8
View File
@@ -386,8 +386,7 @@ func (m *Module) RegisterAccount(ctx context.Context, r AccountRegistration, boo
}
row, err := m.store.RegisterAccount(ctx, store.RegisterAccountParams{
IdentityID: string(id), Email: r.Email, DisplayName: r.DisplayName,
PasswordHash: hash, OrgID: string(orgID), OrgName: r.OrganisationName,
WorkspaceID: string(DefaultWorkspaceID), WorkspaceName: r.WorkspaceName,
PasswordHash: hash, OrgID: string(orgID),
PublicURL: r.PublicURL, Role: string(role), ActorID: string(actor),
RequestID: "account:" + rand.Text(), Bootstrap: bootstrap, Now: time.Now().UTC(),
})
@@ -408,12 +407,7 @@ func registrationScope(ctx context.Context, r *AccountRegistration, bootstrap bo
return "", "", err
}
if bootstrap {
r.OrganisationName = strings.TrimSpace(r.OrganisationName)
r.WorkspaceName = strings.TrimSpace(r.WorkspaceName)
r.PublicURL = strings.TrimRight(r.PublicURL, "/")
if r.WorkspaceName == "" || len(r.WorkspaceName) > 200 || r.OrganisationName == "" || len(r.OrganisationName) > 200 {
return "", "", ErrInvalidIdentity
}
r.PublicURL = strings.TrimRight(strings.TrimSpace(r.PublicURL), "/")
if err := (AuthenticationSettings{PublicURL: r.PublicURL}).Validate(); err != nil {
return "", "", err
}