Replace console password auth with a passwordless surface — the pre-session
login doors plus an identifier-first discovery endpoint — and remove the
password paths.
- Login doors (Public, pre-session): email-OTP, passkey assertion, op.console
login with in-game approval, and setup-token redeem.
- /api/v1/auth/options: identifier-first discovery reporting which console
methods an email can use. The single sanctioned existence oracle; methods
are computed with no role branch, so staff and player accounts in the same
credential state return byte-identical bodies (staffness invisible by
construction).
- Remove password auth: drop StaffUser.PasswordHash and the /auth/login,
/auth/change-password and /users/{id}/reset-password endpoints (and test).
- Data layer: UserByEmail, verified-email uniqueness, setup-token store
(migration 0012).
- Reconcile docs/openapi.yaml with the served surface; the method/path/face/
tier parity gate (TestOpenAPIMatchesServedRoutes) passes.
- felis TUI: in-game MC bind, owner/break-glass OP provisioning, version.
- Velocity /felis command suite.
Consolidates the accumulated backend migration work; the frontend (panel/)
is left untouched. Full Go tree green on WSL (go build ./... && go test ./...).
112 lines
5.4 KiB
Go
112 lines
5.4 KiB
Go
package api
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
)
|
|
|
|
// Sentinel errors the repository and cluster layers return so handlers can map
|
|
// domain outcomes onto HTTP status codes without leaking driver details.
|
|
var (
|
|
// ErrNotFound means the requested server / record does not exist.
|
|
ErrNotFound = errors.New("not found")
|
|
// ErrConflict means an atomic precondition failed (e.g. claim lost the race).
|
|
ErrConflict = errors.New("conflict")
|
|
// ErrLinkCodeInvalid means an account-link code is unknown or expired (spec
|
|
// §10). It is a client error (the verify endpoint exists; the code is bad), so
|
|
// handlers map it to 400, not 404.
|
|
ErrLinkCodeInvalid = errors.New("link code invalid or expired")
|
|
// ErrConsoleUnavailable means the RCON write channel could not be reached —
|
|
// the dial timed out, was refused, or the password was rejected (spec §8).
|
|
// Because readiness IS an RCON probe (spec §141: phase=Running ⟺ RCON
|
|
// answers), a reachable failure here is a transient/racy "the server isn't
|
|
// actually up", not a server bug. Handlers map it to 503, not 500, so the
|
|
// caller is told to wake/retry rather than shown an opaque internal error.
|
|
ErrConsoleUnavailable = errors.New("server console is unavailable")
|
|
// ErrOTPInvalid means an email one-time code is unknown, expired, already
|
|
// consumed, or did not match (spec §B2 onboarding). Like ErrLinkCodeInvalid it
|
|
// is a client error — the verify endpoint exists; the code is bad — so handlers
|
|
// map it to 400, not 404. A wrong-but-not-yet-locked guess collapses to it too,
|
|
// so the response never distinguishes "no such code" from "wrong digits".
|
|
ErrOTPInvalid = errors.New("email code invalid or expired")
|
|
// ErrOTPLocked means the live email code has exhausted its attempt budget: too
|
|
// many wrong guesses (spec §B2). It is distinct from ErrOTPInvalid so handlers
|
|
// can answer 429 (back off / request a new code) rather than inviting another
|
|
// guess against a code that will never accept one.
|
|
ErrOTPLocked = errors.New("email code locked: too many attempts")
|
|
// ErrPasskeyChallengeInvalid means a passkey enrollment ceremony cannot be
|
|
// finished: there is no live (unconsumed, unexpired) challenge for the caller and
|
|
// purpose (Phase 6 WebAuthn bind). Like ErrOTPInvalid it is a client error — the
|
|
// finish endpoint exists; the ceremony state is gone (never begun, already
|
|
// consumed, or expired) — so handlers map it to 400, not 404.
|
|
ErrPasskeyChallengeInvalid = errors.New("passkey challenge invalid or expired")
|
|
// ErrPlayerBindForbidden means a public Bind-Code redemption resolved to a STAFF
|
|
// account (role=admin), which the player-console bootstrap refuses (console-tier
|
|
// access model). Operators authenticate at op.console behind Zero Trust, never via
|
|
// the account-less console.<root_domain> door, so the public bootstrap provably
|
|
// never mints a session for an admin identity. It is distinct from ErrConflict so
|
|
// the handler answers 403 (wrong door) rather than 409 (already linked).
|
|
ErrPlayerBindForbidden = errors.New("bind code belongs to a staff account")
|
|
// ErrEmailTaken means a verified email would collide with another account's
|
|
// already-verified address (spec §B email-first login foundation, migration 0010).
|
|
// VerifyEmailOTP returns it — WITHOUT consuming the code, since the address, not
|
|
// the code, is the problem — when a DIFFERENT user has already proven the same
|
|
// address case-insensitively. It is the clean, application-level counterpart of
|
|
// the users_verified_email_unique index: a sequential double-verify meets this
|
|
// guard and gets a 409 instead of a raw unique-violation 500. Distinct from
|
|
// ErrConflict so the message can name the cause (the email is spoken for).
|
|
ErrEmailTaken = errors.New("email already verified on another account")
|
|
)
|
|
|
|
// apiError is a handler-level error carrying an HTTP status and a stable,
|
|
// machine-readable code. The error envelope matches the platform convention:
|
|
//
|
|
// {"error": {"code": "...", "message": "...", "request_id": "..."}}
|
|
type apiError struct {
|
|
status int
|
|
code string
|
|
msg string
|
|
}
|
|
|
|
func (e *apiError) Error() string { return e.msg }
|
|
|
|
// newError builds an apiError with a formatted message.
|
|
func newError(status int, code, format string, a ...any) *apiError {
|
|
return &apiError{status: status, code: code, msg: fmt.Sprintf(format, a...)}
|
|
}
|
|
|
|
// Common errors reused across handlers.
|
|
var (
|
|
errUnauthorized = newError(http.StatusUnauthorized, "unauthorized", "authentication required")
|
|
errForbidden = newError(http.StatusForbidden, "forbidden", "not permitted")
|
|
errBadRequest = newError(http.StatusBadRequest, "bad_request", "invalid request")
|
|
)
|
|
|
|
// writeJSON writes v as an indented JSON body with the given status.
|
|
func writeJSON(w http.ResponseWriter, status int, v any) {
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
w.WriteHeader(status)
|
|
enc := json.NewEncoder(w)
|
|
enc.SetEscapeHTML(false)
|
|
_ = enc.Encode(v)
|
|
}
|
|
|
|
// writeError renders err as the standard error envelope. Non-apiError values
|
|
// collapse to a 500 so driver/internal details never reach the client.
|
|
func writeError(w http.ResponseWriter, r *http.Request, err error) {
|
|
var ae *apiError
|
|
if !errors.As(err, &ae) {
|
|
ae = newError(http.StatusInternalServerError, "internal", "internal error")
|
|
}
|
|
body := map[string]any{
|
|
"error": map[string]string{
|
|
"code": ae.code,
|
|
"message": ae.msg,
|
|
"request_id": requestIDFromContext(r.Context()),
|
|
},
|
|
}
|
|
writeJSON(w, ae.status, body)
|
|
}
|