Capture accumulated application, UI, CLI, regression tests and design documentation changes, including task project mapping and default CalDAV list naming. Keep deployment data, backups, local credentials and build artifacts out of Git. Move the Compose enrollment token to an ignored local environment file.
76 lines
2.1 KiB
Go
76 lines
2.1 KiB
Go
package server
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"pageswiki/internal/store"
|
|
"strings"
|
|
)
|
|
|
|
type federationEnrollRequest struct {
|
|
FQDN string `json:"fqdn"`
|
|
Fingerprint string `json:"fingerprint"`
|
|
PublicKey string `json:"public_key"`
|
|
Signature string `json:"signature"`
|
|
ManifestVersion int `json:"manifest_version"`
|
|
CapabilitySignature string `json:"capability_signature"`
|
|
}
|
|
|
|
func (s *Server) handleFederationEnrollAPI(
|
|
w http.ResponseWriter,
|
|
r *http.Request,
|
|
) {
|
|
if r.Method != http.MethodPost {
|
|
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
authorization := strings.TrimSpace(r.Header.Get("Authorization"))
|
|
const bearer = "Bearer "
|
|
if !strings.HasPrefix(authorization, bearer) {
|
|
http.Error(w, "bearer token is required", http.StatusUnauthorized)
|
|
return
|
|
}
|
|
var request federationEnrollRequest
|
|
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
campus, instance, err := s.store.RegisterFederationInstance(
|
|
strings.TrimSpace(strings.TrimPrefix(authorization, bearer)),
|
|
request.FQDN,
|
|
request.Fingerprint,
|
|
request.PublicKey,
|
|
request.Signature,
|
|
store.FederationCapabilities{ManifestVersion: request.ManifestVersion, Signature: request.CapabilitySignature},
|
|
)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusUnauthorized)
|
|
return
|
|
}
|
|
domains, err := s.store.ListDomains()
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
filtered := domains[:0]
|
|
if instance.Status == "approved" {
|
|
for _, domain := range domains {
|
|
if domain.CampusUUID == campus.UUID {
|
|
filtered = append(filtered, domain)
|
|
}
|
|
}
|
|
}
|
|
manifest, err := s.store.BuildSignedCampusManifest(*campus, filtered)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_ = json.NewEncoder(w).Encode(map[string]any{
|
|
"campus": campus,
|
|
"instance": instance,
|
|
"domains": filtered,
|
|
"manifest": manifest,
|
|
})
|
|
}
|