Slice 5a — the hard scope ceiling. This exercises the 403 matrix over the real v1 handlers: account-only routes reject agent-scoped credentials; per-agent routes pin an agent-scoped credential to its bound agent; an account-scoped credential passes everywhere it owns.
(t *testing.T)
| 16 | // account-scoped credential passes everywhere it owns. |
| 17 | |
| 18 | func scopeTestServer(t *testing.T) *httptest.Server { |
| 19 | t.Helper() |
| 20 | u := &identity.User{ID: "u_1", Email: "owner@acme.com"} |
| 21 | deps := Deps{ |
| 22 | // Scope-aware auth keyed by bearer: |
| 23 | // acct → account scope |
| 24 | // agtSupport → agent scope bound to support@acme.com |
| 25 | // agtOther → agent scope bound to other@acme.com |
| 26 | PrincipalAuthenticator: func(r *http.Request) (*identity.Principal, error) { |
| 27 | switch r.Header.Get("Authorization") { |
| 28 | case "Bearer acct": |
| 29 | return &identity.Principal{User: u, Scope: identity.ScopeAccount}, nil |
| 30 | case "Bearer agtSupport": |
| 31 | return &identity.Principal{User: u, Scope: identity.ScopeAgent, AgentID: "support@acme.com"}, nil |
| 32 | case "Bearer agtOther": |
| 33 | return &identity.Principal{User: u, Scope: identity.ScopeAgent, AgentID: "other@acme.com"}, nil |
| 34 | } |
| 35 | return nil, errors.New("unauthorized") |
| 36 | }, |
| 37 | ListAgents: func(ctx context.Context, userID string) ([]identity.AgentIdentity, error) { |
| 38 | return []identity.AgentIdentity{sampleAgent()}, nil |
| 39 | }, |
| 40 | GetAgent: func(ctx context.Context, address string) (*identity.AgentIdentity, error) { |
| 41 | switch address { |
| 42 | case "support@acme.com": |
| 43 | a := sampleAgent() |
| 44 | return &a, nil |
| 45 | case "other@acme.com": |
| 46 | a := sampleAgent() |
| 47 | a.ID = "other@acme.com" |
| 48 | return &a, nil |
| 49 | } |
| 50 | return nil, errors.New("not found") |
| 51 | }, |
| 52 | UpdateAgentName: func(ctx context.Context, agentID, userID, name string) error { |
| 53 | return nil |
| 54 | }, |
| 55 | UpdateAgentProtection: func(ctx context.Context, agentID, userID string, cfg identity.ProtectionConfig) error { |
| 56 | return nil |
| 57 | }, |
| 58 | DeleteAgent: func(ctx context.Context, agentID, userID string) error { return nil }, |
| 59 | Legacy: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusTeapot) }), |
| 60 | } |
| 61 | srv := httptest.NewServer(New(deps)) |
| 62 | t.Cleanup(srv.Close) |
| 63 | return srv |
| 64 | } |
| 65 | |
| 66 | func TestScope_AccountOnlyRoutesRejectAgentKeys(t *testing.T) { |
| 67 | srv := scopeTestServer(t) |
no test coverage detected