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
+124
-17
No files matched your search
@@ -218,14 +218,16 @@ func (c *Client) tokenFromRequest(r *http.Request) string {
|
||||
// cookie the same way Middleware does (see tokenFromRequest).
|
||||
|
||||
// PublicUser is the account payload register/login/me return: {"id",
|
||||
// "username", "email", "role"}. It is deliberately distinct from the slimmer
|
||||
// User type Validate/Middleware use (which mirrors GET
|
||||
// /api/session/validate's {"user_id", "username", "role"} instead).
|
||||
// "username", "email", "role", "totp_enabled"}. It is deliberately distinct
|
||||
// from the slimmer User type Validate/Middleware use (which mirrors GET
|
||||
// /api/session/validate's {"user_id", "username", "role"} instead, and does
|
||||
// not carry TOTP status at all).
|
||||
type PublicUser struct {
|
||||
ID int64 `json:"id"`
|
||||
Username string `json:"username"`
|
||||
Email string `json:"email"`
|
||||
Role string `json:"role"`
|
||||
ID int64 `json:"id"`
|
||||
Username string `json:"username"`
|
||||
Email string `json:"email"`
|
||||
Role string `json:"role"`
|
||||
TOTPEnabled bool `json:"totp_enabled"`
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
// 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
|
||||
// starting a session for it.
|
||||
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