Don't show the create-account form once AuthGuard already has an account
Accounts are centralized in AuthGuard now, so this project's own "first launch, create the admin account" flow needs to know whether AuthGuard already has one - not just whether *this* project's local setup has been claimed. A fresh instance pointed at an AuthGuard that already has a superadmin (created via AuthGuard's own web UI, or a sibling project's setup) was incorrectly still showing its own create-account form. Both the status check and the setup/registration handler now also consult AuthGuard's GET /api/setup-status (via the vendored authclient's new SetupStatus method) before deciding: if AuthGuard already has an account, show the login screen instead, and reject a setup attempt that reaches the handler anyway (e.g. a stale page) the same way an already-claimed local setup is rejected. Also updates the vendored shared/backend/authclient to add PublicUser's totp_enabled field (AuthGuard has reported it for a while; the client just hadn't picked it up). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
4e39b5ac67
commit
51e0a4217b
3 files changed
+118
-11
No files matched your search
+47
-8
@@ -77,19 +77,23 @@ import (
|
|||||||
// ----------
|
// ----------
|
||||||
// CollabTable's "first launch: create the first (superadmin) account" flow
|
// CollabTable's "first launch: create the first (superadmin) account" flow
|
||||||
// doesn't map onto AuthGuard, which is not scoped to a single project and
|
// doesn't map onto AuthGuard, which is not scoped to a single project and
|
||||||
// has no concept of "this is CollabTable's initial account" -- and unlike a
|
// has no concept of "this is CollabTable's initial account". CollabTable
|
||||||
// single-admin tool, "no accounts registered yet" can't be answered by
|
|
||||||
// asking AuthGuard either, since AuthGuard is a shared identity store other
|
|
||||||
// projects (and other CollabTable instances) may already have accounts in.
|
|
||||||
// Rather than trying to derive SetupRequired from AuthGuard state, CollabTable
|
|
||||||
// keeps a tiny local flag of its own -- collabtable_setup_state.claimed
|
// keeps a tiny local flag of its own -- collabtable_setup_state.claimed
|
||||||
// (db.go) -- that means only "has this CollabTable instance completed its
|
// (db.go) -- that means only "has this CollabTable instance completed its
|
||||||
// setup screen once", with no credentials of any kind in it. handleSetup
|
// setup screen once", with no credentials of any kind in it. handleSetup
|
||||||
// atomically claims that flag, registers the account against AuthGuard's
|
// atomically claims that flag, registers the account against AuthGuard's
|
||||||
// POST /api/register, and unclaims it again if registration fails (so a bad
|
// POST /api/register, and unclaims it again if registration fails (so a bad
|
||||||
// attempt doesn't permanently lock out setup). This is the smallest change
|
// attempt doesn't permanently lock out setup).
|
||||||
// that preserves the existing one-time-setup UX without forcing AuthGuard to
|
//
|
||||||
// answer a question it has no way to answer.
|
// The local claim alone would still show this "create account" form to a
|
||||||
|
// second, third, ... CollabTable instance's own first launch even though
|
||||||
|
// AuthGuard already has an account from the first one (or from AuthGuard's
|
||||||
|
// own web UI), since accounts are centralized -- there is no per-instance
|
||||||
|
// "initial account" once AuthGuard is shared. handleStatus and handleSetup
|
||||||
|
// both additionally call AuthGuard's GET /api/setup-status
|
||||||
|
// (authclient.Client.SetupStatus) and treat "AuthGuard already has an
|
||||||
|
// account" the same as "CollabTable's own setup is already claimed": show
|
||||||
|
// the login screen, refuse another registration through this flow.
|
||||||
//
|
//
|
||||||
// Known gaps versus the pre-migration behavior (AuthGuard was deliberately
|
// Known gaps versus the pre-migration behavior (AuthGuard was deliberately
|
||||||
// not modified for this migration, so these can only be closed by adding to
|
// not modified for this migration, so these can only be closed by adding to
|
||||||
@@ -445,9 +449,26 @@ func (a *AuthService) handleStatus(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
if !claimed {
|
if !claimed {
|
||||||
|
// The local claim alone isn't enough to decide this: accounts are
|
||||||
|
// centralized in AuthGuard now, so another CollabTable instance (or
|
||||||
|
// AuthGuard's own web UI) may have already created one even though
|
||||||
|
// this instance's own setup was never claimed. Showing the "create
|
||||||
|
// your account" form in that case would let someone spin up a
|
||||||
|
// redundant plain user instead of just logging into the account that
|
||||||
|
// already exists -- see setupRequired below.
|
||||||
|
setupRequired, err := a.authGuard.SetupStatus(r.Context())
|
||||||
|
if err != nil {
|
||||||
|
writeJSONError(w, http.StatusInternalServerError, errAuthGuardUnavailable.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if setupRequired {
|
||||||
writeJSON(w, http.StatusOK, authStatusResponse{SetupRequired: true})
|
writeJSON(w, http.StatusOK, authStatusResponse{SetupRequired: true})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
// AuthGuard already has an account; fall through to the ordinary
|
||||||
|
// "not authenticated" response so the frontend shows the login form
|
||||||
|
// instead of setup.
|
||||||
|
}
|
||||||
|
|
||||||
profile, _, err := a.currentProfile(r)
|
profile, _, err := a.currentProfile(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -507,6 +528,24 @@ func (a *AuthService) handleSetup(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Belt-and-braces: handleStatus already keeps the frontend from ever
|
||||||
|
// showing this form once AuthGuard has any account (see its own
|
||||||
|
// SetupStatus check), but a stale page or a direct API call could still
|
||||||
|
// reach this handler. Reject it the same way as the local claim conflict
|
||||||
|
// above rather than creating a redundant plain user account.
|
||||||
|
if setupRequired, err := a.authGuard.SetupStatus(r.Context()); err != nil || !setupRequired {
|
||||||
|
if unclaimErr := a.db.UnclaimSetup(r.Context()); unclaimErr != nil {
|
||||||
|
writeJSONError(w, http.StatusInternalServerError, "Internal server error")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
writeJSONError(w, http.StatusInternalServerError, errAuthGuardUnavailable.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
writeJSONError(w, http.StatusConflict, errSetupComplete.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
result, err := a.authGuard.Register(r.Context(), username, email, request.Password)
|
result, err := a.authGuard.Register(r.Context(), username, email, request.Password)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// The claim slot is only valid once actually backed by a real
|
// The claim slot is only valid once actually backed by a real
|
||||||
|
|||||||
@@ -218,14 +218,16 @@ func (c *Client) tokenFromRequest(r *http.Request) string {
|
|||||||
// cookie the same way Middleware does (see tokenFromRequest).
|
// cookie the same way Middleware does (see tokenFromRequest).
|
||||||
|
|
||||||
// PublicUser is the account payload register/login/me return: {"id",
|
// PublicUser is the account payload register/login/me return: {"id",
|
||||||
// "username", "email", "role"}. It is deliberately distinct from the slimmer
|
// "username", "email", "role", "totp_enabled"}. It is deliberately distinct
|
||||||
// User type Validate/Middleware use (which mirrors GET
|
// from the slimmer User type Validate/Middleware use (which mirrors GET
|
||||||
// /api/session/validate's {"user_id", "username", "role"} instead).
|
// /api/session/validate's {"user_id", "username", "role"} instead, and does
|
||||||
|
// not carry TOTP status at all).
|
||||||
type PublicUser struct {
|
type PublicUser struct {
|
||||||
ID int64 `json:"id"`
|
ID int64 `json:"id"`
|
||||||
Username string `json:"username"`
|
Username string `json:"username"`
|
||||||
Email string `json:"email"`
|
Email string `json:"email"`
|
||||||
Role string `json:"role"`
|
Role string `json:"role"`
|
||||||
|
TOTPEnabled bool `json:"totp_enabled"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// ManagedUser is one row of GET /api/admin/users, and the payload returned
|
// ManagedUser is one row of GET /api/admin/users, and the payload returned
|
||||||
@@ -359,6 +361,23 @@ func (c *Client) call(ctx context.Context, method, path, token string, body, out
|
|||||||
return resp, nil
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetupStatus calls GET /api/setup-status, an unauthenticated endpoint that
|
||||||
|
// reports whether AuthServer has any account at all. Accounts are
|
||||||
|
// centralized in AuthServer, so a caller with its own "first launch, create
|
||||||
|
// the initial admin" flow should consult this first: when it reports false,
|
||||||
|
// some account already exists (possibly created by a different project, or
|
||||||
|
// directly against AuthServer), and the caller should show its ordinary
|
||||||
|
// login screen instead of its own account-creation form.
|
||||||
|
func (c *Client) SetupStatus(ctx context.Context) (bool, error) {
|
||||||
|
var out struct {
|
||||||
|
SetupRequired bool `json:"setup_required"`
|
||||||
|
}
|
||||||
|
if _, err := c.call(ctx, http.MethodGet, "/api/setup-status", "", nil, &out); err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
return out.SetupRequired, nil
|
||||||
|
}
|
||||||
|
|
||||||
// Register calls POST /api/register, creating a new AuthServer account and
|
// Register calls POST /api/register, creating a new AuthServer account and
|
||||||
// starting a session for it.
|
// starting a session for it.
|
||||||
func (c *Client) Register(ctx context.Context, username, email, password string) (*SessionResult, error) {
|
func (c *Client) Register(ctx context.Context, username, email, password string) (*SessionResult, error) {
|
||||||
|
|||||||
@@ -0,0 +1,49 @@
|
|||||||
|
package authclient
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestSetupStatus(t *testing.T) {
|
||||||
|
cases := []struct {
|
||||||
|
name string
|
||||||
|
response any
|
||||||
|
want bool
|
||||||
|
}{
|
||||||
|
{name: "no accounts yet", response: map[string]bool{"setup_required": true}, want: true},
|
||||||
|
{name: "an account already exists", response: map[string]bool{"setup_required": false}, want: false},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range cases {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Method != http.MethodGet || r.URL.Path != "/api/setup-status" {
|
||||||
|
t.Fatalf("unexpected request: %s %s", r.Method, r.URL.Path)
|
||||||
|
}
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
_ = json.NewEncoder(w).Encode(tc.response)
|
||||||
|
}))
|
||||||
|
defer server.Close()
|
||||||
|
|
||||||
|
client := New(server.URL)
|
||||||
|
got, err := client.SetupStatus(context.Background())
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("SetupStatus() error = %v", err)
|
||||||
|
}
|
||||||
|
if got != tc.want {
|
||||||
|
t.Fatalf("SetupStatus() = %v, want %v", got, tc.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSetupStatusPropagatesTransportError(t *testing.T) {
|
||||||
|
client := New("http://127.0.0.1:0")
|
||||||
|
if _, err := client.SetupStatus(context.Background()); err == nil {
|
||||||
|
t.Fatal("SetupStatus() expected an error against an unreachable server, got nil")
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user