fix(auth): an invite is the only way to an account

The README offered open registration, and the register handler still had
a branch for a registration_settings mode nothing could set. Now every
account after the first needs an invite; the first still needs the setup
token from the log. Migration 0110 drops registration_settings and its
queries, and backups no longer carry it (an older backup that holds it
restores without it). The web form asks for the invite token as required.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-26 08:59:25 -05:00
co-authored by Claude Opus 5.5
parent de335333da
commit dd27e014c0
10 changed files with 69 additions and 137 deletions
+14 -57
View File
@@ -76,7 +76,6 @@ func TestRegister_InviteOnlyMode_RequiresToken(t *testing.T) {
h, pool := testHandlers(t)
// Seed an admin user so we're past the empty-users state.
seedUser(t, pool, "existingadmin", "pw", true)
// Mode is default invite_only.
body := `{"username":"newuser","password":"abcd1234"}`
req := httptest.NewRequest(http.MethodPost, "/api/auth/register", bytes.NewReader([]byte(body)))
@@ -151,62 +150,28 @@ func TestRegister_InviteOnlyMode_InvalidTokenRejects(t *testing.T) {
}
}
func TestRegister_OpenMode_NoTokenNeeded(t *testing.T) {
if os.Getenv("MINSTREL_TEST_DATABASE_URL") == "" {
t.Skip("MINSTREL_TEST_DATABASE_URL not set")
}
h, pool := testHandlers(t)
seedUser(t, pool, "preexisting", "pw", true)
// Switch to open mode; restore invite_only afterwards so other tests are
// not affected (registration_settings is a singleton not truncated by
// ResetDB).
if _, err := pool.Exec(context.Background(),
"UPDATE registration_settings SET mode = 'open' WHERE id = true"); err != nil {
t.Fatalf("set mode: %v", err)
}
t.Cleanup(func() {
if _, err := pool.Exec(context.Background(),
"UPDATE registration_settings SET mode = 'invite_only' WHERE id = true"); err != nil {
t.Logf("cleanup: restore mode failed: %v", err)
}
})
body := `{"username":"openreg","password":"abcd1234"}`
req := httptest.NewRequest(http.MethodPost, "/api/auth/register", bytes.NewReader([]byte(body)))
rec := httptest.NewRecorder()
h.handleRegister(rec, req)
if rec.Code != http.StatusOK {
t.Errorf("status = %d, want 200; body=%s", rec.Code, rec.Body.String())
}
}
func TestRegister_DuplicateUsername_409(t *testing.T) {
if os.Getenv("MINSTREL_TEST_DATABASE_URL") == "" {
t.Skip("MINSTREL_TEST_DATABASE_URL not set")
}
h, pool := testHandlers(t)
resetUsers(t, h)
// Switch to open mode so the second attempt is not rejected by invite
// check before it can reach the INSERT and hit the unique violation.
if _, err := pool.Exec(context.Background(),
"UPDATE registration_settings SET mode = 'open' WHERE id = true"); err != nil {
t.Fatalf("set mode: %v", err)
}
t.Cleanup(func() {
if _, err := pool.Exec(context.Background(),
"UPDATE registration_settings SET mode = 'invite_only' WHERE id = true"); err != nil {
t.Logf("cleanup: restore mode failed: %v", err)
}
})
resetInvites(t, h)
// First register succeeds (empty-users state → first-admin path).
body := `{"username":"taken","password":"abcd1234"}`
req := httptest.NewRequest(http.MethodPost, "/api/auth/register", bytes.NewReader([]byte(body)))
h.handleRegister(httptest.NewRecorder(), req)
// Second register with same username in open mode → 409 (unique violation).
req2 := httptest.NewRequest(http.MethodPost, "/api/auth/register", bytes.NewReader([]byte(body)))
// A valid invite gets the second attempt past the invite check, to the
// INSERT and its unique violation.
if _, err := pool.Exec(context.Background(),
`INSERT INTO user_invites (token, invited_by, expires_at)
SELECT 'dup-invite', id, now() + interval '24 hours' FROM users WHERE username = 'taken'`); err != nil {
t.Fatalf("seed invite: %v", err)
}
dup := `{"username":"taken","password":"abcd1234","invite_token":"dup-invite"}`
req2 := httptest.NewRequest(http.MethodPost, "/api/auth/register", bytes.NewReader([]byte(dup)))
rec2 := httptest.NewRecorder()
h.handleRegister(rec2, req2)
@@ -315,18 +280,10 @@ func TestRegister_FirstUserNeedsSetupToken(t *testing.T) {
t.Fatalf("right token: status = %d, want 200", code)
}
// Once an account exists the token plays no part: the second user is
// governed by registration mode, not the setup token.
if _, err := h.pool.Exec(context.Background(),
`UPDATE registration_settings SET mode = 'open' WHERE id = true`); err != nil {
t.Fatalf("open mode: %v", err)
}
t.Cleanup(func() {
_, _ = h.pool.Exec(context.Background(),
`UPDATE registration_settings SET mode = 'invite_only' WHERE id = true`)
})
if code := register(`{"username":"second","password":"abcd1234"}`); code != http.StatusOK {
t.Errorf("second user without token: status = %d, want 200", code)
// Once an account exists the setup token opens nothing: the second
// account needs an invite, and there is no way to register without one.
if code := register(`{"username":"second","password":"abcd1234","setup_token":"` + token.Value() + `"}`); code != http.StatusBadRequest {
t.Errorf("second user with the setup token and no invite: status = %d, want 400", code)
}
}