feat(httpapi): the API is described by an OpenAPI document generated with Huma (#2028)
Every /api operation is registered with Huma behind one admin-token middleware, served at /api/openapi.json with a docs page at /api/docs, and committed as api/openapi.yaml, which task check keeps current.
This commit is contained in:
32 files changed
+4467
-1105
No files matched your search
+29
-15
@@ -4,6 +4,7 @@
|
||||
package system
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
@@ -11,6 +12,9 @@ import (
|
||||
"net/http"
|
||||
"sync"
|
||||
|
||||
"github.com/danielgtaylor/huma/v2"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/platform/httpapi"
|
||||
"github.com/samcharles93/archie-core/internal/platform/httpjson"
|
||||
)
|
||||
|
||||
@@ -39,39 +43,49 @@ func New(b Bootstrap, rotate func(hash string) error, log *slog.Logger) *Module
|
||||
return &Module{bootstrap: b, rotate: rotate, log: log}
|
||||
}
|
||||
|
||||
// Routes adds the system API to mux behind guard.
|
||||
func (m *Module) Routes(mux *http.ServeMux, guard func(http.Handler) http.Handler) {
|
||||
mux.Handle("GET /api/system/bootstrap", guard(http.HandlerFunc(m.getBootstrap)))
|
||||
mux.Handle("POST /api/admin/token", guard(http.HandlerFunc(m.rotateAdminToken)))
|
||||
// Register adds the system API.
|
||||
func (m *Module) Register(api huma.API) {
|
||||
tags := []string{"System"}
|
||||
huma.Register(api, huma.Operation{
|
||||
OperationID: "get-bootstrap", Method: http.MethodGet, Path: "/api/system/bootstrap",
|
||||
Summary: "Get the bootstrap values, which only the config file sets", Tags: tags,
|
||||
}, m.getBootstrap)
|
||||
huma.Register(api, huma.Operation{
|
||||
OperationID: "rotate-admin-token", Method: http.MethodPost, Path: "/api/admin/token",
|
||||
Summary: "Rotate the admin token; the new one is shown once", Tags: tags,
|
||||
}, m.rotateAdminToken)
|
||||
}
|
||||
|
||||
func (m *Module) getBootstrap(w http.ResponseWriter, _ *http.Request) {
|
||||
// adminToken is a new admin token, returned once.
|
||||
type adminToken struct {
|
||||
Token string `json:"token" doc:"Shown once; only its hash is written to the config"`
|
||||
}
|
||||
|
||||
func (m *Module) getBootstrap(_ context.Context, _ *struct{}) (*httpapi.Reply[Bootstrap], error) {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
httpjson.Write(w, http.StatusOK, m.bootstrap)
|
||||
return httpapi.OK(m.bootstrap), nil
|
||||
}
|
||||
|
||||
// rotateAdminToken generates a new token, writes only its hash to the config,
|
||||
// and returns the token once. The old token and the old hash are never read
|
||||
// back or logged.
|
||||
func (m *Module) rotateAdminToken(w http.ResponseWriter, r *http.Request) {
|
||||
func (m *Module) rotateAdminToken(ctx context.Context, _ *struct{}) (*httpapi.Reply[adminToken], error) {
|
||||
token, err := newToken()
|
||||
if err != nil {
|
||||
m.log.ErrorContext(r.Context(), "generate admin token", "error", err)
|
||||
httpjson.Error(w, http.StatusInternalServerError, "couldn't generate a token")
|
||||
return
|
||||
m.log.ErrorContext(ctx, "generate admin token", "error", err)
|
||||
return nil, huma.NewError(http.StatusInternalServerError, "couldn't generate a token")
|
||||
}
|
||||
hash := httpjson.TokenHash(token)
|
||||
if err := m.rotate(hash); err != nil {
|
||||
m.log.ErrorContext(r.Context(), "write admin token hash", "error", err)
|
||||
httpjson.Error(w, http.StatusInternalServerError, "couldn't write the new token hash to the config")
|
||||
return
|
||||
m.log.ErrorContext(ctx, "write admin token hash", "error", err)
|
||||
return nil, huma.NewError(http.StatusInternalServerError, "couldn't write the new token hash to the config")
|
||||
}
|
||||
m.mu.Lock()
|
||||
m.bootstrap.AdminPrefix, m.bootstrap.AdminSet = hash[:8], true
|
||||
m.mu.Unlock()
|
||||
m.log.InfoContext(r.Context(), "admin token rotated", "audit", true, "action", "admin.token.rotate")
|
||||
httpjson.Write(w, http.StatusOK, map[string]string{"token": token})
|
||||
m.log.InfoContext(ctx, "admin token rotated", "audit", true, "action", "admin.token.rotate")
|
||||
return httpapi.OK(adminToken{Token: token}), nil
|
||||
}
|
||||
|
||||
func newToken() (string, error) {
|
||||
|
||||
Reference in new issue
Block a user