| 1 | package home |
| 2 | |
| 3 | import ( |
| 4 | "crypto/sha256" |
| 5 | "crypto/subtle" |
| 6 | "errors" |
| 7 | "io" |
| 8 | "mime" |
| 9 | "net/http" |
| 10 | "strings" |
| 11 | |
| 12 | "amadan.net/oficina/home/internal/home/directorydata" |
| 13 | "github.com/go-json-experiment/json" |
| 14 | ) |
| 15 | |
| 16 | const agentDirectoryPath = "/api/agent/directory" |
| 17 | |
| 18 | type agentDirectoryApp struct { |
| 19 | App string `json:"app"` |
| 20 | Origin string `json:"origin"` |
| 21 | } |
| 22 | type agentDirectoryTeam struct { |
| 23 | ID string `json:"id"` |
| 24 | Name string `json:"name"` |
| 25 | Apps []agentDirectoryApp `json:"apps"` |
| 26 | } |
| 27 | |
| 28 | // This bearer-only service read is deliberately outside browser sessions. The |
| 29 | // hash is provisioned only on the directory origin; identity assertions remain |
| 30 | // identity-only and the credential origin never serves this endpoint. |
| 31 | func (a *App) agentDirectory(w http.ResponseWriter, r *http.Request) { |
| 32 | w.Header().Set("Cache-Control", "no-store") |
| 33 | w.Header().Set("X-Content-Type-Options", "nosniff") |
| 34 | if a.runtime == nil || a.runtime.role != "directory" || len(a.runtime.agentDirectoryHash) != 32 || r.Host != strings.TrimPrefix(a.origins.Directory, "https://") { |
| 35 | http.NotFound(w, r) |
| 36 | return |
| 37 | } |
| 38 | if r.Method != "POST" { |
| 39 | w.Header().Set("Allow", "POST") |
| 40 | http.Error(w, "Method not allowed", 405) |
| 41 | return |
| 42 | } |
| 43 | parts := strings.Fields(r.Header.Get("Authorization")) |
| 44 | if len(r.Header.Values("Authorization")) != 1 || len(parts) != 2 || !strings.EqualFold(parts[0], "Bearer") || len(parts[1]) > 256 { |
| 45 | w.Header().Set("WWW-Authenticate", "Bearer") |
| 46 | http.Error(w, "Unauthorized", 401) |
| 47 | return |
| 48 | } |
| 49 | sum := sha256.Sum256([]byte(parts[1])) |
| 50 | if subtle.ConstantTimeCompare(sum[:], a.runtime.agentDirectoryHash) != 1 { |
| 51 | w.Header().Set("WWW-Authenticate", "Bearer") |
| 52 | http.Error(w, "Unauthorized", 401) |
| 53 | return |
| 54 | } |
| 55 | media, _, err := mime.ParseMediaType(r.Header.Get("Content-Type")) |
| 56 | if err != nil || media != "application/json" { |
| 57 | http.Error(w, "Use application/json", 415) |
| 58 | return |
| 59 | } |
| 60 | // One opaque subject is the entire request. 1 KiB leaves ample JSON |
| 61 | // framing room while bounding unactionable input before parsing. |
| 62 | body, err := io.ReadAll(http.MaxBytesReader(w, r.Body, 1024)) |
| 63 | var input struct { |
| 64 | Subject string `json:"subject"` |
| 65 | } |
| 66 | if err != nil || json.Unmarshal(body, &input, json.RejectUnknownMembers(true)) != nil { |
| 67 | http.Error(w, "Invalid directory request", 400) |
| 68 | return |
| 69 | } |
| 70 | choices, err := a.directory.store.AgentChoices(r.Context(), input.Subject) |
| 71 | if errors.Is(err, directorydata.ErrInvalid) { |
| 72 | http.Error(w, "Invalid directory request", 400) |
| 73 | return |
| 74 | } |
| 75 | if err != nil { |
| 76 | http.Error(w, "Directory unavailable", 503) |
| 77 | return |
| 78 | } |
| 79 | teams := []agentDirectoryTeam{} |
| 80 | for _, choice := range choices { |
| 81 | if len(teams) == 0 || teams[len(teams)-1].ID != choice.SQID { |
| 82 | teams = append(teams, agentDirectoryTeam{ID: choice.SQID, Name: choice.Name, Apps: []agentDirectoryApp{}}) |
| 83 | } |
| 84 | last := &teams[len(teams)-1] |
| 85 | last.Apps = append(last.Apps, agentDirectoryApp{App: choice.App, Origin: choice.Origin}) |
| 86 | } |
| 87 | encoded, err := json.Marshal(struct { |
| 88 | Teams []agentDirectoryTeam `json:"teams"` |
| 89 | }{teams}) |
| 90 | if err != nil || len(encoded) > 1<<20 { |
| 91 | http.Error(w, "Directory unavailable", 503) |
| 92 | return |
| 93 | } |
| 94 | w.Header().Set("Content-Type", "application/json") |
| 95 | _, _ = w.Write(encoded) |
| 96 | } |
| 97 | |