Extract Grammaratur, Dregarnuhr, Geduldh, Mestionora, Ordoschnellic, Schutzaria, Kunstzeal and Seheweit behind compatible application contracts. Include accumulated editor resilience, navigation and theme UX, remote CLI, storage/schema and DR groundwork, tests and design/operations documentation. Verified: uncached full Go suite; go vet ./...; race tests for all eight components; 11 targeted Chromium tests; Node suites; 19 Python CLI tests; reproducible editor build. No push, release tag or deployment in this checkpoint.
249 lines
8.9 KiB
Go
249 lines
8.9 KiB
Go
package server
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"errors"
|
|
"net/http"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"pageswiki/internal/grammaratur"
|
|
"pageswiki/internal/render"
|
|
"pageswiki/internal/store"
|
|
)
|
|
|
|
type adapterWorkspaceFile struct {
|
|
Path string `json:"path"`
|
|
SemanticPath string `json:"semantic_path"`
|
|
RevisionUUID string `json:"revision_uuid"`
|
|
ContentType string `json:"content_type"`
|
|
ContentBase64 string `json:"content_base64"`
|
|
}
|
|
|
|
func (s *Server) adapterPluginForSession(session *store.AdapterSession) adapterPlugin {
|
|
plugins, _ := s.adapterPlugins(session.DomainUUID)
|
|
for _, plugin := range plugins {
|
|
if plugin.ID == session.PluginID {
|
|
return plugin
|
|
}
|
|
}
|
|
return adapterPlugin{}
|
|
}
|
|
|
|
func workspaceFilename(semanticPath, rootPath, filename string, markdown bool) string {
|
|
if semanticPath == rootPath {
|
|
filename = filepath.Base(filename)
|
|
if filename == "." || filename == "" || filename == "/" {
|
|
filename = "index.html"
|
|
}
|
|
if markdown {
|
|
filename = strings.TrimSuffix(filename, filepath.Ext(filename)) + ".md"
|
|
}
|
|
return filename
|
|
}
|
|
relative := strings.TrimPrefix(semanticPath, strings.TrimRight(rootPath, "/")+"/")
|
|
if filepath.Ext(relative) == "" && filepath.Ext(filename) != "" {
|
|
relative += filepath.Ext(filename)
|
|
}
|
|
if markdown {
|
|
relative = strings.TrimSuffix(relative, filepath.Ext(relative)) + ".md"
|
|
}
|
|
return relative
|
|
}
|
|
|
|
func (s *Server) workspaceFile(session *store.AdapterSession, semanticPath string, plugin adapterPlugin) (adapterWorkspaceFile, error) {
|
|
objectUUID, err := s.store.ResolvePathInDomain(session.DomainUUID, semanticPath)
|
|
if err != nil {
|
|
return adapterWorkspaceFile{}, err
|
|
}
|
|
revision, err := s.store.GetLatestRevisionForLanguages(objectUUID, []string{s.getLocale(session.DomainUUID), "und"})
|
|
if err != nil {
|
|
return adapterWorkspaceFile{}, err
|
|
}
|
|
body := revision.Body
|
|
markdown := plugin.HTMLMarkdown && strings.HasPrefix(strings.ToLower(revision.ContentType), "text/html")
|
|
if markdown {
|
|
body, err = grammaratur.HTMLToMarkdown(body)
|
|
if err != nil {
|
|
return adapterWorkspaceFile{}, err
|
|
}
|
|
}
|
|
return adapterWorkspaceFile{
|
|
Path: workspaceFilename(semanticPath, session.RootPath, revision.Filename, markdown),
|
|
SemanticPath: semanticPath,
|
|
RevisionUUID: revision.UUID, ContentType: revision.ContentType,
|
|
ContentBase64: base64.StdEncoding.EncodeToString([]byte(body)),
|
|
}, nil
|
|
}
|
|
|
|
func adapterBearer(r *http.Request) string {
|
|
value := strings.TrimSpace(r.Header.Get("Authorization"))
|
|
if !strings.HasPrefix(value, "Bearer ") {
|
|
return ""
|
|
}
|
|
return strings.TrimSpace(strings.TrimPrefix(value, "Bearer "))
|
|
}
|
|
|
|
// authorizeAdapterSession re-checks the current principal on every operation;
|
|
// exchanging a ticket must not pin permissions for the lifetime of a session.
|
|
func (s *Server) authorizeAdapterSession(r *http.Request, session *store.AdapterSession, capability string, path string) error {
|
|
if session.PrincipalUUID == "" {
|
|
if mode, _ := s.store.GetConfigInDomain(session.DomainUUID, "authorization_mode"); mode == "enforcing" {
|
|
return store.ErrAccessDenied
|
|
}
|
|
return nil
|
|
}
|
|
// The session token is intentionally separate from the browser auth token;
|
|
// use the principal captured at ticket issuance and re-evaluate its current
|
|
// grants on every request.
|
|
return s.store.Authorize(store.AuthorizationRequest{DomainUUID: session.DomainUUID, PrincipalUUID: session.PrincipalUUID, Capability: capability, TargetType: "path", TargetID: path})
|
|
}
|
|
|
|
func (s *Server) handleAdapterSessionAPI(w http.ResponseWriter, r *http.Request) {
|
|
action := strings.Trim(strings.TrimPrefix(r.URL.Path, "/_/api/v1/adapter/session"), "/")
|
|
if action == "exchange" && r.Method == http.MethodPost {
|
|
var input struct {
|
|
Ticket string `json:"ticket"`
|
|
}
|
|
if !decodeEventJSON(w, r, &input) {
|
|
return
|
|
}
|
|
token, session, err := s.store.ExchangeAdapterTicket(input.Ticket)
|
|
if err != nil {
|
|
writeEventJSON(w, http.StatusUnauthorized, map[string]string{"error": err.Error()})
|
|
return
|
|
}
|
|
revision, err := s.store.GetRevision(session.BaseRevisionUUID)
|
|
if err != nil {
|
|
writeEventJSON(w, 500, map[string]string{"error": err.Error()})
|
|
return
|
|
}
|
|
writeEventJSON(w, 200, map[string]any{
|
|
"session_token": token, "expires_at_ms": session.ExpiresAtMS,
|
|
"plugin_id": session.PluginID,
|
|
"revision_uuid": revision.UUID, "filename": revision.Filename,
|
|
"content_type": revision.ContentType, "lang": revision.Language,
|
|
})
|
|
return
|
|
}
|
|
if action == "content" {
|
|
session, err := s.store.GetAdapterSession(adapterBearer(r))
|
|
if err != nil {
|
|
writeEventJSON(w, http.StatusUnauthorized, map[string]string{"error": err.Error()})
|
|
return
|
|
}
|
|
if r.Method == http.MethodGet {
|
|
if err := s.authorizeAdapterSession(r, session, "read", session.RootPath); err != nil {
|
|
writeEventJSON(w, http.StatusForbidden, map[string]string{"error": "adapter session authorization revoked"})
|
|
return
|
|
}
|
|
file, err := s.workspaceFile(session, session.RootPath, s.adapterPluginForSession(session))
|
|
if err != nil {
|
|
writeEventJSON(w, 500, map[string]string{"error": err.Error()})
|
|
return
|
|
}
|
|
writeEventJSON(w, 200, file)
|
|
return
|
|
}
|
|
if r.Method == http.MethodPut {
|
|
if err := s.authorizeAdapterSession(r, session, "write", session.RootPath); err != nil {
|
|
writeEventJSON(w, http.StatusForbidden, map[string]string{"error": "adapter session authorization revoked"})
|
|
return
|
|
}
|
|
var input struct {
|
|
ContentBase64 string `json:"content_base64"`
|
|
Message string `json:"message"`
|
|
Path string `json:"path"`
|
|
RevisionUUID string `json:"revision_uuid"`
|
|
}
|
|
if !decodeEventJSON(w, r, &input) {
|
|
return
|
|
}
|
|
payload, err := base64.StdEncoding.DecodeString(input.ContentBase64)
|
|
if err != nil {
|
|
writeEventJSON(w, 400, map[string]string{"error": "invalid content_base64"})
|
|
return
|
|
}
|
|
if len(payload) > 64<<20 {
|
|
writeEventJSON(w, 413, map[string]string{"error": "content exceeds 64 MiB"})
|
|
return
|
|
}
|
|
semanticPath := session.RootPath
|
|
if input.Path != "" {
|
|
semanticPath = strings.TrimSpace(input.Path)
|
|
}
|
|
rootPrefix := strings.TrimRight(session.RootPath, "/") + "/"
|
|
if semanticPath != session.RootPath && !strings.HasPrefix(semanticPath, rootPrefix) {
|
|
writeEventJSON(w, 403, map[string]string{"error": "path is outside the adapter workspace"})
|
|
return
|
|
}
|
|
if err := s.authorizeAdapterSession(r, session, "write", semanticPath); err != nil {
|
|
writeEventJSON(w, http.StatusForbidden, map[string]string{"error": "adapter path authorization revoked"})
|
|
return
|
|
}
|
|
objectUUID, err := s.store.ResolvePathInDomain(session.DomainUUID, semanticPath)
|
|
if err != nil {
|
|
writeEventJSON(w, 404, map[string]string{"error": err.Error()})
|
|
return
|
|
}
|
|
baseRevisionUUID := input.RevisionUUID
|
|
if baseRevisionUUID == "" && semanticPath == session.RootPath {
|
|
baseRevisionUUID = session.BaseRevisionUUID
|
|
}
|
|
base, err := s.store.GetRevision(baseRevisionUUID)
|
|
if err != nil || base.ObjectUUID != objectUUID {
|
|
writeEventJSON(w, 409, map[string]string{"error": "invalid base revision"})
|
|
return
|
|
}
|
|
plugin := s.adapterPluginForSession(session)
|
|
if plugin.HTMLMarkdown && strings.HasPrefix(strings.ToLower(base.ContentType), "text/html") {
|
|
payload = []byte(render.Markdown(string(payload)))
|
|
}
|
|
revision, err := s.store.SaveHostedRevisionAt(session, objectUUID, baseRevisionUUID, payload, input.Message)
|
|
if err != nil {
|
|
status := 500
|
|
if errors.Is(err, store.ErrRevisionConflict) {
|
|
status = http.StatusConflict
|
|
}
|
|
writeEventJSON(w, status, map[string]string{"error": err.Error()})
|
|
return
|
|
}
|
|
writeEventJSON(w, 200, map[string]any{"revision_uuid": revision.UUID, "document_hash": revision.DocumentHash})
|
|
return
|
|
}
|
|
}
|
|
if action == "workspace" && r.Method == http.MethodGet {
|
|
session, err := s.store.GetAdapterSession(adapterBearer(r))
|
|
if err != nil {
|
|
writeEventJSON(w, http.StatusUnauthorized, map[string]string{"error": err.Error()})
|
|
return
|
|
}
|
|
if err := s.authorizeAdapterSession(r, session, "read", session.RootPath); err != nil {
|
|
writeEventJSON(w, http.StatusForbidden, map[string]string{"error": "adapter session authorization revoked"})
|
|
return
|
|
}
|
|
paths := []string{session.RootPath}
|
|
children, err := s.store.ListChildrenInDomain(session.DomainUUID, session.RootPath)
|
|
if err != nil {
|
|
writeEventJSON(w, 500, map[string]string{"error": err.Error()})
|
|
return
|
|
}
|
|
paths = append(paths, children...)
|
|
plugin := s.adapterPluginForSession(session)
|
|
files := make([]adapterWorkspaceFile, 0, len(paths))
|
|
for _, path := range paths {
|
|
file, fileErr := s.workspaceFile(session, path, plugin)
|
|
if fileErr == nil {
|
|
files = append(files, file)
|
|
}
|
|
}
|
|
payload := map[string]any{"root_path": session.RootPath, "files": files}
|
|
if session.PluginID == "vim.wasm" {
|
|
payload["vim_settings"] = s.domainVimSettings(session.DomainUUID)
|
|
}
|
|
writeEventJSON(w, 200, payload)
|
|
return
|
|
}
|
|
writeEventJSON(w, http.StatusMethodNotAllowed, map[string]string{"error": "unsupported adapter session operation"})
|
|
}
|