The binary serves the account routes the Vue client already calls: status,
setup, password and passkey sign-in, recovery, sign-out, OIDC, account
passkeys, password and recovery codes, personal tokens and capabilities.
The API authenticates a session cookie, a personal token or the bootstrap
admin token, and unsafe requests pass cross-origin protection. The
dashboard serves the Vue app at /.
Copied from internal/webui/{accounts,passkeys,personal_tokens,
api_capabilities,auth}.go, internal/infrastructure/oidc/{flow,oidc}.go and
internal/app/archieui/account_oidc.go, which archie-ui still imports.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
76 lines
3.4 KiB
Go
76 lines
3.4 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/danielgtaylor/huma/v2"
|
|
)
|
|
|
|
// headerAuth admits the caller its X-Test-Caller header names.
|
|
type headerAuth struct{}
|
|
|
|
func (headerAuth) Authenticate(ctx context.Context, r *http.Request) (context.Context, bool, error) {
|
|
switch r.Header.Get("X-Test-Caller") {
|
|
case "admin":
|
|
return ctx, true, nil
|
|
case "person":
|
|
return ctx, false, nil
|
|
case "broken":
|
|
return nil, false, errors.New("database down")
|
|
default:
|
|
return nil, false, ErrUnauthenticated
|
|
}
|
|
}
|
|
|
|
// TestOperationAccess guards the API's authentication boundary: public
|
|
// operations need no credential, every other operation needs one, only an
|
|
// instance administrator reaches an operation not marked otherwise, and a
|
|
// cross-origin browser request cannot use the session cookie to change
|
|
// anything.
|
|
func TestOperationAccess(t *testing.T) {
|
|
tests := []struct {
|
|
name, method, path, caller, fetchSite string
|
|
want int
|
|
}{
|
|
{name: "public without a credential", method: http.MethodGet, path: "/public", want: http.StatusOK},
|
|
{name: "public sign-in from this origin", method: http.MethodPost, path: "/public", fetchSite: "same-origin", want: http.StatusOK},
|
|
{name: "public sign-in from another origin", method: http.MethodPost, path: "/public", fetchSite: "cross-site", want: http.StatusForbidden},
|
|
{name: "signed-in without a credential", method: http.MethodGet, path: "/signed-in", want: http.StatusUnauthorized},
|
|
{name: "signed-in as a person", method: http.MethodGet, path: "/signed-in", caller: "person", want: http.StatusOK},
|
|
{name: "admin without a credential", method: http.MethodGet, path: "/admin", want: http.StatusUnauthorized},
|
|
{name: "admin as a person", method: http.MethodGet, path: "/admin", caller: "person", want: http.StatusForbidden},
|
|
{name: "admin as the admin", method: http.MethodGet, path: "/admin", caller: "admin", want: http.StatusOK},
|
|
{name: "mutation from this origin", method: http.MethodPost, path: "/admin", caller: "admin", fetchSite: "same-origin", want: http.StatusOK},
|
|
{name: "mutation from another origin", method: http.MethodPost, path: "/admin", caller: "admin", fetchSite: "cross-site", want: http.StatusForbidden},
|
|
{name: "a failed lookup is not a refusal", method: http.MethodGet, path: "/admin", caller: "broken", want: http.StatusServiceUnavailable},
|
|
}
|
|
mux := http.NewServeMux()
|
|
api := New(mux, "test", headerAuth{})
|
|
reached := func(context.Context, *struct{}) (*Reply[string], error) { return OK("reached"), nil }
|
|
for _, method := range []string{http.MethodGet, http.MethodPost} {
|
|
huma.Register(api, Public(huma.Operation{OperationID: "public-" + method, Method: method, Path: "/public"}), reached)
|
|
huma.Register(api, SignedIn(huma.Operation{OperationID: "signed-in-" + method, Method: method, Path: "/signed-in"}), reached)
|
|
huma.Register(api, huma.Operation{OperationID: "admin-" + method, Method: method, Path: "/admin"}, reached)
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
req := httptest.NewRequestWithContext(t.Context(), tt.method, tt.path, nil)
|
|
if tt.caller != "" {
|
|
req.Header.Set("X-Test-Caller", tt.caller)
|
|
}
|
|
if tt.fetchSite != "" {
|
|
req.Header.Set("Sec-Fetch-Site", tt.fetchSite)
|
|
}
|
|
rec := httptest.NewRecorder()
|
|
mux.ServeHTTP(rec, req)
|
|
if rec.Code != tt.want {
|
|
t.Fatalf("status = %d, want %d: %s", rec.Code, tt.want, rec.Body)
|
|
}
|
|
})
|
|
}
|
|
}
|