Extend Go/Python/Node authclient with the full AuthGuard API surface
Previously each authclient only supported GET /api/session/validate - the "just gate requests behind a valid session" case. A service that runs its own login/register/admin-user-management screens proxied to AuthGuard (GameStream's pattern, now also used by InfiniteVideo and OutdoorGames) needs much more: register, login, logout, me, totp_enable/verify/disable, password-reset request/confirm, and managed-user list/create/update/delete. The Go client already had this full surface from GameStream's original migration; Python and Node only had validate(), so both gained the same method set as Go, mirroring its behavior and JSON wire shapes exactly (register/login/logout return the raw Set-Cookie values to forward, totp_required surfaces on a 401 from login, etc). Also adds a SetupStatus/ setup_status/setupStatus method (GET /api/setup-status) so a service's own first-launch flow can tell whether AuthGuard already has an account before showing its own account-creation form, and adds totp_enabled to the Go client's PublicUser (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
db733a6bf4
commit
f5a20b72af
7 files changed
+1169
-42
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