oficina / agent-access Public
Recheck the current app destination for downstream credentials
5 files changed,
+66
−14
connection/directory.go+7 −7connection/server.go+1 −1connection/server_test.go+37 −3grant/server.go+16 −3grant/server_test.go+5 −0
CI cancelled — superseded by a newer push — run details
diff --git a/connection/directory.go b/connection/directory.go| index 0b17929..d99edf4 100644 |
| --- a/connection/directory.go |
| +++ b/connection/directory.go |
| @@ -85,6 +85,10 @@ type credentials struct { |
| } |
| func (c *credentials) teams(ctx context.Context, subject string) ([]grant.Team, error) { |
| + return c.resourceTeams(ctx, subject, "") |
| +} |
| + |
| +func (c *credentials) resourceTeams(ctx context.Context, subject, resource string) ([]grant.Team, error) { |
| teams, err := c.directory.Lookup(ctx, subject) |
| if err != nil { |
| return nil, err |
| @@ -94,7 +98,7 @@ func (c *credentials) teams(ctx context.Context, subject string) ([]grant.Team, |
| found := false |
| for _, app := range team.Apps { |
| for _, route := range c.routes { |
| - if route.Team == team.ID && route.Resource == app.Origin+"/api/v1" { |
| + if route.Team == team.ID && route.Resource == app.Origin+"/api/v1" && (resource == "" || resource == route.Resource) { |
| found = true |
| } |
| } |
| @@ -106,17 +110,13 @@ func (c *credentials) teams(ctx context.Context, subject string) ([]grant.Team, |
| return out, nil |
| } |
| func (c *credentials) Downstream(ctx context.Context, p access.Principal, resource, team string, scopes []string) (string, error) { |
| - teams, err := c.directory.Lookup(ctx, p.Subject) |
| + teams, err := c.resourceTeams(ctx, p.Subject, resource) |
| if err != nil { |
| return "", err |
| } |
| for _, entry := range teams { |
| if entry.ID == team { |
| - for _, app := range entry.Apps { |
| - if resource == app.Origin+"/api/v1" { |
| - return c.store.Downstream(ctx, p, resource, team, scopes) |
| - } |
| - } |
| + return c.store.Downstream(ctx, p, resource, team, scopes) |
| } |
| } |
| return "", access.Fail("forbidden", "The app is no longer available to this team.") |
diff --git a/connection/server.go b/connection/server.go| index 4110581..0e6a2fb 100644 |
| --- a/connection/server.go |
| +++ b/connection/server.go |
| @@ -131,7 +131,7 @@ func New(ctx context.Context, cfg Config, d *db.DB, logger *slog.Logger) (*Serve |
| if err != nil { |
| return nil, err |
| } |
| - grants, err := grant.NewServer(grant.Config{Origin: cfg.Origin, Resource: cfg.Origin + "/mcp", Store: store, ConsentKey: consentKey, Clients: clients, Scopes: scopes, Subject: fresh, Teams: provider.teams, SignInPath: handoff.StartPath}) |
| + grants, err := grant.NewServer(grant.Config{Origin: cfg.Origin, Resource: cfg.Origin + "/mcp", Store: store, ConsentKey: consentKey, Clients: clients, Scopes: scopes, Subject: fresh, Teams: provider.teams, ResourceTeams: provider.resourceTeams, SignInPath: handoff.StartPath}) |
| if err != nil { |
| return nil, err |
| } |
diff --git a/connection/server_test.go b/connection/server_test.go| index c475fc6..3faaa1f 100644 |
| --- a/connection/server_test.go |
| +++ b/connection/server_test.go |
| @@ -55,6 +55,8 @@ func TestHomeBrowserConsentToHTTPMCPAndLogout(t *testing.T) { |
| var calls atomic.Int32 |
| var admitted atomic.Bool |
| admitted.Store(true) |
| + var appLive atomic.Bool |
| + appLive.Store(true) |
| appRegistry, err := access.NewRegistry(access.Operation{Name: "notes_create", Description: "Create a note", Scope: "notes:write", Idempotent: true, |
| Input: json.RawMessage(`{"type":"object","required":["team","title"],"properties":{"team":{"type":"string"},"title":{"type":"string"}},"additionalProperties":false}`), |
| Output: json.RawMessage(`{"type":"object","required":["id"],"properties":{"id":{"type":"integer"}}}`), |
| @@ -88,7 +90,11 @@ func TestHomeBrowserConsentToHTTPMCPAndLogout(t *testing.T) { |
| } |
| teams := []DirectoryTeam{} |
| if admitted.Load() { |
| - teams = append(teams, DirectoryTeam{ID: "acme", Name: "Acme", Apps: []DirectoryApp{{App: "memoria", Origin: app.URL}}}) |
| + apps := []DirectoryApp{{App: "retained", Origin: "https://retained.example.test"}} |
| + if appLive.Load() { |
| + apps = append(apps, DirectoryApp{App: "memoria", Origin: app.URL}) |
| + } |
| + teams = append(teams, DirectoryTeam{ID: "acme", Name: "Acme", Apps: apps}) |
| } |
| w.Header().Set("Content-Type", "application/json") |
| json.NewEncoder(w).Encode(map[string]any{"teams": teams}) |
| @@ -104,7 +110,9 @@ func TestHomeBrowserConsentToHTTPMCPAndLogout(t *testing.T) { |
| } |
| defer d.Close() |
| contracts := appRegistry.List(access.Principal{Subject: subject, Teams: []string{"acme"}, Scopes: []string{"notes:write"}}) |
| - server, err := New(ctx, Config{Origin: serviceOrigin, HomeIssuer: home, ConsumerID: "agent-access", HomeKeys: map[string]string{"key": base64.StdEncoding.EncodeToString(key.SignPub())}, Directory: directory.URL, DirectoryToken: serviceToken, DEK: []byte(strings.Repeat("d", 32)), Transport: transport, Routes: []access.Route{{Team: "acme", Resource: app.URL + "/api/v1", Operations: contracts}}}, d, logger) |
| + retained := append([]access.Operation{}, contracts...) |
| + retained[0].Name = "retained_operation" |
| + server, err := New(ctx, Config{Origin: serviceOrigin, HomeIssuer: home, ConsumerID: "agent-access", HomeKeys: map[string]string{"key": base64.StdEncoding.EncodeToString(key.SignPub())}, Directory: directory.URL, DirectoryToken: serviceToken, DEK: []byte(strings.Repeat("d", 32)), Transport: transport, Routes: []access.Route{{Team: "acme", Resource: app.URL + "/api/v1", Operations: contracts}, {Team: "acme", Resource: "https://retained.example.test/api/v1", Operations: retained}}}, d, logger) |
| if err != nil { |
| t.Fatal(err) |
| } |
| @@ -186,7 +194,7 @@ func TestHomeBrowserConsentToHTTPMCPAndLogout(t *testing.T) { |
| t.Fatal(err) |
| } |
| ops, err := c.Operations(ctx) |
| - if err != nil || len(ops) != 1 { |
| + if err != nil || len(ops) != 2 { |
| t.Fatal("tool discovery", len(ops), err) |
| } |
| info, err := c.Connection(ctx) |
| @@ -224,6 +232,32 @@ func TestHomeBrowserConsentToHTTPMCPAndLogout(t *testing.T) { |
| if calls.Load() != 2 { |
| t.Fatal("app calls", calls.Load()) |
| } |
| + // A remaining app keeps the team admitted, but cannot keep this app's |
| + // already minted credential valid when Home removes its destination. |
| + principal, err := server.Grants.Verify(ctx, parent, serviceOrigin+"/mcp") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + downstream, err := server.Grants.Downstream(ctx, principal, app.URL+"/api/v1", "acme", []string{"notes:write"}) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + remote, err := access.NewRemoteVerifier(serviceOrigin, app.URL+"/api/v1") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + remote.Client.Transport = transport |
| + if _, err = remote.Verify(ctx, downstream); err != nil { |
| + t.Fatal("live app credential", err) |
| + } |
| + appLive.Store(false) |
| + if _, err = remote.Verify(ctx, downstream); err == nil { |
| + t.Fatal("removed app retained downstream authority") |
| + } |
| + if _, err = c.Call(ctx, "notes_create", input); err == nil { |
| + t.Fatal("removed app was routed") |
| + } |
| + appLive.Store(true) |
| admitted.Store(false) |
| if _, err = c.Call(ctx, "notes_create", input); err == nil { |
| t.Fatal("removed member reached app") |
diff --git a/grant/server.go b/grant/server.go| index aad78d1..f582db3 100644 |
| --- a/grant/server.go |
| +++ b/grant/server.go |
| @@ -40,8 +40,11 @@ type Config struct { |
| // Subject reads a verified browser session. Empty means sign-in is needed. |
| Subject func(*http.Request) string |
| // Teams reads CURRENT memberships, including on token verification. |
| - Teams func(context.Context, string) ([]Team, error) |
| - SignInPath string |
| + Teams func(context.Context, string) ([]Team, error) |
| + // ResourceTeams rechecks the current app destination for downstream tokens. |
| + // A missing callback refuses downstream introspection. |
| + ResourceTeams func(context.Context, string, string) ([]Team, error) |
| + SignInPath string |
| } |
| type Server struct { |
| cfg Config |
| @@ -459,7 +462,17 @@ func (s *Server) credential(w http.ResponseWriter, r *http.Request) { |
| } |
| return |
| } |
| - teams, err := s.cfg.Teams(r.Context(), p.Subject) |
| + lookup := s.cfg.Teams |
| + if in.Resource != s.cfg.Resource { |
| + if s.cfg.ResourceTeams == nil { |
| + fail(w, 401, "invalid_token") |
| + return |
| + } |
| + lookup = func(ctx context.Context, subject string) ([]Team, error) { |
| + return s.cfg.ResourceTeams(ctx, subject, in.Resource) |
| + } |
| + } |
| + teams, err := lookup(r.Context(), p.Subject) |
| if err != nil { |
| fail(w, 503, "temporarily_unavailable") |
| return |
diff --git a/grant/server_test.go b/grant/server_test.go| index 03dbf6e..620c989 100644 |
| --- a/grant/server_test.go |
| +++ b/grant/server_test.go |
| @@ -50,6 +50,11 @@ func setup(t *testing.T) (*Server, *httptest.Server, *bool) { |
| return []Team{{ID: "acme", Name: "Acme"}}, nil |
| } |
| return nil, nil |
| + }, ResourceTeams: func(ctx context.Context, subject, resource string) ([]Team, error) { |
| + if admitted && resource == "https://notes.example/api/v1" { |
| + return []Team{{ID: "acme", Name: "Acme"}}, nil |
| + } |
| + return nil, nil |
| }, SignInPath: "/signin"}) |
| if err != nil { |
| t.Fatal(err) |