| 1 | package agentaccess |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "io" |
| 7 | "mime" |
| 8 | "net/http" |
| 9 | "strings" |
| 10 | ) |
| 11 | |
| 12 | type Verifier interface { |
| 13 | Verify(context.Context, string) (Principal, error) |
| 14 | } |
| 15 | type VerifyFunc func(context.Context, string) (Principal, error) |
| 16 | |
| 17 | func (v VerifyFunc) Verify(ctx context.Context, token string) (Principal, error) { |
| 18 | return v(ctx, token) |
| 19 | } |
| 20 | |
| 21 | type principalKey struct{} |
| 22 | |
| 23 | func principalFrom(ctx context.Context) Principal { |
| 24 | p, _ := ctx.Value(principalKey{}).(Principal) |
| 25 | return p |
| 26 | } |
| 27 | |
| 28 | func Authenticate(v Verifier, next http.Handler) http.Handler { |
| 29 | if v == nil { |
| 30 | panic("agentaccess: verifier is required") |
| 31 | } |
| 32 | return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 33 | w.Header().Set("Cache-Control", "no-store") |
| 34 | parts := strings.Fields(r.Header.Get("Authorization")) |
| 35 | if len(r.Header.Values("Authorization")) != 1 || len(parts) != 2 || !strings.EqualFold(parts[0], "Bearer") { |
| 36 | if w.Header().Get("WWW-Authenticate") == "" { |
| 37 | w.Header().Set("WWW-Authenticate", "Bearer") |
| 38 | } |
| 39 | writeFailure(w, Fail("unauthenticated", "Connect to Oficina first.")) |
| 40 | return |
| 41 | } |
| 42 | p, err := v.Verify(r.Context(), parts[1]) |
| 43 | if err != nil { |
| 44 | writeFailure(w, err) |
| 45 | return |
| 46 | } |
| 47 | if p.Subject == "" { |
| 48 | writeFailure(w, Fail("unauthenticated", "Connect to Oficina first.")) |
| 49 | return |
| 50 | } |
| 51 | next.ServeHTTP(w, r.WithContext(context.WithValue(r.Context(), principalKey{}, p))) |
| 52 | }) |
| 53 | } |
| 54 | func (registry *Registry) HTTP(v Verifier) http.Handler { |
| 55 | mux := http.NewServeMux() |
| 56 | mux.HandleFunc("GET /api/v1/connection", func(w http.ResponseWriter, r *http.Request) { |
| 57 | writeJSON(w, 200, ConnectionFrom(principalFrom(r.Context()))) |
| 58 | }) |
| 59 | mux.HandleFunc("GET /api/v1/operations", func(w http.ResponseWriter, r *http.Request) { |
| 60 | writeJSON(w, 200, map[string]any{"operations": registry.List(principalFrom(r.Context()))}) |
| 61 | }) |
| 62 | mux.HandleFunc("POST /api/v1/operations/{name}", func(w http.ResponseWriter, r *http.Request) { |
| 63 | media, _, err := mime.ParseMediaType(r.Header.Get("Content-Type")) |
| 64 | if err != nil || media != "application/json" { |
| 65 | writeFailure(w, Fail("unsupported_media_type", "Send application/json.")) |
| 66 | return |
| 67 | } |
| 68 | raw, err := io.ReadAll(io.LimitReader(r.Body, MaxPayload+1)) |
| 69 | if err != nil { |
| 70 | writeFailure(w, Fail("invalid", "Could not read the request.")) |
| 71 | return |
| 72 | } |
| 73 | result, err := registry.Call(r.Context(), principalFrom(r.Context()), r.PathValue("name"), raw) |
| 74 | if err != nil { |
| 75 | writeFailure(w, err) |
| 76 | return |
| 77 | } |
| 78 | writeJSON(w, 200, result) |
| 79 | }) |
| 80 | return Authenticate(v, mux) |
| 81 | } |
| 82 | func writeJSON(w http.ResponseWriter, status int, value any) { |
| 83 | w.Header().Set("Content-Type", "application/json") |
| 84 | w.WriteHeader(status) |
| 85 | _ = json.NewEncoder(w).Encode(value) |
| 86 | } |
| 87 | func writeFailure(w http.ResponseWriter, err error) { |
| 88 | f := SafeFailure(err) |
| 89 | status := map[string]int{"unauthenticated": 401, "forbidden": 403, "not_found": 404, "invalid": 422, "conflict": 409, "too_large": 413, "unsupported_media_type": 415, "unavailable": 503, "rate_limited": 429}[f.Code] |
| 90 | if status == 0 { |
| 91 | status = 500 |
| 92 | } |
| 93 | if status == 401 { |
| 94 | if w.Header().Get("WWW-Authenticate") == "" { |
| 95 | w.Header().Set("WWW-Authenticate", "Bearer") |
| 96 | } |
| 97 | } |
| 98 | writeJSON(w, status, map[string]any{"error": f}) |
| 99 | } |
| 100 | |
| 101 | // ConnectionInfo is safe discovery data from the current verified grant. |
| 102 | // It contains no bearer credentials or app content. |
| 103 | type ConnectionInfo struct { |
| 104 | Teams []string `json:"teams"` |
| 105 | Scopes []string `json:"scopes"` |
| 106 | } |
| 107 | |
| 108 | func ConnectionFrom(p Principal) ConnectionInfo { |
| 109 | return ConnectionInfo{append([]string{}, p.Teams...), append([]string{}, p.Scopes...)} |
| 110 | } |
| 111 | |